Subversion Repositories SmartDukaan

Rev

Rev 8749 | Rev 8751 | 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 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
 
8750 amit.gupta 245
    @SuppressWarnings("unchecked")
8749 amit.gupta 246
    private static <T> T toObject(DBObject dbo, Type t) {
247
    	dbo.removeField("_id");
248
    	return gs.fromJson(dbo.toString(), t);
249
    }
250
 
8750 amit.gupta 251
    @SuppressWarnings("unchecked")
8749 amit.gupta 252
    private static <T> T getById(DBCollection collection, Long id, Type t) {
253
    	DBObject dbo = collection.findOne(id);
254
    	if(dbo ==null) return null;
255
    	if (dbo.get(id.toString()) != null){
256
    		return gs.fromJson(dbo.get(id.toString()).toString(), t);
257
    	}
258
    	dbo.removeField("_id");
259
    	return gs.fromJson(dbo.toString(), t);
260
    }
261
 
8750 amit.gupta 262
    @SuppressWarnings("unchecked")
8749 amit.gupta 263
    private static <T> void insertOrUpdateById  (DBCollection collection, long id, T t) {
264
    	DBObject dbo = (DBObject)JSON.parse(gs.toJson(t));
265
    	dbo.put("_id", id);
266
    	collection.update(new BasicDBObject("_id", id ), dbo, true, false);
267
 
268
    }
269
 
270
    private static void removeById(DBCollection collection, Long id) {
271
    	collection.remove(new BasicDBObject("_id", id));
272
    }
273
 
274
    public void storeDataObject(String dataObjectName, Object dataObject) throws Exception{
275
    	DBCollection collection = db.getCollection(dataObjectName);
276
    	Map <String, Object>  obj = new HashMap<String, Object>();
277
    	obj.put("1", dataObject);
278
    	insertOrUpdateById(collection, 1L, obj);
279
    }
280
 
8750 amit.gupta 281
    @SuppressWarnings("unchecked")
8749 amit.gupta 282
    public <T> T getDataObject(String dataObjectName, Type t) throws Exception{
283
    	DBCollection collection = db.getCollection(dataObjectName);
284
    	return getById(collection, 1L, t);
285
    }
286
 
287
	public Long getMaxPageId() {
288
		Iterator<DBObject> dbo = views.specialPages.find().sort(new BasicDBObject("_id", -1)).limit(1).iterator();
289
		while  (dbo.hasNext()) {
290
			DBObject dbo1 = dbo.next();
291
			return (Long) dbo1.get("_id");
292
		}
293
		return 0l;
294
	}
295
 
296
	public void deleteEntity(long entityId) {
297
		removeById(views.entity, entityId);
298
		removeById(views.entityMetadata, entityId);
299
	}
300
 
301
	public void createEntity(Entity entity, EntityState entityMetadata) {
302
		insertOrUpdateById(views.entity, entity.getID(), entity.getClass());
303
		insertOrUpdateById(views.entityMetadata, entity.getID(), entityMetadata.getClass());
304
	}
305
 
306
	public void storeCustomSlideScores(long entityId, Map<Long, Double> customSlideScores) {
307
		insertOrUpdateById(views.customSlideScores, entityId, customSlideScores);
308
	}
309
 
310
	public Map<Long,Double> getCustomSlideScores(long entityId) {
311
		Type t = new TypeToken<Map<Long,Double>>() {}.getType();
312
		return getById(views.customSlideScores, entityId, t);
313
	}
314
 
315
	public void storeLearnedBullets( Map<Long, List<ExpandedBullet>> learnedBullets) {
316
 
317
		for(Map.Entry<Long, List<ExpandedBullet>> learnedBullet : learnedBullets.entrySet()) {
318
			insertOrUpdateById(views.learnedBullets, learnedBullet.getKey(), new BasicDBObject(learnedBullet.getKey().toString(), learnedBullet.getValue()));
319
		}
320
	}
321
 
322
 
8750 amit.gupta 323
	@SuppressWarnings("unchecked")
8749 amit.gupta 324
	public Map<Long, List<ExpandedBullet>> getLearnedBullets() {
325
		Type t = new TypeToken<List<ExpandedBullet>>() {}.getType();
326
		Map<Long, List<ExpandedBullet>> learnedBullets  = new HashMap<Long, List<ExpandedBullet>>();
327
		Iterator<DBObject> iter = views.learnedBullets.find();
328
		while (iter.hasNext()) {
329
			DBObject dbo = iter.next();
330
			String featureId = dbo.get("_id").toString();
331
			learnedBullets.put(Long.parseLong(featureId), (List<ExpandedBullet>)gs.fromJson(dbo.get(featureId).toString(), t));
332
		}
333
		return learnedBullets;
334
	}
335
 
336
	public Set<ExpandedBullet> getLearnedBulletsByFeatureId (long featureId){
337
		Type t = new TypeToken<Set<ExpandedBullet>>() {}.getType();
338
		return getById(views.learnedBullets, featureId, t);
339
	}
340
 
341
	public void storeExpertReview(long entityId, ExpertReview expertReview){
342
		Type t = new TypeToken<List<ExpertReview>>() {}.getType();
343
		List<ExpertReview> expertReviews = getById(views.expertReviews, entityId, t);
344
		expertReviews.add(expertReview);
345
		Map<Long,List<ExpertReview>> erMap = new HashMap<Long, List<ExpertReview>>();
346
		erMap.put(entityId, expertReviews);
347
		insertOrUpdateById(views.expertReviews, entityId, t);
348
 
349
	}
350
 
351
	public void storeSearchStats(Long date, Map<String,List<String>> searchTerms){
352
		insertOrUpdateById(views.searchStats, date, searchTerms);
353
	}
354
 
355
	public void deleteSearchStats(Long date){
356
		removeById(views.searchStats, date);
357
	}
358
 
359
	public List<String> getSearchStats(Long date){
360
		Type t = new TypeToken<List<String>>() {}.getType();
361
		return getById(views.searchStats, date, t);
362
	}
363
 
364
	public ExpertReviewSource getExpertReviewSource(long sourceId) {
365
		return getById(views.expertReviewSource, sourceId, ExpertReviewSource.class);
366
	}
367
 
368
	public Set<ExpertReviewSource> getExpertReviewSources() {
369
		Iterator<DBObject> sourcesObj = views.expertReviewSource.find().iterator();
370
		Set<ExpertReviewSource> expSources = new HashSet<ExpertReviewSource>();
371
		while(sourcesObj.hasNext()){
372
			DBObject dbobj = sourcesObj.next();
373
			expSources.add((ExpertReviewSource)toObject(dbobj, ExpertReviewSource.class));
374
		}
375
		return expSources;
376
	}
377
 
378
	public void storeExpertReviewSource(ExpertReviewSource expertReviewSource) {
379
		insertOrUpdateById(views.expertReviewSource, (long)expertReviewSource.hashCode(), expertReviewSource);
380
	}
381
 
382
	public void deleteExpertReviewSource(ExpertReviewSource expertReviewSource) {
383
		removeById(views.expertReviewSource, (long)expertReviewSource.hashCode());
384
	}
385
 
386
	public void storeExpertReview(long entityId, List<ExpertReview> expertReviews) {
387
		Map<Long, List<ExpertReview>> map = new HashMap<Long, List<ExpertReview>>();
388
		map.put(entityId, expertReviews);
389
		insertOrUpdateById(views.expertReviews, entityId, map);
390
	}
391
 
392
	public void deleteExpertReview(long entityId, int index) {
393
		Type t = new TypeToken<List<ExpertReview>>() {}.getType();
394
		List<ExpertReview> er = getById(views.expertReviews, entityId, t);
395
		er.remove(index);
396
		storeExpertReview(entityId, er);
397
	}
398
 
8750 amit.gupta 399
	@SuppressWarnings("unchecked")
8749 amit.gupta 400
	public Map<Long, List<ExpertReview>> getExpertReviews() {
401
		Type t = new TypeToken<List<ExpertReview>>() {}.getType();
402
		Map<Long, List<ExpertReview>> er  = new HashMap<Long, List<ExpertReview>>();
403
		Iterator<DBObject> iter = views.expertReviews.find();
404
		while (iter.hasNext()) {
405
			DBObject dbo = iter.next();
406
			String entityId = dbo.get("_id").toString();
407
			er.put(Long.parseLong(entityId), (List<ExpertReview>)gs.fromJson(dbo.get(entityId).toString(), t));
408
		}
409
		return er;
410
	}
411
 
412
	public void deleteExpertReview(long entityId) {
413
		removeById(views.expertReviews, entityId);
414
	}
415
 
416
 
8750 amit.gupta 417
	@SuppressWarnings("unchecked")
8749 amit.gupta 418
	public Map<Long, List<Long>> getHelpdocEntityIds() {
419
		Type t = new TypeToken<List<Long>>() {}.getType();
420
		Map<Long, List<Long>> er  = new HashMap<Long, List<Long>>();
421
		Iterator<DBObject> iter = views.helpdocEntityIds.find().iterator();
422
		while (iter.hasNext()) {
423
			DBObject dbo = iter.next();
424
			String helpdocId = dbo.get("_id").toString();
425
			er.put(Long.parseLong(helpdocId), (List<Long>)gs.fromJson(dbo.get(helpdocId).toString(), t));
426
		}
427
		return er;
428
	}
429
 
430
	public List<ExpertReview> getExpertReviewByEntity(long entityId) {
431
		Type t = new TypeToken<List<ExpertReview>>() {}.getType();
432
		return getById(views.expertReviews, entityId, t);
433
	}
434
 
435
}