public EdgeBuilderByVertexId prepareEdge(Edge edge) { return prepareEdge( edge.getId(), edge.getVertexId(Direction.OUT), edge.getVertexId(Direction.IN), edge.getLabel(), edge.getVisibility() ); }
private Vertex getOtherVertexFromEdge(Edge edge, Authorizations authorizations) { if (edge.getVertexId(Direction.IN).equals(getId())) { return edge.getVertex(Direction.OUT, authorizations); } if (edge.getVertexId(Direction.OUT).equals(getId())) { return edge.getVertex(Direction.IN, authorizations); } throw new IllegalStateException("Edge does not contain vertex on either end"); }
void addOutEdge(Edge edge) { this.outEdges.put(edge.getId(), new EdgeInfo(edge.getLabel(), edge.getVertexId(Direction.IN))); }
private JSONObject edgeToJson(Edge edge) { JSONObject json = elementToJson(edge); json.put("outVertexId", edge.getVertexId(Direction.OUT)); json.put("inVertexId", edge.getVertexId(Direction.IN)); json.put("label", edge.getLabel()); return json; }
assertEquals("label1", e.getLabel()); assertEquals(1, count(e.getProperties())); assertEquals("valueA", e.getPropertyValues("propA").iterator().next()); assertEquals(1, count(v1.getEdges(Direction.OUT, AUTHORIZATIONS_A))); assertEquals("label1", single(v1.getEdgeLabels(Direction.OUT, AUTHORIZATIONS_A))); assertEquals("label1", single(v2.getEdgeLabels(Direction.IN, AUTHORIZATIONS_A))); e.prepareMutation() .alterEdgeLabel("label2") .save(AUTHORIZATIONS_A); e = graph.getEdge("e1", AUTHORIZATIONS_A); assertEquals("label2", e.getLabel()); assertEquals(1, count(e.getProperties())); assertEquals("valueA", e.getPropertyValues("propA").iterator().next()); v1 = graph.getVertex("v1", AUTHORIZATIONS_A); assertEquals(1, count(v1.getEdges(Direction.OUT, AUTHORIZATIONS_A))); graph.prepareEdge(e.getId(), e.getVertexId(Direction.OUT), e.getVertexId(Direction.IN), e.getLabel(), e.getVisibility()) .alterEdgeLabel("label3") .save(AUTHORIZATIONS_A); e = graph.getEdge("e1", AUTHORIZATIONS_A); assertEquals("label3", e.getLabel()); assertEquals(1, count(e.getProperties())); assertEquals("valueA", e.getPropertyValues("propA").iterator().next()); v1 = graph.getVertex("v1", AUTHORIZATIONS_A); assertEquals(1, count(v1.getEdges(Direction.OUT, AUTHORIZATIONS_A)));
@Override public void markEdgeVisible(Edge edge, Visibility visibility, Authorizations authorizations) { checkNotNull(edge); Vertex out = edge.getVertex(Direction.OUT, FetchHint.ALL_INCLUDING_HIDDEN, authorizations); if (out == null) { throw new SecureGraphException(String.format("Unable to mark edge visible %s, can't find out vertex %s", edge.getId(), edge.getVertexId(Direction.OUT))); } Vertex in = edge.getVertex(Direction.IN, FetchHint.ALL_INCLUDING_HIDDEN, authorizations); if (in == null) { throw new SecureGraphException(String.format("Unable to mark edge visible %s, can't find in vertex %s", edge.getId(), edge.getVertexId(Direction.IN))); } ColumnVisibility columnVisibility = visibilityToAccumuloVisibility(visibility); Mutation outMutation = new Mutation(AccumuloConstants.VERTEX_ROW_KEY_PREFIX + out.getId()); outMutation.putDelete(AccumuloVertex.CF_OUT_EDGE_HIDDEN, new Text(edge.getId()), columnVisibility); Mutation inMutation = new Mutation(AccumuloConstants.VERTEX_ROW_KEY_PREFIX + in.getId()); inMutation.putDelete(AccumuloVertex.CF_IN_EDGE_HIDDEN, new Text(edge.getId()), columnVisibility); addMutations(getVerticesWriter(), outMutation, inMutation); // Remove everything else related to edge. addMutations(getEdgesWriter(), getMarkVisibleRowMutation(AccumuloConstants.EDGE_ROW_KEY_PREFIX + edge.getId(), columnVisibility)); if (out instanceof AccumuloVertex) { ((AccumuloVertex) out).addOutEdge(edge); } if (in instanceof AccumuloVertex) { ((AccumuloVertex) in).addInEdge(edge); } if (hasEventListeners()) { queueEvent(new MarkVisibleEdgeEvent(this, edge)); } }
@Override protected String convert(Edge o) { return o.getLabel(); } });
@Override protected String convert(Edge edge) { return edge.getId(); } }
@Override public void removeEdge(Edge edge, Authorizations authorizations) { checkNotNull(edge); getSearchIndex().removeElement(this, edge, authorizations); ColumnVisibility visibility = visibilityToAccumuloVisibility(edge.getVisibility()); Mutation outMutation = new Mutation(AccumuloConstants.VERTEX_ROW_KEY_PREFIX + edge.getVertexId(Direction.OUT)); outMutation.putDelete(AccumuloVertex.CF_OUT_EDGE, new Text(edge.getId()), visibility); Mutation inMutation = new Mutation(AccumuloConstants.VERTEX_ROW_KEY_PREFIX + edge.getVertexId(Direction.IN)); inMutation.putDelete(AccumuloVertex.CF_IN_EDGE, new Text(edge.getId()), visibility); addMutations(getVerticesWriter(), outMutation, inMutation); // Remove everything else related to edge. addMutations(getEdgesWriter(), getDeleteRowMutation(AccumuloConstants.EDGE_ROW_KEY_PREFIX + edge.getId())); if (hasEventListeners()) { queueEvent(new RemoveEdgeEvent(this, edge)); } }
@Override public void markEdgeHidden(Edge edge, Visibility visibility, Authorizations authorizations) { if (!((InMemoryEdge) edge).canRead(authorizations)) { return; } Vertex inVertex = getVertex(edge.getVertexId(Direction.IN), authorizations); checkNotNull(inVertex, "Could not find in vertex: " + edge.getVertexId(Direction.IN)); Vertex outVertex = getVertex(edge.getVertexId(Direction.OUT), authorizations); checkNotNull(outVertex, "Could not find out vertex: " + edge.getVertexId(Direction.OUT)); this.edges.get(edge.getId()).addHiddenVisibility(visibility); getSearchIndex().addElement(this, edge, authorizations); if (hasEventListeners()) { fireGraphEvent(new MarkHiddenEdgeEvent(this, edge)); } }
@Override public void addElement(SearchIndex searchIndex, Graph graph, Element element, Authorizations authorizations) { if (!getConfig().isUpdateEdgeBoost()) { return; } if (!(element instanceof Edge)) { return; } Element vOut = ((Edge) element).getVertex(Direction.OUT, authorizations); if (vOut != null) { searchIndex.addElement(graph, vOut, authorizations); } Element vIn = ((Edge) element).getVertex(Direction.IN, authorizations); if (vIn != null) { searchIndex.addElement(graph, vIn, authorizations); } }
@Override protected boolean isIncluded(Edge edge) { switch (direction) { case IN: return edge.getVertexId(Direction.IN).equals(getId()); case OUT: return edge.getVertexId(Direction.OUT).equals(getId()); default: return true; } } };
@Test public void testAddEdgeWithProperties() { Vertex v1 = graph.addVertex("v1", VISIBILITY_A, AUTHORIZATIONS_A); Vertex v2 = graph.addVertex("v2", VISIBILITY_A, AUTHORIZATIONS_A); Edge addedEdge = graph.prepareEdge("e1", v1, v2, "label1", VISIBILITY_A) .setProperty("propA", "valueA", VISIBILITY_A) .setProperty("propB", "valueB", VISIBILITY_B) .save(AUTHORIZATIONS_A_AND_B); Edge e = graph.getEdge("e1", AUTHORIZATIONS_A); assertEquals(1, count(e.getProperties())); assertEquals("valueA", e.getPropertyValues("propA").iterator().next()); assertEquals(0, count(e.getPropertyValues("propB"))); e = graph.getEdge("e1", AUTHORIZATIONS_A_AND_B); assertEquals(2, count(e.getProperties())); assertEquals("valueA", e.getPropertyValues("propA").iterator().next()); assertEquals("valueB", e.getPropertyValues("propB").iterator().next()); assertEquals("valueA", e.getPropertyValue("propA")); assertEquals("valueB", e.getPropertyValue("propB")); graph.flush(); assertEvents( new AddVertexEvent(graph, v1), new AddVertexEvent(graph, v2), new AddEdgeEvent(graph, addedEdge), new AddPropertyEvent(graph, addedEdge, addedEdge.getProperty("propA")), new AddPropertyEvent(graph, addedEdge, addedEdge.getProperty("propB")) ); }
boolean foundE1 = false, foundE2 = false; for (Edge e : edges) { if (e.getId().equals("e1")) { assertEquals("e1", e.getPropertyValue("prop1")); foundE1 = true; } else if (e.getId().equals("e2")) { assertEquals("e2", e.getPropertyValue("prop1")); foundE2 = true; } else { assertTrue("Unexpected vertex id: " + e.getId(), false);
Property edgeProperty = e1.getProperty("key1", "prop1"); e1.prepareMutation() .removeProperties("key1", "prop1") .save(AUTHORIZATIONS_A_AND_B); graph.flush(); assertEquals(0, count(e1.getProperties())); e1 = graph.getEdge("e1", AUTHORIZATIONS_A); assertEquals(0, count(e1.getProperties())); assertEvents( new RemovePropertyEvent(graph, e1, edgeProperty)
properties = edgeBuilder.getProperties(); } else { Iterable<Property> existingProperties = existingEdge.getProperties(); Iterable<Property> newProperties = edgeBuilder.getProperties(); properties = new TreeSet<>(toList(existingProperties));
assertEquals("label1", e.getLabel()); assertEquals(1, count(e.getProperties())); assertEquals("valueA", e.getPropertyValues("propA").iterator().next()); assertEquals(1, count(v1.getEdges(Direction.OUT, AUTHORIZATIONS_A))); assertEquals("label1", single(v1.getEdgeLabels(Direction.OUT, AUTHORIZATIONS_A))); assertEquals("label1", single(v2.getEdgeLabels(Direction.IN, AUTHORIZATIONS_A))); e.prepareMutation() .alterEdgeLabel("label2") .save(AUTHORIZATIONS_A); e = graph.getEdge("e1", AUTHORIZATIONS_A); assertEquals("label2", e.getLabel()); assertEquals(1, count(e.getProperties())); assertEquals("valueA", e.getPropertyValues("propA").iterator().next()); v1 = graph.getVertex("v1", AUTHORIZATIONS_A); assertEquals(1, count(v1.getEdges(Direction.OUT, AUTHORIZATIONS_A))); graph.prepareEdge(e.getId(), e.getVertexId(Direction.OUT), e.getVertexId(Direction.IN), e.getLabel(), e.getVisibility()) .alterEdgeLabel("label3") .save(AUTHORIZATIONS_A); e = graph.getEdge("e1", AUTHORIZATIONS_A); assertEquals("label3", e.getLabel()); assertEquals(1, count(e.getProperties())); assertEquals("valueA", e.getPropertyValues("propA").iterator().next()); v1 = graph.getVertex("v1", AUTHORIZATIONS_A); assertEquals(1, count(v1.getEdges(Direction.OUT, AUTHORIZATIONS_A)));
void addInEdge(Edge edge) { this.inEdges.put(edge.getId(), new EdgeInfo(edge.getLabel(), edge.getVertexId(Direction.OUT))); }
@Override public void markEdgeHidden(Edge edge, Visibility visibility, Authorizations authorizations) { checkNotNull(edge); Vertex out = edge.getVertex(Direction.OUT, authorizations); if (out == null) { throw new SecureGraphException(String.format("Unable to mark edge hidden %s, can't find out vertex %s", edge.getId(), edge.getVertexId(Direction.OUT))); } Vertex in = edge.getVertex(Direction.IN, authorizations); if (in == null) { throw new SecureGraphException(String.format("Unable to mark edge hidden %s, can't find in vertex %s", edge.getId(), edge.getVertexId(Direction.IN))); } ColumnVisibility columnVisibility = visibilityToAccumuloVisibility(visibility); Mutation outMutation = new Mutation(AccumuloConstants.VERTEX_ROW_KEY_PREFIX + out.getId()); outMutation.put(AccumuloVertex.CF_OUT_EDGE_HIDDEN, new Text(edge.getId()), columnVisibility, AccumuloElement.HIDDEN_VALUE); Mutation inMutation = new Mutation(AccumuloConstants.VERTEX_ROW_KEY_PREFIX + in.getId()); inMutation.put(AccumuloVertex.CF_IN_EDGE_HIDDEN, new Text(edge.getId()), columnVisibility, AccumuloElement.HIDDEN_VALUE); addMutations(getVerticesWriter(), outMutation, inMutation); // Remove everything else related to edge. addMutations(getEdgesWriter(), getMarkHiddenRowMutation(AccumuloConstants.EDGE_ROW_KEY_PREFIX + edge.getId(), columnVisibility)); if (out instanceof AccumuloVertex) { ((AccumuloVertex) out).removeOutEdge(edge); } if (in instanceof AccumuloVertex) { ((AccumuloVertex) in).removeInEdge(edge); } if (hasEventListeners()) { queueEvent(new MarkHiddenEdgeEvent(this, edge)); } }
@Override public String toString() { if (this instanceof Edge) { Edge edge = (Edge) this; return getId() + ":[" + edge.getVertexId(Direction.OUT) + "-" + edge.getLabel() + "->" + edge.getVertexId(Direction.IN) + "]"; } return getId(); }