@Override public Stream<E> toStream() { return QueryUtils.prepareTerminalOperation(this, true).toStream(); }
@Override public Stream<EObject> toStream() { QueryStepBuilderInternal<S, EObject> finalStep = this.reifyEObjects(); GraphTraversal<S, EObject> traversal = QueryUtils.prepareTerminalOperation(finalStep, true); return traversal.toStream(); }
/** * @param commonVars set of variables of interest * @param tx the graph to get results from * @param graqlTraversal gral traversal corresponding to the provided pattern * @return resulting answer stream */ public static Stream<ConceptMap> streamWithTraversal( Set<Var> commonVars, EmbeddedGraknTx<?> tx, GraqlTraversal graqlTraversal ) { Set<Var> vars = Sets.filter(commonVars, Var::isUserDefinedName); GraphTraversal<Vertex, Map<String, Element>> traversal = graqlTraversal.getGraphTraversal(tx, vars); return traversal.toStream() .map(elements -> makeResults(vars, tx, elements)) .distinct() .sequential() .map(ConceptMapImpl::new); }
@Override public List<QuickSearchResult> doQuickSearch(Collection collection, QuickSearch quickSearch, int limit) { return asQuickSearchResult( collection, indexHandler.findByQuickSearch(collection, quickSearch).limit(limit).toStream() ); }
@Override public List<QuickSearchResult> doKeywordQuickSearch(Collection collection, String keywordType, QuickSearch quickSearch, int limit) { return asQuickSearchResult( collection, indexHandler.findKeywordsByQuickSearch(collection, quickSearch, keywordType).limit(limit).toStream() ); }
@Override public Stream<Map<String, Concept>> stream(Optional<MindmapsGraph> optionalGraph, Optional<MatchOrder> order) { MindmapsGraph graph = optionalGraph.orElseThrow( () -> new IllegalStateException(ErrorMessage.NO_GRAPH.getMessage()) ); for (VarAdmin var : pattern.getVars()) { var.getProperties().forEach(property -> ((VarPropertyInternal) property).checkValid(graph, var)); } GraphTraversal<Vertex, Map<String, Vertex>> traversal = getQuery(graph, order).getTraversal(); return traversal.toStream().map(vertices -> makeResults(graph, vertices)).sequential(); }
private String getWwDocumentsQuickSearchValue(Collection wwDocumentsCollection, Collection wwPersonsCollection, long nodeId, GraphTraversalSource traversalS) { String docCaption = getGenericQuickSearchValue(wwDocumentsCollection, nodeId, traversalS); String authors = traversalS.V(nodeId) .outE("isCreatedBy").has("wwrelation_accepted", true).has("isLatest", true).otherV() .toStream() .map(v -> getGenericQuickSearchValue(wwPersonsCollection, (long) v.id(), traversalS)) .sorted() .collect(Collectors.joining("; ")); return authors + " " + docCaption; }
private String getWwDocumentsQuickSearchValue(Collection wwDocumentsCollection, Collection wwPersonsCollection, long nodeId, GraphTraversalSource traversalS) { String docCaption = getGenericQuickSearchValue(wwDocumentsCollection, nodeId, traversalS); String authors = traversalS.V(nodeId) .outE("isCreatedBy").has("wwrelation_accepted", true).has("isLatest", true).otherV() .toStream() .map(v -> getGenericQuickSearchValue(wwPersonsCollection, (Long) v.id(), traversalS)) .sorted() .collect(Collectors.joining(" ")); return authors + " " + docCaption; }
private String getGenericQuickSearchValue(Collection collection, long nodeId, GraphTraversalSource traversalS) { return traversalS.V(nodeId) .union(collection.getDisplayName().traversalJson()) .map(nodeOrException -> unwrapError(nodeId, nodeOrException)) .toStream().findAny() .orElseGet(() -> { LOG.warn(databaseInvariant, "Displayname traversal resulted in no results vertexId={} collection={} propertyType={}", nodeId, collection.getCollectionName(), collection.getDisplayName().getUniqueTypeId()); return "(empty)"; }); }
private String getGenericQuickSearchValue(Collection collection, long nodeId, GraphTraversalSource traversalS) { return traversalS.V(nodeId) .union(collection.getDisplayName().traversalJson()) .map(nodeOrException -> unwrapError(nodeId, nodeOrException)) .toStream().findAny() .orElseGet(() -> { LOG.debug("Displayname traversal resulted in no results vertexId={} collection={} propertyType={}", nodeId, collection.getCollectionName(), collection.getDisplayName().getUniqueTypeId()); return "";//this value is not displayed anywhere, just used for search }); }
@Override public DataStream<ReadEntity> getCollection(Collection collection, int start, int rows, boolean withRelations, CustomEntityProperties customEntityProperties, CustomRelationProperties customRelationProperties) { GraphTraversal<Vertex, Vertex> entities = getCurrentEntitiesFor(collection.getEntityTypeName()).range(start, start + rows); TinkerPopToEntityMapper tinkerPopToEntityMapper = new TinkerPopToEntityMapper(collection, traversal, mappings, customEntityProperties, customRelationProperties); return new TinkerPopGetCollection( entities.toStream().map(vertex -> tinkerPopToEntityMapper.mapEntity(vertex, withRelations)) ); }
private static Set<String> filterVerticesWithoutIndex(final ChronoGraph graph, final String property, final String value) { checkNotNull(graph, "Precondition violation - argument 'graph' must not be NULL!"); checkNotNull(property, "Precondition violation - argument 'property' must not be NULL!"); checkNotNull(value, "Precondition violation - argument 'value' must not be NULL!"); return graph.traversal().V().toStream().filter(v -> v.property(property).isPresent()).filter(v -> v.value(property).equals(value)).map(v -> (String) v.id()).collect(Collectors.toSet()); }
private static Set<String> filterVerticesWithIndex(final ChronoGraph graph, final String property, final String value) { checkNotNull(graph, "Precondition violation - argument 'graph' must not be NULL!"); checkNotNull(property, "Precondition violation - argument 'property' must not be NULL!"); checkNotNull(value, "Precondition violation - argument 'value' must not be NULL!"); return graph.traversal().V().has(property, value).toStream().map(v -> (String) v.id()).collect(Collectors.toSet()); }
@Override public CompletableFuture<CypherResultSet> submitAsync(String cypher, Map<String, ?> parameters) { Map<String, Object> normalizedParameters = ParameterNormalizer.normalize(parameters); CypherAst ast; try { ast = CypherAst.parse(cypher, normalizedParameters); } catch (Exception e) { return completedFuture(exceptional(e)); } if (ast.getOptions().contains(EXPLAIN)) { return completedFuture(explain(ast)); } DefaultGraphTraversal g = new DefaultGraphTraversal(gts.clone()); Translator<GraphTraversal, P> translator = Translator.builder().traversal(g).enableCypherExtensions().build(); GraphTraversal<?, ?> traversal = ast.buildTranslation(translator); ReturnNormalizer returnNormalizer = ReturnNormalizer.create(ast.getReturnTypes()); List<Result> results = traversal.toStream() .map(returnNormalizer::normalize) .map(Result::new) .collect(toList()); return completedFuture(new CypherResultSet(results.iterator())); } }
private Stream<Relationship> reifiedRelations(Role... roles){ GraphTraversal<Vertex, Vertex> traversal = vertex().tx().getTinkerTraversal().V(). has(Schema.VertexProperty.ID.name(), id().getValue()); if(roles.length == 0){ traversal.in(Schema.EdgeLabel.ROLE_PLAYER.getLabel()); } else { Set<Integer> roleTypesIds = Arrays.stream(roles).map(r -> r.labelId().getValue()).collect(toSet()); traversal.inE(Schema.EdgeLabel.ROLE_PLAYER.getLabel()). has(Schema.EdgeProperty.ROLE_LABEL_ID.name(), P.within(roleTypesIds)).outV(); } return traversal.toStream().map(vertex -> vertex().tx().<Relationship>buildConcept(vertex)); }
/** * Castings are retrieved from the perspective of the {@link Relationship} * * @param roles The {@link Role} which the {@link Thing}s are playing * @return The {@link Casting} which unify a {@link Role} and {@link Thing} with this {@link Relationship} */ public Stream<Casting> castingsRelation(Role... roles){ Set<Role> roleSet = new HashSet<>(Arrays.asList(roles)); if(roleSet.isEmpty()){ return vertex().getEdgesOfType(Direction.OUT, Schema.EdgeLabel.ROLE_PLAYER). map(edge -> Casting.withRelationship(edge, owner)); } //Traversal is used so we can potentially optimise on the index Set<Integer> roleTypesIds = roleSet.stream().map(r -> r.labelId().getValue()).collect(Collectors.toSet()); return vertex().tx().getTinkerTraversal().V(). has(Schema.VertexProperty.ID.name(), id().getValue()). outE(Schema.EdgeLabel.ROLE_PLAYER.getLabel()). has(Schema.EdgeProperty.RELATIONSHIP_TYPE_LABEL_ID.name(), type().labelId().getValue()). has(Schema.EdgeProperty.ROLE_LABEL_ID.name(), P.within(roleTypesIds)). toStream(). map(edge -> vertex().tx().factory().buildEdgeElement(edge)). map(edge -> Casting.withRelationship(edge, owner)); }
private Stream<Relationship> relationEdges(){ //Unfortunately this is a slow process return roles(). flatMap(Role::players). flatMap(type ->{ //Traversal is used here to take advantage of vertex centric index return vertex().tx().getTinkerTraversal().V(). has(Schema.VertexProperty.ID.name(), type.id().getValue()). in(Schema.EdgeLabel.SHARD.getLabel()). in(Schema.EdgeLabel.ISA.getLabel()). outE(Schema.EdgeLabel.ATTRIBUTE.getLabel()). has(Schema.EdgeProperty.RELATIONSHIP_TYPE_LABEL_ID.name(), labelId().getValue()). toStream(). map(edge -> vertex().tx().factory().<Relationship>buildConcept(edge)); }); }
private List<Map<String, Object>> submitAndGet(String cypher, Map<String, Object> parameters) { DefaultGraphTraversal g = new DefaultGraphTraversal(gts); Translator<GraphTraversal, P> translator = Translator.builder() .traversal(g) .enableCypherExtensions() .build(); CypherAst ast = CypherAst.parse(cypher, parameters, procedureContext.getSignatures()); Seq<GremlinStep> ir = ast.translate(flavor, procedureContext); GraphTraversal<?, ?> traversal = TranslationWriter.write(ir, translator, parameters); ReturnNormalizer returnNormalizer = ReturnNormalizer.create(ast.getReturnTypes()); return traversal.toStream() .map(returnNormalizer::normalize) .collect(toList()); }
assertEquals(0, g.traversal().V().toStream().filter(v -> v.value("name").equals("Ghost")).count()); assertEquals(0, g.traversal().V().has("kind", "person").toStream().filter(v -> v.value("name").equals("Ghost")) .count()); g.tx().close(); assertEquals(0, g.traversal().V().toStream().filter(v -> v.value("name").equals("Ghost")).count()); assertEquals(0, g.traversal().V().has("kind", "person").toStream().filter(v -> v.value("name").equals("Ghost")) .count()); g.tx().close();