Search in sources :

Example 1 with IdMapIterator

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

the class Index method readCommit.

/**
	 * Iteratively reads the information from the given commit and all its
	 * ancestors and builds up the index
	 * @param c the commit to read
	 * @param tree the tree of commits
	 */
private void readCommit(Commit c, Tree tree) {
    ArrayDeque<Commit> stack = new ArrayDeque<Commit>();
    while (true) {
        //iteratively read parent commit (if there is any)
        stack.addLast(c);
        long cid = c.getParentCID();
        if (cid == 0) {
            break;
        }
        c = tree.resolveCommit(cid);
    }
    while (!stack.isEmpty()) {
        c = stack.removeLast();
        //read objects from the given commit and put them into the index
        for (Map.Entry<String, IdMap> e : c.getObjects().entrySet()) {
            IdMap m = getObjects(e.getKey());
            IdSet o = getOIDs(e.getKey());
            IdMapIterator it = e.getValue().iterator();
            while (it.hasNext()) {
                it.advance();
                if (it.value() < 0) {
                    //deleted object
                    long prev = m.get(it.key());
                    if (prev != 0) {
                        m.remove(it.key());
                        o.remove(prev);
                    }
                } else {
                    long prev = m.put(it.key(), it.value());
                    if (prev != 0) {
                        //overwrite object with new value
                        o.remove(prev);
                    }
                    o.add(it.value());
                }
            }
        }
    }
}
Also used : IdMap(de.fhg.igd.mongomvcc.helper.IdMap) IdSet(de.fhg.igd.mongomvcc.helper.IdSet) IdMapIterator(de.fhg.igd.mongomvcc.helper.IdMapIterator) Map(java.util.Map) IdHashMap(de.fhg.igd.mongomvcc.helper.IdHashMap) HashMap(java.util.HashMap) IdMap(de.fhg.igd.mongomvcc.helper.IdMap) ArrayDeque(java.util.ArrayDeque)

Example 2 with IdMapIterator

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

the class Tree method addCommit.

/**
	 * Adds a commit to the tree
	 * @param commit the commit to add
	 */
public void addCommit(Commit commit) {
    DBObject o = new BasicDBObject();
    o.put(MongoDBConstants.ID, commit.getCID());
    o.put(MongoDBConstants.TIMESTAMP, commit.getTimestamp());
    o.put(PARENT_CID, commit.getParentCID());
    o.put(ROOT_CID, commit.getRootCID());
    DBObject objs = new BasicDBObject();
    for (Map.Entry<String, IdMap> e : commit.getObjects().entrySet()) {
        DBObject co = new BasicDBObject();
        IdMapIterator it = e.getValue().iterator();
        while (it.hasNext()) {
            it.advance();
            co.put(String.valueOf(it.key()), it.value());
        }
        objs.put(e.getKey(), co);
    }
    o.put(OBJECTS, objs);
    _commits.insert(o);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) IdMap(de.fhg.igd.mongomvcc.helper.IdMap) IdMapIterator(de.fhg.igd.mongomvcc.helper.IdMapIterator) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) IdHashMap(de.fhg.igd.mongomvcc.helper.IdHashMap) HashMap(java.util.HashMap) IdMap(de.fhg.igd.mongomvcc.helper.IdMap) Map(java.util.Map)

Example 3 with IdMapIterator

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

the class MongoDBVMaintenance method doFindUnreferencedDocuments.

private long[] doFindUnreferencedDocuments(String collection, long expiry, TimeUnit unit) {
    long maxTime = getMaxTime(expiry, unit);
    //fetch the OIDs of all documents older than the expiry time
    DBCollection collDocs = _db.getDB().getCollection(collection);
    DBCursor docs = collDocs.find(new BasicDBObject(MongoDBConstants.TIMESTAMP, //also include docs without a timestamp
    new BasicDBObject("$not", new BasicDBObject("$gte", maxTime))), new BasicDBObject(MongoDBConstants.ID, 1));
    IdSet oids = new IdHashSet(docs.count());
    for (DBObject o : docs) {
        oids.add((Long) o.get(MongoDBConstants.ID));
    }
    //iterate through all commits and eliminate referenced documents
    DBCollection collCommits = _db.getDB().getCollection(MongoDBConstants.COLLECTION_COMMITS);
    for (DBObject o : collCommits.find()) {
        Commit c = Tree.deserializeCommit(o);
        Map<String, IdMap> allObjs = c.getObjects();
        IdMap objs = allObjs.get(collection);
        if (objs != null) {
            //eliminate OIDs referenced by this commit
            IdMapIterator mi = objs.iterator();
            while (mi.hasNext()) {
                mi.advance();
                oids.remove(mi.value());
            }
        }
    }
    //the remaining OIDs must be the unreferenced ones
    return oids.toArray();
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) IdMap(de.fhg.igd.mongomvcc.helper.IdMap) Commit(de.fhg.igd.mongomvcc.impl.internal.Commit) IdSet(de.fhg.igd.mongomvcc.helper.IdSet) IdMapIterator(de.fhg.igd.mongomvcc.helper.IdMapIterator) IdHashSet(de.fhg.igd.mongomvcc.helper.IdHashSet) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 4 with IdMapIterator

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

IdMap (de.fhg.igd.mongomvcc.helper.IdMap)4 IdMapIterator (de.fhg.igd.mongomvcc.helper.IdMapIterator)4 BasicDBObject (com.mongodb.BasicDBObject)3 IdSet (de.fhg.igd.mongomvcc.helper.IdSet)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 DBCollection (com.mongodb.DBCollection)2 DBObject (com.mongodb.DBObject)2 IdHashMap (de.fhg.igd.mongomvcc.helper.IdHashMap)2 Commit (de.fhg.igd.mongomvcc.impl.internal.Commit)2 DB (com.mongodb.DB)1 DBCursor (com.mongodb.DBCursor)1 VException (de.fhg.igd.mongomvcc.VException)1 IdHashSet (de.fhg.igd.mongomvcc.helper.IdHashSet)1 IdSetIterator (de.fhg.igd.mongomvcc.helper.IdSetIterator)1 Index (de.fhg.igd.mongomvcc.impl.internal.Index)1 ArrayDeque (java.util.ArrayDeque)1