@Override
  public Object executeIndexQuery(
      OCommandContext iContext,
      OIndex<?> index,
      final INDEX_OPERATION_TYPE iOperationType,
      List<Object> keyParams,
      int fetchLimit) {
    final OIndexDefinition indexDefinition = index.getDefinition();

    final OIndexInternal<?> internalIndex = index.getInternal();
    if (!internalIndex.canBeUsedInEqualityOperators()) return null;

    final Object result;

    if (indexDefinition.getParamCount() == 1) {
      final Object key;
      if (indexDefinition instanceof OIndexDefinitionMultiValue)
        key = ((OIndexDefinitionMultiValue) indexDefinition).createSingleValue(keyParams.get(0));
      else key = indexDefinition.createValue(keyParams);

      if (key == null) return null;

      final Object indexResult;
      if (iOperationType == INDEX_OPERATION_TYPE.GET) indexResult = index.get(key);
      else indexResult = index.count(key);

      result = convertIndexResult(indexResult);
    } else {
      // in case of composite keys several items can be returned in case of we perform search
      // using part of composite key stored in index.

      final OCompositeIndexDefinition compositeIndexDefinition =
          (OCompositeIndexDefinition) indexDefinition;

      final Object keyOne = compositeIndexDefinition.createSingleValue(keyParams);

      if (keyOne == null) return null;

      final Object keyTwo = compositeIndexDefinition.createSingleValue(keyParams);

      if (internalIndex.hasRangeQuerySupport()) {
        if (INDEX_OPERATION_TYPE.COUNT.equals(iOperationType)) {
          result = index.count(keyOne, true, keyTwo, true, fetchLimit);
        } else if (fetchLimit > -1)
          result = index.getValuesBetween(keyOne, true, keyTwo, true, fetchLimit);
        else result = index.getValuesBetween(keyOne, true, keyTwo, true);
      } else {
        if (indexDefinition.getParamCount() == keyParams.size()) {
          final Object indexResult;
          if (iOperationType == INDEX_OPERATION_TYPE.GET) indexResult = index.get(keyOne);
          else indexResult = index.count(keyOne);

          result = convertIndexResult(indexResult);
        } else return null;
      }
    }

    updateProfiler(iContext, index, keyParams, indexDefinition);
    return result;
  }
  @Test
  public void testInsertUpdateTransactionWithIndex() throws Exception {

    databaseDocumentTx.close();
    databaseDocumentTx.open("admin", "admin");
    OSchema schema = databaseDocumentTx.getMetadata().getSchema();
    schema.reload();
    databaseDocumentTx.begin();
    ODocument doc = new ODocument("City");
    doc.field("name", "");
    ODocument doc1 = new ODocument("City");
    doc1.field("name", "");
    doc = databaseDocumentTx.save(doc);
    doc1 = databaseDocumentTx.save(doc1);
    databaseDocumentTx.commit();
    databaseDocumentTx.begin();
    doc = databaseDocumentTx.load(doc);
    doc1 = databaseDocumentTx.load(doc1);
    doc.field("name", "Rome");
    doc1.field("name", "Rome");
    databaseDocumentTx.save(doc);
    databaseDocumentTx.save(doc1);
    databaseDocumentTx.commit();
    OIndex idx = schema.getClass("City").getClassIndex("City.name");
    Collection<?> coll = (Collection<?>) idx.get("Rome");
    Assert.assertEquals(coll.size(), 2);
    Assert.assertEquals(idx.getSize(), 2);
  }
  public void testTransactionalUsageWorks() {
    database.begin(OTransaction.TXTYPE.OPTIMISTIC);
    // OIndex<?> index = getManualIndex();
    ComparableBinary key3 =
        new ComparableBinary(
            new byte[] {
              0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8,
              9, 0, 3
            });
    ODocument doc1 = new ODocument().field("k", "key3");

    final OIndex index = getIndex();
    index.put(key3, doc1);

    ComparableBinary key4 =
        new ComparableBinary(
            new byte[] {
              0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8,
              9, 0, 4
            });
    ODocument doc2 = new ODocument().field("k", "key4");
    index.put(key4, doc2);

    database.commit();

    Assert.assertEquals(index.get(key3), doc1);
    Assert.assertEquals(index.get(key4), doc2);
  }
  @Test(dependsOnMethods = {"testTransactionalUsageWorks"})
  public void testTransactionalUsageBreaks2() {
    OIndex<?> index = getIndex();
    database.begin(OTransaction.TXTYPE.OPTIMISTIC);
    ComparableBinary key7 =
        new ComparableBinary(
            new byte[] {
              0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8,
              9, 0, 7
            });
    ODocument doc1 = new ODocument().field("k", "key7");
    index.put(key7, doc1);

    ComparableBinary key8 =
        new ComparableBinary(
            new byte[] {
              0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8,
              9, 0, 8
            });
    ODocument doc2 = new ODocument().field("k", "key8");
    index.put(key8, doc2);

    database.commit();

    Assert.assertEquals(index.get(key7), doc1);
    Assert.assertEquals(index.get(key8), doc2);
  }
  @Test
  public void vertexIndexLookupWithValue() {
    OrientGraph graph = newGraph();
    createVertexIndexLabel1(graph);
    String value = "value1";

    // verify index created
    Assert.assertEquals(
        graph.getIndexedKeys(Vertex.class, label1), new HashSet<>(Collections.singletonList(key)));
    Assert.assertEquals(
        graph.getIndexedKeys(Vertex.class, label2), new HashSet<>(Collections.emptyList()));
    Assert.assertEquals(
        graph.getIndexedKeys(Edge.class, label1), new HashSet<>(Collections.emptyList()));

    Vertex v1 = graph.addVertex(T.label, label1, key, value);
    Vertex v2 = graph.addVertex(T.label, label2, key, value);

    // looking deep into the internals here - I can't find a nicer way to
    // auto verify that an index is actually used
    GraphTraversal<Vertex, Vertex> traversal =
        graph.traversal().V().has(T.label, P.eq(label1)).has(key, P.eq(value));
    OrientGraphStepStrategy.instance().apply(traversal.asAdmin());

    OrientGraphStep orientGraphStep = (OrientGraphStep) traversal.asAdmin().getStartStep();
    OrientIndexQuery orientIndexQuery = (OrientIndexQuery) orientGraphStep.findIndex().get();

    OIndex index = orientIndexQuery.index;
    Assert.assertEquals(1, index.getSize());
    Assert.assertEquals(v1.id(), index.get(value));
  }
Beispiel #6
0
 public ODocument getByName(String collectionName) throws SqlInjectionException {
   if (Logger.isTraceEnabled()) Logger.trace("Method Start");
   OIndex idx = db.getMetadata().getIndexManager().getIndex(COLLECTION_NAME_INDEX);
   OIdentifiable record = (OIdentifiable) idx.get(collectionName);
   if (record == null) return null;
   return db.load(record.getIdentity());
 }
Beispiel #7
0
 public boolean existsCollection(String collectionName) throws SqlInjectionException {
   if (Logger.isTraceEnabled()) Logger.trace("Method Start");
   OIndex idx = db.getMetadata().getIndexManager().getIndex(COLLECTION_NAME_INDEX);
   OIdentifiable record = (OIdentifiable) idx.get(collectionName);
   if (Logger.isTraceEnabled()) Logger.trace("Method End");
   return (record != null);
 }
Beispiel #8
0
  public <RET extends T> RET get(final String iKey, final String iFetchPlan) {
    final OIdentifiable value = index.get(iKey);
    if (value == null) return null;

    if (value instanceof ORID)
      return (RET) ODatabaseRecordThreadLocal.INSTANCE.get().load(((ORID) value), iFetchPlan);

    return (RET) ((ODocument) value).load(iFetchPlan);
  }
  /** {@inheritDoc} */
  @Override
  public T get(Object iKey) {
    final Object lastIndexResult = lastIndex.get(iKey);

    final Set<OIdentifiable> result = new HashSet<OIdentifiable>();

    if (lastIndexResult != null) result.addAll(applyTailIndexes(lastIndexResult));

    return (T) result;
  }
  private void compareIndexes() {
    ODatabaseRecordThreadLocal.INSTANCE.set(baseDocumentTx);
    OIndexCursor cursor = baseDocumentTx.getMetadata().getIndexManager().getIndex("mi").cursor();

    long lastTs = 0;
    long minLostTs = Long.MAX_VALUE;

    long restoredRecords = 0;

    Map.Entry<Object, OIdentifiable> entry = cursor.nextEntry();
    while (entry != null) {
      ODatabaseRecordThreadLocal.INSTANCE.set(baseDocumentTx);
      Integer key = (Integer) entry.getKey();

      OIdentifiable identifiable = entry.getValue();
      ODocument doc = identifiable.getRecord();

      long ts = doc.<Long>field("ts");
      if (ts > lastTs) lastTs = ts;

      entry = cursor.nextEntry();

      ODatabaseRecordThreadLocal.INSTANCE.set(testDocumentTx);
      OIndex testIndex = testDocumentTx.getMetadata().getIndexManager().getIndex("mi");

      Set<OIdentifiable> result = (Set<OIdentifiable>) testIndex.get(key);
      if (result == null || result.isEmpty()) {
        if (minLostTs > ts) minLostTs = ts;
      } else restoredRecords++;
    }

    ODatabaseRecordThreadLocal.INSTANCE.set(baseDocumentTx);
    System.out.println(
        "Restored entries : "
            + restoredRecords
            + " out of : "
            + baseDocumentTx.getMetadata().getIndexManager().getIndex("mi").getSize());

    long maxInterval = minLostTs == Long.MAX_VALUE ? 0 : lastTs - minLostTs;
    System.out.println("Lost records max interval (ms) : " + maxInterval);

    assertThat(maxInterval).isLessThan(2000);
  }
Beispiel #11
0
  public void testUsage() {
    OIndex<?> index = getIndex();
    ComparableBinary key1 =
        new ComparableBinary(
            new byte[] {
              0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8,
              9, 0, 1
            });
    ODocument doc1 = new ODocument().field("k", "key1");
    index.put(key1, doc1);

    ComparableBinary key2 =
        new ComparableBinary(
            new byte[] {
              0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8,
              9, 0, 2
            });
    ODocument doc2 = new ODocument().field("k", "key1");
    index.put(key2, doc2);

    Assert.assertEquals(index.get(key1), doc1);
    Assert.assertEquals(index.get(key2), doc2);
  }
Beispiel #12
0
  public <RET extends T> RET get(final String iKey) {
    final OIdentifiable value = index.get(iKey);
    if (value == null) return null;

    return (RET) value.getRecord();
  }
Beispiel #13
0
 /**
  * * Returns an edge (link), belonging to the class @LinkDao.MODEL_NAME, by its id (not RID)
  *
  * @param id
  * @return
  */
 public static ORID getRidLinkByUUID(String id) {
   ODatabaseRecordTx db = DbHelper.getConnection();
   OIndex<?> index = db.getMetadata().getIndexManager().getIndex(LinkDao.MODEL_NAME + ".id");
   ORID rid = (ORID) index.get(id);
   return rid;
 }