Search in sources :

Example 1 with VDatabase

use of de.fhg.igd.mongomvcc.VDatabase 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 2 with VDatabase

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

the class IndexTest method stackOverflow.

/**
	 * This test checks if a stack overflows when there are too many commits in the database.
	 * See https://github.com/igd-geo/mongomvcc/pull/2 for more information.
	 * @throws StackOverflowError if the test fails
	 */
@Test
@Ignore("Really slow. Not necessary in all situations. See GitHub issue for more information.")
public void stackOverflow() throws StackOverflowError {
    VCollection persons = _master.getCollection("stack");
    Map<String, Object> elvis = _factory.createDocument();
    elvis.put("name", "elvis");
    for (int i = 0; i < 2500; i++) {
        persons.insert(elvis);
        _master.commit();
    }
    VDatabase db = _factory.createDatabase();
    db.connect(((MongoDBVDatabase) _db).getDB().getName());
    VBranch master = db.checkout(VConstants.MASTER);
    persons = master.getCollection("stack");
    persons.find().size();
}
Also used : VDatabase(de.fhg.igd.mongomvcc.VDatabase) MongoDBVDatabase(de.fhg.igd.mongomvcc.impl.MongoDBVDatabase) MongoDBVDatabase(de.fhg.igd.mongomvcc.impl.MongoDBVDatabase) VBranch(de.fhg.igd.mongomvcc.VBranch) VCollection(de.fhg.igd.mongomvcc.VCollection) Ignore(org.junit.Ignore) Test(org.junit.Test) AbstractMongoDBVDatabaseTest(de.fhg.igd.mongomvcc.impl.AbstractMongoDBVDatabaseTest)

Example 3 with VDatabase

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

the class AbstractMongoDBVDatabaseTest method setUpClass.

/**
	 * Before all unit tests run, make sure the database is clean
	 */
@BeforeClass
public static void setUpClass() {
    VDatabase db = _factory.createDatabase();
    db.connect("mvcctest");
    db.drop();
}
Also used : VDatabase(de.fhg.igd.mongomvcc.VDatabase) BeforeClass(org.junit.BeforeClass)

Aggregations

VDatabase (de.fhg.igd.mongomvcc.VDatabase)3 VBranch (de.fhg.igd.mongomvcc.VBranch)2 VCollection (de.fhg.igd.mongomvcc.VCollection)2 VCursor (de.fhg.igd.mongomvcc.VCursor)1 VFactory (de.fhg.igd.mongomvcc.VFactory)1 AbstractMongoDBVDatabaseTest (de.fhg.igd.mongomvcc.impl.AbstractMongoDBVDatabaseTest)1 MongoDBVDatabase (de.fhg.igd.mongomvcc.impl.MongoDBVDatabase)1 MongoDBVFactory (de.fhg.igd.mongomvcc.impl.MongoDBVFactory)1 BeforeClass (org.junit.BeforeClass)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1