Search in sources :

Example 81 with ODatabaseDocumentTx

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

the class OSequenceTest method shouldSequenceMTTx.

@Test
@Ignore
public void shouldSequenceMTTx() throws Exception {
    OSequence.CreateParams params = new OSequence.CreateParams().setStart(0L);
    OSequence mtSeq = sequences.createSequence("mtSeq", OSequence.SEQUENCE_TYPE.ORDERED, params);
    final int count = 1000;
    final int threads = 2;
    final CountDownLatch latch = new CountDownLatch(count);
    final AtomicInteger errors = new AtomicInteger(0);
    final AtomicInteger success = new AtomicInteger(0);
    ExecutorService service = Executors.newFixedThreadPool(threads);
    for (int i = 0; i < threads; i++) {
        service.execute(new Runnable() {

            @Override
            public void run() {
                ODatabaseDocument databaseDocument = new ODatabaseDocumentTx("memory:" + OSequenceTest.class.getName());
                databaseDocument.open("admin", "admin");
                OSequence mtSeq1 = databaseDocument.getMetadata().getSequenceLibrary().getSequence("mtSeq");
                for (int j = 0; j < count / threads; j++) {
                    for (int retry = 0; retry < 10; ++retry) {
                        try {
                            databaseDocument.begin();
                            mtSeq1.next();
                            databaseDocument.commit();
                            success.incrementAndGet();
                            break;
                        } catch (OConcurrentModificationException e) {
                            if (retry >= 10) {
                                e.printStackTrace();
                                errors.incrementAndGet();
                                break;
                            }
                            // RETRY
                            try {
                                Thread.sleep(10 + new Random().nextInt(100));
                            } catch (InterruptedException e1) {
                            }
                            mtSeq1.reloadSequence();
                            continue;
                        } catch (Exception e) {
                            e.printStackTrace();
                            errors.incrementAndGet();
                        }
                    }
                    latch.countDown();
                }
            }
        });
    }
    latch.await();
    assertThat(errors.get()).isEqualTo(0);
    assertThat(success.get()).isEqualTo(1000);
    mtSeq.reloadSequence();
    assertThat(mtSeq.getDocument().getVersion()).isEqualTo(1001);
    assertThat(mtSeq.current()).isEqualTo(1000);
}
Also used : ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) CountDownLatch(java.util.concurrent.CountDownLatch) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) OSequenceException(com.orientechnologies.orient.core.exception.OSequenceException) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ExecutorService(java.util.concurrent.ExecutorService) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 82 with ODatabaseDocumentTx

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

the class ClassIteratorTest method initializeDatabase.

private static void initializeDatabase() {
    db = new ODatabaseDocumentTx("memory:" + ClassIteratorTest.class.getSimpleName());
    if (db.exists() && RECREATE_DATABASE) {
        db.open("admin", "admin");
        db.drop();
        System.out.println("Dropped database.");
    }
    if (!db.exists()) {
        db.create();
        System.out.println("Created database.");
        final OSchema schema = db.getMetadata().getSchema();
        // Create Person class
        final OClass personClass = schema.createClass("Person");
        personClass.createProperty("First", OType.STRING).setMandatory(true).setNotNull(true).setMin("1");
        System.out.println("Created schema.");
    } else {
        db.open("admin", "admin");
    }
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)

Example 83 with ODatabaseDocumentTx

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

the class OFunctionLibraryTest method before.

@BeforeMethod
public void before() {
    db = new ODatabaseDocumentTx("memory:" + OFunctionLibraryTest.class.getSimpleName());
    db.create();
}
Also used : ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 84 with ODatabaseDocumentTx

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

the class ODocumentSchemafullSerializationTest method before.

@BeforeMethod
public void before() {
    ODatabaseDocumentTx.setDefaultSerializer(serializer);
    databaseDocument = new ODatabaseDocumentTx("memory:" + ODocumentSchemafullSerializationTest.class.getSimpleName()).create();
    // databaseDocument.getMetadata().
    OSchema schema = databaseDocument.getMetadata().getSchema();
    address = schema.createClass("Address");
    address.createProperty(NAME, OType.STRING);
    address.createProperty(NUMBER, OType.INTEGER);
    address.createProperty(CITY, OType.STRING);
    simple = schema.createClass("Simple");
    simple.createProperty(STRING_FIELD, OType.STRING);
    simple.createProperty(INT_FIELD, OType.INTEGER);
    simple.createProperty(SHORT_FIELD, OType.SHORT);
    simple.createProperty(LONG_FIELD, OType.LONG);
    simple.createProperty(FLOAT_NUMBER, OType.FLOAT);
    simple.createProperty(DOUBLE_NUMBER, OType.DOUBLE);
    simple.createProperty(BYTE_FIELD, OType.BYTE);
    simple.createProperty(BOOLEAN_FIELD, OType.BOOLEAN);
    simple.createProperty(DATE_FIELD, OType.DATETIME);
    simple.createProperty(RECORDID_FIELD, OType.LINK);
    simple.createProperty(EMBEDDED_FIELD, OType.EMBEDDED, address);
    simple.createProperty(ANY_FIELD, OType.ANY);
    embSimp = schema.createClass("EmbeddedCollectionSimple");
    embSimp.createProperty(LIST_BOOLEANS, OType.EMBEDDEDLIST);
    embSimp.createProperty(LIST_BYTES, OType.EMBEDDEDLIST);
    embSimp.createProperty(LIST_DATES, OType.EMBEDDEDLIST);
    embSimp.createProperty(LIST_DOUBLES, OType.EMBEDDEDLIST);
    embSimp.createProperty(LIST_FLOATS, OType.EMBEDDEDLIST);
    embSimp.createProperty(LIST_INTEGERS, OType.EMBEDDEDLIST);
    embSimp.createProperty(LIST_LONGS, OType.EMBEDDEDLIST);
    embSimp.createProperty(LIST_SHORTS, OType.EMBEDDEDLIST);
    embSimp.createProperty(LIST_STRINGS, OType.EMBEDDEDLIST);
    embSimp.createProperty(LIST_MIXED, OType.EMBEDDEDLIST);
    embMapSimple = schema.createClass("EmbeddedMapSimple");
    embMapSimple.createProperty(MAP_BYTES, OType.EMBEDDEDMAP);
    embMapSimple.createProperty(MAP_DATE, OType.EMBEDDEDMAP);
    embMapSimple.createProperty(MAP_DOUBLE, OType.EMBEDDEDMAP);
    embMapSimple.createProperty(MAP_FLOAT, OType.EMBEDDEDMAP);
    embMapSimple.createProperty(MAP_INT, OType.EMBEDDEDMAP);
    embMapSimple.createProperty(MAP_LONG, OType.EMBEDDEDMAP);
    embMapSimple.createProperty(MAP_SHORT, OType.EMBEDDEDMAP);
    embMapSimple.createProperty(MAP_STRING, OType.EMBEDDEDMAP);
    OClass clazzEmbComp = schema.createClass("EmbeddedComplex");
    clazzEmbComp.createProperty("addresses", OType.EMBEDDEDLIST, address);
    clazzEmbComp.createProperty("uniqueAddresses", OType.EMBEDDEDSET, address);
    clazzEmbComp.createProperty("addressByStreet", OType.EMBEDDEDMAP, address);
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 85 with ODatabaseDocumentTx

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

the class ODocumentSchemalessBinarySerializationTest method testLinkCollections.

@Test
public void testLinkCollections() {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:ODocumentSchemalessBinarySerializationTest").create();
    try {
        ODocument document = new ODocument();
        Set<ORecordId> linkSet = new HashSet<ORecordId>();
        linkSet.add(new ORecordId(10, 20));
        linkSet.add(new ORecordId(10, 21));
        linkSet.add(new ORecordId(10, 22));
        linkSet.add(new ORecordId(11, 22));
        document.field("linkSet", linkSet, OType.LINKSET);
        List<ORecordId> linkList = new ArrayList<ORecordId>();
        linkList.add(new ORecordId(10, 20));
        linkList.add(new ORecordId(10, 21));
        linkList.add(new ORecordId(10, 22));
        linkList.add(new ORecordId(11, 22));
        document.field("linkList", linkList, OType.LINKLIST);
        byte[] res = serializer.toStream(document, false);
        ODocument extr = (ODocument) serializer.fromStream(res, new ODocument(), new String[] {});
        assertEquals(extr.fields(), document.fields());
        assertEquals(((Set<?>) extr.field("linkSet")).size(), ((Set<?>) document.field("linkSet")).size());
        assertTrue(((Set<?>) extr.field("linkSet")).containsAll((Set<?>) document.field("linkSet")));
        assertEquals(extr.field("linkList"), document.field("linkList"));
    } finally {
        db.drop();
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ArrayList(java.util.ArrayList) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ORecordId(com.orientechnologies.orient.core.id.ORecordId) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)590 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)257 Test (org.testng.annotations.Test)182 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)120 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)81 Test (org.junit.Test)79 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)61 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)47 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)46 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)41 ORID (com.orientechnologies.orient.core.id.ORID)39 ORecordId (com.orientechnologies.orient.core.id.ORecordId)34 Before (org.junit.Before)34 File (java.io.File)33 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)25 ArrayList (java.util.ArrayList)25 BeforeClass (org.testng.annotations.BeforeClass)23 BeforeMethod (org.testng.annotations.BeforeMethod)23 OStorage (com.orientechnologies.orient.core.storage.OStorage)21 List (java.util.List)20