Subversion Repositories SmartDukaan

Rev

Rev 1061 | Rev 2275 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1050 rajveer 1
 
2
package in.shop2020.storage.bdb;
3
 
4
import in.shop2020.metamodel.core.Entity;
5
import in.shop2020.metamodel.core.EntityState;
6
import in.shop2020.util.Utils;
7
 
8
 
9
import java.util.Collection;
10
import java.util.Map;
11
 
12
import com.sleepycat.collections.TransactionRunner;
13
import com.sleepycat.collections.TransactionWorker;
14
import com.sleepycat.je.DatabaseException;
15
 
1153 rajveer 16
/**
17
 * Entry point for storing everything in berkley database. Singleton class which initialises the berkley database.
18
 * @author rajveer
19
 *
20
 */
1050 rajveer 21
public class StorageManager {
22
 
23
    private final ContentDatabase db;
24
    private final ContentViews views;
25
    private final TransactionRunner txnRunner;
26
    private static StorageManager storageUtils;
27
    private static final String homeDir =  Utils.BERKELEY_DB_PATH;
28
 
29
    static{
30
		synchronized(StorageManager.class){
31
			storageUtils = new StorageManager(homeDir);
32
		}
33
	}
34
 
35
    /**
36
     * Run the sample program.
37
     */
38
    public static void main(String[] args) {
39
 
40
        System.out.println("\nRunning sample: " + StorageManager.class);
41
 
42
 
43
        try {
44
        	Collection<Entity> entities=  StorageManager.getStorageManager().getEntitisByCategory(10002);
45
        	for(Entity entity: entities){
46
        		System.out.println(entity.getID());
47
        	}
48
        	//StorageManager.getStorageManager().printEntitis(10002);
49
        } catch (Exception e) {
50
            // If an exception reaches this point, the last transaction did not
51
            // complete.  If the exception is RunRecoveryException, follow
52
            // the Berkeley DB recovery procedures before running again.
53
            e.printStackTrace();
54
        } finally {
55
        	StorageManager.getStorageManager().close();
56
        }
57
    }
58
 
1153 rajveer 59
    /**
60
     * get storage manager
61
     * @return StorageManager
62
     */
1050 rajveer 63
    public static StorageManager getStorageManager(){
64
		return storageUtils;
65
    }
1153 rajveer 66
 
1050 rajveer 67
    /**
68
     * Open the database and views.
69
     */
70
    private StorageManager(String homeDir)
71
        throws DatabaseException {
72
 
73
    	db = new ContentDatabase(homeDir);
74
        views = new ContentViews(db);
75
        txnRunner = new TransactionRunner(db.getEnvironment());
76
    }
77
 
78
 
79
    /**
80
     * Close the database cleanly.
81
     */
82
    public void close() throws DatabaseException {
83
        db.close();
84
    }
85
 
1153 rajveer 86
    /**
87
     * Get the entities for a given category
88
     * @param categoryId
89
     * @return Collection<Entity>
90
     */
1050 rajveer 91
    @SuppressWarnings("unchecked")
92
	public Collection<Entity> getEntitisByCategory(long categoryId){
93
    	return views.getEntityByCategoryMap().duplicates(categoryId);
94
 
95
    }
96
 
1153 rajveer 97
    /**
98
     * get metadata for all entities
99
     * @return
100
     */
1050 rajveer 101
    @SuppressWarnings("unchecked")
102
	public Map<Long, EntityState> getEntitiesMetadata(){
103
    	return views.getEntityMetadataMap();
104
    }
105
 
1153 rajveer 106
    /**
107
     * Get metadata for an given entity.
108
     * @param entityId
109
     * @return
110
     */
1050 rajveer 111
    public EntityState getEntityMetadata(long entityId){
112
    	return (EntityState) views.getEntityMetadataMap().get(entityId);
113
    }
114
 
1153 rajveer 115
    /**
116
     * update metadata for an given entity
117
     * @param entityMetadata
118
     */
1050 rajveer 119
    @SuppressWarnings("unchecked")
120
	public void updateEntityMetadata(EntityState entityMetadata){
121
    	Map states = views.getEntityMetadataMap();
122
    	states.put(new Long(entityMetadata.getID()), entityMetadata);
123
    }
124
 
125
 
1153 rajveer 126
    /**
127
     * Create an entity in database. This is done in transaction.
128
     * @param entity
129
     * @param entityMetadata
130
     * @throws Exception
131
     */
1050 rajveer 132
    public void createEntity(Entity entity, EntityState entityMetadata) throws Exception{
133
    	txnRunner.run(new StoreEntity(entity, entityMetadata));
134
    }
135
 
1153 rajveer 136
    /**
137
     * Delete an entity from database. This is done in transaction.
138
     * @param entityId
139
     * @throws Exception
140
     */
1050 rajveer 141
    public void deleteEntity(long entityId) throws Exception{
142
    	txnRunner.run(new DeleteEntity(entityId));
143
    }
144
 
1153 rajveer 145
    /**
146
     * Get entity for given id.
147
     * @param entityId
148
     * @return
149
     * @throws Exception
150
     */
1050 rajveer 151
    public Entity getEntity(long entityId) throws Exception{
152
    	return (Entity)views.getEntityMap().get(new Long(entityId));
153
    }
1153 rajveer 154
 
155
    /**
156
     * Update an existing entity
157
     * @param entity
158
     * @throws Exception
159
     */
1050 rajveer 160
    public void updateEntity(Entity entity) throws Exception{
161
    	Map entities = views.getEntityMap();
162
		entities.put(new Long(entity.getID()), entity);
163
    }
164
 
1153 rajveer 165
    /**
166
     * Get all entities.
167
     * @return
168
     * @throws Exception
169
     */
1050 rajveer 170
    @SuppressWarnings("unchecked")
171
	public Map<Long, Entity> getEntities() throws Exception{
172
    	return views.getEntityMap();
173
    }
174
 
1153 rajveer 175
    /**
176
     * Get dataobject. Generic method which will return the object with the name.
177
     * @param dataObjectName
178
     * @return
179
     * @throws Exception
180
     */
1050 rajveer 181
    public Object getDataObject(String dataObjectName) throws Exception{
182
    	return views.getDataObjectMap().get(dataObjectName);
183
    }
184
 
1153 rajveer 185
    /**
186
     * Store data object
187
     * @param dataObjectName
188
     * @param dataObject
189
     * @throws Exception
190
     */
1050 rajveer 191
    public void storeDataObject(String dataObjectName, Object dataObject) throws Exception{
192
    	Map dataObjects = views.getDataObjectMap();
193
    	dataObjects.put(dataObjectName, dataObject);
194
    }
195
 
1153 rajveer 196
    /**
197
     * Delete dataobject
198
     * @param dataObjectName
199
     * @throws Exception
200
     */
1050 rajveer 201
    public void deleteDataObject(String dataObjectName) throws Exception{
202
    	views.getDataObjectMap().remove(dataObjectName);
203
    }
204
 
1153 rajveer 205
    /**
206
     * Class extending TransactionWorker used for deleting entity.
207
     * @author rajveer
208
     *
209
     */
1050 rajveer 210
    private class DeleteEntity implements TransactionWorker{
211
    	long entityId;
212
    	public DeleteEntity(long entityId) {
213
			this.entityId = entityId;
214
		}
215
 
216
		@Override
217
		public void doWork() throws Exception {
218
			views.getEntityMap().remove(new Long(entityId));
219
        	views.getEntityMetadataMap().remove(entityId);
220
		}
221
    }
222
 
1153 rajveer 223
    /**
224
     * Class extending TransactionWorker used for creating entity.
225
     * @author rajveer
226
     *
227
     */
1050 rajveer 228
    private class StoreEntity implements TransactionWorker{
229
    	Entity entity;
230
    	EntityState entityMetadata; 
231
    	public StoreEntity(Entity entity, EntityState entityMetadata) {
232
			this.entity = entity;
233
			this.entityMetadata = entityMetadata;
234
			//System.out.println(entity);
235
		}
236
 
237
		@Override
238
		public void doWork() throws Exception {
239
	    	Map entities = views.getEntityMap();
240
	    	entities.put(new Long(entity.getID()), entity);
241
 
242
	    	Map states = views.getEntityMetadataMap();
243
        	states.put(new Long(entityMetadata.getID()), entityMetadata);
244
		}
245
    }
246
 
247
}