Subversion Repositories SmartDukaan

Rev

Rev 2768 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.storage.bdb;

import in.shop2020.metamodel.core.Entity;

import java.io.File;

import com.sleepycat.bind.serial.ClassCatalog;
import com.sleepycat.bind.serial.SerialSerialKeyCreator;
import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.SecondaryConfig;
import com.sleepycat.je.SecondaryDatabase;


/**
 * database for storing various objects of Content Management System
 * @author rajveer
 *
 */
public class ContentDatabase {

    private static final String CLASS_CATALOG = "java_class_catalog";
    private static final String ENTITY_STORE = "entity_store";
    private static final String DATA_OBJECT_STORE = "data_object_store";
    private static final String ENTITY_METADATA_STORE = "entity_metadata_store";
    private static final String HELPDOC_STORE = "helpdoc_store";
    private static final String SPECIAL_PAGE_STORE = "special_page";

    private static final String ENTITY_CATEGORY_INDEX = "entity_category_index";
    
    private static final String DEFINITION_STORE = "definition_store";
    
    private Environment env;
    private Database entityDb;
    private Database entityMetadataDb;
    private Database helpdocDb;
    private Database specialPageDb;
    private Database dataObjectDb;
    private Database definitionDb;
    private SecondaryDatabase entityByCategoryDb;
    private StoredClassCatalog javaCatalog;

    /**
     * Open all storage containers, indices, and catalogs.
     */
    public ContentDatabase(String homeDirectory)
        throws DatabaseException {
        
        // Open the Berkeley DB environment in transactional mode.
        //
        System.out.println("Opening environment in: " + homeDirectory);
        EnvironmentConfig envConfig = new EnvironmentConfig();
        envConfig.setTransactional(true);
        envConfig.setAllowCreate(true);
        env = new Environment(new File(homeDirectory), envConfig);

        // Set the Berkeley DB config for opening all stores.
        //
        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setTransactional(true);
        dbConfig.setAllowCreate(true);

        // Create the Serial class catalog.  This holds the serialized class
        // format for all database records of serial format.
        //
        Database catalogDb = env.openDatabase(null, CLASS_CATALOG, dbConfig);
        javaCatalog = new StoredClassCatalog(catalogDb);

        // Open the Berkeley DB database for the Entity and Slide
        // stores.  The stores are opened with no duplicate keys allowed.
        //
        
        entityDb = env.openDatabase(null, ENTITY_STORE, dbConfig);

        entityMetadataDb = env.openDatabase(null, ENTITY_METADATA_STORE, dbConfig);
        
        helpdocDb = env.openDatabase(null, HELPDOC_STORE, dbConfig);
        specialPageDb   = env.openDatabase(null, SPECIAL_PAGE_STORE, dbConfig);

        dataObjectDb = env.openDatabase(null, DATA_OBJECT_STORE, dbConfig);
        
        definitionDb = env.openDatabase(null, DEFINITION_STORE, dbConfig);
        
        SecondaryConfig secConfig = new SecondaryConfig();
        secConfig.setTransactional(true);
        secConfig.setAllowCreate(true);
        secConfig.setSortedDuplicates(true);
        secConfig.setKeyCreator(new EntityByCategory(javaCatalog, Long.class, Entity.class, Long.class));
        entityByCategoryDb = env.openSecondaryDatabase(null, ENTITY_CATEGORY_INDEX, entityDb, secConfig);
    }

    /**
     * Private class for making Category ad an index to entity database, which extends SerialSerialKeyCreator.
     * Will create the secondary key once some new entity is added. 
     * @author rajveer
     *
     */
    private static class EntityByCategory extends SerialSerialKeyCreator<Object, Object, Object>{

        public EntityByCategory(ClassCatalog classCatalog, Class primaryKeyClass, Class dataClass, Class indexKeyClass) {
                        super(classCatalog, primaryKeyClass, dataClass, indexKeyClass);
                }

                @Override
                public Object createSecondaryKey(Object primaryKeyInput, Object valueInput) {
            Entity entity = (Entity) valueInput;
            return entity.getCategoryID();
                }
    }
    
    
    /**
     * Return the storage environment for the database.
     */
    public final Environment getEnvironment() {
        return env;
    }

    /**
     * Return the class catalog.
     */
    public final StoredClassCatalog getClassCatalog() {

        return javaCatalog;
    }

    /**
     * Return the definition storage container.
     */
    public final Database getDefinitionDatabase() {
        return definitionDb;
    }
    
    /**
     * @return Return the helpdoc storage container
     */
    public final Database getHelpdocDatabase() {
        return helpdocDb;
    }

    /**
     * @return Returns the special page storage container.
     */
    public final Database getSpecialPageDatabase() {
        return specialPageDb;
    }

    /**
     * @return Return the entity storage container.
     */
    public final Database getEntityDatabase() {

        return entityDb;
    }

    /**
     * 
     * @return
     */
    public final SecondaryDatabase getCategoryByCategoryDatabase() {

        return entityByCategoryDb;
    }

    
    /**
     * @return Return the entity metadata storage container.
     */
    public final Database getEntityMetadataDatabase() {

        return entityMetadataDb;
    }
    
    /**
     * @return Return the objects storage container.
     */
    public final Database getDataObjectDatabase() {

        return dataObjectDb;
    }



    /**
     * Close all databases and the environment.
     */
    public void close()
        throws DatabaseException {

        entityByCategoryDb.close();
        
        entityDb.close();
        entityMetadataDb.close();
        helpdocDb.close();
        specialPageDb.close();
        dataObjectDb.close();
        definitionDb.close();
        
        // And don't forget to close the catalog and the environment.
        javaCatalog.close();
        env.close();
    }
}