Search in sources :

Example 1 with VCursor

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"));
}
Also used : VCursor(de.fhg.igd.mongomvcc.VCursor) Test(org.junit.Test)

Example 2 with VCursor

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);
}
Also used : VCursor(de.fhg.igd.mongomvcc.VCursor)

Example 3 with VCursor

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());
}
Also used : DBCursor(com.mongodb.DBCursor) VCursor(de.fhg.igd.mongomvcc.VCursor) VCollection(de.fhg.igd.mongomvcc.VCollection) Test(org.junit.Test)

Example 4 with VCursor

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);
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) VCollection(de.fhg.igd.mongomvcc.VCollection) ByteArrayInputStream(java.io.ByteArrayInputStream) VCursor(de.fhg.igd.mongomvcc.VCursor) Test(org.junit.Test)

Example 5 with VCursor

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);
}
Also used : HashMap(java.util.HashMap) FloatBuffer(java.nio.FloatBuffer) VCursor(de.fhg.igd.mongomvcc.VCursor) VCollection(de.fhg.igd.mongomvcc.VCollection) Test(org.junit.Test)

Aggregations

VCursor (de.fhg.igd.mongomvcc.VCursor)13 Test (org.junit.Test)11 VCollection (de.fhg.igd.mongomvcc.VCollection)7 DBCursor (com.mongodb.DBCursor)2 VBranch (de.fhg.igd.mongomvcc.VBranch)2 HashMap (java.util.HashMap)2 VDatabase (de.fhg.igd.mongomvcc.VDatabase)1 VFactory (de.fhg.igd.mongomvcc.VFactory)1 MongoDBVFactory (de.fhg.igd.mongomvcc.impl.MongoDBVFactory)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 ByteBuffer (java.nio.ByteBuffer)1 FloatBuffer (java.nio.FloatBuffer)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Ignore (org.junit.Ignore)1