Search in sources :

Example 1 with ODatabaseDocumentTx

use of com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx in project orientdb by orientechnologies.

the class OrientGraphFactory method getDatabase.

/**
   * Gives new connection to database. If current factory configured to use pool (see {@link #setupPool(int, int)} method),
   * retrieves connection from pool. Otherwise creates new connection each time.
   *
   * @param iCreate if true automatically creates database if database with given URL does not exist
   * @param iOpen   if true automatically opens the database
   * @return database
   */
public ODatabaseDocumentTx getDatabase(final boolean iCreate, final boolean iOpen) {
    if (pool != null)
        return pool.acquire();
    final ODatabaseDocumentTx db = new ODatabaseDocumentTx(url);
    final String connMode = settings.getConnectionStrategy();
    db.setProperty(OStorageRemote.PARAM_CONNECTION_STRATEGY, connMode);
    for (Map.Entry<String, Object> entry : properties.entrySet()) {
        db.setProperty(entry.getKey(), entry.getValue());
    }
    if (!db.getURL().startsWith("remote:") && !db.exists()) {
        if (iCreate)
            db.create();
        else if (iOpen)
            throw new ODatabaseException("Database '" + url + "' not found");
    } else if (iOpen)
        db.open(user, password);
    return db;
}
Also used : ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with ODatabaseDocumentTx

use of com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx in project orientdb by orientechnologies.

the class AbstractServerClusterInsertTest method recreateIndexNode2.

protected void recreateIndexNode2() {
    // RE-CREATE INDEX ON NODE 1
    ServerRun server = serverInstance.get(1);
    ODatabaseDocumentTx database = poolFactory.get(getDatabaseURL(server), "admin", "admin").acquire();
    try {
        Object result = database.command(new OCommandSQL("create index Person.name on Person (name) unique")).execute();
        System.out.println("recreateIndexNode2: Node2 created index: " + result);
        Assert.assertEquals(expected, ((Number) result).intValue());
    } catch (ODistributedOperationException t) {
        for (ServerRun s : serverInstance) {
            final ODatabaseDocumentTx db = new ODatabaseDocumentTx(getDatabaseURL(s)).open("admin", "admin");
            try {
                List<ODocument> result = db.command(new OCommandSQL("select count(*) as count from Person where name is not null")).execute();
                Assert.assertEquals(expected, ((Number) result.get(0).field("count")).longValue());
                final OClass person = db.getMetadata().getSchema().getClass("Person");
                final int[] clIds = person.getPolymorphicClusterIds();
                long tot = 0;
                for (int clId : clIds) {
                    long count = db.countClusterElements(clId);
                    System.out.println("Cluster " + clId + " record: " + count);
                    tot += count;
                }
                Assert.assertEquals(expected, tot);
            } finally {
                db.close();
            }
        }
        database.activateOnCurrentThread();
        throw t;
    } finally {
        database.close();
    }
    // CHECK ON NODE 1
    server = serverInstance.get(0);
    database = poolFactory.get(getDatabaseURL(server), "admin", "admin").acquire();
    try {
        final long indexSize = database.getMetadata().getIndexManager().getIndex("Person.name").getSize();
        Assert.assertEquals(expected, indexSize);
        System.out.println("recreateIndexNode2: Node1 has the index too, ok");
    } finally {
        database.close();
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODistributedOperationException(com.orientechnologies.orient.server.distributed.task.ODistributedOperationException)

Example 3 with ODatabaseDocumentTx

use of com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx in project orientdb by orientechnologies.

the class OLiveQueryShotdownTest method testShutDown.

@Test
public void testShutDown() throws Exception {
    bootServer();
    ODatabaseDocument db = new ODatabaseDocumentTx("remote:localhost/" + OLiveQueryShotdownTest.class.getSimpleName());
    db.open("admin", "admin");
    db.getMetadata().getSchema().createClass("Test");
    final CountDownLatch error = new CountDownLatch(1);
    try {
        db.command(new OLiveQuery("live select from Test", new OLiveResultListener() {

            @Override
            public void onUnsubscribe(int iLiveToken) {
            }

            @Override
            public void onLiveResult(int iLiveToken, ORecordOperation iOp) throws OException {
            }

            @Override
            public void onError(int iLiveToken) {
                error.countDown();
            }
        })).execute();
        shutdownServer();
        assertTrue("onError method never called on shutdow", error.await(2, TimeUnit.SECONDS));
    } finally {
    //      db.close();
    }
}
Also used : ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OLiveQuery(com.orientechnologies.orient.core.sql.query.OLiveQuery) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OException(com.orientechnologies.common.exception.OException) CountDownLatch(java.util.concurrent.CountDownLatch) OLiveResultListener(com.orientechnologies.orient.core.sql.query.OLiveResultListener) Test(org.junit.Test)

Example 4 with ODatabaseDocumentTx

use of com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx in project YCSB by brianfrankcooper.

the class OrientDBClient method delete.

@Override
public Status delete(String table, String key) {
    while (true) {
        try (ODatabaseDocumentTx db = databasePool.acquire()) {
            final ODictionary<ORecord> dictionary = db.getMetadata().getIndexManager().getDictionary();
            dictionary.remove(key);
            return Status.OK;
        } catch (OConcurrentModificationException cme) {
            continue;
        } catch (Exception e) {
            e.printStackTrace();
            return Status.ERROR;
        }
    }
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException)

Example 5 with ODatabaseDocumentTx

use of com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx in project YCSB by brianfrankcooper.

the class OrientDBClient method update.

@Override
public Status update(String table, String key, HashMap<String, ByteIterator> values) {
    while (true) {
        try (ODatabaseDocumentTx db = databasePool.acquire()) {
            final ODictionary<ORecord> dictionary = db.getMetadata().getIndexManager().getDictionary();
            final ODocument document = dictionary.get(key);
            if (document != null) {
                for (Map.Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) {
                    document.field(entry.getKey(), entry.getValue());
                }
                document.save();
                return Status.OK;
            }
        } catch (OConcurrentModificationException cme) {
            continue;
        } catch (Exception e) {
            e.printStackTrace();
            return Status.ERROR;
        }
    }
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)657 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)287 Test (org.testng.annotations.Test)188 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)123 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)85 Test (org.junit.Test)82 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)65 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)53 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)48 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)42 ORID (com.orientechnologies.orient.core.id.ORID)40 File (java.io.File)36 Before (org.junit.Before)36 ORecordId (com.orientechnologies.orient.core.id.ORecordId)34 IOException (java.io.IOException)26 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)25 ArrayList (java.util.ArrayList)25 ORecord (com.orientechnologies.orient.core.record.ORecord)23 BeforeClass (org.testng.annotations.BeforeClass)23 BeforeMethod (org.testng.annotations.BeforeMethod)23