Search in sources :

Example 31 with OIndexDefinition

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

the class ODatabaseExport method exportManualIndexes.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void exportManualIndexes() throws IOException {
    listener.onMessage("\nExporting manual indexes content...");
    final OIndexManagerProxy indexManager = database.getMetadata().getIndexManager();
    indexManager.reload();
    final Collection<? extends OIndex<?>> indexes = indexManager.getIndexes();
    ODocument exportEntry = new ODocument();
    int manualIndexes = 0;
    writer.beginCollection(1, true, "manualIndexes");
    for (OIndex<?> index : indexes) {
        if (index.getName().equals(ODatabaseImport.EXPORT_IMPORT_MAP_NAME))
            continue;
        if (!index.isAutomatic()) {
            listener.onMessage("\n- Exporting index " + index.getName() + " ...");
            writer.beginObject(2, true, null);
            writer.writeAttribute(3, true, "name", index.getName());
            List<ODocument> indexContent = database.query(new OSQLSynchQuery<ODocument>("select from index:" + index.getName()));
            writer.beginCollection(3, true, "content");
            int i = 0;
            for (ODocument indexEntry : indexContent) {
                if (i > 0)
                    writer.append(",");
                indexEntry.setLazyLoad(false);
                final OIndexDefinition indexDefinition = index.getDefinition();
                exportEntry.reset();
                exportEntry.setLazyLoad(false);
                if (indexDefinition instanceof ORuntimeKeyIndexDefinition && ((ORuntimeKeyIndexDefinition) indexDefinition).getSerializer() != null) {
                    final OBinarySerializer binarySerializer = ((ORuntimeKeyIndexDefinition) indexDefinition).getSerializer();
                    final int dataSize = binarySerializer.getObjectSize(indexEntry.field("key"));
                    final byte[] binaryContent = new byte[dataSize];
                    binarySerializer.serialize(indexEntry.field("key"), binaryContent, 0);
                    exportEntry.field("binary", true);
                    exportEntry.field("key", binaryContent);
                } else {
                    exportEntry.field("binary", false);
                    exportEntry.field("key", indexEntry.field("key"));
                }
                exportEntry.field("rid", indexEntry.field("rid"));
                i++;
                writer.append(exportEntry.toJSON());
                final long percent = indexContent.size() / 10;
                if (percent > 0 && (i % percent) == 0)
                    listener.onMessage(".");
            }
            writer.endCollection(3, true);
            writer.endObject(2, true);
            listener.onMessage("OK (entries=" + index.getSize() + ")");
            manualIndexes++;
        }
    }
    writer.endCollection(1, true);
    listener.onMessage("\nOK (" + manualIndexes + " manual indexes)");
}
Also used : OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) ORuntimeKeyIndexDefinition(com.orientechnologies.orient.core.index.ORuntimeKeyIndexDefinition) OBinarySerializer(com.orientechnologies.common.serialization.types.OBinarySerializer) OIndexManagerProxy(com.orientechnologies.orient.core.index.OIndexManagerProxy) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 32 with OIndexDefinition

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

the class IndexManagerTest method testGetClassIndexes.

@Test(dependsOnMethods = { "createCompositeIndexTestWithListener", "createCompositeIndexTestWithoutListener", "testCreateOnePropertyIndexTest" })
public void testGetClassIndexes() {
    final OIndexManager indexManager = database.getMetadata().getIndexManager();
    final Set<OIndex<?>> indexes = indexManager.getClassIndexes(CLASS_NAME);
    final Set<OIndexDefinition> expectedIndexDefinitions = new HashSet<OIndexDefinition>();
    final OCompositeIndexDefinition compositeIndexOne = new OCompositeIndexDefinition(CLASS_NAME);
    compositeIndexOne.addIndex(new OPropertyIndexDefinition(CLASS_NAME, "fOne", OType.INTEGER));
    compositeIndexOne.addIndex(new OPropertyIndexDefinition(CLASS_NAME, "fTwo", OType.STRING));
    compositeIndexOne.setNullValuesIgnored(false);
    expectedIndexDefinitions.add(compositeIndexOne);
    final OCompositeIndexDefinition compositeIndexTwo = new OCompositeIndexDefinition(CLASS_NAME);
    compositeIndexTwo.addIndex(new OPropertyIndexDefinition(CLASS_NAME, "fOne", OType.INTEGER));
    compositeIndexTwo.addIndex(new OPropertyIndexDefinition(CLASS_NAME, "fTwo", OType.STRING));
    compositeIndexTwo.addIndex(new OPropertyIndexDefinition(CLASS_NAME, "fThree", OType.BOOLEAN));
    compositeIndexTwo.setNullValuesIgnored(false);
    expectedIndexDefinitions.add(compositeIndexTwo);
    final OPropertyIndexDefinition propertyIndex = new OPropertyIndexDefinition(CLASS_NAME, "fOne", OType.INTEGER);
    propertyIndex.setNullValuesIgnored(false);
    expectedIndexDefinitions.add(propertyIndex);
    assertEquals(indexes.size(), 3);
    for (final OIndex index : indexes) {
        assertTrue(expectedIndexDefinitions.contains(index.getDefinition()));
    }
}
Also used : OIndexManager(com.orientechnologies.orient.core.index.OIndexManager) OCompositeIndexDefinition(com.orientechnologies.orient.core.index.OCompositeIndexDefinition) OPropertyIndexDefinition(com.orientechnologies.orient.core.index.OPropertyIndexDefinition) OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) OIndex(com.orientechnologies.orient.core.index.OIndex) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 33 with OIndexDefinition

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

the class IndexManagerTest method testGetClassIndexesBrokenClassNameCase.

@Test(dependsOnMethods = { "createCompositeIndexTestWithListener", "createCompositeIndexTestWithoutListener", "testCreateOnePropertyIndexTest" })
public void testGetClassIndexesBrokenClassNameCase() {
    final OIndexManager indexManager = database.getMetadata().getIndexManager();
    final Set<OIndex<?>> indexes = indexManager.getClassIndexes("ClassforindeXMaNAgerTeST");
    final Set<OIndexDefinition> expectedIndexDefinitions = new HashSet<OIndexDefinition>();
    final OCompositeIndexDefinition compositeIndexOne = new OCompositeIndexDefinition(CLASS_NAME);
    compositeIndexOne.addIndex(new OPropertyIndexDefinition(CLASS_NAME, "fOne", OType.INTEGER));
    compositeIndexOne.addIndex(new OPropertyIndexDefinition(CLASS_NAME, "fTwo", OType.STRING));
    compositeIndexOne.setNullValuesIgnored(false);
    expectedIndexDefinitions.add(compositeIndexOne);
    final OCompositeIndexDefinition compositeIndexTwo = new OCompositeIndexDefinition(CLASS_NAME);
    compositeIndexTwo.addIndex(new OPropertyIndexDefinition(CLASS_NAME, "fOne", OType.INTEGER));
    compositeIndexTwo.addIndex(new OPropertyIndexDefinition(CLASS_NAME, "fTwo", OType.STRING));
    compositeIndexTwo.addIndex(new OPropertyIndexDefinition(CLASS_NAME, "fThree", OType.BOOLEAN));
    compositeIndexTwo.setNullValuesIgnored(false);
    expectedIndexDefinitions.add(compositeIndexTwo);
    final OPropertyIndexDefinition propertyIndex = new OPropertyIndexDefinition(CLASS_NAME, "fOne", OType.INTEGER);
    propertyIndex.setNullValuesIgnored(false);
    expectedIndexDefinitions.add(propertyIndex);
    assertEquals(indexes.size(), 3);
    for (final OIndex index : indexes) {
        assertTrue(expectedIndexDefinitions.contains(index.getDefinition()));
    }
}
Also used : OIndexManager(com.orientechnologies.orient.core.index.OIndexManager) OCompositeIndexDefinition(com.orientechnologies.orient.core.index.OCompositeIndexDefinition) OPropertyIndexDefinition(com.orientechnologies.orient.core.index.OPropertyIndexDefinition) OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) OIndex(com.orientechnologies.orient.core.index.OIndex) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 34 with OIndexDefinition

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

the class SQLDropPropertyIndexTest method testForcePropertyDisabledBrokenCase.

@Test
public void testForcePropertyDisabledBrokenCase() throws Exception {
    database.command(new OCommandSQL("CREATE INDEX DropPropertyIndexCompositeIndex ON DropPropertyIndexTestClass (prop1, prop2) UNIQUE")).execute();
    try {
        database.command(new OCommandSQL("DROP PROPERTY DropPropertyIndextestclaSS.proP1")).execute();
        Assert.fail();
    } catch (OCommandExecutionException e) {
        Assert.assertTrue(e.getMessage().contains("Property used in indexes (" + "DropPropertyIndexCompositeIndex" + "). Please drop these indexes before removing property or use FORCE parameter."));
    }
    database.getMetadata().getIndexManager().reload();
    final OIndex<?> index = database.getMetadata().getSchema().getClass("DropPropertyIndexTestClass").getClassIndex("DropPropertyIndexCompositeIndex");
    Assert.assertNotNull(index);
    final OIndexDefinition indexDefinition = index.getDefinition();
    Assert.assertTrue(indexDefinition instanceof OCompositeIndexDefinition);
    Assert.assertEquals(indexDefinition.getFields(), Arrays.asList("prop1", "prop2"));
    Assert.assertEquals(indexDefinition.getTypes(), new OType[] { EXPECTED_PROP1_TYPE, EXPECTED_PROP2_TYPE });
    Assert.assertEquals(index.getType(), "UNIQUE");
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OCompositeIndexDefinition(com.orientechnologies.orient.core.index.OCompositeIndexDefinition) OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException)

Example 35 with OIndexDefinition

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

the class PropertyIndexTest method testCreateUniqueIndex.

@Test
public void testCreateUniqueIndex() {
    final OSchema schema = database.getMetadata().getSchema();
    final OClass oClass = schema.getClass("PropertyIndexTestClass");
    final OProperty propOne = oClass.getProperty("prop1");
    propOne.createIndex(OClass.INDEX_TYPE.UNIQUE, new ODocument().field("ignoreNullValues", true));
    final Collection<OIndex<?>> indexes = propOne.getIndexes();
    OIndexDefinition indexDefinition = null;
    for (final OIndex<?> index : indexes) {
        if (index.getName().equals("PropertyIndexTestClass.prop1")) {
            indexDefinition = index.getDefinition();
            break;
        }
    }
    Assert.assertNotNull(indexDefinition);
    Assert.assertEquals(indexDefinition.getParamCount(), 1);
    Assert.assertEquals(indexDefinition.getFields().size(), 1);
    Assert.assertTrue(indexDefinition.getFields().contains("prop1"));
    Assert.assertEquals(indexDefinition.getTypes().length, 1);
    Assert.assertEquals(indexDefinition.getTypes()[0], OType.STRING);
    schema.save();
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) OIndex(com.orientechnologies.orient.core.index.OIndex) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

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