Search in sources :

Example 6 with OIndexDefinition

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

the class SQLIndexWithoutSchemaTest method testCreateIndex.

@Test
public void testCreateIndex() {
    database.command(new OCommandSQL("CREATE INDEX indexWithoutSchema ON " + TEST_CLASS + " (prop2) NOTUNIQUE INTEGER")).execute();
    database.getMetadata().getIndexManager().reload();
    final OIndex<?> index = database.getMetadata().getSchema().getClass(TEST_CLASS).getClassIndex("indexWithoutSchema");
    Assert.assertNotNull(index);
    Assert.assertEquals(index.getType(), OClass.INDEX_TYPE.NOTUNIQUE.name());
    final OIndexDefinition definition = index.getDefinition();
    Assert.assertEquals(definition.getFields().size(), 1);
    Assert.assertEquals(definition.getFields().get(0).toLowerCase(), "prop2");
    Assert.assertEquals(definition.getTypes()[0], OType.INTEGER);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition)

Example 7 with OIndexDefinition

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

the class SQLIndexWithoutSchemaTest method testCreateCompositeIndex.

@Test
public void testCreateCompositeIndex() {
    database.command(new OCommandSQL("CREATE INDEX compositeIndexWithoutSchema ON " + TEST_CLASS + " (cp2, cp3) NOTUNIQUE INTEGER, INTEGER METADATA { ignoreNullValues: true }")).execute();
    database.getMetadata().getIndexManager().reload();
    final OIndex<?> index = database.getMetadata().getSchema().getClass(TEST_CLASS).getClassIndex("compositeIndexWithoutSchema");
    Assert.assertNotNull(index);
    Assert.assertEquals(index.getType(), OClass.INDEX_TYPE.NOTUNIQUE.name());
    final OIndexDefinition definition = index.getDefinition();
    Assert.assertEquals(definition.getFields().size(), 2);
    Assert.assertEquals(definition.getFields().get(0).toLowerCase(), "cp2");
    Assert.assertEquals(definition.getFields().get(1).toLowerCase(), "cp3");
    Assert.assertEquals(definition.getTypes()[0], OType.INTEGER);
    Assert.assertEquals(definition.getTypes()[1], OType.INTEGER);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition)

Example 8 with OIndexDefinition

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

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

the class OConsoleDatabaseApp method infoProperty.

@ConsoleCommand(description = "Display a class property", onlineHelp = "Console-Command-Info-Property")
public void infoProperty(@ConsoleParameter(name = "property-name", description = "The name of the property as <class>.<property>") final String iPropertyName) {
    checkForDatabase();
    if (iPropertyName.indexOf('.') == -1)
        throw new OSystemException("Property name is in the format <class>.<property>");
    final String[] parts = iPropertyName.split("\\.");
    final OClass cls = currentDatabase.getMetadata().getImmutableSchemaSnapshot().getClass(parts[0]);
    if (cls == null) {
        message("\n! Class '" + parts[0] + "' does not exist in the database '" + currentDatabaseName + "'");
        return;
    }
    final OProperty prop = cls.getProperty(parts[1]);
    if (prop == null) {
        message("\n! Property '" + parts[1] + "' does not exist in class '" + parts[0] + "'");
        return;
    }
    message("\nPROPERTY '" + prop.getFullName() + "'\n");
    message("\nType.................: " + prop.getType());
    message("\nMandatory............: " + prop.isMandatory());
    message("\nNot null.............: " + prop.isNotNull());
    message("\nRead only............: " + prop.isReadonly());
    message("\nDefault value........: " + prop.getDefaultValue());
    message("\nMinimum value........: " + prop.getMin());
    message("\nMaximum value........: " + prop.getMax());
    message("\nREGEXP...............: " + prop.getRegexp());
    message("\nCollate..............: " + prop.getCollate());
    message("\nLinked class.........: " + prop.getLinkedClass());
    message("\nLinked type..........: " + prop.getLinkedType());
    if (prop.getCustomKeys().size() > 0) {
        message("\n\nCUSTOM ATTRIBUTES");
        final List<ODocument> resultSet = new ArrayList<ODocument>();
        for (final String k : prop.getCustomKeys()) {
            try {
                final ODocument row = new ODocument();
                resultSet.add(row);
                row.field("NAME", k);
                row.field("VALUE", prop.getCustom(k));
            } catch (Exception ignored) {
            }
        }
        final OTableFormatter formatter = new OTableFormatter(this);
        formatter.writeRecords(resultSet, -1);
    }
    final Collection<OIndex<?>> indexes = prop.getAllIndexes();
    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);
    }
}
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) 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 10 with OIndexDefinition

use of com.orientechnologies.orient.core.index.OIndexDefinition 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)

Aggregations

OIndexDefinition (com.orientechnologies.orient.core.index.OIndexDefinition)67 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)29 OCompositeIndexDefinition (com.orientechnologies.orient.core.index.OCompositeIndexDefinition)26 OIndex (com.orientechnologies.orient.core.index.OIndex)26 Test (org.testng.annotations.Test)25 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)18 OPropertyMapIndexDefinition (com.orientechnologies.orient.core.index.OPropertyMapIndexDefinition)13 OIndexCursor (com.orientechnologies.orient.core.index.OIndexCursor)8 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)7 OIndexDefinitionMultiValue (com.orientechnologies.orient.core.index.OIndexDefinitionMultiValue)6 OIndexUnique (com.orientechnologies.orient.core.index.OIndexUnique)6 OPropertyIndexDefinition (com.orientechnologies.orient.core.index.OPropertyIndexDefinition)6 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)5 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)4 OPropertyListIndexDefinition (com.orientechnologies.orient.core.index.OPropertyListIndexDefinition)4 OPropertyRidBagIndexDefinition (com.orientechnologies.orient.core.index.OPropertyRidBagIndexDefinition)4 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)4 HashSet (java.util.HashSet)4 ConsoleCommand (com.orientechnologies.common.console.annotation.ConsoleCommand)3 OSystemException (com.orientechnologies.common.exception.OSystemException)3