use of de.fhg.igd.mongomvcc.VCursor in project mongomvcc by igd-geo.
the class MongoDBVCollectionTest method find.
/**
* Tests if one object matching a given example can be retrieved from a collection
*/
@Test
public void find() {
for (int i = 0; i < 20; ++i) {
putPerson(String.valueOf(i), i);
}
_master.commit();
VCursor vc = _master.getCollection("persons").find(_factory.createDocument("name", "10"));
assertEquals(1, vc.size());
Map<String, Object> p = vc.iterator().next();
assertEquals("10", p.get("name"));
assertEquals(Integer.valueOf(10), p.get("age"));
}
use of de.fhg.igd.mongomvcc.VCursor in project mongomvcc by igd-geo.
the class MongoDBVCollectionTest method update.
/**
* Puts a person into the database and then updates his age
* @param commit true if a commit should be made after the put
*/
private void update(boolean commit) {
Map<String, Object> p = putPerson("Peter", 26);
// p has a UID now. alter it and insert it again
assertNotNull(p.get("uid"));
// happy birthday, peter!
p.put("age", 27);
_master.getCollection("persons").insert(p);
if (commit) {
_master.commit();
}
// retrieve peter again and check his age
VCursor vc = _master.getCollection("persons").find(_factory.createDocument("name", "Peter"));
assertEquals(1, vc.size());
Map<String, Object> p2 = vc.iterator().next();
assertDocEquals(p, p2);
}
use of de.fhg.igd.mongomvcc.VCursor in project mongomvcc by igd-geo.
the class MongoDBVCollectionTest method lifetimeDeletedOptimization.
/**
* Tests if lifetime optimization takes effect. Objects that have
* been deleted should not be loaded but filtered out on the
* database level already.
*/
@Test
public void lifetimeDeletedOptimization() {
// insert two documents to skip in-index shortcut
putPerson("Max", 6);
putPerson("Pax", 8);
_master.commit();
VCollection persons = _master.getCollection("persons");
VCursor cursor = persons.find();
DBCursor dbcursor = extractDBCursor(cursor);
assertEquals(2, cursor.size());
assertTrue(hasAttachedFilter(cursor));
assertEquals(2, dbcursor.size());
putPerson("Elvis", 3);
_master.commit();
persons = _master.getCollection("persons");
cursor = persons.find();
dbcursor = extractDBCursor(cursor);
assertEquals(3, cursor.size());
assertTrue(hasAttachedFilter(cursor));
assertEquals(3, dbcursor.size());
persons.delete(_factory.createDocument("name", "Max"));
_master.commit();
persons = _master.getCollection("persons");
cursor = persons.find();
dbcursor = extractDBCursor(cursor);
assertEquals(2, cursor.size());
assertTrue(hasAttachedFilter(cursor));
assertEquals(2, dbcursor.size());
}
use of de.fhg.igd.mongomvcc.VCursor in project mongomvcc by igd-geo.
the class MongoDBVLargeCollectionTest method largeObjectByte.
/**
* Tests if large objects with byte arrays/streams/buffers can be saved in the database
* @throws Exception if something goes wrong
*/
@Test
public void largeObjectByte() throws Exception {
VCollection coll = _master.getLargeCollection("images");
byte[] test = new byte[1024 * 1024];
for (int i = 0; i < test.length; ++i) {
test[i] = (byte) (i & 0xFF);
}
Map<String, Object> obj = new HashMap<String, Object>();
obj.put("name", "Mona Lisa");
obj.put("data", test);
coll.insert(obj);
VCursor vc = coll.find();
assertEquals(1, vc.size());
Map<String, Object> obj2 = vc.iterator().next();
assertEquals("Mona Lisa", obj2.get("name"));
assertArrayEquals(test, (byte[]) obj2.get("data"));
ByteArrayInputStream bais = new ByteArrayInputStream(test);
obj = new HashMap<String, Object>();
obj.put("name", "Mona Lisa");
obj.put("data", bais);
coll.insert(obj);
Map<String, Object> obj3 = coll.findOne(_factory.createDocument("uid", obj.get("uid")));
assertEquals("Mona Lisa", obj3.get("name"));
InputStream is3 = (InputStream) obj3.get("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[64 * 1024];
int read;
while ((read = is3.read(buf)) > 0) {
baos.write(buf, 0, read);
}
assertArrayEquals(test, baos.toByteArray());
ByteBuffer bb = ByteBuffer.wrap(test);
obj = new HashMap<String, Object>();
obj.put("name", "Mona Lisa");
obj.put("data", bb);
coll.insert(obj);
Map<String, Object> obj4 = coll.findOne(_factory.createDocument("uid", obj.get("uid")));
assertEquals("Mona Lisa", obj4.get("name"));
ByteBuffer bb4 = (ByteBuffer) obj4.get("data");
bb4.rewind();
byte[] test4 = new byte[bb4.remaining()];
bb4.get(test4);
assertArrayEquals(test, test4);
}
use of de.fhg.igd.mongomvcc.VCursor in project mongomvcc by igd-geo.
the class MongoDBVLargeCollectionTest method largeObjectFloat.
/**
* Tests if large objects with float arrays/streams/buffers can be saved in the database
* @throws Exception if something goes wrong
*/
@Test
public void largeObjectFloat() throws Exception {
VCollection coll = _master.getLargeCollection("images");
float[] test = new float[1024 * 1024];
for (int i = 0; i < test.length; ++i) {
test[i] = (byte) (i & 0xFF);
}
Map<String, Object> obj = new HashMap<String, Object>();
obj.put("name", "Mona Lisa");
obj.put("data", test);
coll.insert(obj);
VCursor vc = coll.find();
assertEquals(1, vc.size());
Map<String, Object> obj2 = vc.iterator().next();
assertEquals("Mona Lisa", obj2.get("name"));
assertArrayEquals(test, (float[]) obj2.get("data"), 0.00001f);
FloatBuffer bb = FloatBuffer.wrap(test);
obj = new HashMap<String, Object>();
obj.put("name", "Mona Lisa");
obj.put("data", bb);
coll.insert(obj);
Map<String, Object> obj4 = coll.findOne(_factory.createDocument("uid", obj.get("uid")));
assertEquals("Mona Lisa", obj4.get("name"));
FloatBuffer bb4 = (FloatBuffer) obj4.get("data");
bb4.rewind();
float[] test4 = new float[bb4.remaining()];
bb4.get(test4);
assertArrayEquals(test, test4, 0.00001f);
}
Aggregations