@Override public long getID(int pos) { return vertices.get(pos).longId(); }
public static long getVertexId(Object id) { if (null == id) return 0; if (id instanceof JanusGraphVertex) //allows vertices to be "re-attached" to the current transaction return ((JanusGraphVertex) id).longId(); if (id instanceof Long) return (Long) id; if (id instanceof Number) return ((Number) id).longValue(); try { // handles the case of a user passing a "detached" Vertex (DetachedVertex, StarVertex, etc). if (id instanceof Vertex) return Long.parseLong(((Vertex) id).id().toString()); else return Long.valueOf(id.toString()); } catch (NumberFormatException e) { return 0; } }
/** * Converts a JanusGraph vertex id of a given vertex to the user provided id as the inverse mapping of {@link #toVertexId(long)}. * * @param v Vertex * @return original user provided id * @deprecated Use {@link org.janusgraph.graphdb.idmanagement.IDManager#fromVertexId(long)} */ public static long fromVertexID(JanusGraphVertex v) { Preconditions.checkArgument(v.hasId(), "Invalid vertex provided: %s", v); return fromVertexId(v.longId()); }
/** * Utility method used to convert the list of vertices into a list of vertex ids (assuming all vertices have ids) * * @param vertices * @return */ private static LongArrayList toLongList(List<JanusGraphVertex> vertices) { LongArrayList result = new LongArrayList(vertices.size()); for (JanusGraphVertex n : vertices) { result.add(n.longId()); } return result; }
private static JanusGraphVertex vInTx(JanusGraphVertex v, JanusGraphTransaction tx) { if (!v.hasId()) return v; else return tx.getVertex(v.longId()); }
public boolean isPartitionedVertex(JanusGraphVertex vertex) { return vertex.hasId() && idInspector.isPartitionedVertex(vertex.longId()); }
public InternalVertex getOtherPartitionVertex(JanusGraphVertex partitionedVertex, long otherPartition) { Preconditions.checkArgument(isPartitionedVertex(partitionedVertex)); return getExistingVertex(idManager.getPartitionedVertexId(partitionedVertex.longId(), otherPartition)); }
public InternalVertex[] getAllRepresentatives(JanusGraphVertex partitionedVertex, boolean restrict2Partitions) { Preconditions.checkArgument(isPartitionedVertex(partitionedVertex)); long[] ids; if (!restrict2Partitions || !config.hasRestrictedPartitions()) { ids = idManager.getPartitionedVertexRepresentatives(partitionedVertex.longId()); } else { int[] restrictedPartitions = config.getRestrictedPartitions(); ids = new long[restrictedPartitions.length]; for (int i=0;i<ids.length;i++) { ids[i]=idManager.getPartitionedVertexId(partitionedVertex.longId(),restrictedPartitions[i]); } } Preconditions.checkArgument(ids.length>0); InternalVertex[] vertices = new InternalVertex[ids.length]; for (int i=0;i<ids.length;i++) vertices[i]=getExistingVertex(ids[i]); return vertices; }
public int getPartitionID(JanusGraphVertex vertex) { long p = idManager.getPartitionId(vertex.longId()); assertTrue(p>=0 && p<idManager.getPartitionBound() && p<Integer.MAX_VALUE); return (int)p; }
@Override public void sendMessage(MessageScope messageScope, M m) { if (messageScope instanceof MessageScope.Local) { vertexMemory.sendMessage(vertexId, m, messageScope); } else { ((MessageScope.Global) messageScope).vertices().forEach(v -> { long vertexId; if (v instanceof JanusGraphVertex) vertexId=((JanusGraphVertex)v).longId(); else vertexId = (Long)v.id(); vertexMemory.sendMessage(vertexMemory.getCanonicalId(vertexId), m, messageScope); }); } }
public Stream<M> receiveMessages(MessageScope messageScope) { if (messageScope instanceof MessageScope.Global) { M message = vertexMemory.getMessage(vertexId,messageScope); if (message == null) return Stream.empty(); else return Stream.of(message); } else { final MessageScope.Local<M> localMessageScope = (MessageScope.Local) messageScope; final BiFunction<M,Edge,M> edgeFct = localMessageScope.getEdgeFunction(); final List<Edge> edges; try (final Traversal<Vertex, Edge> reverseIncident = FulgoraUtil.getReverseElementTraversal(localMessageScope,vertex,vertex.tx())) { edges = IteratorUtils.list(reverseIncident); } catch (Exception e) { throw new JanusGraphException("Unable to close traversal", e); } return edges.stream() .map(e -> { M msg = vertexMemory.getMessage(vertexMemory.getCanonicalId(((JanusGraphEdge) e).otherVertex(vertex).longId()), localMessageScope); return msg == null ? null : edgeFct.apply(msg, e); }) .filter(Objects::nonNull); } }
private void verifyWriteAccess(JanusGraphVertex... vertices) { if (config.isReadOnly()) throw new ReadOnlyTransactionException("Cannot create new entities in read-only transaction"); for (JanusGraphVertex v : vertices) { if (v.hasId() && idInspector.isUnmodifiableVertex(v.longId()) && !v.isNew()) throw new SchemaViolationException("Cannot modify unmodifiable vertex: "+v); } verifyAccess(vertices); }
@Override public Long retrieveSchemaByName(String typeName) { // Get a consistent tx Configuration customTxOptions = backend.getStoreFeatures().getKeyConsistentTxConfig(); StandardJanusGraphTx consistentTx = null; try { consistentTx = StandardJanusGraph.this.newTransaction(new StandardTransactionBuilder(getConfiguration(), StandardJanusGraph.this, customTxOptions).groupName(GraphDatabaseConfiguration.METRICS_SCHEMA_PREFIX_DEFAULT)); consistentTx.getTxHandle().disableCache(); JanusGraphVertex v = Iterables.getOnlyElement(QueryUtil.getVertices(consistentTx, BaseKey.SchemaName, typeName), null); return v!=null?v.longId():null; } finally { TXUtils.rollbackQuietly(consistentTx); } }
public void executeMultiQuery(final Collection<InternalVertex> vertices, final SliceQuery sq, final QueryProfiler profiler) { LongArrayList vertexIds = new LongArrayList(vertices.size()); for (InternalVertex v : vertices) { if (!v.isNew() && v.hasId() && (v instanceof CacheVertex) && !v.hasLoadedRelations(sq)) vertexIds.add(v.longId()); } if (!vertexIds.isEmpty()) { List<EntryList> results = QueryProfiler.profile(profiler, sq, true, q -> graph.edgeMultiQuery(vertexIds, q, txHandle)); int pos = 0; for (JanusGraphVertex v : vertices) { if (pos<vertexIds.size() && vertexIds.get(pos) == v.longId()) { final EntryList vresults = results.get(pos); ((CacheVertex) v).loadRelations(sq, query -> vresults); pos++; } } } }
/** * Tests that timestamped edges can be updated */ @Test public void testTimestampedEdgeUpdates() { clopen(option(GraphDatabaseConfiguration.STORE_META_TIMESTAMPS, "edgestore"), true, option(GraphDatabaseConfiguration.STORE_META_TTL, "edgestore"), true); // Transaction 1: Init graph with two vertices and one edge JanusGraphTransaction tx = graph.buildTransaction().commitTime(Instant.ofEpochSecond(100)).start(); JanusGraphVertex v1 = tx.addVertex(); JanusGraphVertex v2 = tx.addVertex(); Edge e = v1.addEdge("related",v2); e.property("time", 25); tx.commit(); tx = graph.buildTransaction().commitTime(Instant.ofEpochSecond(200)).start(); v1 = tx.getVertex(v1.longId()); assertNotNull(v1); e = Iterators.getOnlyElement(v1.edges(Direction.OUT, "related")); assertNotNull(e); assertEquals(Integer.valueOf(25), e.value("time")); e.property("time", 125); tx.commit(); tx = graph.buildTransaction().commitTime(Instant.ofEpochSecond(300)).start(); v1 = tx.getVertex(v1.longId()); assertNotNull(v1); e = Iterators.getOnlyElement(v1.edges(Direction.OUT, "related")); assertEquals(Integer.valueOf(125), e.value("time")); e.remove(); tx.commit(); }
/** * This test exercises different types of updates against cardinality restricted properties * to ensure that the resulting behavior is fully consistent. */ @Test public void testPropertyCardinality() { PropertyKey uid = mgmt.makePropertyKey("uid").dataType(Long.class).cardinality(Cardinality.SINGLE).make(); PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make(); mgmt.buildIndex("byUid", Vertex.class).addKey(uid).unique().buildCompositeIndex(); mgmt.buildIndex("byName", Vertex.class).addKey(name).buildCompositeIndex(); finishSchema(); JanusGraphVertex v1 = tx.addVertex(); v1.property("name", "name1"); JanusGraphVertex v2 = tx.addVertex(); v2.property("uid", 512); newTx(); v1 = tx.getVertex(v1.longId()); v1.property("name", "name2"); //Ensure that the old index record gets removed v2 = tx.getVertex(v2.longId()); v2.property("uid", 512); //Ensure that replacement is allowed newTx(); assertCount(0, tx.query().has("name", "name1").vertices()); assertCount(1, tx.query().has("name", "name2").vertices()); assertCount(1, tx.query().has("uid", 512).vertices()); }
/** * Very simple graph operation to ensure minimal functionality and cleanup */ @Test public void testBasic() throws BackendException { PropertyKey uid = makeVertexIndexedUniqueKey("name", String.class); finishSchema(); JanusGraphVertex n1 = tx.addVertex(); uid = tx.getPropertyKey("name"); n1.property(uid.name(), "abcd"); clopen(); long nid = n1.longId(); uid = tx.getPropertyKey("name"); assertTrue(getV(tx, nid) != null); assertTrue(getV(tx, uid.longId()) != null); assertMissing(tx, nid + 64); uid = tx.getPropertyKey(uid.name()); n1 = getV(tx, nid); assertEquals(n1, getOnlyVertex(tx.query().has(uid.name(), "abcd"))); assertEquals(1, Iterables.size(n1.query().relations())); //TODO: how to expose relations? assertEquals("abcd", n1.value(uid.name())); assertCount(1, tx.query().vertices()); close(); JanusGraphFactory.drop(graph); open(config); assertEmpty(tx.query().vertices()); }