use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class ORidBagTest method testJsonSerialization.
@Test
public void testJsonSerialization() {
ODocument externalDoc = new ODocument();
ODocument testDocument = new ODocument();
ORidBag highLevelRidBag = new ORidBag();
for (int i = 0; i < 10; i++) highLevelRidBag.add(new ODocument());
testDocument.field("ridBag", highLevelRidBag);
testDocument.field("externalDoc", externalDoc);
final List<ODocument> embeddedList = new ArrayList<ODocument>();
ODocument embeddedListDoc = new ODocument();
ORidBag embeddedListDocRidBag = new ORidBag();
for (int i = 0; i < 10; i++) embeddedListDocRidBag.add(new ODocument());
embeddedListDoc.field("ridBag", embeddedListDocRidBag);
embeddedListDoc.field("externalDoc", externalDoc);
embeddedList.add(embeddedListDoc);
Set<ODocument> embeddedSet = new HashSet<ODocument>();
ODocument embeddedSetDoc = new ODocument();
ORidBag embeddedSetDocRidBag = new ORidBag();
for (int i = 0; i < 10; i++) embeddedSetDocRidBag.add(new ODocument());
embeddedSetDoc.field("ridBag", embeddedSetDocRidBag);
embeddedSetDoc.field("externalDoc", externalDoc);
embeddedSet.add(embeddedSetDoc);
Map<String, ODocument> embeddedMap = new HashMap<String, ODocument>();
ODocument embeddedMapDoc = new ODocument();
ORidBag embeddedMapDocRidBag = new ORidBag();
for (int i = 0; i < 10; i++) embeddedMapDocRidBag.add(new ODocument());
embeddedMapDoc.field("ridBag", embeddedMapDocRidBag);
embeddedMapDoc.field("externalDoc", externalDoc);
embeddedMap.put("k1", embeddedMapDoc);
testDocument.field("embeddedList", embeddedList, OType.EMBEDDEDLIST);
testDocument.field("embeddedSet", embeddedSet, OType.EMBEDDEDSET);
testDocument.field("embeddedMap", embeddedMap, OType.EMBEDDEDMAP);
testDocument.save();
testDocument.reload();
final String json = testDocument.toJSON();
ODocument doc = new ODocument();
doc.fromJSON(json);
Assert.assertTrue(ODocumentHelper.hasSameContentOf(doc, database, testDocument, database, null));
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class ORidBagTest method testSimultaneousIterationAndRemove.
public void testSimultaneousIterationAndRemove() {
ORidBag ridBag = new ORidBag();
ODocument document = new ODocument();
document.field("ridBag", ridBag);
assertEmbedded(ridBag.isEmbedded());
for (int i = 0; i < 10; i++) {
ODocument docToAdd = new ODocument();
docToAdd.save();
ridBag.add(docToAdd);
}
assertEmbedded(ridBag.isEmbedded());
document.save();
document.reload();
ridBag = document.field("ridBag");
Set<OIdentifiable> docs = Collections.newSetFromMap(new IdentityHashMap<OIdentifiable, Boolean>());
for (OIdentifiable id : ridBag) docs.add(id);
database.begin();
ridBag = document.field("ridBag");
assertEmbedded(ridBag.isEmbedded());
for (int i = 0; i < 10; i++) {
ODocument docToAdd = new ODocument();
docToAdd.save();
docs.add(docToAdd);
ridBag.add(docToAdd);
}
assertEmbedded(ridBag.isEmbedded());
for (int i = 0; i < 10; i++) {
ODocument docToAdd = new ODocument();
docs.add(docToAdd);
ridBag.add(docToAdd);
}
assertEmbedded(ridBag.isEmbedded());
for (OIdentifiable identifiable : ridBag) {
Assert.assertTrue(docs.remove(identifiable));
ridBag.remove(identifiable);
Assert.assertEquals(ridBag.size(), docs.size());
int counter = 0;
for (OIdentifiable id : ridBag) {
Assert.assertTrue(docs.contains(id));
counter++;
}
Assert.assertEquals(counter, docs.size());
assertEmbedded(ridBag.isEmbedded());
}
document.save();
database.commit();
Assert.assertEquals(ridBag.size(), 0);
document.reload();
ridBag = document.field("ridBag");
Assert.assertEquals(ridBag.size(), 0);
Assert.assertEquals(docs.size(), 0);
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class ORidBagTest method testAddRemoveInTheMiddleOfIteration.
public void testAddRemoveInTheMiddleOfIteration() {
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"));
final List<OIdentifiable> initialRids = new ArrayList<OIdentifiable>();
initialRids.add(new ORecordId("#77:2"));
initialRids.add(new ORecordId("#77:2"));
initialRids.add(new ORecordId("#77:3"));
initialRids.add(new ORecordId("#77:4"));
initialRids.add(new ORecordId("#77:4"));
initialRids.add(new ORecordId("#77:4"));
initialRids.add(new ORecordId("#77:5"));
initialRids.add(new ORecordId("#77:6"));
int counter = 0;
Iterator<OIdentifiable> iterator = bag.iterator();
bag.remove(new ORecordId("#77:2"));
initialRids.remove(new ORecordId("#77:2"));
while (iterator.hasNext()) {
counter++;
if (counter == 1) {
bag.remove(new ORecordId("#77:1"));
initialRids.remove(new ORecordId("#77:1"));
bag.remove(new ORecordId("#77:2"));
initialRids.remove(new ORecordId("#77:2"));
}
if (counter == 3) {
bag.remove(new ORecordId("#77:4"));
initialRids.remove(new ORecordId("#77:4"));
}
if (counter == 5) {
bag.remove(new ORecordId("#77:6"));
initialRids.remove(new ORecordId("#77:6"));
}
initialRids.contains(iterator.next());
}
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());
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class JSONTest method testEmbeddedRIDBagDeserialisationWhenFieldTypeIsProvided.
@Test
public void testEmbeddedRIDBagDeserialisationWhenFieldTypeIsProvided() throws Exception {
ODocument documentSource = new ODocument();
documentSource.fromJSON("{FirstName:\"Student A 0\",in_EHasGoodStudents:[#57:0],@fieldTypes:\"in_EHasGoodStudents=g\"}");
ORidBag bag = documentSource.field("in_EHasGoodStudents");
Assert.assertEquals(bag.size(), 1);
OIdentifiable rid = bag.rawIterator().next();
Assert.assertTrue(rid.getIdentity().getClusterId() == 57);
Assert.assertTrue(rid.getIdentity().getClusterPosition() == 0);
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class LinkBagIndexTest method testIndexRidBagUpdateAddItemInTxRollback.
public void testIndexRidBagUpdateAddItemInTxRollback() throws Exception {
final ODocument docOne = new ODocument();
docOne.save();
final ODocument docTwo = new ODocument();
docTwo.save();
final ODocument docThree = new ODocument();
docThree.save();
final ODocument document = new ODocument("RidBagIndexTestClass");
final ORidBag ridBag = new ORidBag();
ridBag.add(docOne);
ridBag.add(docTwo);
document.field("ridBag", ridBag);
document.save();
database.begin();
ODocument loadedDocument = database.load(document.getIdentity());
loadedDocument.<ORidBag>field("ridBag").add(docThree);
loadedDocument.save();
database.rollback();
List<ODocument> result = database.command(new OCommandSQL("select key, rid from index:ridBagIndex")).execute();
Assert.assertNotNull(result);
Assert.assertEquals(result.size(), 2);
for (ODocument d : result) {
Assert.assertTrue(d.containsField("key"));
Assert.assertTrue(d.containsField("rid"));
if (!d.field("key").equals(docOne.getIdentity()) && !d.field("key").equals(docTwo.getIdentity())) {
Assert.fail("Unknown key found: " + d.field("key"));
}
}
}
Aggregations