public void clopen(Object... settings) {
   config = getConfiguration();
   if (mgmt != null && mgmt.isOpen()) mgmt.rollback();
   if (null != tx && tx.isOpen()) tx.commit();
   if (settings != null && settings.length > 0) {
     Map<TestConfigOption, Object> options = validateConfigOptions(settings);
     TitanManagement gconf = null;
     ModifiableConfiguration lconf =
         new ModifiableConfiguration(
             GraphDatabaseConfiguration.ROOT_NS, config, BasicConfiguration.Restriction.LOCAL);
     for (Map.Entry<TestConfigOption, Object> option : options.entrySet()) {
       if (option.getKey().option.isLocal()) {
         lconf.set(option.getKey().option, option.getValue(), option.getKey().umbrella);
       } else {
         if (gconf == null) gconf = graph.openManagement();
         gconf.set(
             ConfigElement.getPath(option.getKey().option, option.getKey().umbrella),
             option.getValue());
       }
     }
     if (gconf != null) gconf.commit();
     lconf.close();
   }
   if (null != graph && graph.isOpen()) graph.close();
   Preconditions.checkNotNull(config);
   open(config);
 }
Пример #2
0
  public StandardTitanTx(
      StandardTitanGraph graph, TransactionConfig config, BackendTransaction txHandle) {
    Preconditions.checkNotNull(graph);
    Preconditions.checkArgument(graph.isOpen());
    Preconditions.checkNotNull(config);
    Preconditions.checkNotNull(txHandle);
    this.graph = graph;
    this.config = config;
    this.idInspector = graph.getIDInspector();
    this.txHandle = txHandle;

    temporaryID = new AtomicLong(-1);
    Cache<StandardElementQuery, List<Object>> indexCacheBuilder =
        CacheBuilder.newBuilder()
            .weigher(
                new Weigher<StandardElementQuery, List<Object>>() {
                  @Override
                  public int weigh(StandardElementQuery q, List<Object> r) {
                    return 2 + r.size();
                  }
                })
            .maximumWeight(DEFAULT_CACHE_SIZE)
            .build();
    int concurrencyLevel;
    if (config.isSingleThreaded()) {
      vertexCache = new SimpleVertexCache();
      addedRelations = new SimpleBufferAddedRelations();
      concurrencyLevel = 1;
      typeCache = new HashMap<String, TitanType>();
      newVertexIndexEntries = new SimpleIndexCache();
    } else {
      vertexCache = new ConcurrentVertexCache();
      addedRelations = new ConcurrentBufferAddedRelations();
      concurrencyLevel = 4;
      typeCache = new ConcurrentHashMap<String, TitanType>();
      newVertexIndexEntries = new ConcurrentIndexCache();
    }
    for (SystemType st : SystemKey.values()) typeCache.put(st.getName(), st);

    indexCache =
        CacheBuilder.newBuilder()
            .weigher(
                new Weigher<StandardElementQuery, List<Object>>() {
                  @Override
                  public int weigh(StandardElementQuery q, List<Object> r) {
                    return 2 + r.size();
                  }
                })
            .concurrencyLevel(concurrencyLevel)
            .maximumWeight(DEFAULT_CACHE_SIZE)
            .build();

    uniqueLocks = UNINITIALIZED_LOCKS;
    deletedRelations = EMPTY_DELETED_RELATIONS;
    this.isOpen = true;
  }
Пример #3
0
 public StandardTitanTx getNextTx() {
   Preconditions.checkArgument(isClosed());
   if (!config.isThreadBound())
     throw new IllegalStateException(
         "Cannot access element because its enclosing transaction is closed and unbound");
   else return (StandardTitanTx) graph.getCurrentThreadTx();
 }
Пример #4
0
 @Override
 public boolean containsVertex(final long vertexid) {
   verifyOpen();
   if (vertexCache.contains(vertexid)) {
     if (!vertexCache.get(vertexid, vertexConstructor).isRemoved()) return true;
     else return false;
   } else if (vertexid > 0 && graph.containsVertexID(vertexid, txHandle)) return true;
   else return false;
 }
Пример #5
0
 @Override
 public TitanVertex addVertex() {
   verifyWriteAccess();
   StandardVertex vertex =
       new StandardVertex(this, temporaryID.decrementAndGet(), ElementLifeCycle.New);
   vertex.addProperty(SystemKey.VertexState, (byte) 0);
   if (config.hasAssignIDsImmediately()) graph.assignID(vertex);
   vertexCache.add(vertex, vertex.getID());
   return vertex;
 }
Пример #6
0
 public TitanLabel makeEdgeLabel(EdgeLabelDefinition definition) {
   verifyOpen();
   TitanLabelVertex label =
       new TitanLabelVertex(this, temporaryID.decrementAndGet(), ElementLifeCycle.New);
   addProperty(label, SystemKey.TypeName, definition.getName());
   addProperty(label, SystemKey.RelationTypeDefinition, definition);
   addProperty(label, SystemKey.TypeClass, TitanTypeClass.LABEL);
   graph.assignID(label);
   vertexCache.add(label, label.getID());
   typeCache.put(definition.getName(), label);
   return label;
 }
Пример #7
0
 public TitanKey makePropertyKey(PropertyKeyDefinition definition) {
   verifyOpen();
   TitanKeyVertex prop =
       new TitanKeyVertex(this, temporaryID.decrementAndGet(), ElementLifeCycle.New);
   addProperty(prop, SystemKey.TypeName, definition.getName());
   addProperty(prop, SystemKey.PropertyKeyDefinition, definition);
   addProperty(prop, SystemKey.TypeClass, TitanTypeClass.KEY);
   graph.assignID(prop);
   Preconditions.checkArgument(prop.getID() > 0);
   vertexCache.add(prop, prop.getID());
   typeCache.put(definition.getName(), prop);
   return prop;
 }
Пример #8
0
  @Test
  public void testValueOrdering() {
    StandardTitanGraph graph = (StandardTitanGraph) StorageSetup.getInMemoryGraph();
    TitanLabel father = graph.makeLabel("father").manyToOne().make();
    for (int i = 1; i <= 5; i++) graph.makeKey("key" + i).single().dataType(Integer.class).make();

    TitanVertex v1 = graph.addVertex(null), v2 = graph.addVertex(null);
    TitanEdge e1 = v1.addEdge("father", v2);
    for (int i = 1; i <= 5; i++) e1.setProperty("key" + i, i);

    graph.commit();

    e1.remove();
    graph.commit();
  }
Пример #9
0
 @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();
   }
 }
Пример #10
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();
   }
 }
Пример #11
0
 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();
   }
 }
 public void open(WriteConfiguration config) {
   graph = (StandardTitanGraph) TitanFactory.open(config);
   features = graph.getConfiguration().getStoreFeatures();
   tx = graph.newTransaction();
   mgmt = graph.openManagement();
 }
 public void newTx() {
   if (null != tx && tx.isOpen()) tx.commit();
   // tx = graph.newThreadBoundTransaction();
   tx = graph.newTransaction();
 }
  public void close() {
    if (mgmt != null && mgmt.isOpen()) mgmt.rollback();
    if (null != tx && tx.isOpen()) tx.commit();

    if (null != graph && graph.isOpen()) graph.close();
  }
 public void finishSchema() {
   if (mgmt != null && mgmt.isOpen()) mgmt.commit();
   mgmt = graph.openManagement();
   newTx();
   graph.tx().commit();
 }
Пример #16
0
        @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();
        }
Пример #17
0
 private Entry serialize(StandardTitanGraph graph, TitanEdge e, int pos) {
   EdgeSerializer edgeSerializer = graph.getEdgeSerializer();
   InternalRelation r = (InternalRelation) e;
   return edgeSerializer.writeRelation(r, pos, r.tx());
 }