Search in sources :

Example 1 with VCollection

use of de.fhg.igd.mongomvcc.VCollection in project mongomvcc by igd-geo.

the class AbstractMongoDBVDatabaseTest method putPerson.

/**
	 * Put a person into the database
	 * @param name the person's name
	 * @param age the person's age
	 * @return the person (after the put)
	 */
protected Map<String, Object> putPerson(String name, int age) {
    VCollection persons = _master.getCollection("persons");
    assertNotNull(persons);
    Map<String, Object> peter = new HashMap<String, Object>();
    peter.put("name", name);
    peter.put("age", age);
    persons.insert(peter);
    assertNotNull(peter.get("uid"));
    return peter;
}
Also used : HashMap(java.util.HashMap) VCollection(de.fhg.igd.mongomvcc.VCollection)

Example 2 with VCollection

use of de.fhg.igd.mongomvcc.VCollection in project mongomvcc by igd-geo.

the class FiveMinutes method main.

/**
	 * Runs the tutorial
	 * @param args the program arguments
	 */
public static void main(String[] args) {
    // 1. Connect to a database
    VFactory factory = new MongoDBVFactory();
    VDatabase db = factory.createDatabase();
    db.connect("mongomvcc-five-minutes-tutorial");
    // Checkout the "master" branch
    VBranch master = db.checkout(VConstants.MASTER);
    // 2. Put something into the index
    VCollection persons = master.getCollection("persons");
    Map<String, Object> elvis = factory.createDocument();
    elvis.put("name", "Elvis");
    elvis.put("age", 3);
    persons.insert(elvis);
    // insert another person
    persons.insert(factory.createDocument("name", "Peter"));
    // 3. Commit index to the database
    long firstCid = master.commit();
    // 4. Read documents from the database
    VCursor c = persons.find();
    for (Map<String, Object> person : c) {
        System.out.print("Person { name: " + person.get("name"));
        if (person.containsKey("age")) {
            System.out.print(", age: " + person.get("age"));
        }
        System.out.println(" }");
    }
    Map<String, Object> elvis2 = persons.findOne(factory.createDocument("name", "Elvis"));
    if (elvis2 != null) {
        System.out.println("Elvis lives!");
    }
    // 5. Make another commit
    persons.insert(factory.createDocument("name", "Max"));
    elvis.put("age", 4);
    persons.insert(elvis);
    master.commit();
    // 6. Checkout a previous version
    System.out.println("There are " + persons.find().size() + " persons");
    Map<String, Object> elvis3 = persons.findOne(factory.createDocument("name", "Elvis"));
    System.out.println("Elvis is now " + elvis3.get("age") + " years old");
    VBranch oldMaster = db.checkout(firstCid);
    VCollection oldPersons = oldMaster.getCollection("persons");
    System.out.println("Previously, there were only " + oldPersons.find().size() + " persons");
    Map<String, Object> oldElvis = oldPersons.findOne(factory.createDocument("name", "Elvis"));
    System.out.println("Last year, Elvis was " + oldElvis.get("age") + " years old");
    // 7. Drop the database
    db.drop();
}
Also used : VDatabase(de.fhg.igd.mongomvcc.VDatabase) VBranch(de.fhg.igd.mongomvcc.VBranch) VFactory(de.fhg.igd.mongomvcc.VFactory) MongoDBVFactory(de.fhg.igd.mongomvcc.impl.MongoDBVFactory) MongoDBVFactory(de.fhg.igd.mongomvcc.impl.MongoDBVFactory) VCursor(de.fhg.igd.mongomvcc.VCursor) VCollection(de.fhg.igd.mongomvcc.VCollection)

Example 3 with VCollection

use of de.fhg.igd.mongomvcc.VCollection in project mongomvcc by igd-geo.

the class MongoDBVDatabaseBenchmark method mvccInsert.

private void mvccInsert(int offset) {
    VCollection coll = _master.getCollection("persons");
    for (long i = 0; i < DOCUMENTS; ++i) {
        Map<String, Object> obj = _factory.createDocument();
        obj.put("name", String.valueOf(i));
        obj.put("age", i + offset);
        obj.put("uid", 1 + i + offset);
        coll.insert(obj);
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) VCollection(de.fhg.igd.mongomvcc.VCollection)

Example 4 with VCollection

use of de.fhg.igd.mongomvcc.VCollection in project mongomvcc by igd-geo.

the class MongoDBVDatabaseBenchmark method mvccInsertDelete.

/**
	 * Inserts a lot of documents using MongoMVCC and then deletes them
	 */
@Test
@BenchmarkOptions(benchmarkRounds = 2, warmupRounds = 1)
public void mvccInsertDelete() {
    mvccInsert();
    VCollection coll = _master.getCollection("persons");
    for (long i = 0; i < DOCUMENTS; ++i) {
        coll.delete(1 + i);
    }
}
Also used : VCollection(de.fhg.igd.mongomvcc.VCollection) Test(org.junit.Test) BenchmarkOptions(com.carrotsearch.junitbenchmarks.BenchmarkOptions)

Example 5 with VCollection

use of de.fhg.igd.mongomvcc.VCollection 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

VCollection (de.fhg.igd.mongomvcc.VCollection)22 Test (org.junit.Test)17 VBranch (de.fhg.igd.mongomvcc.VBranch)10 VCursor (de.fhg.igd.mongomvcc.VCursor)7 BasicDBObject (com.mongodb.BasicDBObject)4 AbstractMongoDBVDatabaseTest (de.fhg.igd.mongomvcc.impl.AbstractMongoDBVDatabaseTest)3 HashMap (java.util.HashMap)3 BenchmarkOptions (com.carrotsearch.junitbenchmarks.BenchmarkOptions)2 DBCursor (com.mongodb.DBCursor)2 DBObject (com.mongodb.DBObject)2 VDatabase (de.fhg.igd.mongomvcc.VDatabase)2 Ignore (org.junit.Ignore)2 VException (de.fhg.igd.mongomvcc.VException)1 VFactory (de.fhg.igd.mongomvcc.VFactory)1 VHistory (de.fhg.igd.mongomvcc.VHistory)1 MongoDBVDatabase (de.fhg.igd.mongomvcc.impl.MongoDBVDatabase)1 MongoDBVFactory (de.fhg.igd.mongomvcc.impl.MongoDBVFactory)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1