Permalink
Browse files

wrapping of index handling for DAO's

  • Loading branch information...
hofmeister committed Nov 28, 2012
1 parent 48ff47a commit 6706f6c56e3b07e13c5a469aff17750a8ab8fd37
@@ -27,6 +27,7 @@
private final String collectionName;
private final Class<T> entryClass;
private DBCollection collection;
+ private List<DBObject> sortKeys = new ArrayList<DBObject>();
public AbstractDAO(String collectionName,Class<T> entryClass) {
this.collectionName = collectionName;
@@ -38,6 +39,41 @@ public void afterInject() {
collection = db.getCollection(collectionName);
}
+
+ protected void addSortKey(String field,int dir) {
+ sortKeys.add(new BasicDBObject(field, dir));
+ }
+
+ protected void ensureIndex(String ... fields) {
+ if (fields.length == 0) {
+ return;
+ }
+
+ BasicDBObject keys = new BasicDBObject();
+ for(String field:fields) {
+ keys.put(field,1);
+ }
+
+ coll().ensureIndex(keys);
+ }
+
+ protected void ensureSortedIndex(String ... fields) {
+ if (fields.length == 0) {
+ return;
+ }
+ for(DBObject sortKey:sortKeys) {
+
+ BasicDBObject keys = new BasicDBObject();
+ for(String field:fields) {
+ keys.put(field,1);
+ }
+ keys.putAll(sortKey);
+ coll().ensureIndex(keys);
+ }
+ }
+
+
+
public DBCollection coll() {
return collection;
}
@@ -13,8 +13,9 @@
private String id;
public String getId() {
- if (id == null)
+ if (id == null) {
id = UUID.randomUUID().toString();
+ }
return id;
}

0 comments on commit 6706f6c

Please sign in to comment.