@Override default Collection<Vertex> fromFields(Map<String, Object> fields) { Vertex element = createElement(fields); if(element == null) return null; return Collections.singleton(element); }
@Override public Set<String> toFields(Set<String> propertyKeys) { Set<String> fields = super.toFields(propertyKeys); Set<String> outFields = outVertexSchema.toFields(propertyKeys); fields.addAll(outFields); Set<String> inFields = inVertexSchema.toFields(propertyKeys); fields.addAll(inFields); return fields; }
protected PredicatesHolder getVertexPredicates(List<Vertex> vertices, Direction direction) { PredicatesHolder outPredicates = this.outVertexSchema.toPredicates(vertices); PredicatesHolder inPredicates = this.inVertexSchema.toPredicates(vertices); if(direction.equals(Direction.OUT)) return outPredicates; if(direction.equals(Direction.IN)) return inPredicates; return PredicatesHolderFactory.or(inPredicates, outPredicates); }
/** * Receives a list of vertices and converts them to predicates * @param vertices A list of vertices * @return Predicate holder */ default PredicatesHolder toPredicates(List<? extends Vertex> vertices) { if(vertices == null || vertices.size() == 0) return PredicatesHolderFactory.abort(); HashSet<PredicatesHolder> predicates = new HashSet<>(); vertices.stream().collect(Collectors.groupingBy(Vertex::label)).forEach((label, labelVertices) -> { HasContainer labelPredicate = new HasContainer(T.label.getAccessor(), P.eq(label)); HasContainer ids = new HasContainer(T.id.getAccessor(), P.within(labelVertices.stream().map(Vertex::id).collect(Collectors.toSet()))); PredicatesHolder labelPredicates = PredicatesHolderFactory.and(ids, labelPredicate); PredicatesHolder toPredicates = toPredicates(labelPredicates); predicates.add(toPredicates); }); return PredicatesHolderFactory.or(predicates); } }
@Override public Collection<Edge> fromFields(Map<String, Object> fields) { Map<String, Object> edgeProperties = getProperties(fields); if(edgeProperties == null) return null; Vertex outVertex = outVertexSchema.createElement(fields); if(outVertex == null) return null; Vertex inVertex = inVertexSchema.createElement(fields); if(inVertex == null) return null; UniEdge uniEdge = new UniEdge(edgeProperties, outVertex, inVertex, this, graph); return Collections.singleton(uniEdge); }
protected PredicatesHolder getVertexPredicates(List<Vertex> vertices, Direction direction) { PredicatesHolder outPredicates = this.outVertexSchema.toPredicates(vertices); PredicatesHolder inPredicates = this.inVertexSchema.toPredicates(vertices); if(direction.equals(Direction.OUT) && outPredicates.notAborted()) return outPredicates; if(direction.equals(Direction.IN) && inPredicates.notAborted()) return inPredicates; if (outPredicates.notAborted() && inPredicates.notAborted()) return PredicatesHolderFactory.or(inPredicates, outPredicates); else if (outPredicates.isAborted()) return inPredicates; else if (inPredicates.isAborted()) return outPredicates; else return PredicatesHolderFactory.abort(); }
@Override public Set<String> toFields(Set<String> propertyKeys) { Set<String> fields = super.toFields(propertyKeys); Set<String> outFields = outVertexSchema.toFields(propertyKeys); fields.addAll(outFields); Set<String> inFields = inVertexSchema.toFields(propertyKeys); fields.addAll(inFields); return fields; }
@Override public Collection<Edge> fromFields(Map<String, Object> fields) { Map<String, Object> edgeProperties = getProperties(fields); if(edgeProperties == null) return null; Vertex outVertex = outVertexSchema.createElement(fields); if(outVertex == null) return null; Vertex inVertex = inVertexSchema.createElement(fields); if(inVertex == null) return null; UniEdge uniEdge = new UniEdge(edgeProperties, outVertex, inVertex, this, graph); return Collections.singleton(uniEdge); }
protected PredicatesHolder getVertexPredicates(List<Vertex> vertices, Direction direction) { PredicatesHolder outPredicates = this.outVertexSchema.toPredicates(vertices); PredicatesHolder inPredicates = this.inVertexSchema.toPredicates(vertices); if (direction.equals(Direction.OUT) && outPredicates.notAborted()) return outPredicates; if (direction.equals(Direction.IN) && inPredicates.notAborted()) return inPredicates; if (outPredicates.notAborted() && inPredicates.notAborted()) return PredicatesHolderFactory.or(inPredicates, outPredicates); else if (outPredicates.isAborted()) return inPredicates; else if (inPredicates.isAborted()) return outPredicates; else return PredicatesHolderFactory.abort(); } }
@Override protected Set<String> toFields() { Set<String> edgeFields = super.toFields(); Set<String> outVertexFields = outVertexSchema instanceof RestVertex ? ((RestVertex) outVertexSchema).toFields() : outVertexSchema.toFields(Collections.emptySet()); Set<String> inVertexFields = inVertexSchema instanceof RestVertex ? ((RestVertex) inVertexSchema).toFields() : inVertexSchema.toFields(Collections.emptySet()); return Stream.of(edgeFields, outVertexFields, inVertexFields).flatMap(Collection::stream).collect(Collectors.toSet()); }
@Override protected Edge create(Map<String, Object> fields) { Map<String, Object> properties = getProperties(fields); if (properties == null) return null; return new UniEdge(properties, outVertexSchema.createElement(fields), inVertexSchema.createElement(fields), this, graph); }
public QueryBuilder createQueryBuilder(SearchVertexQuery query) { PredicatesHolder edgePredicates = this.toPredicates(query.getPredicates()); if(edgePredicates.isAborted()) return null; PredicatesHolder childPredicates = childVertexSchema.toPredicates(query.getVertices()); childPredicates = PredicatesHolderFactory.and(edgePredicates, childPredicates); QueryBuilder childQuery = createNestedQueryBuilder(childPredicates); if(query.getDirection().equals(parentDirection.opposite())) { if (childPredicates.isAborted()) return null; return childQuery; } else if (!query.getDirection().equals(Direction.BOTH)) childQuery = null; PredicatesHolder parentPredicates = parentVertexSchema.toPredicates(query.getVertices()); QueryBuilder parentQuery = createQueryBuilder(parentPredicates); if(parentQuery != null) { // if (parentPredicates.isAborted()) return null; QueryBuilder edgeQuery = createNestedQueryBuilder(edgePredicates); if (edgeQuery != null) { parentQuery = QueryBuilders.boolQuery().must(parentQuery).must(edgeQuery); } } if(query.getDirection().equals(parentDirection) && parentPredicates.notAborted()) return parentQuery; else if(childQuery == null && parentPredicates.notAborted()) return parentQuery; else if(parentQuery == null && childPredicates.notAborted()) return childQuery; else if(parentPredicates.isAborted() && childPredicates.isAborted()) return null; else return QueryBuilders.boolQuery().should(parentQuery).should(childQuery); }
@Override public Set<String> toFields(Set<String> propertyKeys) { Set<String> fields = super.toFields(propertyKeys).stream() .map(key -> path + "." + key).collect(Collectors.toSet()); Set<String> parentFields = parentVertexSchema.toFields(propertyKeys); fields.addAll(parentFields); Set<String> childFields = childVertexSchema.toFields(propertyKeys); fields.addAll(childFields); return fields; }
@Override public Collection<UniEdge> fromVertex(UniVertex parentVertex, Map<String, Object> fields) { Map edgeProperties = this.getProperties(fields); Object inVertex; Object outVertex; if (edgeProperties == null) return null; outVertex = this.outVertexSchema.equals(this.parentVertexSchema) ? parentVertex : this.outVertexSchema.createElement(fields); inVertex = this.outVertexSchema.equals(this.parentVertexSchema) ? parentVertex : this.inVertexSchema.createElement(fields); if (inVertex == null || outVertex == null) return null; UniEdge uniEdge = new UniEdge(edgeProperties, (Vertex)outVertex, (Vertex)inVertex, this, this.graph); return Collections.singleton(uniEdge); } }
@Override public BaseRequest addElement(Edge element) throws NoSuchElementException{ Map<String, Object> outVertexFields = outVertexSchema.toFields(element.outVertex()); Map<String, Object> inVertexFields = inVertexSchema.toFields(element.inVertex()); Map<String, Object> fields = toFields(element); if (fields == null || outVertexFields == null || inVertexFields == null) throw new NoSuchElementException(); Map<String, Object> stringObjectMap = Stream.of(outVertexFields.entrySet(), inVertexFields.entrySet(), fields.entrySet()).flatMap(Collection::stream) .map(entry -> { String[] split = entry.getKey().split("\\."); return new HashMap.SimpleEntry<>(split[split.length - 1], entry.getValue()); }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); Map<String, Object> urlMap = new HashMap<>(); urlMap.putAll(stringObjectMap); urlMap.put("resource", resource); return insertElement(urlMap, stringObjectMap); }
private UniEdge createEdge(Vertex parentVertex, Map<String, Object> edgeFields) { Map<String, Object> edgeProperties = getProperties(edgeFields); if (edgeProperties == null) return null; Vertex childVertex = childVertexSchema.createElement(edgeFields); if (childVertex == null) return null; UniEdge edge = new UniEdge(edgeProperties, parentDirection.equals(Direction.OUT) ? parentVertex : childVertex, parentDirection.equals(Direction.IN) ? parentVertex : childVertex, this, graph); return edge; }
private Map<String, Object> getVertexFields(Edge edge, Direction direction) { VertexSchema vertexSchema = direction.equals(parentDirection) ? parentVertexSchema : childVertexSchema; Vertex parent = edge.vertices(direction).next(); return vertexSchema.toFields(parent); }
@Override public Map<String, Object> toFields(Edge edge) { Map<String, Object> edgeFields = getFields(edge); Map<String, Object> inFields = inVertexSchema.toFields(edge.inVertex()); Map<String, Object> outFields = outVertexSchema.toFields(edge.outVertex()); return ConversionUtils.merge(Lists.newArrayList(edgeFields, inFields, outFields), this::mergeFields, false); }
@Override public Map<String, Object> toFields(Edge edge) { Map<String, Object> edgeFields = getFields(edge); Map<String, Object> inFields = inVertexSchema.toFields(edge.inVertex()); Map<String, Object> outFields = outVertexSchema.toFields(edge.outVertex()); return ConversionUtils.merge(Lists.newArrayList(edgeFields, inFields, outFields), this::mergeFields, false); }