@Override public TitanType getType(String name) { verifyOpen(); TitanType type = typeCache.get(name); if (type == null) type = (TitanType) Iterables.getOnlyElement(getVertices(SystemKey.TypeName, name), null); return type; }
@Override public TitanEdge addEdge(TitanVertex outVertex, TitanVertex inVertex, TitanLabel label) { verifyWriteAccess(outVertex, inVertex); outVertex = ((InternalVertex) outVertex).it(); inVertex = ((InternalVertex) inVertex).it(); Preconditions.checkNotNull(label); Lock uniqueLock = FakeLock.INSTANCE; if (config.hasVerifyUniqueness() && (label.isUnique(Direction.OUT) || label.isUnique(Direction.IN))) uniqueLock = getUniquenessLock(outVertex, label, inVertex); uniqueLock.lock(); try { // Check uniqueness if (config.hasVerifyUniqueness()) { if (label.isUnique(Direction.OUT)) { Preconditions.checkArgument( Iterables.isEmpty( query(outVertex) .includeHidden() .type(label) .direction(Direction.OUT) .titanEdges()), "An edge with the given type already exists on the out-vertex"); } if (label.isUnique(Direction.IN)) { Preconditions.checkArgument( Iterables.isEmpty( query(inVertex).includeHidden().type(label).direction(Direction.IN).titanEdges()), "An edge with the given type already exists on the in-vertex"); } } StandardEdge edge = new StandardEdge( temporaryID.decrementAndGet(), label, (InternalVertex) outVertex, (InternalVertex) inVertex, ElementLifeCycle.New); if (config.hasAssignIDsImmediately()) graph.assignID(edge); connectRelation(edge); return edge; } finally { uniqueLock.unlock(); } }
@Override public Iterable<Vertex> getVertices() { if (!addedRelations.isEmpty()) { // There are possible new vertices List<Vertex> newVs = new ArrayList<Vertex>(); for (InternalVertex v : vertexCache.getAll()) { if (v.isNew() && !(v instanceof TitanType)) newVs.add(v); } return Iterables.concat(newVs, new VertexIterable(graph, this)); } else { return (Iterable) new VertexIterable(graph, this); } }
public TitanProperty addPropertyInternal(TitanVertex vertex, TitanKey key, Object value) { verifyWriteAccess(vertex); vertex = ((InternalVertex) vertex).it(); Preconditions.checkNotNull(key); value = AttributeUtil.verifyAttribute(key, value); Lock uniqueLock = FakeLock.INSTANCE; if (config.hasVerifyUniqueness() && (key.isUnique(Direction.OUT) || key.isUnique(Direction.IN))) uniqueLock = getUniquenessLock(vertex, key, value); uniqueLock.lock(); try { // Check uniqueness if (config.hasVerifyUniqueness()) { if (key.isUnique(Direction.OUT)) { Preconditions.checkArgument( Iterables.isEmpty( query(vertex).includeHidden().type(key).direction(Direction.OUT).properties()), "An property with the given key already exists on the vertex and the property key is defined as out-unique"); } if (key.isUnique(Direction.IN)) { Preconditions.checkArgument( Iterables.isEmpty(getVertices(key, value)), "The given value is already used as a property and the property key is defined as in-unique"); } } StandardProperty prop = new StandardProperty( temporaryID.decrementAndGet(), key, (InternalVertex) vertex, value, ElementLifeCycle.New); if (config.hasAssignIDsImmediately()) graph.assignID(prop); connectRelation(prop); return prop; } finally { uniqueLock.unlock(); } }
@Override public TitanVertex getVertex(TitanKey key, Object attribute) { Preconditions.checkArgument( key.isUnique(Direction.IN), "Key is not uniquely associated to value [%s]", key.getName()); return Iterables.getOnlyElement(getVertices(key, attribute), null); }
@Override public Iterator<TitanRelation> execute(final VertexCentricQuery query) { if (query.getVertex().isNew()) return Iterators.emptyIterator(); final EdgeSerializer edgeSerializer = graph.getEdgeSerializer(); FittedSliceQuery sq = edgeSerializer.getQuery(query); final boolean fittedQuery = sq.isFitted(); final InternalVertex v = query.getVertex(); final boolean needsFiltering = !sq.isFitted() || !deletedRelations.isEmpty(); if (needsFiltering && sq.hasLimit()) sq = new FittedSliceQuery(sq, QueryUtil.updateLimit(sq.getLimit(), 1.1)); Iterable<TitanRelation> result = null; double limitMultiplier = 1.0; int previousDiskSize = 0; boolean finished; do { finished = true; Iterable<Entry> iter = null; if (v instanceof CacheVertex) { CacheVertex cv = (CacheVertex) v; iter = ((CacheVertex) v) .loadRelations( sq, new Retriever<SliceQuery, List<Entry>>() { @Override public List<Entry> get(SliceQuery query) { return graph.edgeQuery(v.getID(), query, txHandle); } }); } else { iter = graph.edgeQuery(v.getID(), sq, txHandle); } result = Iterables.transform( iter, new Function<Entry, TitanRelation>() { @Nullable @Override public TitanRelation apply(@Nullable Entry entry) { return edgeSerializer.readRelation(v, entry); } }); if (needsFiltering) { result = Iterables.filter( result, new Predicate<TitanRelation>() { @Override public boolean apply(@Nullable TitanRelation relation) { // Filter out updated and deleted relations return (relation == ((InternalRelation) relation).it() && !deletedRelations.containsKey(Long.valueOf(relation.getID()))) && (fittedQuery || query.matches(relation)); } }); } // Determine termination if (needsFiltering && query.hasLimit()) { if (!IterablesUtil.sizeLargerOrEqualThan(result, query.getLimit())) { int currentDiskSize = IterablesUtil.size(iter); if (currentDiskSize > previousDiskSize) { finished = false; previousDiskSize = currentDiskSize; limitMultiplier *= 2; sq = new FittedSliceQuery( sq, QueryUtil.updateLimit(sq.getLimit(), limitMultiplier)); } } } } while (!finished); return result.iterator(); }
@Override public boolean containsType(String name) { verifyOpen(); return (typeCache.containsKey(name) || !Iterables.isEmpty(getVertices(SystemKey.TypeName, name))); }