Search in sources :

Example 31 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class ORidBagTest method testAddRemove.

public void testAddRemove() {
    ORidBag bag = new ORidBag();
    bag.setAutoConvertToRecord(false);
    bag.add(new ORecordId("#77:2"));
    bag.add(new ORecordId("#77:2"));
    bag.add(new ORecordId("#77:3"));
    bag.add(new ORecordId("#77:4"));
    bag.add(new ORecordId("#77:4"));
    bag.add(new ORecordId("#77:4"));
    bag.add(new ORecordId("#77:5"));
    bag.add(new ORecordId("#77:6"));
    bag.remove(new ORecordId("#77:1"));
    bag.remove(new ORecordId("#77:2"));
    bag.remove(new ORecordId("#77:2"));
    bag.remove(new ORecordId("#77:4"));
    bag.remove(new ORecordId("#77:6"));
    Assert.assertTrue(bag.contains(new ORecordId("#77:3")));
    Assert.assertTrue(bag.contains(new ORecordId("#77:4")));
    Assert.assertTrue(bag.contains(new ORecordId("#77:5")));
    Assert.assertTrue(!bag.contains(new ORecordId("#77:2")));
    Assert.assertTrue(!bag.contains(new ORecordId("#77:6")));
    Assert.assertTrue(!bag.contains(new ORecordId("#77:1")));
    Assert.assertTrue(!bag.contains(new ORecordId("#77:0")));
    assertEmbedded(bag.isEmbedded());
    final List<OIdentifiable> rids = new ArrayList<OIdentifiable>();
    rids.add(new ORecordId("#77:3"));
    rids.add(new ORecordId("#77:4"));
    rids.add(new ORecordId("#77:4"));
    rids.add(new ORecordId("#77:5"));
    for (OIdentifiable identifiable : bag) assertTrue(rids.remove(identifiable));
    assertTrue(rids.isEmpty());
    for (OIdentifiable identifiable : bag) rids.add(identifiable);
    ODocument doc = new ODocument();
    doc.field("ridbag", bag);
    doc.save();
    ORID rid = doc.getIdentity();
    doc = database.load(rid);
    doc.setLazyLoad(false);
    bag = doc.field("ridbag");
    assertEmbedded(bag.isEmbedded());
    Assert.assertTrue(bag.contains(new ORecordId("#77:3")));
    Assert.assertTrue(bag.contains(new ORecordId("#77:4")));
    Assert.assertTrue(bag.contains(new ORecordId("#77:5")));
    Assert.assertTrue(!bag.contains(new ORecordId("#77:2")));
    Assert.assertTrue(!bag.contains(new ORecordId("#77:6")));
    Assert.assertTrue(!bag.contains(new ORecordId("#77:1")));
    Assert.assertTrue(!bag.contains(new ORecordId("#77:0")));
    for (OIdentifiable identifiable : bag) assertTrue(rids.remove(identifiable));
    assertTrue(rids.isEmpty());
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ORID(com.orientechnologies.orient.core.id.ORID) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 32 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class ORidBagTest method testSaveLoad.

public void testSaveLoad() throws Exception {
    Set<OIdentifiable> expected = new HashSet<OIdentifiable>(8);
    expected.add(new ORecordId("#77:12"));
    expected.add(new ORecordId("#77:13"));
    expected.add(new ORecordId("#77:14"));
    expected.add(new ORecordId("#77:15"));
    expected.add(new ORecordId("#77:16"));
    expected.add(new ORecordId("#77:17"));
    expected.add(new ORecordId("#77:18"));
    expected.add(new ORecordId("#77:19"));
    expected.add(new ORecordId("#77:20"));
    expected.add(new ORecordId("#77:21"));
    expected.add(new ORecordId("#77:22"));
    ODocument doc = new ODocument();
    final ORidBag bag = new ORidBag();
    bag.addAll(expected);
    doc.field("ridbag", bag);
    assertEmbedded(bag.isEmbedded());
    doc.save();
    final ORID id = doc.getIdentity();
    OStorage storage = database.getStorage();
    database.close();
    storage.close(true, false);
    database = new ODatabaseDocumentTx(database.getURL());
    database.open("admin", "admin");
    doc = database.load(id);
    doc.setLazyLoad(false);
    final ORidBag loaded = doc.field("ridbag");
    assertEmbedded(loaded.isEmbedded());
    Assert.assertEquals(loaded.size(), expected.size());
    for (OIdentifiable identifiable : loaded) Assert.assertTrue(expected.remove(identifiable));
    Assert.assertTrue(expected.isEmpty());
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OStorage(com.orientechnologies.orient.core.storage.OStorage) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ORID(com.orientechnologies.orient.core.id.ORID) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 33 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class ORidBagTest method testAddMixedValues.

public void testAddMixedValues() {
    ORidBag ridBag = new ORidBag();
    ODocument document = new ODocument();
    document.field("ridBag", ridBag);
    assertEmbedded(ridBag.isEmbedded());
    List<OIdentifiable> itemsToAdd = new ArrayList<OIdentifiable>();
    for (int i = 0; i < 10; i++) {
        ODocument docToAdd = new ODocument();
        docToAdd.save();
        for (int k = 0; k < 2; k++) {
            ridBag.add(docToAdd);
            itemsToAdd.add(docToAdd);
        }
    }
    assertEmbedded(ridBag.isEmbedded());
    document.save();
    document.reload();
    ridBag = document.field("ridBag");
    for (int i = 0; i < 10; i++) {
        ODocument docToAdd = new ODocument();
        docToAdd.save();
        for (int k = 0; k < 2; k++) {
            ridBag.add(docToAdd);
            itemsToAdd.add(docToAdd);
        }
    }
    for (int i = 0; i < 10; i++) {
        ODocument docToAdd = new ODocument();
        ridBag.add(docToAdd);
        itemsToAdd.add(docToAdd);
    }
    assertEmbedded(ridBag.isEmbedded());
    document.save();
    document.reload();
    ridBag = document.field("ridBag");
    database.begin();
    for (int i = 0; i < 10; i++) {
        ODocument docToAdd = new ODocument();
        docToAdd.save();
        for (int k = 0; k < 2; k++) {
            ridBag.add(docToAdd);
            itemsToAdd.add(docToAdd);
        }
    }
    for (int i = 0; i < 10; i++) {
        ODocument docToAdd = new ODocument();
        ridBag.add(docToAdd);
        itemsToAdd.add(docToAdd);
    }
    assertEmbedded(ridBag.isEmbedded());
    document.save();
    database.commit();
    assertEmbedded(ridBag.isEmbedded());
    Assert.assertEquals(ridBag.size(), itemsToAdd.size());
    document.reload();
    Assert.assertEquals(ridBag.size(), itemsToAdd.size());
    for (OIdentifiable id : ridBag) Assert.assertTrue(itemsToAdd.remove(id));
    Assert.assertTrue(itemsToAdd.isEmpty());
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 34 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class ORidBagTest method massiveInsertionIteration.

private void massiveInsertionIteration(Random rnd, List<OIdentifiable> rids, ORidBag bag) {
    Iterator<OIdentifiable> bagIterator = bag.iterator();
    while (bagIterator.hasNext()) {
        OIdentifiable bagValue = bagIterator.next();
        Assert.assertTrue(rids.contains(bagValue));
    }
    Assert.assertEquals(bag.size(), rids.size());
    for (int i = 0; i < 100; i++) {
        if (rnd.nextDouble() < 0.2 & rids.size() > 5) {
            final int index = rnd.nextInt(rids.size());
            final OIdentifiable rid = rids.remove(index);
            bag.remove(rid);
        } else {
            final int positionIndex = rnd.nextInt(300);
            final long position = positionIndex;
            final ORecordId recordId = new ORecordId(1, position);
            rids.add(recordId);
            bag.add(recordId);
        }
    }
    bagIterator = bag.iterator();
    while (bagIterator.hasNext()) {
        final OIdentifiable bagValue = bagIterator.next();
        Assert.assertTrue(rids.contains(bagValue));
        if (rnd.nextDouble() < 0.05) {
            bagIterator.remove();
            Assert.assertTrue(rids.remove(bagValue));
        }
    }
    Assert.assertEquals(bag.size(), rids.size());
    bagIterator = bag.iterator();
    while (bagIterator.hasNext()) {
        final OIdentifiable bagValue = bagIterator.next();
        Assert.assertTrue(rids.contains(bagValue));
    }
}
Also used : OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 35 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class ORidBagTest method testRemove.

public void testRemove() {
    final Set<OIdentifiable> expected = new HashSet<OIdentifiable>(8);
    expected.add(new ORecordId("#77:12"));
    expected.add(new ORecordId("#77:13"));
    expected.add(new ORecordId("#77:14"));
    expected.add(new ORecordId("#77:15"));
    expected.add(new ORecordId("#77:16"));
    final ORidBag bag = new ORidBag();
    assertEmbedded(bag.isEmbedded());
    bag.setAutoConvertToRecord(false);
    bag.addAll(expected);
    assertEmbedded(bag.isEmbedded());
    bag.remove(new ORecordId("#77:23"));
    assertEmbedded(bag.isEmbedded());
    final Set<OIdentifiable> expectedTwo = new HashSet<OIdentifiable>(8);
    expectedTwo.addAll(expected);
    for (OIdentifiable identifiable : bag) {
        assertTrue(expectedTwo.remove(identifiable));
    }
    Assert.assertTrue(expectedTwo.isEmpty());
    expected.remove(new ORecordId("#77:14"));
    bag.remove(new ORecordId("#77:14"));
    assertEmbedded(bag.isEmbedded());
    expectedTwo.addAll(expected);
    for (OIdentifiable identifiable : bag) {
        assertTrue(expectedTwo.remove(identifiable));
    }
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Aggregations

OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)536 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)278 ORecordId (com.orientechnologies.orient.core.id.ORecordId)120 Test (org.testng.annotations.Test)104 HashSet (java.util.HashSet)89 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)79 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)70 ORID (com.orientechnologies.orient.core.id.ORID)56 OIndexCursor (com.orientechnologies.orient.core.index.OIndexCursor)47 Test (org.junit.Test)43 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)42 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)41 ArrayList (java.util.ArrayList)39 ORecord (com.orientechnologies.orient.core.record.ORecord)35 Map (java.util.Map)31 ByteBuffer (java.nio.ByteBuffer)28 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)26 OIndexTxAwareOneValue (com.orientechnologies.orient.core.index.OIndexTxAwareOneValue)22 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)22 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)21