/** * Adds custom ID functionality to the given graph, supporting either custom vertex IDs, custom edge IDs, or both. * * @param baseGraph the base graph which does not necessarily support custom IDs * @param supportVertexIds whether to support custom vertex IDs * @param supportEdgeIds whether to support custom edge IDs */ public IdGraph(KeyIndexableGraph baseGraph, boolean supportVertexIds, boolean supportEdgeIds) { this.baseGraph = checkNotNull(baseGraph, "baseGraph"); this.supportVertexIds = supportVertexIds; this.supportEdgeIds = supportEdgeIds; checkArgument(supportVertexIds || supportEdgeIds, "If neither custom vertex IDs nor custom edge IDs are supported, IdGraph can't help you!"); // Create indices if (supportVertexIds && !baseGraph.getIndexedKeys(Vertex.class).contains(ID)) { baseGraph.createKeyIndex(ID, Vertex.class); } if (supportEdgeIds && !baseGraph.getIndexedKeys(Edge.class).contains(ID)) { baseGraph.createKeyIndex(ID, Edge.class); } features = baseGraph.getFeatures().copyFeatures(); features.isWrapper = true; features.ignoresSuppliedIds = false; }
public Vertex getVertex(final Object id) { if (null == id) { throw new IllegalArgumentException("vertex identifier cannot be null"); } if (supportVertexIds) { final Iterable<Vertex> i = baseGraph.getVertices(ID, id); final Iterator<Vertex> iter = i.iterator(); if (!iter.hasNext()) { return null; } else { Vertex v = iter.next(); if (iter.hasNext()) { LOGGER.warning("multiple vertices exist with id '" + id + "'. Arbitarily choosing " + v); } return new IdVertex(v, this); } } else { Vertex base = baseGraph.getVertex(id); return null == base ? null : new IdVertex(base, this); } }
@Override public void shutdown() { baseGraph.shutdown(); }
private void createIndices() { if (supportVertexIds && !baseGraph.getIndexedKeys(Vertex.class).contains(ID)) { baseGraph.createKeyIndex(ID, Vertex.class); } if (supportEdgeIds && !baseGraph.getIndexedKeys(Edge.class).contains(ID)) { baseGraph.createKeyIndex(ID, Edge.class); } }
public void migrate(final KeyIndexableGraph source, final KeyIndexableGraph target) { for (Vertex vs : source.getVertices()) { Vertex vt = target.addVertex(vs.getId()); for (Vertex vs : source.getVertices()) { Vertex vt = target.getVertex(vs.getId()); Vertex cur = target.addVertex(null); target.addEdge(null, last, cur, Extendo.REST); } else { target.addEdge(null, vt, cur, Extendo.NOTES); Vertex v2t = target.getVertex(v2s.getId()); target.addEdge(null, cur, v2t, Extendo.FIRST); last = cur;
protected Set<String> getIndexedKeys(final Class<? extends Element> elementClass) { return ((KeyIndexableGraph) graph).getIndexedKeys(elementClass); }
private <T extends Element> void createKeyIndex(final KeyIndexableGraph kig, final String key, Class<T> elementClass, final Parameter... indexParameters) { LOGGER.log(Level.INFO, "creating {0} index on property key {1}", new Object[]{elementClass.getSimpleName(), key}); kig.createKeyIndex(key, elementClass, indexParameters); LOGGER.log(Level.FINEST, "created {0} index on property key {1}", new Object[]{elementClass.getSimpleName(), key}); }
public AtomList createAtomList() { return framedGraph.frame(this.getPropertyGraph().addVertex(null), AtomList.class); }
public Iterable<Vertex> getVertices(final String key, final Object value) { if (supportVertexIds && key.equals(ID)) { throw new IllegalArgumentException("index key " + ID + " is reserved by IdGraph"); } else { return new IdVertexIterable(baseGraph.getVertices(key, value), this); } }
/** * Add the historical vertex in the vertex versions chain * * @param latestGraphVersion The latest graph version * @param newVersion The new version to be committed * @param activeModifiedVertex The active vertex that was modified. * @param latestHistoricVertex Latest historical vertex (the previous * version of the newly created historic vertex) * @param newHistoricVertex The new historic vertex to be added in the chain */ private void addHistoricalVertexInChain(V latestGraphVersion, V newVersion, ActiveVersionedVertex<V> activeModifiedVertex, HistoricVersionedVertex<V> latestHistoricVertex, HistoricVersionedVertex newHistoricVertex) { newHistoricVertex.getRaw().setProperty(VEProps.REF_TO_LATEST_HISTORIC_ID_KEY, latestHistoricVertex.getHardId()); newHistoricVertex.getRaw().setProperty(VEProps.VALID_MAX_VERSION_PROP_KEY, latestGraphVersion); Edge prevEdge = ElementUtils.getSingleElement(latestHistoricVertex.getRaw().getEdges(Direction.OUT, VEProps.PREV_VERSION_LABEL)); if (prevEdge != null) { Vertex inVertex = prevEdge.getVertex(Direction.IN); getUneventableGraph().removeEdge(prevEdge); getBaseGraph().addEdge(edgeIdFactory.createId(), (Vertex) newHistoricVertex.getRaw(), inVertex, VEProps.PREV_VERSION_LABEL); } getBaseGraph().addEdge(edgeIdFactory.createId(), latestHistoricVertex.getRaw(), (Vertex) newHistoricVertex.getRaw(), VEProps.PREV_VERSION_LABEL); }
public void removeIsolatedAtoms(final Filter filter) { List<Vertex> toRemove = new LinkedList<Vertex>(); for (Vertex v : brain.getBrainGraph().getPropertyGraph().getVertices()) { if (null != v.getProperty("value") && !v.getEdges(Direction.IN).iterator().hasNext() && !v.getEdges(Direction.OUT).iterator().hasNext()) { Atom a = brain.getBrainGraph().getAtom(v); if (filter.isVisible(a)) { toRemove.add(v); } } } for (Vertex v : toRemove) { // note: we assume from the above that there are no dependent vertices (i.e. list nodes) to remove first brain.getBrainGraph().getPropertyGraph().removeVertex(v); } }
public Edge getEdge(final Object id) { if (null == id) { throw new IllegalArgumentException("edge identifier cannot be null"); } if (supportEdgeIds) { Iterable<Edge> i = baseGraph.getEdges(ID, id); Iterator<Edge> iter = i.iterator(); if (!iter.hasNext()) { return null; } else { Edge e = iter.next(); if (iter.hasNext()) { LOGGER.warning("multiple edges exist with id '" + id + "'. Arbitarily choosing " + e); } return new IdEdge(e, this); } } else { Edge base = baseGraph.getEdge(id); return null == base ? null : new IdEdge(base, this); } }
/** * Add a plain edge to the graph. * * Note: this is not an eventable vertex. * * @param id The id of the edge to set, if null, new id will be generated. * @return plain created edge. */ private Edge addPlainEdgeToGraph(Object id, Vertex out, Vertex in, String label) { validateNewId(id, Edge.class); final Edge edge; // TODO: Ensure we get raw vertices here. Object idVal = id == null ? edgeIdFactory.createId() : id; if (isNaturalIds()) { // we create an id just in case the underline doesn't ignore // supplied ids // and cannot recieve null but we ignore this id in the logic. edge = getUneventableGraph().addEdge(edgeIdFactory.createId(), out, in, label); edge.setProperty(VEProps.NATURAL_EDGE_ID_PROP_KEY, idVal); } else { edge = getUneventableGraph().addEdge(idVal, out, in, label); } return edge; }
public Atom getAtom(final String key) { Vertex v = this.getPropertyGraph().getVertex(key); return null == v ? null : getAtom(v); }
@Override public <E extends Element> void dropKeyIndex(String key, Class<E> elementClass) { checkIndexableKey(key, elementClass); baseGraph.dropKeyIndex(key, elementClass); }
public VersionedGraphBase(T baseGraph, GraphIdentifierBehavior<V> identifierBehavior, Configuration conf) { Preconditions.checkNotNull(baseGraph, "Base graph must be set."); Preconditions.checkNotNull(identifierBehavior, "Identifier behavior must be set."); this.underlineGraph = baseGraph; this.identifierBehavior = identifierBehavior; this.identifierBehavior.setGraph(this); if (conf == null) { this.conf = new Configuration.ConfBuilder().build(); } else { this.conf = conf; } this.features = baseGraph.getFeatures().copyFeatures(); features.isWrapper = true; if (this.conf.useNaturalIds || (this.conf.useNaturalIdsOnlyIfSuppliedIdsAreIgnored && baseGraph.getFeatures().ignoresSuppliedIds)) { features.ignoresSuppliedIds = true; } }
public HistoricVersionedEdge<V> getEdgeByHardId(Object id) { Preconditions.checkNotNull(id, "id must be set."); Edge edge; if (isNaturalIds()) { edge = getSingleEdge(VEProps.NATURAL_EDGE_ID_PROP_KEY, id); } else { edge = getBaseGraph().getEdge(id); } if (edge != null) { if (edge instanceof HistoricVersionedEdge) { return (HistoricVersionedEdge<V>) edge; } else { utils.ensureHistoricType(edge); V start = utils.getStartVersion(edge); return new HistoricVersionedEdge<V>(edge, this, Range.range(start, start)); } } else { log.debug("Edge with [{}] was not found.", id); return null; } }
if (!keyIndexedGraph.getIndexedKeys(Vertex.class).contains(VEProps.NATURAL_VERTEX_ID_PROP_KEY)) { keyIndexedGraph.createKeyIndex(VEProps.NATURAL_VERTEX_ID_PROP_KEY, Vertex.class); if (!keyIndexedGraph.getIndexedKeys(Edge.class).contains(VEProps.NATURAL_EDGE_ID_PROP_KEY)) { keyIndexedGraph.createKeyIndex(VEProps.NATURAL_EDGE_ID_PROP_KEY, Edge.class);
public <T extends Element> Set<String> getIndexedKeys(Class<T> elementClass) { return ((KeyIndexableGraph) this.baseGraph).getIndexedKeys(elementClass); } }
@Override @SuppressWarnings("rawtypes") // indexParameters public <E extends Element> void createKeyIndex(String key, Class<E> elementClass, Parameter... indexParameters) { checkIndexableKey(key, elementClass); baseGraph.createKeyIndex(key, elementClass, indexParameters); }