Created
August 27, 2013 23:23
-
-
Save pferrel/6360322 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package finderbots.recommenders.hadoop.mongo; | |
import com.google.code.morphia.Datastore; | |
import com.google.code.morphia.DatastoreImpl; | |
import com.google.code.morphia.Key; | |
import com.google.code.morphia.Morphia; | |
import com.google.code.morphia.dao.DAO; | |
import com.google.code.morphia.query.Query; | |
import com.google.code.morphia.query.QueryResults; | |
import com.google.code.morphia.query.UpdateOperations; | |
import com.google.code.morphia.query.UpdateResults; | |
import com.mongodb.DBCollection; | |
import com.mongodb.Mongo; | |
import com.mongodb.WriteConcern; | |
import com.mongodb.WriteResult; | |
import java.lang.reflect.ParameterizedType; | |
import java.util.List; | |
/** | |
* @author Olafur Gauti Gudmundsson | |
* @author Scott Hernandez | |
*/ | |
@SuppressWarnings({ "unchecked", "rawtypes" }) | |
public class BasicNamedDAO<T, K> implements DAO<T, K> { | |
protected Class<T> entityClazz; | |
protected DatastoreImpl ds; | |
protected String collectionName; | |
/** | |
* <p> Only calls this from your derived class when you explicitly declare the generic types with concrete classes </p> <p> {@code class | |
* MyDao extends DAO<MyEntity, String>} </p> | |
*/ | |
public BasicNamedDAO(final Mongo mongo, final Morphia morphia, final String dbName, final String collectionName) { | |
initDS(mongo, morphia, dbName); | |
initType(((Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0])); | |
this.collectionName = collectionName; | |
} | |
protected void initType(final Class<T> type) { | |
entityClazz = type; | |
ds.getMapper().addMappedClass(type); | |
} | |
protected void initDS(final Mongo mon, final Morphia mor, final String db) { | |
ds = new DatastoreImpl(mor, mon, db); | |
} | |
/** | |
* The underlying collection for this DAO | |
*/ | |
@Override | |
public DBCollection getCollection() { | |
return ds.getCollection(collectionName);//todo: not sure if this works! | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#createQuery() | |
*/ | |
@Override | |
public Query<T> createQuery() { | |
return ds.createQuery(this.collectionName, entityClazz);//todo: not sure about the methods that use Query | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#createUpdateOperations() | |
*/ | |
@Override | |
public UpdateOperations<T> createUpdateOperations() { | |
return null;//todo: not allowed but can't throw because the Base class doesn't | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#getEntityClass() | |
*/ | |
@Override | |
public Class<T> getEntityClass() { | |
return entityClazz; | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#save(T) | |
*/ | |
@Override | |
public Key<T> save(final T entity) { | |
return ds.save(this.collectionName, entity);// This works. i've tried it. | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#save(T, com.mongodb.WriteConcern) | |
*/ | |
@Override | |
public Key<T> save(final T entity, final WriteConcern wc) { | |
return ds.save(this.collectionName, entity, wc);// This works, I've tried it | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#updateFirst(com.google.code.morphia.query.Query, com.google.code.morphia.query.UpdateOperations) | |
*/ | |
@Override | |
public UpdateResults<T> updateFirst(final Query<T> q, final UpdateOperations<T> ops) { | |
//return ds.updateFirst(q, ops); | |
return null;//todo: not allowed but can't throw because the Base class doesn't | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#update(com.google.code.morphia.query.Query, com.google.code.morphia.query.UpdateOperations) | |
*/ | |
@Override | |
public UpdateResults<T> update(final Query<T> q, final UpdateOperations<T> ops) { | |
//return ds.update(q, ops); | |
return null;//todo: does not work with named collections, can't throw exception 'cause then it doesn't override--argh! | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#delete(T) | |
*/ | |
@Override | |
public WriteResult delete(final T entity) { | |
return ds.delete(this.collectionName, entity);// This works, I've tried it | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#delete(T, com.mongodb.WriteConcern) | |
*/ | |
@Override | |
public WriteResult delete(final T entity, final WriteConcern wc) { | |
//todo: I doubt this work with explicitly named collections? | |
return ds.delete(entity, wc); | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#deleteById(K) | |
*/ | |
@Override | |
public WriteResult deleteById(final K id) { | |
return ds.delete(this.collectionName, id); | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#deleteByQuery(com.google.code.morphia.query.Query) | |
*/ | |
@Override | |
public WriteResult deleteByQuery(final Query<T> q) { | |
return ds.delete(this.collectionName, q); | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#get(K) | |
*/ | |
@Override | |
public T get(final K id) { | |
return ds.get(this.collectionName, this.entityClazz, id); | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#findIds(java.lang.String, java.lang.Object) | |
*/ | |
@Override | |
public List<K> findIds(final String key, final Object value) { | |
//return (List<K>) keysToIds(ds.find(entityClazz, key, value).asKeyList()); | |
return null;//todo: does not work with named collections | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#findIds() | |
*/ | |
@Override | |
public List<K> findIds() { | |
//return (List<K>) keysToIds(ds.find(this.collectionName, entityClazz).asKeyList()); | |
return null;//todo: does not work with named collections | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#findIds(com.google.code.morphia.query.Query) | |
*/ | |
@Override | |
public List<K> findIds(final Query<T> q) { | |
//return (List<K>) keysToIds(q.asKeyList()); | |
return null;//todo: does not work with named collections | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#exists(java.lang.String, java.lang.Object) | |
*/ | |
@Override | |
public boolean exists(final String key, final Object value) { | |
//return exists(ds.find(entityClazz, key, value)); | |
if(ds.exists(key) != null ){//todo: exist uses a mapper so don't think it works with named collections | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#exists(com.google.code.morphia.query.Query) | |
*/ | |
// unchanged from BasicDAO | |
@Override | |
public boolean exists(final Query<T> q) { | |
return ds.getCount(q) > 0; | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#count() | |
*/ | |
@Override | |
public long count() { | |
return ds.getCount(this.collectionName); | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#count(java.lang.String, java.lang.Object) | |
*/ | |
@Override | |
public long count(final String key, final Object value) { | |
return count(ds.find(entityClazz, key, value));// todo: I doubt this works with named collections | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#count(com.google.code.morphia.query.Query) | |
*/ | |
@Override | |
public long count(final Query<T> q) { | |
return ds.getCount(q);//todo: not sure if this works with named collections | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#findOne(java.lang.String, java.lang.Object) | |
*/ | |
@Override | |
public T findOne(final String key, final Object value) { | |
return ds.find(this.collectionName, entityClazz, key, value, 0, 0, true).get(); | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#findOne(com.google.code.morphia.query.Query) | |
*/ | |
@Override | |
public T findOne(final Query<T> q) { | |
return q.get(); | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#find() | |
*/ | |
@Override | |
public QueryResults<T> find() { | |
return createQuery(); | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#find(com.google.code.morphia.query.Query) | |
*/ | |
@Override | |
public QueryResults<T> find(final Query<T> q) { | |
return q; | |
} | |
/* (non-Javadoc) | |
* @see com.google.code.morphia.DAO#getDatastore() | |
*/ | |
@Override | |
public Datastore getDatastore() { | |
return ds; | |
} | |
@Override | |
public void ensureIndexes() { | |
ds.ensureIndexes(entityClazz);//todo: not sure if this works with named collections | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment