Subversion Repositories SmartDukaan

Rev

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