Search in sources :

Example 46 with OIndexCursor

use of com.orientechnologies.orient.core.index.OIndexCursor in project orientdb by orientechnologies.

the class IndexTxAwareMultiValueGetValuesTest method testRemoveAllWithinTransaction.

@Test
public void testRemoveAllWithinTransaction() {
    database.getMetadata().getIndexManager().reload();
    database.begin();
    final OIndex<?> index = database.getMetadata().getIndexManager().getIndex("idxTxAwareMultiValueGetValuesTest");
    Assert.assertTrue(index instanceof OIndexTxAwareMultiValue);
    final int clusterId = database.getDefaultClusterId();
    final List<Long> positions = getValidPositions(clusterId);
    index.put(1, new ORecordId(clusterId, positions.get(1)));
    index.put(2, new ORecordId(clusterId, positions.get(2)));
    index.remove(1, null);
    Assert.assertNotNull(database.getTransaction().getIndexChanges("idxTxAwareMultiValueGetValuesTest"));
    Set<OIdentifiable> result = new HashSet<OIdentifiable>();
    OIndexCursor cursor = index.iterateEntries(Arrays.asList(1, 2), true);
    cursorToSet(cursor, result);
    Assert.assertEquals(result.size(), 1);
    database.commit();
    cursor = index.iterateEntries(Arrays.asList(1, 2), true);
    cursorToSet(cursor, result);
    Assert.assertEquals(result.size(), 1);
}
Also used : OIndexTxAwareMultiValue(com.orientechnologies.orient.core.index.OIndexTxAwareMultiValue) OIndexCursor(com.orientechnologies.orient.core.index.OIndexCursor) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 47 with OIndexCursor

use of com.orientechnologies.orient.core.index.OIndexCursor in project orientdb by orientechnologies.

the class IndexTxAwareMultiValueGetValuesTest method testRemoveOne.

@Test
public void testRemoveOne() {
    database.getMetadata().getIndexManager().reload();
    database.begin();
    final OIndex<?> index = database.getMetadata().getIndexManager().getIndex("idxTxAwareMultiValueGetValuesTest");
    Assert.assertTrue(index instanceof OIndexTxAwareMultiValue);
    final int clusterId = database.getDefaultClusterId();
    final List<Long> positions = getValidPositions(clusterId);
    final ORecordId firstRecordId = new ORecordId(clusterId, positions.get(0));
    index.put(1, firstRecordId);
    index.put(1, new ORecordId(clusterId, positions.get(1)));
    index.put(2, new ORecordId(clusterId, positions.get(2)));
    database.commit();
    Assert.assertNull(database.getTransaction().getIndexChanges("idxTxAwareMultiValueGetValuesTest"));
    Set<OIdentifiable> resultOne = new HashSet<OIdentifiable>();
    OIndexCursor cursor = index.iterateEntries(Arrays.asList(1, 2), true);
    cursorToSet(cursor, resultOne);
    Assert.assertEquals(resultOne.size(), 3);
    database.begin();
    index.remove(1, firstRecordId);
    Assert.assertNotNull(database.getTransaction().getIndexChanges("idxTxAwareMultiValueGetValuesTest"));
    Set<OIdentifiable> resultTwo = new HashSet<OIdentifiable>();
    cursor = index.iterateEntries(Arrays.asList(1, 2), true);
    cursorToSet(cursor, resultTwo);
    Assert.assertEquals(resultTwo.size(), 2);
    database.rollback();
    Assert.assertNull(database.getTransaction().getIndexChanges("idxTxAwareMultiValueGetValuesTest"));
    Set<OIdentifiable> resultThree = new HashSet<OIdentifiable>();
    cursor = index.iterateEntries(Arrays.asList(1, 2), true);
    cursorToSet(cursor, resultThree);
    Assert.assertEquals(resultThree.size(), 3);
}
Also used : OIndexTxAwareMultiValue(com.orientechnologies.orient.core.index.OIndexTxAwareMultiValue) OIndexCursor(com.orientechnologies.orient.core.index.OIndexCursor) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 48 with OIndexCursor

use of com.orientechnologies.orient.core.index.OIndexCursor in project orientdb by orientechnologies.

the class IndexCrashRestoreMultiValueAddDeleteIT method compareIndexes.

private void compareIndexes() {
    baseDocumentTx.activateOnCurrentThread();
    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) {
        baseDocumentTx.activateOnCurrentThread();
        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();
        testDocumentTx.activateOnCurrentThread();
        OIndex testIndex = testDocumentTx.getMetadata().getIndexManager().getIndex("mi");
        Set<OIdentifiable> result = (Set<OIdentifiable>) testIndex.get(key);
        if (result == null || result.size() != 10) {
            if (minLostTs > ts)
                minLostTs = ts;
        } else {
            boolean cnt = true;
            for (int i = 0; i < 10; i++) {
                if (!result.contains(new ORecordId("#0:" + i))) {
                    cnt = false;
                    break;
                }
            }
            if (!cnt) {
                if (minLostTs > ts)
                    minLostTs = ts;
            } else
                restoredRecords++;
        }
    }
    baseDocumentTx.activateOnCurrentThread();
    System.out.println("Restored entries : " + restoredRecords + " out of : " + baseDocumentTx.getMetadata().getIndexManager().getIndex("mi").getSize());
    System.out.println("Lost records max interval : " + (minLostTs == Long.MAX_VALUE ? 0 : lastTs - minLostTs));
}
Also used : Set(java.util.Set) OIndex(com.orientechnologies.orient.core.index.OIndex) OIndexCursor(com.orientechnologies.orient.core.index.OIndexCursor) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId) AtomicLong(java.util.concurrent.atomic.AtomicLong) Map(java.util.Map) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 49 with OIndexCursor

use of com.orientechnologies.orient.core.index.OIndexCursor in project orientdb by orientechnologies.

the class OQueryOperatorContainsKey method executeIndexQuery.

@Override
public OIndexCursor executeIndexQuery(OCommandContext iContext, OIndex<?> index, List<Object> keyParams, boolean ascSortOrder) {
    final OIndexDefinition indexDefinition = index.getDefinition();
    OIndexCursor cursor;
    final OIndexInternal<?> internalIndex = index.getInternal();
    if (!internalIndex.canBeUsedInEqualityOperators())
        return null;
    if (indexDefinition.getParamCount() == 1) {
        if (!((indexDefinition instanceof OPropertyMapIndexDefinition) && ((OPropertyMapIndexDefinition) indexDefinition).getIndexBy() == OPropertyMapIndexDefinition.INDEX_BY.KEY))
            return null;
        final Object key = ((OIndexDefinitionMultiValue) indexDefinition).createSingleValue(keyParams.get(0));
        if (key == null)
            return null;
        final Object indexResult = index.get(key);
        if (indexResult == null || indexResult instanceof OIdentifiable)
            cursor = new OIndexCursorSingleValue((OIdentifiable) indexResult, key);
        else
            cursor = new OIndexCursorCollectionValue((Collection<OIdentifiable>) indexResult, key);
    } 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;
        if (!((compositeIndexDefinition.getMultiValueDefinition() instanceof OPropertyMapIndexDefinition) && ((OPropertyMapIndexDefinition) compositeIndexDefinition.getMultiValueDefinition()).getIndexBy() == OPropertyMapIndexDefinition.INDEX_BY.KEY))
            return null;
        final Object keyOne = compositeIndexDefinition.createSingleValue(keyParams);
        if (keyOne == null)
            return null;
        if (internalIndex.hasRangeQuerySupport()) {
            final Object keyTwo = compositeIndexDefinition.createSingleValue(keyParams);
            cursor = index.iterateEntriesBetween(keyOne, true, keyTwo, true, ascSortOrder);
        } else {
            if (indexDefinition.getParamCount() == keyParams.size()) {
                final Object indexResult = index.get(keyOne);
                if (indexResult == null || indexResult instanceof OIdentifiable)
                    cursor = new OIndexCursorSingleValue((OIdentifiable) indexResult, keyOne);
                else
                    cursor = new OIndexCursorCollectionValue((Collection<OIdentifiable>) indexResult, keyOne);
            } else
                return null;
        }
    }
    updateProfiler(iContext, index, keyParams, indexDefinition);
    return cursor;
}
Also used : OCompositeIndexDefinition(com.orientechnologies.orient.core.index.OCompositeIndexDefinition) OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) OPropertyMapIndexDefinition(com.orientechnologies.orient.core.index.OPropertyMapIndexDefinition) OIndexCursorCollectionValue(com.orientechnologies.orient.core.index.OIndexCursorCollectionValue) OIndexCursorSingleValue(com.orientechnologies.orient.core.index.OIndexCursorSingleValue) OIndexCursor(com.orientechnologies.orient.core.index.OIndexCursor) OIndexDefinitionMultiValue(com.orientechnologies.orient.core.index.OIndexDefinitionMultiValue) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 50 with OIndexCursor

use of com.orientechnologies.orient.core.index.OIndexCursor in project orientdb by orientechnologies.

the class OQueryOperatorContainsText method executeIndexQuery.

@Override
public OIndexCursor executeIndexQuery(OCommandContext iContext, OIndex<?> index, List<Object> keyParams, boolean ascSortOrder) {
    final OIndexDefinition indexDefinition = index.getDefinition();
    if (indexDefinition.getParamCount() > 1)
        return null;
    final OIndex<?> internalIndex = index.getInternal();
    OIndexCursor cursor;
    if (internalIndex instanceof OIndexFullText) {
        final Object key = indexDefinition.createValue(keyParams);
        final Object indexResult = index.get(key);
        if (indexResult == null || indexResult instanceof OIdentifiable)
            cursor = new OIndexCursorSingleValue((OIdentifiable) indexResult, key);
        else
            cursor = new OIndexCursorCollectionValue((Collection<OIdentifiable>) indexResult, key);
    } else
        return null;
    updateProfiler(iContext, internalIndex, keyParams, indexDefinition);
    return cursor;
}
Also used : OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) OIndexCursorCollectionValue(com.orientechnologies.orient.core.index.OIndexCursorCollectionValue) OIndexCursorSingleValue(com.orientechnologies.orient.core.index.OIndexCursorSingleValue) OIndexCursor(com.orientechnologies.orient.core.index.OIndexCursor) OIndexFullText(com.orientechnologies.orient.core.index.OIndexFullText) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Aggregations

OIndexCursor (com.orientechnologies.orient.core.index.OIndexCursor)54 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)47 ORecordId (com.orientechnologies.orient.core.id.ORecordId)42 Test (org.testng.annotations.Test)41 HashSet (java.util.HashSet)40 OIndexTxAwareMultiValue (com.orientechnologies.orient.core.index.OIndexTxAwareMultiValue)20 OIndexTxAwareOneValue (com.orientechnologies.orient.core.index.OIndexTxAwareOneValue)20 OIndexDefinition (com.orientechnologies.orient.core.index.OIndexDefinition)8 OCompositeIndexDefinition (com.orientechnologies.orient.core.index.OCompositeIndexDefinition)7 OIndexDefinitionMultiValue (com.orientechnologies.orient.core.index.OIndexDefinitionMultiValue)6 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)6 OIndex (com.orientechnologies.orient.core.index.OIndex)3 OIndexCursorCollectionValue (com.orientechnologies.orient.core.index.OIndexCursorCollectionValue)3 OIndexCursorSingleValue (com.orientechnologies.orient.core.index.OIndexCursorSingleValue)3 Map (java.util.Map)3 Set (java.util.Set)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)2 OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2