Search in sources :

Example 36 with OIndex

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

the class OConsoleDatabaseApp method listIndexes.

@ConsoleCommand(description = "Display all indexes", aliases = { "indexes" }, onlineHelp = "Console-Command-List-Indexes")
public void listIndexes() {
    if (currentDatabaseName != null) {
        message("\n\nINDEXES");
        final List<ODocument> resultSet = new ArrayList<ODocument>();
        int totalIndexes = 0;
        long totalRecords = 0;
        final List<OIndex<?>> indexes = new ArrayList<OIndex<?>>(currentDatabase.getMetadata().getIndexManager().getIndexes());
        Collections.sort(indexes, new Comparator<OIndex<?>>() {

            public int compare(OIndex<?> o1, OIndex<?> o2) {
                return o1.getName().compareToIgnoreCase(o2.getName());
            }
        });
        long totalIndexedRecords = 0;
        for (final OIndex<?> index : indexes) {
            final ODocument row = new ODocument();
            resultSet.add(row);
            final long indexSize = index.getKeySize();
            totalIndexedRecords += indexSize;
            row.field("NAME", index.getName());
            row.field("TYPE", index.getType());
            row.field("RECORDS", indexSize);
            try {
                final OIndexDefinition indexDefinition = index.getDefinition();
                final long size = index.getKeySize();
                if (indexDefinition != null) {
                    row.field("CLASS", indexDefinition.getClassName());
                    row.field("COLLATE", indexDefinition.getCollate().getName());
                    final List<String> fields = indexDefinition.getFields();
                    final StringBuilder buffer = new StringBuilder();
                    for (int i = 0; i < fields.size(); ++i) {
                        if (buffer.length() > 0)
                            buffer.append(",");
                        buffer.append(fields.get(i));
                        buffer.append("(");
                        buffer.append(indexDefinition.getTypes()[i]);
                        buffer.append(")");
                    }
                    row.field("FIELDS", buffer.toString());
                }
                totalIndexes++;
                totalRecords += size;
            } catch (Exception ignored) {
            }
        }
        final OTableFormatter formatter = new OTableFormatter(this);
        final ODocument footer = new ODocument();
        footer.field("NAME", "TOTAL");
        footer.field("RECORDS", totalIndexedRecords);
        formatter.setFooter(footer);
        formatter.writeRecords(resultSet, -1);
    } else
        message("\nNo database selected yet.");
}
Also used : OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) OIndex(com.orientechnologies.orient.core.index.OIndex) OSystemException(com.orientechnologies.common.exception.OSystemException) OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) ORetryQueryException(com.orientechnologies.orient.core.exception.ORetryQueryException) OIOException(com.orientechnologies.common.io.OIOException) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) ConsoleCommand(com.orientechnologies.common.console.annotation.ConsoleCommand)

Example 37 with OIndex

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

the class OConsoleDatabaseApp method infoClass.

@ConsoleCommand(aliases = { "desc" }, description = "Display a class in the schema", onlineHelp = "Console-Command-Info-Class")
public void infoClass(@ConsoleParameter(name = "class-name", description = "The name of the class") final String iClassName) {
    checkForDatabase();
    final OClass cls = currentDatabase.getMetadata().getImmutableSchemaSnapshot().getClass(iClassName);
    if (cls == null) {
        message("\n! Class '" + iClassName + "' does not exist in the database '" + currentDatabaseName + "'");
        return;
    }
    message("\nCLASS '" + cls.getName() + "'\n");
    final long count = currentDatabase.countClass(cls.getName(), false);
    message("\nRecords..............: " + count);
    if (cls.getShortName() != null)
        message("\nAlias................: " + cls.getShortName());
    if (cls.hasSuperClasses())
        message("\nSuper classes........: " + Arrays.toString(cls.getSuperClassesNames().toArray()));
    message("\nDefault cluster......: " + currentDatabase.getClusterNameById(cls.getDefaultClusterId()) + " (id=" + cls.getDefaultClusterId() + ")");
    final StringBuilder clusters = new StringBuilder();
    for (int clId : cls.getClusterIds()) {
        if (clusters.length() > 0)
            clusters.append(", ");
        clusters.append(currentDatabase.getClusterNameById(clId));
        clusters.append("(");
        clusters.append(clId);
        clusters.append(")");
    }
    message("\nSupported clusters...: " + clusters.toString());
    message("\nCluster selection....: " + cls.getClusterSelection().getName());
    message("\nOversize.............: " + cls.getClassOverSize());
    if (!cls.getSubclasses().isEmpty()) {
        message("\nSubclasses.........: ");
        int i = 0;
        for (OClass c : cls.getSubclasses()) {
            if (i > 0)
                message(", ");
            message(c.getName());
            ++i;
        }
        out.println();
    }
    if (cls.properties().size() > 0) {
        message("\n\nPROPERTIES");
        final List<ODocument> resultSet = new ArrayList<ODocument>();
        for (final OProperty p : cls.properties()) {
            try {
                final ODocument row = new ODocument();
                resultSet.add(row);
                row.field("NAME", p.getName());
                row.field("TYPE", (Object) p.getType());
                row.field("LINKED-TYPE/CLASS", p.getLinkedClass() != null ? p.getLinkedClass() : p.getLinkedType());
                row.field("MANDATORY", p.isMandatory());
                row.field("READONLY", p.isReadonly());
                row.field("NOT-NULL", p.isNotNull());
                row.field("MIN", p.getMin() != null ? p.getMin() : "");
                row.field("MAX", p.getMax() != null ? p.getMax() : "");
                row.field("COLLATE", p.getCollate() != null ? p.getCollate().getName() : "");
                row.field("DEFAULT", p.getDefaultValue() != null ? p.getDefaultValue() : "");
            } catch (Exception ignored) {
            }
        }
        final OTableFormatter formatter = new OTableFormatter(this);
        formatter.writeRecords(resultSet, -1);
    }
    final Set<OIndex<?>> indexes = cls.getClassIndexes();
    if (!indexes.isEmpty()) {
        message("\n\nINDEXES (" + indexes.size() + " altogether)");
        final List<ODocument> resultSet = new ArrayList<ODocument>();
        for (final OIndex<?> index : indexes) {
            final ODocument row = new ODocument();
            resultSet.add(row);
            row.field("NAME", index.getName());
            final OIndexDefinition indexDefinition = index.getDefinition();
            if (indexDefinition != null) {
                final List<String> fields = indexDefinition.getFields();
                row.field("PROPERTIES", fields);
            }
        }
        final OTableFormatter formatter = new OTableFormatter(this);
        formatter.writeRecords(resultSet, -1);
    }
    if (cls.getCustomKeys().size() > 0) {
        message("\n\nCUSTOM ATTRIBUTES");
        final List<ODocument> resultSet = new ArrayList<ODocument>();
        for (final String k : cls.getCustomKeys()) {
            try {
                final ODocument row = new ODocument();
                resultSet.add(row);
                row.field("NAME", k);
                row.field("VALUE", cls.getCustom(k));
            } catch (Exception ignored) {
            // IGNORED
            }
        }
        final OTableFormatter formatter = new OTableFormatter(this);
        formatter.writeRecords(resultSet, -1);
    }
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) OIndex(com.orientechnologies.orient.core.index.OIndex) OSystemException(com.orientechnologies.common.exception.OSystemException) OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) ORetryQueryException(com.orientechnologies.orient.core.exception.ORetryQueryException) OIOException(com.orientechnologies.common.io.OIOException) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) ConsoleCommand(com.orientechnologies.common.console.annotation.ConsoleCommand)

Example 38 with OIndex

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

the class PropertyIndexTest method testGetAllIndexes.

@Test(dependsOnMethods = "createAdditionalSchemas")
public void testGetAllIndexes() {
    final OSchema schema = database.getMetadata().getSchema();
    final OClass oClass = schema.getClass("PropertyIndexTestClass");
    final OProperty propOne = oClass.getProperty("prop1");
    final Collection<OIndex<?>> indexes = propOne.getAllIndexes();
    Assert.assertEquals(indexes.size(), 5);
    Assert.assertNotNull(containsIndex(indexes, "PropertyIndexTestClass.prop1"));
    Assert.assertNotNull(containsIndex(indexes, "propOne0"));
    Assert.assertNotNull(containsIndex(indexes, "propOne1"));
    Assert.assertNotNull(containsIndex(indexes, "propOne2"));
    Assert.assertNotNull(containsIndex(indexes, "propOne4"));
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OIndex(com.orientechnologies.orient.core.index.OIndex) OClass(com.orientechnologies.orient.core.metadata.schema.OClass)

Example 39 with OIndex

use of com.orientechnologies.orient.core.index.OIndex 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 40 with OIndex

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

the class OAbstractRecordReplicatedTask method init.

public OAbstractRecordReplicatedTask init(final ORecordId iRid, final int iVersion) {
    this.rid = iRid;
    this.version = iVersion;
    final ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
    if (db != null) {
        final OClass clazz = db.getMetadata().getSchema().getClassByClusterId(rid.getClusterId());
        if (clazz != null) {
            final Set<OIndex<?>> indexes = clazz.getIndexes();
            if (indexes != null && !indexes.isEmpty()) {
                for (OIndex idx : indexes) if (idx.isUnique())
                    // UNIQUE INDEX: RETURN THE HASH OF THE NAME TO USE THE SAME PARTITION ID AVOIDING CONCURRENCY ON INDEX UPDATES
                    partitionKey = idx.getName().hashCode();
            }
        }
    }
    return this;
}
Also used : OIndex(com.orientechnologies.orient.core.index.OIndex) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Aggregations

OIndex (com.orientechnologies.orient.core.index.OIndex)98 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)54 Test (org.testng.annotations.Test)50 OIndexDefinition (com.orientechnologies.orient.core.index.OIndexDefinition)26 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)20 OIndexManager (com.orientechnologies.orient.core.index.OIndexManager)18 OCompositeIndexDefinition (com.orientechnologies.orient.core.index.OCompositeIndexDefinition)16 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)16 Test (org.junit.Test)14 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)12 Collection (java.util.Collection)11 OPropertyIndexDefinition (com.orientechnologies.orient.core.index.OPropertyIndexDefinition)9 OPropertyMapIndexDefinition (com.orientechnologies.orient.core.index.OPropertyMapIndexDefinition)8 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)8 HashSet (java.util.HashSet)6 OConfigurationException (com.orientechnologies.orient.core.exception.OConfigurationException)5 OIndexManagerProxy (com.orientechnologies.orient.core.index.OIndexManagerProxy)5 OIndexUnique (com.orientechnologies.orient.core.index.OIndexUnique)5 Map (java.util.Map)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4