use of org.apache.tinkerpop.gremlin.structure.Element in project titan by thinkaurelius.
the class TitanGraphBaseTest method verifyElementOrder.
public static void verifyElementOrder(Iterator<? extends Element> elements, String key, Order order, int expectedCount) {
Comparable previous = null;
int count = 0;
while (elements.hasNext()) {
Element element = elements.next();
Comparable current = element.value(key);
if (previous != null) {
int cmp = previous.compareTo(current);
assertTrue(previous + " <> " + current + " @ " + count, order == Order.ASC ? cmp <= 0 : cmp >= 0);
}
previous = current;
count++;
}
assertEquals(expectedCount, count);
}
use of org.apache.tinkerpop.gremlin.structure.Element in project grakn by graknlabs.
the class GraqlTraversal method getGraphTraversal.
/**
* Get the {@code GraphTraversal} that this {@code GraqlTraversal} represents.
*/
// Because 'union' accepts an array, we can't use generics
@SuppressWarnings("unchecked")
public GraphTraversal<Vertex, Map<String, Element>> getGraphTraversal(EmbeddedGraknTx<?> tx, Set<Var> vars) {
if (fragments().size() == 1) {
// If there are no disjunctions, we don't need to union them and get a performance boost
ImmutableList<Fragment> list = Iterables.getOnlyElement(fragments());
return getConjunctionTraversal(tx, tx.getTinkerTraversal().V(), vars, list);
} else {
Traversal[] traversals = fragments().stream().map(list -> getConjunctionTraversal(tx, __.V(), vars, list)).toArray(Traversal[]::new);
// This is a sneaky trick - we want to do a union but tinkerpop requires all traversals to start from
// somewhere, so we start from a single arbitrary vertex.
GraphTraversal traversal = tx.getTinkerTraversal().V().limit(1).union(traversals);
return selectVars(traversal, vars);
}
}
use of org.apache.tinkerpop.gremlin.structure.Element in project sqlg by pietermartin.
the class SqlgRepeatStepBarrier method test.
public <S, E> boolean test(final Traverser.Admin<S> traverser, final Traversal.Admin<S, E> traversal) {
if (this.untilHasContainer != null) {
Element e = (Element) traverser.get();
return this.untilHasContainer.getPredicate().test(e.value(this.untilHasContainer.getKey()));
}
if (traversal.getSteps().size() == 1 && traversal.getSteps().get(0) instanceof HasStep) {
if (this.untilHasContainers == null) {
HasStep hasStep = (HasStep) traversal.getSteps().get(0);
this.untilHasContainers = hasStep.getHasContainers();
if (this.untilHasContainers.size() == 1) {
this.untilHasContainer = this.untilHasContainers.get(0);
Element e = (Element) traverser.get();
return this.untilHasContainer.getPredicate().test(e.value(this.untilHasContainer.getKey()));
}
}
for (HasContainer hasContainer : this.untilHasContainers) {
if (!hasContainer.test((Element) traverser.get())) {
return false;
}
}
return true;
} else {
final Traverser.Admin<S> split = traverser.split();
split.setSideEffects(traversal.getSideEffects());
split.setBulk(1l);
traversal.reset();
traversal.addStart(split);
// filter
return traversal.hasNext();
}
}
use of org.apache.tinkerpop.gremlin.structure.Element in project sqlg by pietermartin.
the class SqlgTraversalFilterStepBarrier method processNextStart.
@Override
protected Traverser.Admin<S> processNextStart() throws NoSuchElementException {
if (this.first) {
this.first = false;
Multimap<String, Traverser.Admin<S>> startRecordIds = LinkedListMultimap.create();
while (this.starts.hasNext()) {
Traverser.Admin<S> start = this.starts.next();
// However the traverser might have been created from a step and strategy that does not care for TraverserRequirement.ONE_BULK so we force it here.
if (start instanceof SqlgTraverser) {
((SqlgTraverser) start).setRequiresOneBulk(true);
}
List<Object> startObjects = start.path().objects();
StringBuilder recordIdConcatenated = new StringBuilder();
for (Object startObject : startObjects) {
if (startObject instanceof Element) {
Element e = (Element) startObject;
recordIdConcatenated.append(e.id().toString());
} else {
recordIdConcatenated.append(startObject.toString());
}
}
startRecordIds.put(recordIdConcatenated.toString(), start);
this.filterTraversal.addStart(start);
}
while (this.filterTraversal.hasNext()) {
Traverser.Admin<?> filterTraverser = this.filterTraversal.nextTraverser();
List<Object> filterTraverserObjects = filterTraverser.path().objects();
String startId = "";
for (Object filteredTraverserObject : filterTraverserObjects) {
if (filteredTraverserObject instanceof Element) {
Element e = (Element) filteredTraverserObject;
startId += e.id().toString();
} else {
startId += filteredTraverserObject.toString();
}
if (startRecordIds.containsKey(startId)) {
results.addAll(startRecordIds.get(startId));
startRecordIds.removeAll(startId);
}
if (startRecordIds.isEmpty()) {
break;
}
}
}
this.results.sort((o1, o2) -> {
SqlgTraverser x = (SqlgTraverser) o1;
SqlgTraverser y = (SqlgTraverser) o2;
return Long.compare(x.getStartElementIndex(), y.getStartElementIndex());
});
this.resultIterator = this.results.iterator();
}
if (this.resultIterator.hasNext()) {
Traverser.Admin<S> traverser = this.resultIterator.next();
return traverser;
} else {
// The standard TraversalFilterStep.filter calls TraversalUtil.test which normally resets the traversal for every incoming start.
reset();
throw FastNoSuchElementException.instance();
}
}
use of org.apache.tinkerpop.gremlin.structure.Element in project sqlg by pietermartin.
the class SqlgAndStepBarrier method processNextStart.
@Override
protected Traverser.Admin<S> processNextStart() {
if (this.first) {
this.first = false;
Multimap<String, Traverser.Admin<S>> startRecordIds = LinkedListMultimap.create();
while (this.starts.hasNext()) {
Traverser.Admin<S> start = this.starts.next();
if (start instanceof SqlgTraverser) {
((SqlgTraverser) start).setRequiresOneBulk(true);
}
List<Object> startObjects = start.path().objects();
StringBuilder recordIdConcatenated = new StringBuilder();
for (Object startObject : startObjects) {
if (startObject instanceof Element) {
Element e = (Element) startObject;
recordIdConcatenated.append(e.id().toString());
} else {
recordIdConcatenated.append(startObject.toString());
}
}
startRecordIds.put(recordIdConcatenated.toString(), start);
}
boolean first = true;
for (Traversal.Admin<S, ?> andTraversal : this.andTraversals) {
for (Traverser.Admin<S> start : startRecordIds.values()) {
andTraversal.addStart(start);
}
// Need to use a tmp map here to prevent duplicates.
// If the and traversals yields multiple results the start must only be added once.
Multimap<String, Traverser.Admin<S>> tmpStartRecordIds = LinkedListMultimap.create();
tmpStartRecordIds.putAll(startRecordIds);
List<Traverser.Admin<S>> tmpResult = new ArrayList<>();
while (andTraversal.hasNext()) {
Traverser.Admin<?> filterTraverser = andTraversal.nextTraverser();
List<Object> filterTraverserObjects = filterTraverser.path().objects();
String startId = "";
for (Object filteredTraverserObject : filterTraverserObjects) {
if (filteredTraverserObject instanceof Element) {
Element e = (Element) filteredTraverserObject;
startId += e.id().toString();
} else {
startId += filteredTraverserObject.toString();
}
if (tmpStartRecordIds.containsKey(startId)) {
if (first) {
this.results.addAll(tmpStartRecordIds.get(startId));
} else {
tmpResult.addAll(tmpStartRecordIds.get(startId));
}
tmpStartRecordIds.removeAll(startId);
}
}
}
if (!first) {
this.results = new ArrayList<>(CollectionUtils.intersection(this.results, tmpResult));
}
first = false;
}
this.results.sort((o1, o2) -> {
SqlgTraverser x = (SqlgTraverser) o1;
SqlgTraverser y = (SqlgTraverser) o2;
return Long.compare(x.getStartElementIndex(), y.getStartElementIndex());
});
this.resultIterator = this.results.iterator();
}
if (this.resultIterator.hasNext()) {
Traverser.Admin<S> traverser = this.resultIterator.next();
return traverser;
} else {
// The standard TraversalFilterStep.filter calls TraversalUtil.test which normally resets the traversal for every incoming start.
reset();
throw FastNoSuchElementException.instance();
}
}
Aggregations