Rev 1061 | Rev 2768 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.storage.bdb;import in.shop2020.metamodel.core.Entity;import in.shop2020.metamodel.core.EntityState;import in.shop2020.util.Utils;import java.util.Collection;import java.util.Map;import com.sleepycat.collections.TransactionRunner;import com.sleepycat.collections.TransactionWorker;import com.sleepycat.je.DatabaseException;/*** Entry point for storing everything in berkley database. Singleton class which initialises the berkley database.* @author rajveer**/public class StorageManager {private final ContentDatabase db;private final ContentViews views;private final TransactionRunner txnRunner;private static StorageManager storageUtils;private static final String homeDir = Utils.BERKELEY_DB_PATH;static{synchronized(StorageManager.class){storageUtils = new StorageManager(homeDir);}}/*** Run the sample program.*/public static void main(String[] args) {System.out.println("\nRunning sample: " + StorageManager.class);try {Collection<Entity> entities= StorageManager.getStorageManager().getEntitisByCategory(10002);for(Entity entity: entities){System.out.println(entity.getID());}//StorageManager.getStorageManager().printEntitis(10002);} catch (Exception e) {// If an exception reaches this point, the last transaction did not// complete. If the exception is RunRecoveryException, follow// the Berkeley DB recovery procedures before running again.e.printStackTrace();} finally {StorageManager.getStorageManager().close();}}/*** get storage manager* @return StorageManager*/public static StorageManager getStorageManager(){return storageUtils;}/*** Open the database and views.*/private StorageManager(String homeDir)throws DatabaseException {db = new ContentDatabase(homeDir);views = new ContentViews(db);txnRunner = new TransactionRunner(db.getEnvironment());}/*** Close the database cleanly.*/public void close() throws DatabaseException {db.close();}/*** Get the entities for a given category* @param categoryId* @return Collection<Entity>*/@SuppressWarnings("unchecked")public Collection<Entity> getEntitisByCategory(long categoryId){return views.getEntityByCategoryMap().duplicates(categoryId);}/*** get metadata for all entities* @return*/@SuppressWarnings("unchecked")public Map<Long, EntityState> getEntitiesMetadata(){return views.getEntityMetadataMap();}/*** Get metadata for an given entity.* @param entityId* @return*/public EntityState getEntityMetadata(long entityId){return (EntityState) views.getEntityMetadataMap().get(entityId);}/*** update metadata for an given entity* @param entityMetadata*/@SuppressWarnings("unchecked")public void updateEntityMetadata(EntityState entityMetadata){Map states = views.getEntityMetadataMap();states.put(new Long(entityMetadata.getID()), entityMetadata);}/*** Create an entity in database. This is done in transaction.* @param entity* @param entityMetadata* @throws Exception*/public void createEntity(Entity entity, EntityState entityMetadata) throws Exception{txnRunner.run(new StoreEntity(entity, entityMetadata));}/*** Delete an entity from database. This is done in transaction.* @param entityId* @throws Exception*/public void deleteEntity(long entityId) throws Exception{txnRunner.run(new DeleteEntity(entityId));}/*** Get entity for given id.* @param entityId* @return* @throws Exception*/public Entity getEntity(long entityId) throws Exception{return (Entity)views.getEntityMap().get(new Long(entityId));}/*** Update an existing entity* @param entity* @throws Exception*/public void updateEntity(Entity entity) throws Exception{Map entities = views.getEntityMap();entities.put(new Long(entity.getID()), entity);}/*** Get all entities.* @return* @throws Exception*/@SuppressWarnings("unchecked")public Map<Long, Entity> getEntities() throws Exception{return views.getEntityMap();}/*** Get dataobject. Generic method which will return the object with the name.* @param dataObjectName* @return* @throws Exception*/public Object getDataObject(String dataObjectName) throws Exception{return views.getDataObjectMap().get(dataObjectName);}/*** Store data object* @param dataObjectName* @param dataObject* @throws Exception*/public void storeDataObject(String dataObjectName, Object dataObject) throws Exception{Map dataObjects = views.getDataObjectMap();dataObjects.put(dataObjectName, dataObject);}/*** Delete dataobject* @param dataObjectName* @throws Exception*/public void deleteDataObject(String dataObjectName) throws Exception{views.getDataObjectMap().remove(dataObjectName);}/*** Class extending TransactionWorker used for deleting entity.* @author rajveer**/private class DeleteEntity implements TransactionWorker{long entityId;public DeleteEntity(long entityId) {this.entityId = entityId;}@Overridepublic void doWork() throws Exception {views.getEntityMap().remove(new Long(entityId));views.getEntityMetadataMap().remove(entityId);}}/*** Class extending TransactionWorker used for creating entity.* @author rajveer**/private class StoreEntity implements TransactionWorker{Entity entity;EntityState entityMetadata;public StoreEntity(Entity entity, EntityState entityMetadata) {this.entity = entity;this.entityMetadata = entityMetadata;//System.out.println(entity);}@Overridepublic void doWork() throws Exception {Map entities = views.getEntityMap();entities.put(new Long(entity.getID()), entity);Map states = views.getEntityMetadataMap();states.put(new Long(entityMetadata.getID()), entityMetadata);}}}