Search in sources :

Example 1 with VException

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

the class MongoDBVDatabase method connect.

@Override
public void connect(String name, String host, int port) throws VException {
    Mongo mongo;
    try {
        mongo = new Mongo(new ServerAddress(host, port));
    } catch (UnknownHostException e) {
        throw new VException("Unknown host", e);
    }
    connectInternal(name, mongo);
}
Also used : UnknownHostException(java.net.UnknownHostException) VException(de.fhg.igd.mongomvcc.VException) Mongo(com.mongodb.Mongo) ServerAddress(com.mongodb.ServerAddress)

Example 2 with VException

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

the class Tree method getChildren.

@Override
public long[] getChildren(long cid) {
    if (cid != 0 && !existsCommit(cid)) {
        throw new VException("Unknown commit: " + cid);
    }
    DBCursor c = _commits.find(new BasicDBObject(PARENT_CID, cid), new BasicDBObject(MongoDBConstants.ID, 1));
    long[] r = new long[c.count()];
    int i = 0;
    for (DBObject o : c) {
        r[i++] = (Long) o.get(MongoDBConstants.ID);
    }
    return r;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) VException(de.fhg.igd.mongomvcc.VException) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 3 with VException

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

the class MongoDBVCollectionTest method conflict.

/**
 * Tests if the database throws an exception if a conflict arises
 * @throws Exception if everything works as expected
 */
@Test
public void conflict() throws Exception {
    // obtain collection to get a snapshot of the current database
    _master.getCollection("persons").find();
    assertEquals(0, _master.getCollection("persons").find().size());
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    class ConflictCallable implements Callable<Object> {

        @Override
        public Object call() throws Exception {
            // obtain collection to get a snapshot of the current database
            _master.getCollection("persons").find();
            assertEquals(0, _master.getCollection("persons").find().size());
            latch2.countDown();
            try {
                latch1.await();
            } catch (InterruptedException e) {
                fail();
                throw new RuntimeException(e);
            }
            // insert new person and commit
            putPerson("Peter", 26);
            _master.commit();
            assertEquals(1, _master.getCollection("persons").find().size());
            return null;
        }
    }
    // let other thread make commit based on the same head
    FutureTask<Object> ft1 = new FutureTask<Object>(new ConflictCallable());
    Executor exe = Executors.newCachedThreadPool();
    exe.execute(ft1);
    // wait until other thread has obtained the same branch
    while (!latch2.await(100, TimeUnit.MILLISECONDS)) {
        // check if the other thread threw an exception meanwhile
        if (ft1.isDone()) {
            ft1.get();
        }
    }
    // let other thread commit its result
    latch1.countDown();
    // insert new person and commit
    putPerson("Max", 5);
    // wait for the other thread to exit
    ft1.get();
    // should throw here because of a conflict
    try {
        _master.commit();
    } catch (VException e) {
        // this is what we expect
        return;
    }
    fail("Should have thrown a VException up to now");
}
Also used : Executor(java.util.concurrent.Executor) FutureTask(java.util.concurrent.FutureTask) VException(de.fhg.igd.mongomvcc.VException) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 4 with VException

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

the class DefaultAccessStrategy method onResolve.

@Override
public void onResolve(Map<String, Object> obj) {
    @SuppressWarnings("unchecked") List<String> binaryAttributes = (List<String>) obj.get(BINARY_ATTRIBUTES);
    if (binaryAttributes == null) {
        // nothing to do
        return;
    }
    try {
        for (String attr : binaryAttributes) {
            long gridId = (Long) obj.get(attr);
            obj.put(attr, _convert.convert(gridId));
        }
    } catch (IOException e) {
        throw new VException("Could not read binary data", e);
    }
    obj.remove(BINARY_ATTRIBUTES);
}
Also used : VException(de.fhg.igd.mongomvcc.VException) List(java.util.List) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 5 with VException

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

the class MongoDBVBranch method commit.

@Override
public long commit() {
    Index idx = getIndex();
    // clone dirty objects because we clear them below
    Map<String, IdMap> dos = new HashMap<String, IdMap>(idx.getDirtyObjects());
    Commit head = getHeadCommit();
    Commit c = new Commit(_db.getCounter().getNextId(), head.getCID(), _rootCid, dos);
    _tree.addCommit(c);
    updateHead(c);
    // mark deleted objects as deleted in the database
    DB db = _db.getDB();
    String lifetimeAttr = MongoDBConstants.LIFETIME + "." + getRootCid();
    for (Map.Entry<String, IdSet> e : idx.getDeletedOids().entrySet()) {
        DBCollection dbc = db.getCollection(e.getKey());
        IdSetIterator li = e.getValue().iterator();
        while (li.hasNext()) {
            long oid = li.next();
            // save the CID of the commit where the object has been deleted
            dbc.update(new BasicDBObject(MongoDBConstants.ID, oid), new BasicDBObject("$set", new BasicDBObject(lifetimeAttr, head.getCID())));
        }
    }
    // mark dirty objects as inserted
    String instimeAttr = MongoDBConstants.LIFETIME + ".i" + getRootCid();
    for (Map.Entry<String, IdMap> e : dos.entrySet()) {
        DBCollection dbc = db.getCollection(e.getKey());
        IdMap m = e.getValue();
        IdMapIterator li = m.iterator();
        while (li.hasNext()) {
            li.advance();
            long oid = li.value();
            if (oid == -1) {
                // do not save time of insertion
                continue;
            }
            // save the CID of the commit where the object has been inserted
            dbc.update(new BasicDBObject(MongoDBConstants.ID, oid), new BasicDBObject("$set", new BasicDBObject(instimeAttr, head.getCID())));
        }
    }
    // reset index
    idx.clearDirtyObjects();
    // update named branch's head
    if (_name != null) {
        // and then update it
        synchronized (this) {
            // check for conflicts (i.e. if another thread has already updated the branch's head)
            if (_tree.resolveBranch(_name).getCID() != c.getParentCID()) {
                throw new VException("Branch " + _name + " has already been " + "updated by another commit");
            }
            _tree.updateBranchHead(_name, c.getCID());
        }
    }
    return c.getCID();
}
Also used : HashMap(java.util.HashMap) IdMapIterator(de.fhg.igd.mongomvcc.helper.IdMapIterator) Index(de.fhg.igd.mongomvcc.impl.internal.Index) IdSetIterator(de.fhg.igd.mongomvcc.helper.IdSetIterator) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) IdMap(de.fhg.igd.mongomvcc.helper.IdMap) Commit(de.fhg.igd.mongomvcc.impl.internal.Commit) IdSet(de.fhg.igd.mongomvcc.helper.IdSet) VException(de.fhg.igd.mongomvcc.VException) HashMap(java.util.HashMap) IdMap(de.fhg.igd.mongomvcc.helper.IdMap) Map(java.util.Map) DB(com.mongodb.DB)

Aggregations

VException (de.fhg.igd.mongomvcc.VException)8 BasicDBObject (com.mongodb.BasicDBObject)3 DBObject (com.mongodb.DBObject)2 Mongo (com.mongodb.Mongo)2 UnknownHostException (java.net.UnknownHostException)2 Test (org.junit.Test)2 DB (com.mongodb.DB)1 DBCollection (com.mongodb.DBCollection)1 DBCursor (com.mongodb.DBCursor)1 ServerAddress (com.mongodb.ServerAddress)1 VBranch (de.fhg.igd.mongomvcc.VBranch)1 VCollection (de.fhg.igd.mongomvcc.VCollection)1 IdMap (de.fhg.igd.mongomvcc.helper.IdMap)1 IdMapIterator (de.fhg.igd.mongomvcc.helper.IdMapIterator)1 IdSet (de.fhg.igd.mongomvcc.helper.IdSet)1 IdSetIterator (de.fhg.igd.mongomvcc.helper.IdSetIterator)1 Commit (de.fhg.igd.mongomvcc.impl.internal.Commit)1 Index (de.fhg.igd.mongomvcc.impl.internal.Index)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1