@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 TitanVertex) //allows vertices to be "re-attached" to the current transaction return ((TitanVertex) 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()).longValue(); } catch (NumberFormatException e) { return 0; } }
/** * Converts a Titan 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 */ public static final long fromVertexID(TitanVertex 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 final LongArrayList toLongList(List<TitanVertex> vertices) { LongArrayList result = new LongArrayList(vertices.size()); for (TitanVertex n : vertices) { result.add(n.longId()); } return result; }
private static TitanVertex vInTx(TitanVertex v, TitanTransaction tx) { if (!v.hasId()) return v; else return tx.getVertex(v.longId()); }
public boolean isPartitionedVertex(TitanVertex vertex) { return vertex.hasId() && idInspector.isPartitionedVertex(vertex.longId()); }
public InternalVertex getOtherPartitionVertex(TitanVertex partitionedVertex, long otherPartition) { Preconditions.checkArgument(isPartitionedVertex(partitionedVertex)); return getExistingVertex(idManager.getPartitionedVertexId(partitionedVertex.longId(), otherPartition)); }
public InternalVertex[] getAllRepresentatives(TitanVertex partitionedVertex, boolean restrict2Partitions) { Preconditions.checkArgument(isPartitionedVertex(partitionedVertex)); long[] ids; if (!restrict2Partitions || !config.hasRestrictedPartitions()) { ids = idManager.getPartitionedVertexRepresentatives(partitionedVertex.longId()); } else { int[] restrictedParititions = config.getRestrictedPartitions(); ids = new long[restrictedParititions.length]; for (int i=0;i<ids.length;i++) { ids[i]=idManager.getPartitionedVertexId(partitionedVertex.longId(),restrictedParititions[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(TitanVertex vertex) { long p = idManager.getPartitionId(vertex.longId()); assertTrue(p>=0 && p<idManager.getPartitionBound() && p<Integer.MAX_VALUE); return (int)p; }
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 Traversal<Vertex, Edge> reverseIncident = FulgoraUtil.getReverseElementTraversal(localMessageScope,vertex,vertex.tx()); final BiFunction<M,Edge,M> edgeFct = localMessageScope.getEdgeFunction(); return IteratorUtils.stream(reverseIncident) .map(e -> { M msg = vertexMemory.getMessage(vertexMemory.getCanonicalId(((TitanEdge) e).otherVertex(vertex).longId()), localMessageScope); return msg == null ? null : edgeFct.apply(msg, e); }) .filter(m -> m != null); } }
@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 TitanVertex) vertexId=((TitanVertex)v).longId(); else vertexId = (Long)v.id(); vertexMemory.sendMessage(vertexMemory.getCanonicalId(vertexId), m, messageScope); }); } }
private void verifyWriteAccess(TitanVertex... vertices) { if (config.isReadOnly()) throw new UnsupportedOperationException("Cannot create new entities in read-only transaction"); for (TitanVertex 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(); StandardTitanTx consistentTx = null; try { consistentTx = StandardTitanGraph.this.newTransaction(new StandardTransactionBuilder(getConfiguration(), StandardTitanGraph.this, customTxOptions).groupName(GraphDatabaseConfiguration.METRICS_SCHEMA_PREFIX_DEFAULT)); consistentTx.getTxHandle().disableCache(); TitanVertex 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 vids = new LongArrayList(vertices.size()); for (InternalVertex v : vertices) { if (!v.isNew() && v.hasId() && (v instanceof CacheVertex) && !v.hasLoadedRelations(sq)) vids.add(v.longId()); } if (!vids.isEmpty()) { List<EntryList> results = QueryProfiler.profile(profiler, sq, true, q -> graph.edgeMultiQuery(vids, q, txHandle)); int pos = 0; for (TitanVertex v : vertices) { if (pos<vids.size() && vids.get(pos) == v.longId()) { final EntryList vresults = results.get(pos); ((CacheVertex) v).loadRelations(sq, new Retriever<SliceQuery, EntryList>() { @Override public EntryList get(SliceQuery query) { return vresults; } }); pos++; } } } }
/** * 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(); TitanVertex v1 = tx.addVertex(); v1.property("name", "name1"); TitanVertex 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() { PropertyKey uid = makeVertexIndexedUniqueKey("name", String.class); finishSchema(); TitanVertex 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(); TitanCleanup.clear(graph); open(config); assertEmpty(tx.query().vertices()); }