Subversion Repositories SmartDukaan

Rev

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