Esempio n. 1
0
 @Override
 public boolean hasNew(StandardElementQuery query) {
   if (query.getType() == StandardElementQuery.Type.VERTEX) return hasModifications();
   else if (query.getType() == StandardElementQuery.Type.EDGE)
     return !addedRelations.isEmpty();
   else throw new AssertionError("Unexpected type: " + query.getType());
 }
Esempio n. 2
0
 public void removeRelation(InternalRelation relation) {
   Preconditions.checkArgument(!relation.isRemoved());
   relation = relation.it();
   // Delete from Vertex
   for (int i = 0; i < relation.getLen(); i++) {
     relation.getVertex(i).removeRelation(relation);
   }
   // Update transaction data structures
   if (relation.isNew()) {
     addedRelations.remove(relation);
     if (isVertexIndexProperty(relation)) newVertexIndexEntries.remove((TitanProperty) relation);
   } else {
     Preconditions.checkArgument(relation.isLoaded());
     if (deletedRelations == EMPTY_DELETED_RELATIONS) {
       if (config.isSingleThreaded()) {
         deletedRelations = new HashMap<Long, InternalRelation>();
       } else {
         synchronized (this) {
           if (deletedRelations == EMPTY_DELETED_RELATIONS)
             deletedRelations = new ConcurrentHashMap<Long, InternalRelation>();
         }
       }
     }
     deletedRelations.put(Long.valueOf(relation.getID()), relation);
   }
 }
Esempio n. 3
0
 private void connectRelation(InternalRelation r) {
   for (int i = 0; i < r.getLen(); i++) {
     boolean success = r.getVertex(i).addRelation(r);
     if (!success) throw new AssertionError("Could not connect relation: " + r);
   }
   addedRelations.add(r);
   if (isVertexIndexProperty(r)) newVertexIndexEntries.add((TitanProperty) r);
 }
Esempio n. 4
0
 @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);
   }
 }
Esempio n. 5
0
 @Override
 public synchronized void commit() {
   Preconditions.checkArgument(isOpen(), "The transaction has already been closed");
   try {
     if (hasModifications()) {
       graph.save(addedRelations.getAll(), deletedRelations.values(), this);
     }
     txHandle.commit();
   } catch (Exception e) {
     try {
       txHandle.rollback();
     } catch (StorageException e1) {
       throw new TitanException("Could not rollback after a failed commit", e);
     }
     throw new TitanException(
         "Could not commit transaction due to exception during persistence", e);
   } finally {
     close();
   }
 }
Esempio n. 6
0
 @Override
 public boolean hasModifications() {
   return !addedRelations.isEmpty() || !deletedRelations.isEmpty();
 }
Esempio n. 7
0
        @Override
        public Iterator<TitanElement> getNew(final StandardElementQuery query) {
          Preconditions.checkArgument(
              query.getType() == StandardElementQuery.Type.VERTEX
                  || query.getType() == StandardElementQuery.Type.EDGE);
          if (query.getType() == StandardElementQuery.Type.VERTEX && hasModifications()) {
            // Collect all keys from the query - ASSUMPTION: query is an AND of KeyAtom
            final Set<TitanKey> keys = Sets.newHashSet();
            KeyAtom<TitanKey> standardIndexKey = null;
            for (KeyCondition<TitanKey> cond : query.getCondition().getChildren()) {
              KeyAtom<TitanKey> atom = (KeyAtom<TitanKey>) cond;
              if (atom.getRelation() == Cmp.EQUAL && isVertexIndexProperty(atom.getKey()))
                standardIndexKey = atom;
              keys.add(atom.getKey());
            }
            Iterator<TitanVertex> vertices;
            if (standardIndexKey == null) {
              Set<TitanVertex> vertexSet = Sets.newHashSet();
              for (TitanRelation r :
                  addedRelations.getView(
                      new Predicate<InternalRelation>() {
                        @Override
                        public boolean apply(@Nullable InternalRelation relation) {
                          return keys.contains(relation.getType());
                        }
                      })) {
                vertexSet.add(((TitanProperty) r).getVertex());
              }
              for (TitanRelation r : deletedRelations.values()) {
                if (keys.contains(r.getType())) {
                  TitanVertex v = ((TitanProperty) r).getVertex();
                  if (!v.isRemoved()) vertexSet.add(v);
                }
              }
              vertices = vertexSet.iterator();
            } else {
              vertices =
                  Iterators.transform(
                      newVertexIndexEntries
                          .get(standardIndexKey.getCondition(), standardIndexKey.getKey())
                          .iterator(),
                      new Function<TitanProperty, TitanVertex>() {
                        @Nullable
                        @Override
                        public TitanVertex apply(@Nullable TitanProperty o) {
                          return o.getVertex();
                        }
                      });
            }

            return (Iterator)
                Iterators.filter(
                    vertices,
                    new Predicate<TitanVertex>() {
                      @Override
                      public boolean apply(@Nullable TitanVertex vertex) {
                        return query.matches(vertex);
                      }
                    });
          } else if (query.getType() == StandardElementQuery.Type.EDGE
              && !addedRelations.isEmpty()) {
            return (Iterator)
                addedRelations
                    .getView(
                        new Predicate<InternalRelation>() {
                          @Override
                          public boolean apply(@Nullable InternalRelation relation) {
                            return (relation instanceof TitanEdge)
                                && !relation.isHidden()
                                && query.matches(relation);
                          }
                        })
                    .iterator();
          } else throw new IllegalArgumentException("Unexpected type: " + query.getType());
        }