Subversion Repositories SmartDukaan

Rev

Rev 8770 | Rev 8809 | 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();
198
    		map.put((Long)dbo.get("_id"), (Entity)toObject(dbo, Entity.class));
199
    	}
200
    	return map;
201
    }
202
 
203
 
204
 
205
    /**
206
     * Returns all special pages from the database.
207
     */
208
    public Map<Long, SpecialPage> getSpecialPages() {
209
    	Map<Long,SpecialPage> map = new TreeMap<Long, SpecialPage>(); 
210
    	Iterator<DBObject> iterator = views.specialPages.find().iterator();
211
    	while(iterator.hasNext()){
212
    		DBObject dbo = iterator.next();
213
    		Long pageId = (Long)dbo.removeField("_id");
214
    		map.put(pageId, (SpecialPage)toObject(dbo, SpecialPage.class));
215
    	}
216
    	return map;
217
    }
218
 
219
    /**
220
     * Updates a given special page in the database.
221
     */
222
    public void updateSpecialPage(SpecialPage specialPage) {
223
    	insertOrUpdateById(views.specialPages, specialPage.getID(), specialPage);
224
    }
225
 
226
    /**
227
     * Returns a special page object given a id after looking it up from map
228
     * of values loaded from database.
229
     */
230
    public SpecialPage getSpecialPage(long specialPageId) {
231
        return (SpecialPage)getById(views.specialPages, specialPageId, SpecialPage.class);
232
    }
233
 
234
    /**
235
     * Removes a special page from database, if exists in database.
236
     */
237
    public void deleteSpecialPage(long specialPageId) {
238
    	removeById(views.specialPages, specialPageId);
239
    }
240
 
8750 amit.gupta 241
    @SuppressWarnings("unchecked")
8749 amit.gupta 242
    private static <T> T toObject(DBObject dbo, Type t) {
243
    	dbo.removeField("_id");
8751 amit.gupta 244
    	return (T)gs.fromJson(dbo.toString(), t);
8749 amit.gupta 245
    }
246
 
8750 amit.gupta 247
    @SuppressWarnings("unchecked")
8749 amit.gupta 248
    private static <T> T getById(DBCollection collection, Long id, Type t) {
249
    	DBObject dbo = collection.findOne(id);
250
    	if(dbo ==null) return null;
251
    	if (dbo.get(id.toString()) != null){
8751 amit.gupta 252
    		return (T)gs.fromJson(dbo.get(id.toString()).toString(), t);
8749 amit.gupta 253
    	}
254
    	dbo.removeField("_id");
8751 amit.gupta 255
    	return (T)gs.fromJson(dbo.toString(), t);
8749 amit.gupta 256
    }
257
 
8770 amit.gupta 258
    private static <T> void insertOrUpdateById  (DBCollection collection, long id, T obj) {
259
    	DBObject dbo = (DBObject)JSON.parse(gs.toJson(obj));
8749 amit.gupta 260
    	dbo.put("_id", id);
261
    	collection.update(new BasicDBObject("_id", id ), dbo, true, false);
262
 
263
    }
264
 
265
    private static void removeById(DBCollection collection, Long id) {
266
    	collection.remove(new BasicDBObject("_id", id));
267
    }
268
 
269
    public void storeDataObject(String dataObjectName, Object dataObject) throws Exception{
270
    	DBCollection collection = db.getCollection(dataObjectName);
271
    	Map <String, Object>  obj = new HashMap<String, Object>();
272
    	obj.put("1", dataObject);
273
    	insertOrUpdateById(collection, 1L, obj);
274
    }
275
 
8750 amit.gupta 276
    @SuppressWarnings("unchecked")
8749 amit.gupta 277
    public <T> T getDataObject(String dataObjectName, Type t) throws Exception{
278
    	DBCollection collection = db.getCollection(dataObjectName);
8751 amit.gupta 279
    	return (T)getById(collection, 1L, t);
8749 amit.gupta 280
    }
281
 
282
	public Long getMaxPageId() {
283
		Iterator<DBObject> dbo = views.specialPages.find().sort(new BasicDBObject("_id", -1)).limit(1).iterator();
284
		while  (dbo.hasNext()) {
285
			DBObject dbo1 = dbo.next();
286
			return (Long) dbo1.get("_id");
287
		}
288
		return 0l;
289
	}
290
 
291
	public void deleteEntity(long entityId) {
292
		removeById(views.entity, entityId);
293
		removeById(views.entityMetadata, entityId);
294
	}
295
 
296
	public void createEntity(Entity entity, EntityState entityMetadata) {
8770 amit.gupta 297
		insertOrUpdateById(views.entity, entity.getID(), entity);
298
		insertOrUpdateById(views.entityMetadata, entity.getID(), entityMetadata);
8749 amit.gupta 299
	}
300
 
301
	public void storeCustomSlideScores(long entityId, Map<Long, Double> customSlideScores) {
302
		insertOrUpdateById(views.customSlideScores, entityId, customSlideScores);
303
	}
304
 
305
	public Map<Long,Double> getCustomSlideScores(long entityId) {
306
		Type t = new TypeToken<Map<Long,Double>>() {}.getType();
307
		return getById(views.customSlideScores, entityId, t);
308
	}
309
 
310
	public void storeLearnedBullets( Map<Long, List<ExpandedBullet>> learnedBullets) {
311
 
312
		for(Map.Entry<Long, List<ExpandedBullet>> learnedBullet : learnedBullets.entrySet()) {
313
			insertOrUpdateById(views.learnedBullets, learnedBullet.getKey(), new BasicDBObject(learnedBullet.getKey().toString(), learnedBullet.getValue()));
314
		}
315
	}
316
 
317
 
8750 amit.gupta 318
	@SuppressWarnings("unchecked")
8749 amit.gupta 319
	public Map<Long, List<ExpandedBullet>> getLearnedBullets() {
320
		Type t = new TypeToken<List<ExpandedBullet>>() {}.getType();
321
		Map<Long, List<ExpandedBullet>> learnedBullets  = new HashMap<Long, List<ExpandedBullet>>();
322
		Iterator<DBObject> iter = views.learnedBullets.find();
323
		while (iter.hasNext()) {
324
			DBObject dbo = iter.next();
325
			String featureId = dbo.get("_id").toString();
326
			learnedBullets.put(Long.parseLong(featureId), (List<ExpandedBullet>)gs.fromJson(dbo.get(featureId).toString(), t));
327
		}
328
		return learnedBullets;
329
	}
330
 
331
	public Set<ExpandedBullet> getLearnedBulletsByFeatureId (long featureId){
332
		Type t = new TypeToken<Set<ExpandedBullet>>() {}.getType();
333
		return getById(views.learnedBullets, featureId, t);
334
	}
335
 
336
	public void storeExpertReview(long entityId, ExpertReview expertReview){
337
		Type t = new TypeToken<List<ExpertReview>>() {}.getType();
338
		List<ExpertReview> expertReviews = getById(views.expertReviews, entityId, t);
339
		expertReviews.add(expertReview);
340
		Map<Long,List<ExpertReview>> erMap = new HashMap<Long, List<ExpertReview>>();
341
		erMap.put(entityId, expertReviews);
342
		insertOrUpdateById(views.expertReviews, entityId, t);
343
 
344
	}
345
 
346
	public void storeSearchStats(Long date, Map<String,List<String>> searchTerms){
347
		insertOrUpdateById(views.searchStats, date, searchTerms);
348
	}
349
 
350
	public void deleteSearchStats(Long date){
351
		removeById(views.searchStats, date);
352
	}
353
 
354
	public List<String> getSearchStats(Long date){
355
		Type t = new TypeToken<List<String>>() {}.getType();
356
		return getById(views.searchStats, date, t);
357
	}
358
 
359
	public ExpertReviewSource getExpertReviewSource(long sourceId) {
360
		return getById(views.expertReviewSource, sourceId, ExpertReviewSource.class);
361
	}
362
 
363
	public Set<ExpertReviewSource> getExpertReviewSources() {
364
		Iterator<DBObject> sourcesObj = views.expertReviewSource.find().iterator();
365
		Set<ExpertReviewSource> expSources = new HashSet<ExpertReviewSource>();
366
		while(sourcesObj.hasNext()){
367
			DBObject dbobj = sourcesObj.next();
368
			expSources.add((ExpertReviewSource)toObject(dbobj, ExpertReviewSource.class));
369
		}
370
		return expSources;
371
	}
372
 
373
	public void storeExpertReviewSource(ExpertReviewSource expertReviewSource) {
374
		insertOrUpdateById(views.expertReviewSource, (long)expertReviewSource.hashCode(), expertReviewSource);
375
	}
376
 
377
	public void deleteExpertReviewSource(ExpertReviewSource expertReviewSource) {
378
		removeById(views.expertReviewSource, (long)expertReviewSource.hashCode());
379
	}
380
 
381
	public void storeExpertReview(long entityId, List<ExpertReview> expertReviews) {
382
		Map<Long, List<ExpertReview>> map = new HashMap<Long, List<ExpertReview>>();
383
		map.put(entityId, expertReviews);
384
		insertOrUpdateById(views.expertReviews, entityId, map);
385
	}
386
 
387
	public void deleteExpertReview(long entityId, int index) {
388
		Type t = new TypeToken<List<ExpertReview>>() {}.getType();
389
		List<ExpertReview> er = getById(views.expertReviews, entityId, t);
390
		er.remove(index);
391
		storeExpertReview(entityId, er);
392
	}
393
 
8750 amit.gupta 394
	@SuppressWarnings("unchecked")
8749 amit.gupta 395
	public Map<Long, List<ExpertReview>> getExpertReviews() {
396
		Type t = new TypeToken<List<ExpertReview>>() {}.getType();
397
		Map<Long, List<ExpertReview>> er  = new HashMap<Long, List<ExpertReview>>();
398
		Iterator<DBObject> iter = views.expertReviews.find();
399
		while (iter.hasNext()) {
400
			DBObject dbo = iter.next();
401
			String entityId = dbo.get("_id").toString();
402
			er.put(Long.parseLong(entityId), (List<ExpertReview>)gs.fromJson(dbo.get(entityId).toString(), t));
403
		}
404
		return er;
405
	}
406
 
407
	public void deleteExpertReview(long entityId) {
408
		removeById(views.expertReviews, entityId);
409
	}
410
 
411
 
8750 amit.gupta 412
	@SuppressWarnings("unchecked")
8749 amit.gupta 413
	public Map<Long, List<Long>> getHelpdocEntityIds() {
414
		Type t = new TypeToken<List<Long>>() {}.getType();
415
		Map<Long, List<Long>> er  = new HashMap<Long, List<Long>>();
416
		Iterator<DBObject> iter = views.helpdocEntityIds.find().iterator();
417
		while (iter.hasNext()) {
418
			DBObject dbo = iter.next();
419
			String helpdocId = dbo.get("_id").toString();
420
			er.put(Long.parseLong(helpdocId), (List<Long>)gs.fromJson(dbo.get(helpdocId).toString(), t));
421
		}
422
		return er;
423
	}
424
 
425
	public List<ExpertReview> getExpertReviewByEntity(long entityId) {
426
		Type t = new TypeToken<List<ExpertReview>>() {}.getType();
427
		return getById(views.expertReviews, entityId, t);
428
	}
429
 
430
}