use of de.fhg.igd.mongomvcc.helper.IdMap in project mongomvcc by igd-geo.
the class Tree method resolveCollectionObjects.
private static IdMap resolveCollectionObjects(DBObject o) {
Set<String> keys = o.keySet();
IdMap r = new IdHashMap(keys.size());
for (String k : keys) {
r.put(Long.parseLong(k), (Long) o.get(k));
}
return r;
}
use of de.fhg.igd.mongomvcc.helper.IdMap in project mongomvcc by igd-geo.
the class MongoDBVCollection method find.
@Override
public VCursor find() {
//ask index for OIDs
IdMap objs = _branch.getIndex().find(_name);
if (objs.size() == 0) {
return MongoDBVCursor.EMPTY;
}
//ask MongoDB for objects with the given OIDs
if (objs.size() == 1) {
//shortcut for one object
return createCursor(_delegate.find(new BasicDBObject(OID, objs.values()[0])), null);
} else {
DBObject qo = new BasicDBObject();
qo.putAll(_branch.getQueryObject());
return createCursor(_delegate.find(qo), new OIDInIndexFilter());
}
}
use of de.fhg.igd.mongomvcc.helper.IdMap 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();
}
use of de.fhg.igd.mongomvcc.helper.IdMap 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();
}
use of de.fhg.igd.mongomvcc.helper.IdMap in project mongomvcc by igd-geo.
the class Tree method deserializeCommit.
/**
* Deserializes a database object to a commit
* @param o the object
* @return the commit
*/
public static Commit deserializeCommit(DBObject o) {
long cid = (Long) o.get(MongoDBConstants.ID);
Long timestampL = (Long) o.get(MongoDBConstants.TIMESTAMP);
long timestamp = timestampL != null ? timestampL : 0;
long parentCID = (Long) o.get(PARENT_CID);
long rootCID = (Long) o.get(ROOT_CID);
DBObject objs = (DBObject) o.get(OBJECTS);
Map<String, IdMap> objects = new HashMap<String, IdMap>();
for (String k : objs.keySet()) {
if (!k.equals(MongoDBConstants.ID)) {
objects.put(k, resolveCollectionObjects((DBObject) objs.get(k)));
}
}
return new Commit(cid, timestamp, parentCID, rootCID, objects);
}
Aggregations