use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class ORidBagTest method testDocumentHelper.
@Test
public void testDocumentHelper() {
ODocument document = new ODocument();
ODocument embeddedDocument = new ODocument();
List<ODocument> embeddedList = new ArrayList<ODocument>();
ORidBag highLevelRidBag = new ORidBag();
for (int i = 0; i < 10; i++) {
ODocument docToAdd = new ODocument();
for (int j = 0; j < 2; j++) highLevelRidBag.add(docToAdd);
}
ORidBag embeddedRidBag = new ORidBag();
for (int i = 0; i < 10; i++) {
ODocument docToAdd = new ODocument();
embeddedRidBag.add(docToAdd);
}
document.field("ridBag", highLevelRidBag);
embeddedList.add(embeddedDocument);
embeddedDocument.field("ridBag", embeddedRidBag);
document.field("embeddedList", embeddedList, OType.EMBEDDEDLIST);
document.save();
document.reload();
ODocument documentCopy = database.load(document.getIdentity(), "*:-1", true);
Assert.assertNotSame(document, documentCopy);
Assert.assertTrue(ODocumentHelper.hasSameContentOf(document, database, documentCopy, database, null));
Iterator<OIdentifiable> iterator = documentCopy.<ORidBag>field("ridBag").iterator();
iterator.next();
iterator.remove();
Assert.assertTrue(!ODocumentHelper.hasSameContentOf(document, database, documentCopy, database, null));
documentCopy.reload("*:-1", true);
embeddedList = documentCopy.field("embeddedList");
ODocument doc = embeddedList.get(0);
iterator = doc.<ORidBag>field("ridBag").iterator();
iterator.next();
iterator.remove();
Assert.assertTrue(!ODocumentHelper.hasSameContentOf(document, database, documentCopy, database, null));
documentCopy.reload("*:-1", true);
ODocument docToAdd = new ODocument();
docToAdd.save();
iterator = documentCopy.<ORidBag>field("ridBag").iterator();
iterator.next();
iterator.remove();
documentCopy.<ORidBag>field("ridBag").add(docToAdd.getIdentity());
Assert.assertTrue(!ODocumentHelper.hasSameContentOf(document, database, documentCopy, database, null));
documentCopy.reload("*:-1", true);
embeddedList = documentCopy.field("embeddedList");
doc = embeddedList.get(0);
iterator = doc.<ORidBag>field("ridBag").iterator();
OIdentifiable remvedItem = iterator.next();
iterator.remove();
doc.<ORidBag>field("ridBag").add(docToAdd.getIdentity());
Assert.assertTrue(!ODocumentHelper.hasSameContentOf(document, database, documentCopy, database, null));
doc.<ORidBag>field("ridBag").remove(docToAdd.getIdentity());
doc.<ORidBag>field("ridBag").add(remvedItem);
Assert.assertTrue(ODocumentHelper.hasSameContentOf(document, database, documentCopy, database, null));
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class ORidBagTest method testSizeNotChangeAfterRemoveNotExistentElement.
@Test
public void testSizeNotChangeAfterRemoveNotExistentElement() throws Exception {
final ODocument bob = new ODocument();
final ODocument fred = new ODocument().save();
final ODocument jim = new ODocument().save();
ORidBag teamMates = new ORidBag();
teamMates.add(bob);
teamMates.add(fred);
Assert.assertEquals(teamMates.size(), 2);
teamMates.remove(jim);
Assert.assertEquals(teamMates.size(), 2);
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class ORidBagTest method testMassiveChanges.
public void testMassiveChanges() {
ODocument document = new ODocument();
ORidBag bag = new ORidBag();
assertEmbedded(bag.isEmbedded());
Random random = new Random();
List<OIdentifiable> rids = new ArrayList<OIdentifiable>();
document.field("bag", bag);
document.save();
ORID rid = document.getIdentity();
for (int i = 0; i < 10; i++) {
document = database.load(rid);
document.setLazyLoad(false);
bag = document.field("bag");
assertEmbedded(bag.isEmbedded());
massiveInsertionIteration(random, rids, bag);
assertEmbedded(bag.isEmbedded());
document.save();
}
document.delete();
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class ORidBagTest method testCycle.
public void testCycle() {
ODocument docOne = new ODocument();
ORidBag ridBagOne = new ORidBag();
ODocument docTwo = new ODocument();
ORidBag ridBagTwo = new ORidBag();
docOne.field("ridBag", ridBagOne);
docTwo.field("ridBag", ridBagTwo);
ridBagOne.add(docTwo);
ridBagTwo.add(docOne);
docOne.save();
docOne = database.load(docOne.getIdentity(), "*:-1", false);
ridBagOne = docOne.field("ridBag");
docTwo = database.load(docTwo.getIdentity(), "*:-1", false);
ridBagTwo = docTwo.field("ridBag");
Assert.assertEquals(ridBagOne.iterator().next(), docTwo);
Assert.assertEquals(ridBagTwo.iterator().next(), docOne);
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class ORidBagTest method testAddSBTreeAddInMemoryIterateAndRemove.
public void testAddSBTreeAddInMemoryIterateAndRemove() {
List<OIdentifiable> rids = new ArrayList<OIdentifiable>();
ORidBag bag = new ORidBag();
bag.setAutoConvertToRecord(false);
assertEmbedded(bag.isEmbedded());
bag.add(new ORecordId("#77:2"));
rids.add(new ORecordId("#77:2"));
bag.add(new ORecordId("#77:2"));
rids.add(new ORecordId("#77:2"));
bag.add(new ORecordId("#77:3"));
rids.add(new ORecordId("#77:3"));
bag.add(new ORecordId("#77:4"));
rids.add(new ORecordId("#77:4"));
bag.add(new ORecordId("#77:4"));
rids.add(new ORecordId("#77:4"));
bag.add(new ORecordId("#77:7"));
rids.add(new ORecordId("#77:7"));
bag.add(new ORecordId("#77:8"));
rids.add(new ORecordId("#77:8"));
assertEmbedded(bag.isEmbedded());
ODocument doc = new ODocument();
doc.field("ridbag", bag);
doc.save();
ORID rid = doc.getIdentity();
OStorage storage = database.getStorage();
database.close();
storage.close(true, false);
database = new ODatabaseDocumentTx(database.getURL());
database.open("admin", "admin");
doc = database.load(rid);
doc.setLazyLoad(false);
bag = doc.field("ridbag");
assertEmbedded(bag.isEmbedded());
bag.add(new ORecordId("#77:0"));
rids.add(new ORecordId("#77:0"));
bag.add(new ORecordId("#77:1"));
rids.add(new ORecordId("#77:1"));
bag.add(new ORecordId("#77:2"));
rids.add(new ORecordId("#77:2"));
bag.add(new ORecordId("#77:3"));
rids.add(new ORecordId("#77:3"));
bag.add(new ORecordId("#77:3"));
rids.add(new ORecordId("#77:3"));
bag.add(new ORecordId("#77:5"));
rids.add(new ORecordId("#77:5"));
bag.add(new ORecordId("#77:6"));
rids.add(new ORecordId("#77:6"));
assertEmbedded(bag.isEmbedded());
Iterator<OIdentifiable> iterator = bag.iterator();
int r2c = 0;
int r3c = 0;
int r6c = 0;
int r4c = 0;
int r7c = 0;
while (iterator.hasNext()) {
OIdentifiable identifiable = iterator.next();
if (identifiable.equals(new ORecordId("#77:2"))) {
if (r2c < 2) {
r2c++;
iterator.remove();
rids.remove(identifiable);
}
}
if (identifiable.equals(new ORecordId("#77:3"))) {
if (r3c < 1) {
r3c++;
iterator.remove();
rids.remove(identifiable);
}
}
if (identifiable.equals(new ORecordId("#77:6"))) {
if (r6c < 1) {
r6c++;
iterator.remove();
rids.remove(identifiable);
}
}
if (identifiable.equals(new ORecordId("#77:4"))) {
if (r4c < 1) {
r4c++;
iterator.remove();
rids.remove(identifiable);
}
}
if (identifiable.equals(new ORecordId("#77:7"))) {
if (r7c < 1) {
r7c++;
iterator.remove();
rids.remove(identifiable);
}
}
}
assertEquals(r2c, 2);
assertEquals(r3c, 1);
assertEquals(r6c, 1);
assertEquals(r4c, 1);
assertEquals(r7c, 1);
for (OIdentifiable identifiable : bag) assertTrue(rids.remove(identifiable));
assertTrue(rids.isEmpty());
for (OIdentifiable identifiable : bag) rids.add(identifiable);
doc = new ODocument();
final ORidBag otherBag = new ORidBag();
for (OIdentifiable id : bag) otherBag.add(id);
assertEmbedded(otherBag.isEmbedded());
doc.field("ridbag", otherBag);
doc.save();
rid = doc.getIdentity();
doc = database.load(rid);
doc.setLazyLoad(false);
bag = doc.field("ridbag");
assertEmbedded(bag.isEmbedded());
for (OIdentifiable identifiable : bag) assertTrue(rids.remove(identifiable));
assertTrue(rids.isEmpty());
}
Aggregations