Subversion Repositories SmartDukaan

Rev

Rev 8909 | Rev 11957 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8749 amit.gupta 1
package in.shop2020.storage.mongo;
2
 
3
import in.shop2020.metamodel.core.BulletDataObject;
4
import in.shop2020.metamodel.core.Entity;
5
import in.shop2020.metamodel.core.EntityState;
6
import in.shop2020.metamodel.core.ExpertReview;
7
import in.shop2020.metamodel.core.ExpertReviewSource;
8
import in.shop2020.metamodel.core.Helpdoc;
9
import in.shop2020.metamodel.core.SpecialPage;
10
import in.shop2020.metamodel.util.ExpandedBullet;
11
import in.shop2020.storage.mongo.adapters.BDOAdapter;
12
 
13
import java.lang.reflect.Type;
14
import java.util.ArrayList;
15
import java.util.Collection;
16
import java.util.HashMap;
17
import java.util.HashSet;
18
import java.util.Iterator;
19
import java.util.List;
20
import java.util.Map;
21
import java.util.Set;
22
import java.util.TreeMap;
23
 
8810 amit.gupta 24
import org.apache.commons.logging.Log;
25
import org.apache.commons.logging.LogFactory;
26
 
8749 amit.gupta 27
import com.google.gson.Gson;
28
import com.google.gson.GsonBuilder;
29
import com.google.gson.reflect.TypeToken;
30
import com.mongodb.BasicDBObject;
31
import com.mongodb.DB;
32
import com.mongodb.DBCollection;
33
import com.mongodb.DBObject;
34
import com.mongodb.MongoClient;
35
import com.mongodb.util.JSON;
36
 
37
/**
38
 * Entry point for storing everything in berkley database. Singleton class which initialises the berkley database.
39
 * @author rajveer
40
 *
41
 */
42
public class StorageManager {
8810 amit.gupta 43
	private static Log            log                         = LogFactory.getLog(StorageManager.class);
8749 amit.gupta 44
	private static DB db;
9218 amit.gupta 45
    public static ContentViews views;
8749 amit.gupta 46
    private static StorageManager storageUtils;
47
    private static final Gson gs = new GsonBuilder().
48
    	registerTypeAdapter(BulletDataObject.class, new BDOAdapter()).
8865 amit.gupta 49
    	//registerTypeAdapter(FreeformContent.class, new FFCAdapter()).
8749 amit.gupta 50
    	create();
51
 
52
    static{
53
		synchronized(StorageManager.class){
54
			try {
55
				storageUtils = new StorageManager("CONTENT");
56
			} catch (Exception e) {
57
				// TODO Auto-generated catch block
58
				e.printStackTrace();
59
			}
60
		}
61
	}
62
 
63
    /**
64
     * Run the sample program.
65
     * @throws Exception 
66
     */
67
    public static void main(String[] args) throws Exception {
68
    	Map<Long,Double> m = StorageManager.getStorageManager().getCustomSlideScores(1008185);
69
    	System.out.println(m.containsKey(130002l));
70
    }
71
 
72
    /**
73
     * get storage manager
74
     * @return MongoStorageManager
75
     */
76
    public static StorageManager getStorageManager(){
77
		return storageUtils;
78
    }
79
 
80
    /**
81
     * Open the database and views.
82
     */
83
    private StorageManager(String mongoDB) throws Exception {
84
			MongoClient mongo = new MongoClient( "localhost" , 27017 );
85
			db = mongo.getDB(mongoDB);	
86
			views = ContentViews.getInstance(db);
87
    }
88
 
89
    /**
90
     * Get the entities for a given category
91
     * @param categoryId
92
     * @return Collection<Entity>
93
     */
94
    public Collection<Entity> getEntitisByCategory(long categoryId){
95
    	List <Entity> list = new ArrayList<Entity>();
96
    	DBObject dbo = new BasicDBObject();
8870 amit.gupta 97
    	dbo.put("categoryID", categoryId);
8749 amit.gupta 98
    	Iterator<DBObject> it = views.entity.find(dbo).iterator();
99
    	while (it.hasNext()) {
100
    		list.add((Entity)toObject(it.next(),Entity.class));
101
    	}
102
    	return list;
103
 
104
    }
105
 
106
 
107
    /**
108
     * get all helpdocs
109
     * @return
110
     */
111
	public Map<Long, Helpdoc> getHelpdocs(){
112
    	Map<Long, Helpdoc> helpdocs = new HashMap<Long, Helpdoc>();
113
    	Iterator<DBObject> iterator = views.helpdoc.find().iterator();
114
    	while (iterator.hasNext()){
115
    		DBObject dbo = iterator.next();
116
    		helpdocs.put((Long)dbo.get("_id"), (Helpdoc)toObject(dbo, Helpdoc.class));
117
    	}
118
    	return helpdocs;
119
    }
120
 
121
    /**
122
     * Get helpdoc for an given id
123
     * @param helpdocId
124
     * @return
125
     */
126
    public Helpdoc getHelpdoc(long helpdocId){
127
    	return getById(views.helpdoc, helpdocId, Helpdoc.class);
128
    }
129
 
130
    /**
131
     * Delete helpdoc for an given id
132
     * @param helpdocId
133
     * @return
134
     */
135
    public void deleteHelpdoc(long helpdocId){
136
    	views.helpdoc.remove(new BasicDBObject("_id", helpdocId));
137
    }
138
 
139
	public void updateHelpdoc(Helpdoc helpdoc){
140
    	insertOrUpdateById(views.helpdoc, helpdoc.getID(), helpdoc);
141
    }
142
 
143
	/**
144
     * get metadata for all entities
145
     * @return
146
     */
147
	public Map<Long, EntityState> getEntitiesMetadata(){
148
    	Map<Long,EntityState> es = new TreeMap<Long, EntityState>();
149
    	Iterator<DBObject> iterator = views.entityMetadata.find().iterator();
150
    	while (iterator.hasNext()){
151
    		DBObject dbo = iterator.next();
152
    		es.put((Long)dbo.get("_id"),(EntityState)toObject(dbo, EntityState.class));
153
    	}
154
    	return es;
155
    }
156
 
157
    /**
158
     * Get metadata for an given entity.
159
     * @param entityId
160
     * @return
161
     */
162
    public EntityState getEntityMetadata(long entityId){
163
    	return (EntityState)getById(views.entityMetadata, entityId, EntityState.class);
164
    }
165
 
166
    /**
167
     * update metadata for an given entity
168
     * @param entityMetadata
169
     */
170
	public void updateEntityMetadata(EntityState entityMetadata){
171
    	insertOrUpdateById(views.entityMetadata, entityMetadata.getID(), entityMetadata);
172
    }
173
 
174
    /**
175
     * Get entity for given id.
176
     * @param entityId
177
     * @return
178
     * @throws Exception
179
     */
180
    public Entity getEntity(long entityId) throws Exception{
181
    	return (Entity)getById(views.entity, entityId, Entity.class);
182
    }
183
 
184
    /**
185
     * Update an existing entity
186
     * @param entity
187
     * @throws Exception
188
     */
189
    public void updateEntity(Entity entity) throws Exception{
190
    	insertOrUpdateById(views.entity,entity.getID(),entity);
191
    }
192
 
193
    /**
194
     * Get all entities.
195
     * @return
196
     * @throws Exception
197
     */
198
	public Map<Long, Entity> getEntities() throws Exception{
199
    	Map<Long,Entity> map = new TreeMap<Long, Entity>(); 
200
    	Iterator<DBObject> iterator = views.entity.find().iterator();
201
    	while(iterator.hasNext()){
202
    		DBObject dbo = iterator.next();
203
    		map.put((Long)dbo.get("_id"), (Entity)toObject(dbo, Entity.class));
204
    	}
205
    	return map;
206
    }
207
 
208
 
209
 
210
    /**
211
     * Returns all special pages from the database.
212
     */
213
    public Map<Long, SpecialPage> getSpecialPages() {
214
    	Map<Long,SpecialPage> map = new TreeMap<Long, SpecialPage>(); 
215
    	Iterator<DBObject> iterator = views.specialPages.find().iterator();
216
    	while(iterator.hasNext()){
217
    		DBObject dbo = iterator.next();
218
    		Long pageId = (Long)dbo.removeField("_id");
219
    		map.put(pageId, (SpecialPage)toObject(dbo, SpecialPage.class));
220
    	}
221
    	return map;
222
    }
223
 
224
    /**
225
     * Updates a given special page in the database.
226
     */
227
    public void updateSpecialPage(SpecialPage specialPage) {
228
    	insertOrUpdateById(views.specialPages, specialPage.getID(), specialPage);
229
    }
230
 
231
    /**
232
     * Returns a special page object given a id after looking it up from map
233
     * of values loaded from database.
234
     */
235
    public SpecialPage getSpecialPage(long specialPageId) {
236
        return (SpecialPage)getById(views.specialPages, specialPageId, SpecialPage.class);
237
    }
238
 
239
    /**
240
     * Removes a special page from database, if exists in database.
241
     */
242
    public void deleteSpecialPage(long specialPageId) {
243
    	removeById(views.specialPages, specialPageId);
244
    }
245
 
8750 amit.gupta 246
    @SuppressWarnings("unchecked")
8749 amit.gupta 247
    private static <T> T toObject(DBObject dbo, Type t) {
248
    	dbo.removeField("_id");
8751 amit.gupta 249
    	return (T)gs.fromJson(dbo.toString(), t);
8749 amit.gupta 250
    }
251
 
8750 amit.gupta 252
    @SuppressWarnings("unchecked")
9218 amit.gupta 253
    public static <T> T getById(DBCollection collection, Long id, Type t) {
8749 amit.gupta 254
    	DBObject dbo = collection.findOne(id);
255
    	if(dbo ==null) return null;
256
    	if (dbo.get(id.toString()) != null){
8751 amit.gupta 257
    		return (T)gs.fromJson(dbo.get(id.toString()).toString(), t);
8749 amit.gupta 258
    	}
259
    	dbo.removeField("_id");
8751 amit.gupta 260
    	return (T)gs.fromJson(dbo.toString(), t);
8749 amit.gupta 261
    }
262
 
9218 amit.gupta 263
    public static <T> void insertOrUpdateById  (DBCollection collection, long id, T obj) {
8770 amit.gupta 264
    	DBObject dbo = (DBObject)JSON.parse(gs.toJson(obj));
8749 amit.gupta 265
    	dbo.put("_id", id);
266
    	collection.update(new BasicDBObject("_id", id ), dbo, true, false);
9218 amit.gupta 267
    }
268
 
269
    public static <T> void addById  (DBCollection collection, long id, T obj) {
270
    	DBObject dbo = (DBObject)JSON.parse(gs.toJson(obj));
8749 amit.gupta 271
 
9218 amit.gupta 272
    	//dbo.put("_id", id);
273
    	collection.update(new BasicDBObject("_id", id ), new BasicDBObject("$set", dbo), true, false);
8749 amit.gupta 274
    }
275
 
276
    private static void removeById(DBCollection collection, Long id) {
277
    	collection.remove(new BasicDBObject("_id", id));
278
    }
279
 
280
    public void storeDataObject(String dataObjectName, Object dataObject) throws Exception{
281
    	DBCollection collection = db.getCollection(dataObjectName);
282
    	Map <String, Object>  obj = new HashMap<String, Object>();
283
    	obj.put("1", dataObject);
284
    	insertOrUpdateById(collection, 1L, obj);
285
    }
286
 
8750 amit.gupta 287
    @SuppressWarnings("unchecked")
8749 amit.gupta 288
    public <T> T getDataObject(String dataObjectName, Type t) throws Exception{
289
    	DBCollection collection = db.getCollection(dataObjectName);
8751 amit.gupta 290
    	return (T)getById(collection, 1L, t);
8749 amit.gupta 291
    }
292
 
293
	public Long getMaxPageId() {
294
		Iterator<DBObject> dbo = views.specialPages.find().sort(new BasicDBObject("_id", -1)).limit(1).iterator();
295
		while  (dbo.hasNext()) {
296
			DBObject dbo1 = dbo.next();
297
			return (Long) dbo1.get("_id");
298
		}
299
		return 0l;
300
	}
301
 
302
	public void deleteEntity(long entityId) {
303
		removeById(views.entity, entityId);
304
		removeById(views.entityMetadata, entityId);
305
	}
306
 
307
	public void createEntity(Entity entity, EntityState entityMetadata) {
8770 amit.gupta 308
		insertOrUpdateById(views.entity, entity.getID(), entity);
309
		insertOrUpdateById(views.entityMetadata, entity.getID(), entityMetadata);
8749 amit.gupta 310
	}
311
 
312
	public void storeCustomSlideScores(long entityId, Map<Long, Double> customSlideScores) {
313
		insertOrUpdateById(views.customSlideScores, entityId, customSlideScores);
314
	}
315
 
316
	public Map<Long,Double> getCustomSlideScores(long entityId) {
317
		Type t = new TypeToken<Map<Long,Double>>() {}.getType();
318
		return getById(views.customSlideScores, entityId, t);
319
	}
320
 
321
	public void storeLearnedBullets( Map<Long, List<ExpandedBullet>> learnedBullets) {
322
 
323
		for(Map.Entry<Long, List<ExpandedBullet>> learnedBullet : learnedBullets.entrySet()) {
324
			insertOrUpdateById(views.learnedBullets, learnedBullet.getKey(), new BasicDBObject(learnedBullet.getKey().toString(), learnedBullet.getValue()));
325
		}
326
	}
327
 
328
 
8750 amit.gupta 329
	@SuppressWarnings("unchecked")
8749 amit.gupta 330
	public Map<Long, List<ExpandedBullet>> getLearnedBullets() {
331
		Type t = new TypeToken<List<ExpandedBullet>>() {}.getType();
332
		Map<Long, List<ExpandedBullet>> learnedBullets  = new HashMap<Long, List<ExpandedBullet>>();
333
		Iterator<DBObject> iter = views.learnedBullets.find();
334
		while (iter.hasNext()) {
335
			DBObject dbo = iter.next();
336
			String featureId = dbo.get("_id").toString();
337
			learnedBullets.put(Long.parseLong(featureId), (List<ExpandedBullet>)gs.fromJson(dbo.get(featureId).toString(), t));
338
		}
339
		return learnedBullets;
340
	}
341
 
342
	public Set<ExpandedBullet> getLearnedBulletsByFeatureId (long featureId){
343
		Type t = new TypeToken<Set<ExpandedBullet>>() {}.getType();
344
		return getById(views.learnedBullets, featureId, t);
345
	}
346
 
347
 
348
	public void storeSearchStats(Long date, Map<String,List<String>> searchTerms){
349
		insertOrUpdateById(views.searchStats, date, searchTerms);
350
	}
351
 
352
	public void deleteSearchStats(Long date){
353
		removeById(views.searchStats, date);
354
	}
355
 
356
	public List<String> getSearchStats(Long date){
357
		Type t = new TypeToken<List<String>>() {}.getType();
358
		return getById(views.searchStats, date, t);
359
	}
360
 
361
	public ExpertReviewSource getExpertReviewSource(long sourceId) {
362
		return getById(views.expertReviewSource, sourceId, ExpertReviewSource.class);
363
	}
364
 
365
	public Set<ExpertReviewSource> getExpertReviewSources() {
366
		Iterator<DBObject> sourcesObj = views.expertReviewSource.find().iterator();
367
		Set<ExpertReviewSource> expSources = new HashSet<ExpertReviewSource>();
368
		while(sourcesObj.hasNext()){
369
			DBObject dbobj = sourcesObj.next();
370
			expSources.add((ExpertReviewSource)toObject(dbobj, ExpertReviewSource.class));
371
		}
372
		return expSources;
373
	}
374
 
375
	public void storeExpertReviewSource(ExpertReviewSource expertReviewSource) {
376
		insertOrUpdateById(views.expertReviewSource, (long)expertReviewSource.hashCode(), expertReviewSource);
377
	}
378
 
379
	public void deleteExpertReviewSource(ExpertReviewSource expertReviewSource) {
380
		removeById(views.expertReviewSource, (long)expertReviewSource.hashCode());
381
	}
382
 
8905 amit.gupta 383
	public void storeExpertReview(long entityId, ExpertReview expertReview){
384
		Type t = new TypeToken<List<ExpertReview>>() {}.getType();
385
		List<ExpertReview> expertReviews = getById(views.expertReviews, entityId, t);
386
		if(expertReviews == null) {
387
			expertReviews = new ArrayList<ExpertReview>();
388
		}
389
		expertReviews.add(expertReview);
390
		Map<Long,List<ExpertReview>> erMap = new HashMap<Long, List<ExpertReview>>();
391
		erMap.put(entityId, expertReviews);
8906 amit.gupta 392
		insertOrUpdateById(views.expertReviews, entityId, erMap);
8905 amit.gupta 393
 
394
	}
395
 
8749 amit.gupta 396
	public void storeExpertReview(long entityId, List<ExpertReview> expertReviews) {
397
		Map<Long, List<ExpertReview>> map = new HashMap<Long, List<ExpertReview>>();
398
		map.put(entityId, expertReviews);
399
		insertOrUpdateById(views.expertReviews, entityId, map);
400
	}
401
 
402
	public void deleteExpertReview(long entityId, int index) {
403
		Type t = new TypeToken<List<ExpertReview>>() {}.getType();
404
		List<ExpertReview> er = getById(views.expertReviews, entityId, t);
405
		er.remove(index);
406
		storeExpertReview(entityId, er);
407
	}
408
 
8750 amit.gupta 409
	@SuppressWarnings("unchecked")
8749 amit.gupta 410
	public Map<Long, List<ExpertReview>> getExpertReviews() {
411
		Type t = new TypeToken<List<ExpertReview>>() {}.getType();
412
		Map<Long, List<ExpertReview>> er  = new HashMap<Long, List<ExpertReview>>();
413
		Iterator<DBObject> iter = views.expertReviews.find();
414
		while (iter.hasNext()) {
415
			DBObject dbo = iter.next();
416
			String entityId = dbo.get("_id").toString();
417
			er.put(Long.parseLong(entityId), (List<ExpertReview>)gs.fromJson(dbo.get(entityId).toString(), t));
418
		}
419
		return er;
420
	}
421
 
422
	public void deleteExpertReview(long entityId) {
423
		removeById(views.expertReviews, entityId);
424
	}
425
 
426
 
8750 amit.gupta 427
	@SuppressWarnings("unchecked")
8749 amit.gupta 428
	public Map<Long, List<Long>> getHelpdocEntityIds() {
429
		Type t = new TypeToken<List<Long>>() {}.getType();
430
		Map<Long, List<Long>> er  = new HashMap<Long, List<Long>>();
431
		Iterator<DBObject> iter = views.helpdocEntityIds.find().iterator();
432
		while (iter.hasNext()) {
433
			DBObject dbo = iter.next();
434
			String helpdocId = dbo.get("_id").toString();
435
			er.put(Long.parseLong(helpdocId), (List<Long>)gs.fromJson(dbo.get(helpdocId).toString(), t));
436
		}
437
		return er;
438
	}
439
 
440
	public List<ExpertReview> getExpertReviewByEntity(long entityId) {
441
		Type t = new TypeToken<List<ExpertReview>>() {}.getType();
442
		return getById(views.expertReviews, entityId, t);
443
	}
444
 
445
}