use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.has in project open-kilda by telstra.
the class FermaFlowMeterRepository method findFirstUnassignedMeter.
@Override
public Optional<MeterId> findFirstUnassignedMeter(SwitchId switchId, MeterId lowestMeterId, MeterId highestMeterId) {
String switchIdAsStr = SwitchIdConverter.INSTANCE.toGraphProperty(switchId);
Long lowestMeterIdAsLong = MeterIdConverter.INSTANCE.toGraphProperty(lowestMeterId);
Long highestMeterIdAsLong = MeterIdConverter.INSTANCE.toGraphProperty(highestMeterId);
try (GraphTraversal<?, ?> traversal = framedGraph().traverse(g -> g.V().hasLabel(FlowMeterFrame.FRAME_LABEL).has(FlowMeterFrame.METER_ID_PROPERTY, P.gte(lowestMeterIdAsLong)).has(FlowMeterFrame.METER_ID_PROPERTY, P.lt(highestMeterIdAsLong)).has(FlowMeterFrame.SWITCH_PROPERTY, switchIdAsStr).values(FlowMeterFrame.METER_ID_PROPERTY).order().math("_ + 1").as("a").where(__.not(__.V().hasLabel(FlowMeterFrame.FRAME_LABEL).has(FlowMeterFrame.SWITCH_PROPERTY, switchIdAsStr).values(FlowMeterFrame.METER_ID_PROPERTY).where(P.eq("a")))).select("a").limit(1)).getRawTraversal()) {
if (traversal.hasNext()) {
return traversal.tryNext().map(l -> ((Double) l).longValue()).map(MeterIdConverter.INSTANCE::toEntityAttribute);
}
} catch (Exception e) {
throw new PersistenceException("Failed to traverse", e);
}
try (GraphTraversal<?, ?> traversal = framedGraph().traverse(g -> g.V().hasLabel(FlowMeterFrame.FRAME_LABEL).has(FlowMeterFrame.METER_ID_PROPERTY, lowestMeterIdAsLong).has(FlowMeterFrame.SWITCH_PROPERTY, switchIdAsStr)).getRawTraversal()) {
if (!traversal.hasNext()) {
return Optional.of(lowestMeterId);
}
} catch (Exception e) {
throw new PersistenceException("Failed to traverse", e);
}
return Optional.empty();
}
use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.has in project open-kilda by telstra.
the class FermaFlowMeterRepository method exists.
@Override
public boolean exists(SwitchId switchId, MeterId meterId) {
String switchIdAsStr = SwitchIdConverter.INSTANCE.toGraphProperty(switchId);
Long meterIdAsLong = MeterIdConverter.INSTANCE.toGraphProperty(meterId);
try (GraphTraversal<?, ?> traversal = framedGraph().traverse(g -> g.V().hasLabel(FlowMeterFrame.FRAME_LABEL).has(FlowMeterFrame.METER_ID_PROPERTY, meterIdAsLong).has(FlowMeterFrame.SWITCH_PROPERTY, switchIdAsStr)).getRawTraversal()) {
return traversal.hasNext();
} catch (Exception e) {
throw new PersistenceException("Failed to traverse", e);
}
}
use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.has in project grakn by graknlabs.
the class ThingImpl method attributeRelationship.
private Relationship attributeRelationship(Attribute attribute, boolean isInferred) {
Schema.ImplicitType has = Schema.ImplicitType.HAS;
Schema.ImplicitType hasValue = Schema.ImplicitType.HAS_VALUE;
Schema.ImplicitType hasOwner = Schema.ImplicitType.HAS_OWNER;
// Is this attribute a key to me?
if (type().keys().anyMatch(rt -> rt.equals(attribute.type()))) {
has = Schema.ImplicitType.KEY;
hasValue = Schema.ImplicitType.KEY_VALUE;
hasOwner = Schema.ImplicitType.KEY_OWNER;
}
Label label = attribute.type().getLabel();
RelationshipType hasAttribute = vertex().tx().getSchemaConcept(has.getLabel(label));
Role hasAttributeOwner = vertex().tx().getSchemaConcept(hasOwner.getLabel(label));
Role hasAttributeValue = vertex().tx().getSchemaConcept(hasValue.getLabel(label));
if (hasAttribute == null || hasAttributeOwner == null || hasAttributeValue == null || type().plays().noneMatch(play -> play.equals(hasAttributeOwner))) {
throw GraknTxOperationException.hasNotAllowed(this, attribute);
}
EdgeElement attributeEdge = addEdge(AttributeImpl.from(attribute), Schema.EdgeLabel.ATTRIBUTE);
if (isInferred)
attributeEdge.property(Schema.EdgeProperty.IS_INFERRED, true);
return vertex().tx().factory().buildRelation(attributeEdge, hasAttribute, hasAttributeOwner, hasAttributeValue);
}
use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.has in project janusgraph by JanusGraph.
the class JanusGraphTest method testGraphCentricQueryProfilingWithLimitAdjusting.
@Test
public void testGraphCentricQueryProfilingWithLimitAdjusting() throws BackendException {
Runnable dataLoader = () -> {
final PropertyKey name = makeKey("name", String.class);
final JanusGraphIndex compositeNameIndex = mgmt.buildIndex("nameIdx", Vertex.class).addKey(name).buildCompositeIndex();
finishSchema();
newTx();
for (int i = 0; i < 3000; i++) {
tx.addVertex("name", "bob");
}
tx.commit();
};
clopen(option(ADJUST_LIMIT), false, option(HARD_MAX_LIMIT), 100000);
dataLoader.run();
newTx();
Metrics mCompSingle = tx.traversal().V().has("name", "bob").profile().next().getMetrics(0);
assertEquals(2, mCompSingle.getNested().size());
Metrics nested = (Metrics) mCompSingle.getNested().toArray()[1];
Map<String, String> nameIdxAnnotations = new HashMap() {
{
put("condition", "(name = bob)");
put("orders", "[]");
put("isFitted", "true");
put("isOrdered", "true");
// 100000 is HARD_MAX_LIMIT
put("query", "multiKSQ[1]@100000");
put("index", "nameIdx");
}
};
assertEquals(nameIdxAnnotations, nested.getAnnotations());
List<Metrics> backendQueryMetrics = nested.getNested().stream().map(m -> (Metrics) m).collect(Collectors.toList());
assertEquals(1, backendQueryMetrics.size());
Map<String, String> backendAnnotations = new HashMap() {
{
put("query", "nameIdx:multiKSQ[1]@100000");
put("limit", 100000);
}
};
assertEquals(backendAnnotations, backendQueryMetrics.get(0).getAnnotations());
assertTrue(backendQueryMetrics.get(0).getDuration(TimeUnit.MICROSECONDS) > 0);
close();
JanusGraphFactory.drop(graph);
clopen(option(ADJUST_LIMIT), false, option(HARD_MAX_LIMIT), Integer.MAX_VALUE);
dataLoader.run();
newTx();
mCompSingle = tx.traversal().V().has("name", "bob").profile().next().getMetrics(0);
assertEquals(2, mCompSingle.getNested().size());
nested = (Metrics) mCompSingle.getNested().toArray()[1];
nameIdxAnnotations = new HashMap() {
{
put("condition", "(name = bob)");
put("orders", "[]");
put("isFitted", "true");
put("isOrdered", "true");
put("query", "multiKSQ[1]");
put("index", "nameIdx");
}
};
assertEquals(nameIdxAnnotations, nested.getAnnotations());
backendQueryMetrics = nested.getNested().stream().map(m -> (Metrics) m).collect(Collectors.toList());
assertEquals(1, backendQueryMetrics.size());
backendAnnotations = new HashMap() {
{
put("query", "nameIdx:multiKSQ[1]");
}
};
assertEquals(backendAnnotations, backendQueryMetrics.get(0).getAnnotations());
assertTrue(backendQueryMetrics.get(0).getDuration(TimeUnit.MICROSECONDS) > 0);
close();
JanusGraphFactory.drop(graph);
clopen(option(ADJUST_LIMIT), true);
dataLoader.run();
newTx();
mCompSingle = tx.traversal().V().has("name", "bob").profile().next().getMetrics(0);
assertEquals("JanusGraphStep([],[name.eq(bob)])", mCompSingle.getName());
assertTrue(mCompSingle.getDuration(TimeUnit.MICROSECONDS) > 0);
assertEquals(2, mCompSingle.getNested().size());
nested = (Metrics) mCompSingle.getNested().toArray()[0];
assertEquals(QueryProfiler.CONSTRUCT_GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
nested = (Metrics) mCompSingle.getNested().toArray()[1];
assertEquals(QueryProfiler.GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
nameIdxAnnotations = new HashMap() {
{
put("condition", "(name = bob)");
put("orders", "[]");
put("isFitted", "true");
put("isOrdered", "true");
put("query", "multiKSQ[1]@4000");
put("index", "nameIdx");
}
};
assertEquals(nameIdxAnnotations, nested.getAnnotations());
backendQueryMetrics = nested.getNested().stream().map(m -> (Metrics) m).collect(Collectors.toList());
assertEquals(3, backendQueryMetrics.size());
int limit = 1000;
// due to LimitAdjustingIterator, there are three backend queries with limits 1000, 2000, and 4000, respectively.
for (Metrics backendQueryMetric : backendQueryMetrics) {
int queryLimit = limit;
backendAnnotations = new HashMap() {
{
put("query", "nameIdx:multiKSQ[1]@" + queryLimit);
put("limit", queryLimit);
}
};
assertEquals(backendAnnotations, backendQueryMetric.getAnnotations());
assertTrue(backendQueryMetric.getDuration(TimeUnit.MICROSECONDS) > 0);
limit = limit * 2;
}
}
use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.has in project janusgraph by JanusGraph.
the class JanusGraphIoRegistryTest method testTinkerPopPredicatesAsGryo.
/**
* This is necessary since we replace the default TinkerPop PSerializer
*
* @throws Exception
*/
@Test
public void testTinkerPopPredicatesAsGryo() throws SerializationException {
// Don't change this trivially. At the time of this writing (TinkerPop
// 3.2.3), this is how many P predicate methods were defined. If this
// fails, then JanusGraphPSerializer needs to be updated to add/remove
// any TinkerPop predicates!
assertEquals(15, Stream.of(P.class.getDeclaredMethods()).filter(m -> Modifier.isStatic(m.getModifiers())).filter(p -> {
log.debug("Predicate: {}", p);
return !p.isSynthetic();
}).count());
Graph graph = EmptyGraph.instance();
GraphTraversalSource g = graph.traversal();
// TinkerPop Predicates
GraphTraversal[] traversals = { g.V().has("age", within(5000)), g.V().has("age", without(5000)), g.V().has("age", within(5000, 45)), g.V().has("age", inside(45, 5000)), g.V().and(has("age", between(45, 5000)), has("name", within("pluto"))), g.V().or(has("age", between(45, 5000)), has("name", within("pluto", "neptune"))) };
serializationTest(traversals);
}
Aggregations