Search in sources :

Example 56 with OIndex

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

the class OServerCommandPostStudio method executeClassIndexes.

private void executeClassIndexes(final OHttpRequest iRequest, final OHttpResponse iResponse, final ODatabaseDocument db, final String operation, final String rid, final String className, final Map<String, String> fields) throws IOException {
    // GET THE TARGET CLASS
    final OClass cls = db.getMetadata().getSchema().getClass(rid);
    if (cls == null) {
        iResponse.send(OHttpUtils.STATUS_INTERNALERROR_CODE, "Error", OHttpUtils.CONTENT_TEXT_PLAIN, "Error: Class '" + rid + "' not found.", null);
        return;
    }
    if ("add".equals(operation)) {
        iRequest.data.commandInfo = "Studio add index";
        try {
            final String[] fieldNames = OPatternConst.PATTERN_COMMA_SEPARATED.split(fields.get("fields").trim());
            final String indexType = fields.get("type");
            cls.createIndex(fields.get("name"), indexType, fieldNames);
            iResponse.send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_TEXT_PLAIN, "Index " + fields.get("name") + " created successfully", null);
        } catch (Exception e) {
            iResponse.send(OHttpUtils.STATUS_INTERNALERROR_CODE, "Error on creating a new index for class " + rid + ": " + e, OHttpUtils.CONTENT_TEXT_PLAIN, "Error on creating a new index for class " + rid + ": " + e, null);
        }
    } else if ("del".equals(operation)) {
        iRequest.data.commandInfo = "Studio delete index";
        try {
            final OIndex<?> index = cls.getClassIndex(className);
            if (index == null) {
                iResponse.send(OHttpUtils.STATUS_INTERNALERROR_CODE, "Error", OHttpUtils.CONTENT_TEXT_PLAIN, "Error: Index '" + className + "' not found in class '" + rid + "'.", null);
                return;
            }
            db.getMetadata().getIndexManager().dropIndex(index.getName());
            iResponse.send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_TEXT_PLAIN, "Index " + className + " deleted successfully.", null);
        } catch (Exception e) {
            iResponse.send(OHttpUtils.STATUS_INTERNALERROR_CODE, "Error on deletion index '" + className + "' for class " + rid + ": " + e, OHttpUtils.CONTENT_TEXT_PLAIN, "Error on deletion index '" + className + "' for class " + rid + ": " + e, null);
        }
    } else
        iResponse.send(OHttpUtils.STATUS_INTERNALERROR_CODE, "Error", OHttpUtils.CONTENT_TEXT_PLAIN, "Operation not supported", null);
}
Also used : OIndex(com.orientechnologies.orient.core.index.OIndex) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) IOException(java.io.IOException)

Example 57 with OIndex

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

the class OServerCommandGetDatabase method exportClass.

public static void exportClass(final ODatabaseDocument db, final OJSONWriter json, final OClass cls) throws IOException {
    json.beginObject();
    json.writeAttribute("name", cls.getName());
    json.writeAttribute("superClass", cls.getSuperClass() != null ? cls.getSuperClass().getName() : "");
    json.beginCollection("superClasses");
    int i = 0;
    for (OClass oClass : cls.getSuperClasses()) {
        json.write((i > 0 ? "," : "") + "\"" + oClass.getName() + "\"");
        i++;
    }
    json.endCollection();
    json.writeAttribute("alias", cls.getShortName());
    json.writeAttribute("abstract", cls.isAbstract());
    json.writeAttribute("strictmode", cls.isStrictMode());
    json.writeAttribute("clusters", cls.getClusterIds());
    json.writeAttribute("defaultCluster", cls.getDefaultClusterId());
    json.writeAttribute("clusterSelection", cls.getClusterSelection().getName());
    if (cls instanceof OClassImpl) {
        final Map<String, String> custom = ((OClassImpl) cls).getCustomInternal();
        if (custom != null && !custom.isEmpty()) {
            json.writeAttribute("custom", custom);
        }
    }
    try {
        json.writeAttribute("records", db.countClass(cls.getName()));
    } catch (OSecurityAccessException e) {
        json.writeAttribute("records", "? (Unauthorized)");
    } catch (Exception e) {
        json.writeAttribute("records", "? (Error)");
    }
    if (cls.properties() != null && cls.properties().size() > 0) {
        json.beginCollection("properties");
        for (final OProperty prop : cls.properties()) {
            json.beginObject();
            json.writeAttribute("name", prop.getName());
            if (prop.getLinkedClass() != null)
                json.writeAttribute("linkedClass", prop.getLinkedClass().getName());
            if (prop.getLinkedType() != null)
                json.writeAttribute("linkedType", prop.getLinkedType().toString());
            json.writeAttribute("type", prop.getType().toString());
            json.writeAttribute("mandatory", prop.isMandatory());
            json.writeAttribute("readonly", prop.isReadonly());
            json.writeAttribute("notNull", prop.isNotNull());
            json.writeAttribute("min", prop.getMin());
            json.writeAttribute("max", prop.getMax());
            json.writeAttribute("regexp", prop.getRegexp());
            json.writeAttribute("collate", prop.getCollate() != null ? prop.getCollate().getName() : "default");
            json.writeAttribute("defaultValue", prop.getDefaultValue());
            if (prop instanceof OPropertyImpl) {
                final Map<String, String> custom = ((OPropertyImpl) prop).getCustomInternal();
                if (custom != null && !custom.isEmpty()) {
                    json.writeAttribute("custom", custom);
                }
            }
            json.endObject();
        }
        json.endCollection();
    }
    final Set<OIndex<?>> indexes = cls.getIndexes();
    if (!indexes.isEmpty()) {
        json.beginCollection("indexes");
        for (final OIndex<?> index : indexes) {
            json.beginObject();
            json.writeAttribute("name", index.getName());
            json.writeAttribute("type", index.getType());
            final OIndexDefinition indexDefinition = index.getDefinition();
            if (indexDefinition != null && !indexDefinition.getFields().isEmpty())
                json.writeAttribute("fields", indexDefinition.getFields());
            json.endObject();
        }
        json.endCollection();
    }
    json.endObject();
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) OIndex(com.orientechnologies.orient.core.index.OIndex) OClassImpl(com.orientechnologies.orient.core.metadata.schema.OClassImpl) OPropertyImpl(com.orientechnologies.orient.core.metadata.schema.OPropertyImpl) IOException(java.io.IOException) OSecurityAccessException(com.orientechnologies.orient.core.exception.OSecurityAccessException) OSecurityAccessException(com.orientechnologies.orient.core.exception.OSecurityAccessException) OClass(com.orientechnologies.orient.core.metadata.schema.OClass)

Example 58 with OIndex

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

the class CRUDDocumentPhysicalTest method testMultiValues.

@Test(dependsOnMethods = "testDoubleChanges")
public void testMultiValues() {
    ODocument vDoc = database.newInstance();
    vDoc.setClassName("Profile");
    vDoc.field("nick", "Jacky").field("name", "Jack").field("surname", "Tramiel");
    vDoc.save();
    // add a new record with the same name "nameA".
    vDoc = database.newInstance();
    vDoc.setClassName("Profile");
    vDoc.field("nick", "Jack").field("name", "Jack").field("surname", "Bauer");
    vDoc.save();
    Collection<OIndex<?>> indexes = database.getMetadata().getSchema().getClass("Profile").getProperty("name").getIndexes();
    Assert.assertEquals(indexes.size(), 1);
    OIndex<?> indexName = indexes.iterator().next();
    // We must get 2 records for "nameA".
    Collection<OIdentifiable> vName1 = (Collection<OIdentifiable>) indexName.get("Jack");
    Assert.assertEquals(vName1.size(), 2);
    // Remove this last record.
    database.delete(vDoc);
    // We must get 1 record for "nameA".
    vName1 = (Collection<OIdentifiable>) indexName.get("Jack");
    Assert.assertEquals(vName1.size(), 1);
}
Also used : OIndex(com.orientechnologies.orient.core.index.OIndex) Collection(java.util.Collection) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 59 with OIndex

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

the class CRUDDocumentPhysicalTest method testDoubleChanges.

@Test(dependsOnMethods = "testUpdate")
public void testDoubleChanges() {
    ODocument vDoc = database.newInstance();
    vDoc.setClassName("Profile");
    vDoc.field("nick", "JayM1").field("name", "Jay").field("surname", "Miner");
    vDoc.save();
    Assert.assertEquals(vDoc.getIdentity().getClusterId(), vDoc.getSchemaClass().getDefaultClusterId());
    vDoc = database.load(vDoc.getIdentity());
    vDoc.field("nick", "JayM2");
    vDoc.field("nick", "JayM3");
    vDoc.save();
    Set<OIndex<?>> indexes = database.getMetadata().getSchema().getClass("Profile").getProperty("nick").getIndexes();
    Assert.assertEquals(indexes.size(), 1);
    OIndex indexDefinition = indexes.iterator().next();
    OIdentifiable vOldName = (OIdentifiable) indexDefinition.get("JayM1");
    Assert.assertNull(vOldName);
    OIdentifiable vIntermediateName = (OIdentifiable) indexDefinition.get("JayM2");
    Assert.assertNull(vIntermediateName);
    OIdentifiable vNewName = (OIdentifiable) indexDefinition.get("JayM3");
    Assert.assertNotNull(vNewName);
}
Also used : OIndex(com.orientechnologies.orient.core.index.OIndex) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 60 with OIndex

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

the class OHashIndexMTSpeedTest method main.

public static void main(String[] args) throws IOException, InterruptedException {
    OGlobalConfiguration.ENVIRONMENT_LOCK_MANAGER_CONCURRENCY_LEVEL.setValue(64);
    String buildDirectory = System.getProperty("buildDirectory", ".");
    if (buildDirectory == null)
        buildDirectory = ".";
    final String dbUrl = "plocal:" + buildDirectory + "/uniqueHashIndexTest";
    databaseDocumentTx = new ODatabaseDocumentTx(dbUrl);
    if (databaseDocumentTx.exists()) {
        databaseDocumentTx.open("admin", "admin");
        databaseDocumentTx.drop();
    }
    databaseDocumentTx.create();
    ODocument metadata = new ODocument().field("partitions", concurrencyLevel * 8);
    final OIndex<?> userIndex = databaseDocumentTx.getMetadata().getIndexManager().createIndex("User.id", "UNIQUE", new OSimpleKeyIndexDefinition(-1, OType.LONG), new int[0], null, metadata, "AUTOSHARDING");
    Orient.instance().scheduleTask(new TimerTask() {

        private ODatabaseDocumentTx db = databaseDocumentTx.copy();

        @Override
        public void run() {
            db.activateOnCurrentThread();
            final OIndex<?> index = db.getMetadata().getIndexManager().getIndex("User.id");
            final long count = index.getKeySize();
            System.out.println(String.format("entries=%d %d/sec", count, ((count - lastCount) * 1000 / 2000)));
            lastCount = count;
        }
    }, 2000, 2000);
    final Thread[] threads = new Thread[concurrencyLevel];
    for (int i = 0; i < concurrencyLevel; ++i) {
        final int threadId = i;
        threads[i] = new Thread() {

            @Override
            public void run() {
                final ODatabaseDocumentTx db = databaseDocumentTx.copy();
                db.activateOnCurrentThread();
                db.declareIntent(new OIntentMassiveInsert());
                long totalPerThread = total / concurrencyLevel;
                if (threadId == concurrencyLevel - 1)
                    totalPerThread += total % concurrencyLevel;
                final OIndex<?> index = db.getMetadata().getIndexManager().getIndex("User.id");
                if (useTx)
                    db.begin();
                for (long k = totalPerThread * threadId; k < totalPerThread * (threadId + 1); ++k) {
                    index.put(k, new ORecordId(0, k));
                    if (useTx && (k - totalPerThread) % 2 == 0) {
                        db.commit();
                        db.begin();
                    }
                }
                if (useTx)
                    db.commit();
            }
        };
    }
    final long beginTime = System.currentTimeMillis();
    for (int i = 0; i < concurrencyLevel; ++i) {
        threads[i].start();
    }
    for (int i = 0; i < concurrencyLevel; ++i) {
        threads[i].join();
    }
    final OIndex<?> index = databaseDocumentTx.getMetadata().getIndexManager().getIndex("User.id");
    final long foundKeys = index.getKeySize();
    databaseDocumentTx.activateOnCurrentThread();
    databaseDocumentTx.close();
    final long endTime = System.currentTimeMillis();
    System.out.println(String.format("TOTAL TIME: %s AVG %d/sec", OIOUtils.getTimeAsString(endTime - beginTime), foundKeys * 1000 / (endTime - beginTime)));
}
Also used : OIndex(com.orientechnologies.orient.core.index.OIndex) OSimpleKeyIndexDefinition(com.orientechnologies.orient.core.index.OSimpleKeyIndexDefinition) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OIntentMassiveInsert(com.orientechnologies.orient.core.intent.OIntentMassiveInsert) ORecordId(com.orientechnologies.orient.core.id.ORecordId) TimerTask(java.util.TimerTask) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

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