additional query method on mongotemplate using default collection
This commit is contained in:
@@ -13,29 +13,33 @@ public class ControllerCounter {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
public double getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(int count) {
|
||||
public void setCount(double count) {
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public Map<String, Integer> getMethods() {
|
||||
public Map<String, Double> getMethods() {
|
||||
return methods;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setMethods(Map<String, Integer> methods) {
|
||||
public void setMethods(Map<String, Double> methods) {
|
||||
this.methods = methods;
|
||||
}
|
||||
|
||||
private String name;
|
||||
|
||||
private int count;
|
||||
|
||||
private Map<String, Integer> methods;
|
||||
private double count;
|
||||
|
||||
private Map<String, Double> methods;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ControllerCounter [name=" + name + ", count=" + count
|
||||
+ ", methods=" + methods + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.springframework.datastore.document.mongodb;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
public class FluentDbObject {
|
||||
|
||||
public static DBObject DbObject(Tuple... entries) {
|
||||
BasicDBObject map = new BasicDBObject();
|
||||
|
||||
for (Tuple entry : entries) {
|
||||
map.put(entry.t1, entry.t2);
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public static Tuple pair(String o1, Object o2) {
|
||||
return new Tuple(o1, o2);
|
||||
}
|
||||
|
||||
public static class Tuple {
|
||||
private String t1;
|
||||
private Object t2;
|
||||
|
||||
public Tuple(String t1, Object t2) {
|
||||
this.t1 = t1;
|
||||
this.t2 = t2;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,5 +20,4 @@ import com.mongodb.DBObject;
|
||||
public interface MongoReader<T> {
|
||||
|
||||
T read(Class<? extends T> clazz, DBObject dbo);
|
||||
//T read(DBObject dbo);
|
||||
}
|
||||
|
||||
@@ -180,6 +180,19 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> implements
|
||||
}
|
||||
}
|
||||
|
||||
public <T> List<T> queryForCollection(Class<T> targetClass) {
|
||||
|
||||
List<T> results = new ArrayList<T>();
|
||||
DBCollection collection = getConnection().getCollection(getDefaultCollectionName());
|
||||
for (DBObject dbo : collection.find()) {
|
||||
Object obj = mongoConverter.read(targetClass, dbo);
|
||||
//effectively acts as a query on the collection restricting it to elements of a specific type
|
||||
if (targetClass.isInstance(obj)) {
|
||||
results.add(targetClass.cast(obj));
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
public <T> List<T> queryForCollection(String collectionName, Class<T> targetClass) {
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import org.junit.Test;
|
||||
import org.springframework.datastore.document.analytics.ControllerCounter;
|
||||
import org.springframework.datastore.document.analytics.MvcEvent;
|
||||
import org.springframework.datastore.document.analytics.Parameters;
|
||||
import org.springframework.datastore.document.mongodb.MongoReader;
|
||||
import org.springframework.datastore.document.mongodb.MongoTemplate;
|
||||
|
||||
import com.mongodb.BasicDBList;
|
||||
@@ -25,7 +26,7 @@ import com.mongodb.Mongo;
|
||||
import com.mongodb.QueryBuilder;
|
||||
import com.mongodb.WriteResult;
|
||||
|
||||
@Ignore
|
||||
|
||||
public class MvcAnalyticsTests {
|
||||
|
||||
private MongoTemplate mongoTemplate;
|
||||
@@ -36,27 +37,68 @@ public class MvcAnalyticsTests {
|
||||
DB db = m.getDB("mvc");
|
||||
mongoTemplate = new MongoTemplate(db, "mvc");
|
||||
mongoTemplate.afterPropertiesSet();
|
||||
// mongoTemplate.dropCollection("mvc");
|
||||
// mongoTemplate.createCollection("mvc");
|
||||
//mongoTemplate.dropCollection("counters");
|
||||
//mongoTemplate.createCollection("counters");
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clean() {
|
||||
mongoTemplate.dropCollection("mvc");
|
||||
mongoTemplate.createCollection("mvc");
|
||||
mongoTemplate.dropCollection("counters");
|
||||
mongoTemplate.createCollection("counters");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadData() {
|
||||
public void loadMvcEventData() {
|
||||
|
||||
// datasize, favoriteRestId
|
||||
createAndStoreForP1(5, 1);
|
||||
createAndStoreForP1(6, 2);
|
||||
createAndStoreForP1(3, 3);
|
||||
createAndStoreForP1(8, 4);
|
||||
createAndStoreMvcEvent(5, 1);
|
||||
createAndStoreMvcEvent(6, 2);
|
||||
createAndStoreMvcEvent(3, 3);
|
||||
createAndStoreMvcEvent(8, 4);
|
||||
|
||||
List<MvcEvent> mvcEvents = mongoTemplate.queryForCollection("mvc",
|
||||
MvcEvent.class);
|
||||
Assert.assertEquals(22, mvcEvents.size());
|
||||
|
||||
List<MvcEvent> mvcEvents2 = mongoTemplate.queryForCollection("mvc", MvcEvent.class,
|
||||
new MongoReader<MvcEvent>() {
|
||||
public MvcEvent read(Class<? extends MvcEvent> clazz, DBObject dbo) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void loadCounterData() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
storeCounterData("SignUpController", "createForm");
|
||||
storeCounterData("SignUpController", "create");
|
||||
storeCounterData("SignUpController", "show");
|
||||
storeCounterData("RestaurantController", "addFavoriteRestaurant");
|
||||
}
|
||||
for (int i = 0; i< 5;i++) {
|
||||
storeCounterData("RestaurantController", "list");
|
||||
storeCounterData("SignUpController", "show");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryCounterData() {
|
||||
DBObject query = QueryBuilder.start("name").is("SignUpController").get();
|
||||
for (DBObject dbo : mongoTemplate.getCollection("counters").find(query)) {
|
||||
System.out.println(dbo);
|
||||
}
|
||||
List<ControllerCounter> counters = mongoTemplate.queryForList("counters", "{ 'name' : 'SignUpController'} ", ControllerCounter.class);
|
||||
for (ControllerCounter controllerCounter : counters) {
|
||||
System.out.println(controllerCounter);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* var start = new Date(2010,9,1); var end = new Date(2010,11,1);
|
||||
@@ -66,13 +108,12 @@ public class MvcAnalyticsTests {
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void listAll() {
|
||||
public void listAllMvcEvents() {
|
||||
List<MvcEvent> mvcEvents = mongoTemplate.queryForCollection("mvc",
|
||||
MvcEvent.class);
|
||||
for (MvcEvent mvcEvent : mvcEvents) {
|
||||
System.out.println(mvcEvent.getDate());
|
||||
}
|
||||
// System.out.println(mvcEvents);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -126,7 +167,7 @@ public class MvcAnalyticsTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void storeCounterInfo() {
|
||||
public void storeControllerCounterInfo() {
|
||||
|
||||
BasicDBObject query = new BasicDBObject("name", "controller1");
|
||||
|
||||
@@ -156,8 +197,24 @@ public class MvcAnalyticsTests {
|
||||
DBObject changes = new BasicDBObject("$inc", new BasicDBObject("methods.find", 1));
|
||||
mongoTemplate.getConnection().getCollection("counters").update(query, changes, true, false);
|
||||
}
|
||||
|
||||
|
||||
public void storeCounterData(String controllerName, String methodName) {
|
||||
BasicDBObject query = new BasicDBObject("name", controllerName);
|
||||
|
||||
BasicDBObject changes = new BasicDBObject();
|
||||
changes.put("$set", new BasicDBObject("name", controllerName));
|
||||
changes.put("$inc", new BasicDBObject("count", 1));
|
||||
|
||||
WriteResult r = mongoTemplate.getCollection("counters").update(query, changes, true,false);
|
||||
System.out.println(r);
|
||||
|
||||
changes = new BasicDBObject("$inc", new BasicDBObject("methods." + methodName, 1));
|
||||
r = mongoTemplate.getConnection().getCollection("counters").update(query, changes, true, false);
|
||||
System.out.println(r);
|
||||
}
|
||||
|
||||
private void createAndStoreForP1(int dataSize, int p1) {
|
||||
private void createAndStoreMvcEvent(int dataSize, int p1) {
|
||||
for (int i = 0; i < dataSize; i++) {
|
||||
MvcEvent event = generateEvent(p1);
|
||||
mongoTemplate.save(event);
|
||||
@@ -168,8 +225,8 @@ public class MvcAnalyticsTests {
|
||||
ControllerCounter cc = new ControllerCounter();
|
||||
cc.setName("controller2");
|
||||
cc.setCount(0);
|
||||
Map<String, Integer> methods = new HashMap<String, Integer>();
|
||||
methods.put("find", 1);
|
||||
Map<String, Double> methods = new HashMap<String, Double>();
|
||||
methods.put("find", 1D);
|
||||
cc.setMethods(methods);
|
||||
return cc;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user