Subversion Repositories SmartDukaan

Rev

Rev 1061 | Rev 2275 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1061 Rev 1153
Line 11... Line 11...
11
 
11
 
12
import com.sleepycat.collections.TransactionRunner;
12
import com.sleepycat.collections.TransactionRunner;
13
import com.sleepycat.collections.TransactionWorker;
13
import com.sleepycat.collections.TransactionWorker;
14
import com.sleepycat.je.DatabaseException;
14
import com.sleepycat.je.DatabaseException;
15
 
15
 
-
 
16
/**
-
 
17
 * Entry point for storing everything in berkley database. Singleton class which initialises the berkley database.
-
 
18
 * @author rajveer
-
 
19
 *
-
 
20
 */
16
public class StorageManager {
21
public class StorageManager {
17
 
22
 
18
    private final ContentDatabase db;
23
    private final ContentDatabase db;
19
    private final ContentViews views;
24
    private final ContentViews views;
20
    private final TransactionRunner txnRunner;
25
    private final TransactionRunner txnRunner;
Line 49... Line 54...
49
        } finally {
54
        } finally {
50
        	StorageManager.getStorageManager().close();
55
        	StorageManager.getStorageManager().close();
51
        }
56
        }
52
    }
57
    }
53
 
58
 
-
 
59
    /**
-
 
60
     * get storage manager
-
 
61
     * @return StorageManager
-
 
62
     */
54
    public static StorageManager getStorageManager(){
63
    public static StorageManager getStorageManager(){
55
		return storageUtils;
64
		return storageUtils;
56
    }
65
    }
-
 
66
    
57
    /**
67
    /**
58
     * Open the database and views.
68
     * Open the database and views.
59
     */
69
     */
60
    private StorageManager(String homeDir)
70
    private StorageManager(String homeDir)
61
        throws DatabaseException {
71
        throws DatabaseException {
Line 71... Line 81...
71
     */
81
     */
72
    public void close() throws DatabaseException {
82
    public void close() throws DatabaseException {
73
        db.close();
83
        db.close();
74
    }
84
    }
75
 
85
 
-
 
86
    /**
-
 
87
     * Get the entities for a given category
-
 
88
     * @param categoryId
-
 
89
     * @return Collection<Entity>
76
    
90
     */
77
    @SuppressWarnings("unchecked")
91
    @SuppressWarnings("unchecked")
78
	public Collection<Entity> getEntitisByCategory(long categoryId){
92
	public Collection<Entity> getEntitisByCategory(long categoryId){
79
    	return views.getEntityByCategoryMap().duplicates(categoryId);
93
    	return views.getEntityByCategoryMap().duplicates(categoryId);
80
    	
94
    	
81
    }
95
    }
82
    
96
    
-
 
97
    /**
-
 
98
     * get metadata for all entities
-
 
99
     * @return
-
 
100
     */
83
    @SuppressWarnings("unchecked")
101
    @SuppressWarnings("unchecked")
84
	public Map<Long, EntityState> getEntitiesMetadata(){
102
	public Map<Long, EntityState> getEntitiesMetadata(){
85
    	return views.getEntityMetadataMap();
103
    	return views.getEntityMetadataMap();
86
    }
104
    }
87
    
105
    
-
 
106
    /**
-
 
107
     * Get metadata for an given entity.
-
 
108
     * @param entityId
-
 
109
     * @return
-
 
110
     */
88
    public EntityState getEntityMetadata(long entityId){
111
    public EntityState getEntityMetadata(long entityId){
89
    	return (EntityState) views.getEntityMetadataMap().get(entityId);
112
    	return (EntityState) views.getEntityMetadataMap().get(entityId);
90
    }
113
    }
91
 
114
 
-
 
115
    /**
-
 
116
     * update metadata for an given entity
-
 
117
     * @param entityMetadata
-
 
118
     */
92
    @SuppressWarnings("unchecked")
119
    @SuppressWarnings("unchecked")
93
	public void updateEntityMetadata(EntityState entityMetadata){
120
	public void updateEntityMetadata(EntityState entityMetadata){
94
    	Map states = views.getEntityMetadataMap();
121
    	Map states = views.getEntityMetadataMap();
95
    	states.put(new Long(entityMetadata.getID()), entityMetadata);
122
    	states.put(new Long(entityMetadata.getID()), entityMetadata);
96
    }
123
    }
97
 
124
 
98
    
125
    
-
 
126
    /**
-
 
127
     * Create an entity in database. This is done in transaction.
-
 
128
     * @param entity
-
 
129
     * @param entityMetadata
-
 
130
     * @throws Exception
-
 
131
     */
99
    public void createEntity(Entity entity, EntityState entityMetadata) throws Exception{
132
    public void createEntity(Entity entity, EntityState entityMetadata) throws Exception{
100
    	txnRunner.run(new StoreEntity(entity, entityMetadata));
133
    	txnRunner.run(new StoreEntity(entity, entityMetadata));
101
    }
134
    }
102
    
135
    
-
 
136
    /**
-
 
137
     * Delete an entity from database. This is done in transaction.
-
 
138
     * @param entityId
-
 
139
     * @throws Exception
-
 
140
     */
103
    public void deleteEntity(long entityId) throws Exception{
141
    public void deleteEntity(long entityId) throws Exception{
104
    	txnRunner.run(new DeleteEntity(entityId));
142
    	txnRunner.run(new DeleteEntity(entityId));
105
    }
143
    }
106
 
144
 
-
 
145
    /**
-
 
146
     * Get entity for given id.
-
 
147
     * @param entityId
-
 
148
     * @return
-
 
149
     * @throws Exception
-
 
150
     */
107
    public Entity getEntity(long entityId) throws Exception{
151
    public Entity getEntity(long entityId) throws Exception{
108
    	return (Entity)views.getEntityMap().get(new Long(entityId));
152
    	return (Entity)views.getEntityMap().get(new Long(entityId));
109
    }
153
    }
-
 
154
    
-
 
155
    /**
-
 
156
     * Update an existing entity
-
 
157
     * @param entity
-
 
158
     * @throws Exception
-
 
159
     */
110
    public void updateEntity(Entity entity) throws Exception{
160
    public void updateEntity(Entity entity) throws Exception{
111
    	Map entities = views.getEntityMap();
161
    	Map entities = views.getEntityMap();
112
		entities.put(new Long(entity.getID()), entity);
162
		entities.put(new Long(entity.getID()), entity);
113
    }
163
    }
114
	
164
	
-
 
165
    /**
-
 
166
     * Get all entities.
-
 
167
     * @return
-
 
168
     * @throws Exception
-
 
169
     */
115
    @SuppressWarnings("unchecked")
170
    @SuppressWarnings("unchecked")
116
	public Map<Long, Entity> getEntities() throws Exception{
171
	public Map<Long, Entity> getEntities() throws Exception{
117
    	return views.getEntityMap();
172
    	return views.getEntityMap();
118
    }
173
    }
119
 
174
 
-
 
175
    /**
-
 
176
     * Get dataobject. Generic method which will return the object with the name.
-
 
177
     * @param dataObjectName
-
 
178
     * @return
-
 
179
     * @throws Exception
-
 
180
     */
120
    public Object getDataObject(String dataObjectName) throws Exception{
181
    public Object getDataObject(String dataObjectName) throws Exception{
121
    	return views.getDataObjectMap().get(dataObjectName);
182
    	return views.getDataObjectMap().get(dataObjectName);
122
    }
183
    }
123
    
184
    
-
 
185
    /**
-
 
186
     * Store data object
-
 
187
     * @param dataObjectName
-
 
188
     * @param dataObject
-
 
189
     * @throws Exception
-
 
190
     */
124
    public void storeDataObject(String dataObjectName, Object dataObject) throws Exception{
191
    public void storeDataObject(String dataObjectName, Object dataObject) throws Exception{
125
    	Map dataObjects = views.getDataObjectMap();
192
    	Map dataObjects = views.getDataObjectMap();
126
    	dataObjects.put(dataObjectName, dataObject);
193
    	dataObjects.put(dataObjectName, dataObject);
127
    }
194
    }
128
    
195
    
-
 
196
    /**
-
 
197
     * Delete dataobject
-
 
198
     * @param dataObjectName
-
 
199
     * @throws Exception
-
 
200
     */
129
    public void deleteDataObject(String dataObjectName) throws Exception{
201
    public void deleteDataObject(String dataObjectName) throws Exception{
130
    	views.getDataObjectMap().remove(dataObjectName);
202
    	views.getDataObjectMap().remove(dataObjectName);
131
    }
203
    }
132
 
204
 
-
 
205
    /**
-
 
206
     * Class extending TransactionWorker used for deleting entity.
-
 
207
     * @author rajveer
-
 
208
     *
-
 
209
     */
133
    private class DeleteEntity implements TransactionWorker{
210
    private class DeleteEntity implements TransactionWorker{
134
    	long entityId;
211
    	long entityId;
135
    	public DeleteEntity(long entityId) {
212
    	public DeleteEntity(long entityId) {
136
			this.entityId = entityId;
213
			this.entityId = entityId;
137
		}
214
		}
Line 141... Line 218...
141
			views.getEntityMap().remove(new Long(entityId));
218
			views.getEntityMap().remove(new Long(entityId));
142
        	views.getEntityMetadataMap().remove(entityId);
219
        	views.getEntityMetadataMap().remove(entityId);
143
		}
220
		}
144
    }
221
    }
145
 
222
 
-
 
223
    /**
-
 
224
     * Class extending TransactionWorker used for creating entity.
-
 
225
     * @author rajveer
146
    
226
     *
-
 
227
     */
147
    private class StoreEntity implements TransactionWorker{
228
    private class StoreEntity implements TransactionWorker{
148
    	Entity entity;
229
    	Entity entity;
149
    	EntityState entityMetadata; 
230
    	EntityState entityMetadata; 
150
    	public StoreEntity(Entity entity, EntityState entityMetadata) {
231
    	public StoreEntity(Entity entity, EntityState entityMetadata) {
151
			this.entity = entity;
232
			this.entity = entity;