Subversion Repositories SmartDukaan

Rev

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