
com.nominanuda.store.mongo.MongoBlobStore Maven / Gradle / Ivy
The newest version!
package com.nominanuda.store.mongo;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoException;
import com.nominanuda.lang.Check;
import com.nominanuda.store.api.KeyValueStore;
public class MongoBlobStore implements KeyValueStore{
private static final String _ID = "_id";
private static final String BLOB_FIELD_NAME = "blob";
private DBCollection collection;
@Override
public void put(String k, byte[] v) throws MongoException {
DBObject o = new BasicDBObject();
o.put(BLOB_FIELD_NAME, v);
o.put(_ID, k);
collection.update(new BasicDBObject(_ID, k), o, true, false);
}
@Override
public byte[] get(String k) throws MongoException {
DBCursor cur = collection.find(new BasicDBObject(_ID, k));
return cur.hasNext()
? (byte[])cur.next().get(BLOB_FIELD_NAME)
: null;
}
@Override
public boolean exists(String k) throws MongoException {
return get(k) != null;
}
public void setCollection(DBCollection collection) {
this.collection = Check.notNull(collection);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy