Search in sources :

Example 41 with ORidBag

use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag 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 42 with ORidBag

use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag 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 43 with ORidBag

use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag 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 44 with ORidBag

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

the class ORidBagTest method testAddNewItemsAndRemoveThem.

public void testAddNewItemsAndRemoveThem() {
    final List<OIdentifiable> rids = new ArrayList<OIdentifiable>();
    ORidBag ridBag = new ORidBag();
    int size = 0;
    for (int i = 0; i < 10; i++) {
        ODocument docToAdd = new ODocument();
        for (int k = 0; k < 2; k++) {
            ridBag.add(docToAdd);
            rids.add(docToAdd);
            size++;
        }
    }
    Assert.assertEquals(ridBag.size(), size);
    ODocument document = new ODocument();
    document.field("ridBag", ridBag);
    document.save();
    document = database.load(document.getIdentity(), "*:-1", true);
    ridBag = document.field("ridBag");
    Assert.assertEquals(ridBag.size(), size);
    final List<OIdentifiable> newDocs = new ArrayList<OIdentifiable>();
    for (int i = 0; i < 10; i++) {
        ODocument docToAdd = new ODocument();
        for (int k = 0; k < 2; k++) {
            ridBag.add(docToAdd);
            rids.add(docToAdd);
            newDocs.add(docToAdd);
            size++;
        }
    }
    Assert.assertEquals(ridBag.size(), size);
    Random rnd = new Random();
    for (int i = 0; i < newDocs.size(); i++) {
        if (rnd.nextBoolean()) {
            OIdentifiable newDoc = newDocs.get(i);
            rids.remove(newDoc);
            ridBag.remove(newDoc);
            newDocs.remove(newDoc);
            size--;
        }
    }
    for (OIdentifiable identifiable : ridBag) {
        if (newDocs.contains(identifiable) && rnd.nextBoolean()) {
            ridBag.remove(identifiable);
            rids.remove(identifiable);
            size--;
        }
    }
    Assert.assertEquals(ridBag.size(), size);
    List<OIdentifiable> ridsCopy = new ArrayList<OIdentifiable>(rids);
    for (OIdentifiable identifiable : ridBag) {
        Assert.assertTrue(rids.remove(identifiable));
    }
    Assert.assertTrue(rids.isEmpty());
    document.save();
    document = database.load(document.getIdentity(), "*:-1", false);
    ridBag = document.field("ridBag");
    rids.addAll(ridsCopy);
    for (OIdentifiable identifiable : ridBag) {
        Assert.assertTrue(rids.remove(identifiable));
    }
    Assert.assertTrue(rids.isEmpty());
    Assert.assertEquals(ridBag.size(), size);
}
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 45 with ORidBag

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

the class OCompositeIndexDefinitionTest method testDocumentToIndexRidBagValueSuccessfulOne.

@Test
public void testDocumentToIndexRidBagValueSuccessfulOne() {
    final ODocument document = new ODocument();
    final ORidBag ridBag = new ORidBag();
    ridBag.setAutoConvertToRecord(false);
    ridBag.add(new ORecordId("#1:10"));
    ridBag.add(new ORecordId("#1:11"));
    ridBag.add(new ORecordId("#1:11"));
    document.field("fOne", 12);
    document.field("fTwo", ridBag);
    final OCompositeIndexDefinition compositeIndexDefinition = new OCompositeIndexDefinition("testCollectionClass");
    compositeIndexDefinition.addIndex(new OPropertyIndexDefinition("testCollectionClass", "fOne", OType.INTEGER));
    compositeIndexDefinition.addIndex(new OPropertyRidBagIndexDefinition("testCollectionClass", "fTwo"));
    final Object result = compositeIndexDefinition.getDocumentValueToIndex(document);
    final ArrayList<OCompositeKey> expectedResult = new ArrayList<OCompositeKey>();
    expectedResult.add(new OCompositeKey(12, new ORecordId("#1:10")));
    expectedResult.add(new OCompositeKey(12, new ORecordId("#1:11")));
    expectedResult.add(new OCompositeKey(12, new ORecordId("#1:11")));
    Assert.assertEquals(result, expectedResult);
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Aggregations

ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)135 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)103 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)68 ORecordId (com.orientechnologies.orient.core.id.ORecordId)37 ArrayList (java.util.ArrayList)27 Test (org.testng.annotations.Test)24 ORID (com.orientechnologies.orient.core.id.ORID)21 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)15 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)13 OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)11 OStorage (com.orientechnologies.orient.core.storage.OStorage)7 HashSet (java.util.HashSet)7 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)6 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)6 ORecordLazyMultiValue (com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)4 ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 DatabaseAbstractTest (com.orientechnologies.DatabaseAbstractTest)3