Rev 1050 | Rev 2275 | 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;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();}}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();}@SuppressWarnings("unchecked")public Collection<Entity> getEntitisByCategory(long categoryId){return views.getEntityByCategoryMap().duplicates(categoryId);}@SuppressWarnings("unchecked")public Map<Long, EntityState> getEntitiesMetadata(){return views.getEntityMetadataMap();}public EntityState getEntityMetadata(long entityId){return (EntityState) views.getEntityMetadataMap().get(entityId);}@SuppressWarnings("unchecked")public void updateEntityMetadata(EntityState entityMetadata){Map states = views.getEntityMetadataMap();states.put(new Long(entityMetadata.getID()), entityMetadata);}public void createEntity(Entity entity, EntityState entityMetadata) throws Exception{txnRunner.run(new StoreEntity(entity, entityMetadata));}public void deleteEntity(long entityId) throws Exception{txnRunner.run(new DeleteEntity(entityId));}public Entity getEntity(long entityId) throws Exception{return (Entity)views.getEntityMap().get(new Long(entityId));}public void updateEntity(Entity entity) throws Exception{Map entities = views.getEntityMap();entities.put(new Long(entity.getID()), entity);}@SuppressWarnings("unchecked")public Map<Long, Entity> getEntities() throws Exception{return views.getEntityMap();}public Object getDataObject(String dataObjectName) throws Exception{return views.getDataObjectMap().get(dataObjectName);}public void storeDataObject(String dataObjectName, Object dataObject) throws Exception{Map dataObjects = views.getDataObjectMap();dataObjects.put(dataObjectName, dataObject);}public void deleteDataObject(String dataObjectName) throws Exception{views.getDataObjectMap().remove(dataObjectName);}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);}}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);}}}