Search in sources :

Example 21 with OIndexManager

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

the class OImmutableClass method areIndexed.

@Override
public boolean areIndexed(Collection<String> fields) {
    final OIndexManager indexManager = getDatabase().getMetadata().getIndexManager();
    final boolean currentClassResult = indexManager.areIndexed(name, fields);
    initSuperClasses();
    if (currentClassResult)
        return true;
    for (OImmutableClass superClass : superClasses) {
        if (superClass.areIndexed(fields))
            return true;
    }
    return false;
}
Also used : OIndexManager(com.orientechnologies.orient.core.index.OIndexManager)

Example 22 with OIndexManager

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

the class OTransactionRealAbstract method updateIdentityAfterCommit.

public void updateIdentityAfterCommit(final ORID oldRid, final ORID newRid) {
    if (oldRid.equals(newRid))
        // NO CHANGE, IGNORE IT
        return;
    // XXX: Identity update may mutate the index keys, so we have to identify and reinsert potentially affected index keys to keep
    // the OTransactionIndexChanges.changesPerKey in a consistent state.
    final List<KeyChangesUpdateRecord> keyRecordsToReinsert = new ArrayList<KeyChangesUpdateRecord>();
    final OIndexManager indexManager = getDatabase().getMetadata().getIndexManager();
    for (Entry<String, OTransactionIndexChanges> entry : indexEntries.entrySet()) {
        final OIndex<?> index = indexManager.getIndex(entry.getKey());
        if (index == null)
            throw new OTransactionException("Cannot find index '" + entry.getValue() + "' while committing transaction");
        final Dependency[] fieldRidDependencies = getIndexFieldRidDependencies(index);
        if (!isIndexMayDependOnRids(fieldRidDependencies))
            continue;
        final OTransactionIndexChanges indexChanges = entry.getValue();
        for (final Iterator<OTransactionIndexChangesPerKey> iterator = indexChanges.changesPerKey.values().iterator(); iterator.hasNext(); ) {
            final OTransactionIndexChangesPerKey keyChanges = iterator.next();
            if (isIndexKeyMayDependOnRid(keyChanges.key, oldRid, fieldRidDependencies)) {
                keyRecordsToReinsert.add(new KeyChangesUpdateRecord(keyChanges, indexChanges));
                iterator.remove();
            }
        }
    }
    // Update the identity.
    final ORecordOperation rec = getRecordEntry(oldRid);
    if (rec != null) {
        updatedRids.put(newRid.copy(), oldRid.copy());
        if (!rec.getRecord().getIdentity().equals(newRid)) {
            ORecordInternal.onBeforeIdentityChanged(rec.getRecord());
            final ORecordId recordId = (ORecordId) rec.getRecord().getIdentity();
            if (recordId == null) {
                ORecordInternal.setIdentity(rec.getRecord(), new ORecordId(newRid));
            } else {
                recordId.setClusterPosition(newRid.getClusterPosition());
                recordId.setClusterId(newRid.getClusterId());
            }
            ORecordInternal.onAfterIdentityChanged(rec.getRecord());
        }
    }
    for (KeyChangesUpdateRecord record : keyRecordsToReinsert) record.indexChanges.changesPerKey.put(record.keyChanges.key, record.keyChanges);
    // Update the indexes.
    final List<OTransactionRecordIndexOperation> transactionIndexOperations = recordIndexOperations.get(translateRid(oldRid));
    if (transactionIndexOperations != null) {
        for (final OTransactionRecordIndexOperation indexOperation : transactionIndexOperations) {
            OTransactionIndexChanges indexEntryChanges = indexEntries.get(indexOperation.index);
            if (indexEntryChanges == null)
                continue;
            final OTransactionIndexChangesPerKey keyChanges = indexEntryChanges.changesPerKey.get(indexOperation.key);
            if (keyChanges != null)
                updateChangesIdentity(oldRid, newRid, keyChanges);
        }
    }
}
Also used : ORecordId(com.orientechnologies.orient.core.id.ORecordId) OIndexManager(com.orientechnologies.orient.core.index.OIndexManager) ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException)

Example 23 with OIndexManager

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

the class IndexManagerTest method testAreIndexedThreePropertiesBrokenClassNameCase.

@Test(dependsOnMethods = { "createCompositeIndexTestWithListener", "createCompositeIndexTestWithoutListener", "testCreateOnePropertyIndexTest" })
public void testAreIndexedThreePropertiesBrokenClassNameCase() {
    final OIndexManager indexManager = database.getMetadata().getIndexManager();
    final boolean result = indexManager.areIndexed("ClaSSForIndeXManagerTeST", Arrays.asList("fTwo", "fOne", "fThree"));
    assertTrue(result);
}
Also used : OIndexManager(com.orientechnologies.orient.core.index.OIndexManager) Test(org.testng.annotations.Test)

Example 24 with OIndexManager

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

the class ODatabaseCompare method compareIndexes.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void compareIndexes(ODocumentHelper.RIDMapper ridMapper) {
    listener.onMessage("\nStarting index comparison:");
    boolean ok = true;
    final OIndexManager indexManagerOne = makeDbCall(databaseOne, new ODbRelatedCall<OIndexManager>() {

        public OIndexManager call(ODatabaseDocumentInternal database) {
            return database.getMetadata().getIndexManager();
        }
    });
    final OIndexManager indexManagerTwo = makeDbCall(databaseTwo, new ODbRelatedCall<OIndexManager>() {

        public OIndexManager call(ODatabaseDocumentInternal database) {
            return database.getMetadata().getIndexManager();
        }
    });
    final Collection<? extends OIndex<?>> indexesOne = makeDbCall(databaseOne, new ODbRelatedCall<Collection<? extends OIndex<?>>>() {

        public Collection<? extends OIndex<?>> call(ODatabaseDocumentInternal database) {
            return indexManagerOne.getIndexes();
        }
    });
    int indexesSizeOne = makeDbCall(databaseTwo, new ODbRelatedCall<Integer>() {

        public Integer call(ODatabaseDocumentInternal database) {
            return indexesOne.size();
        }
    });
    int indexesSizeTwo = makeDbCall(databaseTwo, new ODbRelatedCall<Integer>() {

        public Integer call(ODatabaseDocumentInternal database) {
            return indexManagerTwo.getIndexes().size();
        }
    });
    if (exportImportHashTable != null)
        indexesSizeTwo--;
    if (indexesSizeOne != indexesSizeTwo) {
        ok = false;
        listener.onMessage("\n- ERR: Amount of indexes are different.");
        listener.onMessage("\n--- DB1: " + indexesSizeOne);
        listener.onMessage("\n--- DB2: " + indexesSizeTwo);
        listener.onMessage("\n");
        ++differences;
    }
    final Iterator<? extends OIndex<?>> iteratorOne = makeDbCall(databaseOne, new ODbRelatedCall<Iterator<? extends OIndex<?>>>() {

        public Iterator<? extends OIndex<?>> call(ODatabaseDocumentInternal database) {
            return indexesOne.iterator();
        }
    });
    while (makeDbCall(databaseOne, new ODbRelatedCall<Boolean>() {

        public Boolean call(ODatabaseDocumentInternal database) {
            return iteratorOne.hasNext();
        }
    })) {
        final OIndex indexOne = makeDbCall(databaseOne, new ODbRelatedCall<OIndex<?>>() {

            public OIndex<?> call(ODatabaseDocumentInternal database) {
                return iteratorOne.next();
            }
        });
        final OIndex<?> indexTwo = makeDbCall(databaseTwo, new ODbRelatedCall<OIndex<?>>() {

            public OIndex<?> call(ODatabaseDocumentInternal database) {
                return indexManagerTwo.getIndex(indexOne.getName());
            }
        });
        if (indexTwo == null) {
            ok = false;
            listener.onMessage("\n- ERR: Index " + indexOne.getName() + " is absent in DB2.");
            ++differences;
            continue;
        }
        if (!indexOne.getType().equals(indexTwo.getType())) {
            ok = false;
            listener.onMessage("\n- ERR: Index types for index " + indexOne.getName() + " are different.");
            listener.onMessage("\n--- DB1: " + indexOne.getType());
            listener.onMessage("\n--- DB2: " + indexTwo.getType());
            listener.onMessage("\n");
            ++differences;
            continue;
        }
        if (!indexOne.getClusters().equals(indexTwo.getClusters())) {
            ok = false;
            listener.onMessage("\n- ERR: Clusters to index for index " + indexOne.getName() + " are different.");
            listener.onMessage("\n--- DB1: " + indexOne.getClusters());
            listener.onMessage("\n--- DB2: " + indexTwo.getClusters());
            listener.onMessage("\n");
            ++differences;
            continue;
        }
        if (indexOne.getDefinition() == null && indexTwo.getDefinition() != null) {
            ok = false;
            listener.onMessage("\n- ERR: Index definition for index " + indexOne.getName() + " for DB2 is not null.");
            ++differences;
            continue;
        } else if (indexOne.getDefinition() != null && indexTwo.getDefinition() == null) {
            ok = false;
            listener.onMessage("\n- ERR: Index definition for index " + indexOne.getName() + " for DB2 is null.");
            ++differences;
            continue;
        } else if (indexOne.getDefinition() != null && !indexOne.getDefinition().equals(indexTwo.getDefinition())) {
            ok = false;
            listener.onMessage("\n- ERR: Index definitions for index " + indexOne.getName() + " are different.");
            listener.onMessage("\n--- DB1: " + indexOne.getDefinition());
            listener.onMessage("\n--- DB2: " + indexTwo.getDefinition());
            listener.onMessage("\n");
            ++differences;
            continue;
        }
        final long indexOneSize = makeDbCall(databaseOne, new ODbRelatedCall<Long>() {

            public Long call(ODatabaseDocumentInternal database) {
                return indexOne.getSize();
            }
        });
        final long indexTwoSize = makeDbCall(databaseTwo, new ODbRelatedCall<Long>() {

            public Long call(ODatabaseDocumentInternal database) {
                return indexTwo.getSize();
            }
        });
        if (indexOneSize != indexTwoSize) {
            ok = false;
            listener.onMessage("\n- ERR: Amount of entries for index " + indexOne.getName() + " are different.");
            listener.onMessage("\n--- DB1: " + indexOneSize);
            listener.onMessage("\n--- DB2: " + indexTwoSize);
            listener.onMessage("\n");
            ++differences;
        }
        if (compareIndexMetadata) {
            final ODocument metadataOne = indexOne.getMetadata();
            final ODocument metadataTwo = indexTwo.getMetadata();
            if (metadataOne == null && metadataTwo != null) {
                ok = false;
                listener.onMessage("\n- ERR: Metadata for index " + indexOne.getName() + " for DB1 is null but for DB2 is not.");
                listener.onMessage("\n");
                ++differences;
            } else if (metadataOne != null && metadataTwo == null) {
                ok = false;
                listener.onMessage("\n- ERR: Metadata for index " + indexOne.getName() + " for DB1 is not null but for DB2 is null.");
                listener.onMessage("\n");
                ++differences;
            } else if (metadataOne != null && metadataTwo != null && !ODocumentHelper.hasSameContentOf(metadataOne, databaseOne, metadataTwo, databaseTwo, ridMapper)) {
                ok = false;
                listener.onMessage("\n- ERR: Metadata for index " + indexOne.getName() + " for DB1 and for DB2 are different.");
                makeDbCall(databaseOne, new ODbRelatedCall<Object>() {

                    @Override
                    public Object call(ODatabaseDocumentInternal database) {
                        listener.onMessage("\n--- M1: " + metadataOne);
                        return null;
                    }
                });
                makeDbCall(databaseTwo, new ODbRelatedCall<Object>() {

                    @Override
                    public Object call(ODatabaseDocumentInternal database) {
                        listener.onMessage("\n--- M2: " + metadataTwo);
                        return null;
                    }
                });
                listener.onMessage("\n");
                ++differences;
            }
        }
        if (((compareEntriesForAutomaticIndexes && !indexOne.getType().equals("DICTIONARY")) || !indexOne.isAutomatic())) {
            final OIndexKeyCursor indexKeyCursorOne = makeDbCall(databaseOne, new ODbRelatedCall<OIndexKeyCursor>() {

                public OIndexKeyCursor call(ODatabaseDocumentInternal database) {
                    return indexOne.keyCursor();
                }
            });
            Object key = makeDbCall(databaseOne, new ODbRelatedCall<Object>() {

                @Override
                public Object call(ODatabaseDocumentInternal database) {
                    return indexKeyCursorOne.next(-1);
                }
            });
            while (key != null) {
                final Object indexKey = key;
                Object indexOneValue = makeDbCall(databaseOne, new ODbRelatedCall<Object>() {

                    public Object call(ODatabaseDocumentInternal database) {
                        return indexOne.get(indexKey);
                    }
                });
                final Object indexTwoValue = makeDbCall(databaseTwo, new ODbRelatedCall<Object>() {

                    public Object call(ODatabaseDocumentInternal database) {
                        return indexTwo.get(indexKey);
                    }
                });
                if (indexTwoValue == null) {
                    ok = false;
                    listener.onMessage("\n- ERR: Entry with key " + key + " is absent in index " + indexOne.getName() + " for DB2.");
                    ++differences;
                } else if (indexOneValue instanceof Set && indexTwoValue instanceof Set) {
                    final Set<Object> indexOneValueSet = (Set<Object>) indexOneValue;
                    final Set<Object> indexTwoValueSet = (Set<Object>) indexTwoValue;
                    if (!ODocumentHelper.compareSets(databaseOne, indexOneValueSet, databaseTwo, indexTwoValueSet, ridMapper)) {
                        ok = false;
                        reportIndexDiff(indexOne, key, indexOneValue, indexTwoValue);
                    }
                } else if (indexOneValue instanceof ORID && indexTwoValue instanceof ORID) {
                    if (ridMapper != null && ((ORID) indexOneValue).isPersistent()) {
                        OIdentifiable identifiable = ridMapper.map((ORID) indexOneValue);
                        if (identifiable != null)
                            indexOneValue = identifiable.getIdentity();
                    }
                    if (!indexOneValue.equals(indexTwoValue)) {
                        ok = false;
                        reportIndexDiff(indexOne, key, indexOneValue, indexTwoValue);
                    }
                } else if (!indexOneValue.equals(indexTwoValue)) {
                    ok = false;
                    reportIndexDiff(indexOne, key, indexOneValue, indexTwoValue);
                }
                key = makeDbCall(databaseOne, new ODbRelatedCall<Object>() {

                    @Override
                    public Object call(ODatabaseDocumentInternal database) {
                        return indexKeyCursorOne.next(-1);
                    }
                });
            }
        }
    }
    if (ok)
        listener.onMessage("OK");
}
Also used : Set(java.util.Set) OIndex(com.orientechnologies.orient.core.index.OIndex) ODbRelatedCall(com.orientechnologies.orient.core.record.impl.ODocumentHelper.ODbRelatedCall) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) Iterator(java.util.Iterator) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OIndexKeyCursor(com.orientechnologies.orient.core.index.OIndexKeyCursor) OIndexManager(com.orientechnologies.orient.core.index.OIndexManager) Collection(java.util.Collection)

Example 25 with OIndexManager

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

the class IndexManagerTest method testGetClassInvolvedIndexesTwoProperties.

@Test(dependsOnMethods = { "createCompositeIndexTestWithListener", "createCompositeIndexTestWithoutListener", "testCreateOnePropertyIndexTest" })
public void testGetClassInvolvedIndexesTwoProperties() {
    final OIndexManager indexManager = database.getMetadata().getIndexManager();
    final Set<OIndex<?>> result = indexManager.getClassInvolvedIndexes(CLASS_NAME, Arrays.asList("fTwo", "fOne"));
    assertEquals(result.size(), 2);
    assertTrue(containsIndex(result, "compositeone"));
    assertTrue(containsIndex(result, "compositetwo"));
}
Also used : OIndexManager(com.orientechnologies.orient.core.index.OIndexManager) OIndex(com.orientechnologies.orient.core.index.OIndex) Test(org.testng.annotations.Test)

Aggregations

OIndexManager (com.orientechnologies.orient.core.index.OIndexManager)46 Test (org.testng.annotations.Test)40 OIndex (com.orientechnologies.orient.core.index.OIndex)18 OPropertyIndexDefinition (com.orientechnologies.orient.core.index.OPropertyIndexDefinition)5 OCompositeIndexDefinition (com.orientechnologies.orient.core.index.OCompositeIndexDefinition)3 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)3 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)2 OIndexDefinition (com.orientechnologies.orient.core.index.OIndexDefinition)2 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)2 HashSet (java.util.HashSet)2 OException (com.orientechnologies.common.exception.OException)1 OCommandCacheSoftRefs (com.orientechnologies.orient.core.cache.OCommandCacheSoftRefs)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1 ORecordOperation (com.orientechnologies.orient.core.db.record.ORecordOperation)1 OSecurityException (com.orientechnologies.orient.core.exception.OSecurityException)1 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)1 ORID (com.orientechnologies.orient.core.id.ORID)1 ORecordId (com.orientechnologies.orient.core.id.ORecordId)1 OIndexKeyCursor (com.orientechnologies.orient.core.index.OIndexKeyCursor)1