Search in sources :

Example 66 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema in project orientdb by orientechnologies.

the class OCommandExecutorSQLCreateClassTest method init.

@BeforeClass
public static void init() throws Exception {
    db = new ODatabaseDocumentTx("memory:" + OCommandExecutorSQLCreateClassTest.class.getSimpleName());
    if (db.exists()) {
        db.open("admin", "admin");
        db.drop();
    }
    db.create();
    final OSchema schema = db.getMetadata().getSchema();
    schema.createClass("User", schema.getClass("V"));
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)

Example 67 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema in project orientdb by orientechnologies.

the class OBonsaiTreeRepair method repairDatabaseRidbags.

public void repairDatabaseRidbags(ODatabaseDocument db, OCommandOutputListener outputListener) {
    message(outputListener, "Repair of ridbags is started ...\n");
    final OMetadata metadata = db.getMetadata();
    final OSchema schema = metadata.getSchema();
    final OClass edgeClass = schema.getClass(OrientEdgeType.CLASS_NAME);
    if (edgeClass != null) {
        final HashMap<String, Set<ORID>> processedVertexes = new HashMap<String, Set<ORID>>();
        final long countEdges = db.countClass(edgeClass.getName());
        message(outputListener, countEdges + " will be processed.");
        long counter = 0;
        for (ODocument edge : db.browseClass(edgeClass.getName())) {
            try {
                final String label;
                if (edge.field(OrientElement.LABEL_FIELD_NAME) != null) {
                    label = edge.field(OrientElement.LABEL_FIELD_NAME);
                } else if (!edge.getClassName().equals(edgeClass.getName())) {
                    label = edge.getClassName();
                } else {
                    counter++;
                    continue;
                }
                final ODocument inVertex = edge.<OIdentifiable>field(OrientBaseGraph.CONNECTION_IN).getRecord();
                final ODocument outVertex = edge.<OIdentifiable>field(OrientBaseGraph.CONNECTION_OUT).getRecord();
                final String inVertexName = OrientVertex.getConnectionFieldName(Direction.IN, label, true);
                final String outVertexName = OrientVertex.getConnectionFieldName(Direction.OUT, label, true);
                Set<ORID> inVertexes = processedVertexes.get(inVertexName);
                if (inVertexes == null) {
                    inVertexes = new HashSet<ORID>();
                    processedVertexes.put(inVertexName, inVertexes);
                }
                Set<ORID> outVertexes = processedVertexes.get(outVertexName);
                if (outVertexes == null) {
                    outVertexes = new HashSet<ORID>();
                    processedVertexes.put(outVertexName, outVertexes);
                }
                if (inVertex.field(inVertexName) instanceof ORidBag) {
                    if (inVertexes.add(inVertex.getIdentity())) {
                        inVertex.field(inVertexName, new ORidBag());
                    }
                    final ORidBag inRidBag = inVertex.field(inVertexName);
                    inRidBag.add(edge.getIdentity());
                    inVertex.save();
                }
                if (outVertex.field(outVertexName) instanceof ORidBag) {
                    if (outVertexes.add(outVertex.getIdentity())) {
                        outVertex.field(outVertexName, new ORidBag());
                    }
                    final ORidBag outRidBag = outVertex.field(outVertexName);
                    outRidBag.add(edge.getIdentity());
                    outVertex.save();
                }
                counter++;
                if (counter > 0 && counter % 1000 == 0)
                    message(outputListener, counter + " edges were processed out of " + countEdges + " \n.");
            } catch (Exception e) {
                e.printStackTrace();
                message(outputListener, "Error during processing of edge with id " + edge.getIdentity() + "\n");
            }
        }
        message(outputListener, "Processed " + counter + " from " + countEdges + ".");
    }
    message(outputListener, "repair of ridbags is completed\n");
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OMetadata(com.orientechnologies.orient.core.metadata.OMetadata) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 68 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema in project orientdb by orientechnologies.

the class IndexTest method testNullIndexKeysSupportInTx.

public void testNullIndexKeysSupportInTx() {
    final ODatabaseDocumentTx databaseDocumentTx = (ODatabaseDocumentTx) database.getUnderlying();
    final OSchema schema = databaseDocumentTx.getMetadata().getSchema();
    final OClass clazz = schema.createClass("NullIndexKeysSupportInTx", 1, null);
    clazz.createProperty("nullField", OType.STRING);
    ODocument metadata = new ODocument();
    metadata.field("ignoreNullValues", false);
    clazz.createIndex("NullIndexKeysSupportInTxIndex", INDEX_TYPE.NOTUNIQUE.toString(), null, metadata, new String[] { "nullField" });
    database.begin();
    for (int i = 0; i < 20; i++) {
        if (i % 5 == 0) {
            ODocument document = new ODocument("NullIndexKeysSupportInTx");
            document.field("nullField", (Object) null);
            document.save();
        } else {
            ODocument document = new ODocument("NullIndexKeysSupportInTx");
            document.field("nullField", "val" + i);
            document.save();
        }
    }
    database.commit();
    List<ODocument> result = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>("select from NullIndexKeysSupportInTx where nullField = 'val3'"));
    Assert.assertEquals(result.size(), 1);
    Assert.assertEquals(result.get(0).field("nullField"), "val3");
    final String query = "select from NullIndexKeysSupportInTx where nullField is null";
    result = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>("select from NullIndexKeysSupportInTx where nullField is null"));
    Assert.assertEquals(result.size(), 4);
    for (ODocument document : result) Assert.assertNull(document.field("nullField"));
    final ODocument explain = databaseDocumentTx.command(new OCommandSQL("explain " + query)).execute();
    Assert.assertTrue(explain.<Set<String>>field("involvedIndexes").contains("NullIndexKeysSupportInTxIndex"));
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 69 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema in project orientdb by orientechnologies.

the class IndexTest method testCreateIndexAbstractClass.

public void testCreateIndexAbstractClass() {
    final ODatabaseDocumentTx databaseDocumentTx = (ODatabaseDocumentTx) database.getUnderlying();
    final OSchema schema = databaseDocumentTx.getMetadata().getSchema();
    OClass abstractClass = schema.createAbstractClass("TestCreateIndexAbstractClass");
    abstractClass.createProperty("value", OType.STRING).setMandatory(true).createIndex(INDEX_TYPE.UNIQUE);
    schema.createClass("TestCreateIndexAbstractClassChildOne", abstractClass);
    schema.createClass("TestCreateIndexAbstractClassChildTwo", abstractClass);
    ODocument docOne = new ODocument("TestCreateIndexAbstractClassChildOne");
    docOne.field("value", "val1");
    docOne.save();
    ODocument docTwo = new ODocument("TestCreateIndexAbstractClassChildTwo");
    docTwo.field("value", "val2");
    docTwo.save();
    final String queryOne = "select from TestCreateIndexAbstractClass where value = 'val1'";
    List<ODocument> resultOne = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>(queryOne));
    Assert.assertEquals(resultOne.size(), 1);
    Assert.assertEquals((Object) resultOne.get(0), (Object) docOne);
    ODocument explain = databaseDocumentTx.command(new OCommandSQL("explain " + queryOne)).execute();
    Assert.assertTrue(explain.<Collection<String>>field("involvedIndexes").contains("TestCreateIndexAbstractClass.value"));
    final String queryTwo = "select from TestCreateIndexAbstractClass where value = 'val2'";
    List<ODocument> resultTwo = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>(queryTwo));
    Assert.assertEquals(resultTwo.size(), 1);
    Assert.assertEquals((Object) resultTwo.get(0), (Object) docTwo);
    explain = databaseDocumentTx.command(new OCommandSQL("explain " + queryTwo)).execute();
    Assert.assertTrue(explain.<Collection<String>>field("involvedIndexes").contains("TestCreateIndexAbstractClass.value"));
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 70 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema in project orientdb by orientechnologies.

the class IndexTest method testIndexPaginationTest.

public void testIndexPaginationTest() {
    ODatabaseDocumentTx databaseDocumentTx = (ODatabaseDocumentTx) database.getUnderlying();
    final OSchema schema = databaseDocumentTx.getMetadata().getSchema();
    final OClass indexPaginationTest = schema.createClass("IndexPaginationTestClass", 1, null);
    indexPaginationTest.createProperty("prop", OType.INTEGER);
    indexPaginationTest.createIndex("IndexPaginationTest", INDEX_TYPE.UNIQUE, "prop", "@rid");
    List<ORID> rids = new ArrayList<ORID>();
    for (int i = 99; i >= 0; i--) {
        final ODocument document = new ODocument("IndexPaginationTestClass");
        document.field("prop", i / 2);
        document.save();
        rids.add(document.getIdentity());
    }
    List<ODocument> result = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>("select from index:IndexPaginationTest order by key limit 5"));
    Assert.assertEquals(result.size(), 5);
    int lastKey = -1;
    ORID lastRid = null;
    for (ODocument document : result) {
        document.setLazyLoad(false);
        if (lastKey > -1)
            Assert.assertTrue(lastKey <= (Integer) document.<OCompositeKey>field("key").getKeys().get(0));
        lastKey = (Integer) document.<OCompositeKey>field("key").getKeys().get(0);
        lastRid = document.field("rid");
        Assert.assertTrue(rids.remove(document.<OIdentifiable>field("rid").getIdentity()));
    }
    while (true) {
        result = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>("select from index:IndexPaginationTest where key > ? order by key limit 5"), new OCompositeKey(lastKey, lastRid));
        if (result.isEmpty())
            break;
        Assert.assertEquals(result.size(), 5);
        for (ODocument document : result) {
            document.setLazyLoad(false);
            if (lastKey > -1)
                Assert.assertTrue(lastKey <= (Integer) document.<OCompositeKey>field("key").getKeys().get(0));
            lastKey = (Integer) document.<OCompositeKey>field("key").getKeys().get(0);
            lastRid = document.field("rid", OType.LINK);
            Assert.assertTrue(rids.remove(document.<ORID>field("rid", OType.LINK)));
        }
    }
    Assert.assertTrue(rids.isEmpty());
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)203 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)165 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)122 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)54 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)46 Test (org.testng.annotations.Test)35 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)31 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)23 Test (org.junit.Test)19 Before (org.junit.Before)16 ORID (com.orientechnologies.orient.core.id.ORID)15 OIndex (com.orientechnologies.orient.core.index.OIndex)15 Collection (java.util.Collection)15 Set (java.util.Set)15 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)9 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)9 BeforeClass (org.testng.annotations.BeforeClass)9 OStorage (com.orientechnologies.orient.core.storage.OStorage)6 ArrayList (java.util.ArrayList)5 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)4