Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
1050 rajveer 1
package in.shop2020.storage.bdb;
2
 
3
import in.shop2020.metamodel.core.Entity;
4
 
5
import java.io.File;
6
 
7
import com.sleepycat.bind.serial.ClassCatalog;
8
import com.sleepycat.bind.serial.SerialSerialKeyCreator;
9
import com.sleepycat.bind.serial.StoredClassCatalog;
10
import com.sleepycat.je.Database;
11
import com.sleepycat.je.DatabaseConfig;
12
import com.sleepycat.je.DatabaseException;
13
import com.sleepycat.je.Environment;
14
import com.sleepycat.je.EnvironmentConfig;
15
import com.sleepycat.je.SecondaryConfig;
16
import com.sleepycat.je.SecondaryDatabase;
17
 
1153 rajveer 18
 
19
/**
20
 * database for storing various objects of Content Management System
21
 * @author rajveer
22
 *
23
 */
1050 rajveer 24
public class ContentDatabase {
25
 
26
    private static final String CLASS_CATALOG = "java_class_catalog";
27
    private static final String ENTITY_STORE = "entity_store";
28
    private static final String DATA_OBJECT_STORE = "data_object_store";
29
    private static final String ENTITY_METADATA_STORE = "entity_metadata_store";
2275 rajveer 30
    private static final String HELPDOC_STORE = "helpdoc_store";
1050 rajveer 31
 
32
    private static final String ENTITY_CATEGORY_INDEX = "entity_category_index";
33
 
34
    private static final String DEFINITION_STORE = "definition_store";
35
 
36
    private Environment env;
37
    private Database entityDb;
38
    private Database entityMetadataDb;
2275 rajveer 39
    private Database helpdocDb;
1050 rajveer 40
    private Database dataObjectDb;
41
    private Database definitionDb;
42
    private SecondaryDatabase entityByCategoryDb;
43
    private StoredClassCatalog javaCatalog;
44
 
45
    /**
46
     * Open all storage containers, indices, and catalogs.
47
     */
48
    public ContentDatabase(String homeDirectory)
49
        throws DatabaseException {
1153 rajveer 50
 
1050 rajveer 51
        // Open the Berkeley DB environment in transactional mode.
52
        //
53
        System.out.println("Opening environment in: " + homeDirectory);
54
        EnvironmentConfig envConfig = new EnvironmentConfig();
55
        envConfig.setTransactional(true);
56
        envConfig.setAllowCreate(true);
57
        env = new Environment(new File(homeDirectory), envConfig);
58
 
59
        // Set the Berkeley DB config for opening all stores.
60
        //
61
        DatabaseConfig dbConfig = new DatabaseConfig();
62
        dbConfig.setTransactional(true);
63
        dbConfig.setAllowCreate(true);
64
 
65
        // Create the Serial class catalog.  This holds the serialized class
66
        // format for all database records of serial format.
67
        //
68
        Database catalogDb = env.openDatabase(null, CLASS_CATALOG, dbConfig);
69
        javaCatalog = new StoredClassCatalog(catalogDb);
70
 
71
        // Open the Berkeley DB database for the Entity and Slide
72
        // stores.  The stores are opened with no duplicate keys allowed.
73
        //
74
 
75
        entityDb = env.openDatabase(null, ENTITY_STORE, dbConfig);
76
 
77
        entityMetadataDb = env.openDatabase(null, ENTITY_METADATA_STORE, dbConfig);
78
 
2275 rajveer 79
        helpdocDb = env.openDatabase(null, HELPDOC_STORE, dbConfig);
80
 
1050 rajveer 81
        dataObjectDb = env.openDatabase(null, DATA_OBJECT_STORE, dbConfig);
82
 
83
        definitionDb = env.openDatabase(null, DEFINITION_STORE, dbConfig);
84
 
85
        SecondaryConfig secConfig = new SecondaryConfig();
86
        secConfig.setTransactional(true);
87
        secConfig.setAllowCreate(true);
88
        secConfig.setSortedDuplicates(true);
89
        secConfig.setKeyCreator(new EntityByCategory(javaCatalog, Long.class, Entity.class, Long.class));
90
        entityByCategoryDb = env.openSecondaryDatabase(null, ENTITY_CATEGORY_INDEX, entityDb, secConfig);
91
    }
92
 
1153 rajveer 93
    /**
94
     * Private class for making Category ad an index to entity database, which extends SerialSerialKeyCreator.
95
     * Will create the secondary key once some new entity is added. 
96
     * @author rajveer
97
     *
98
     */
1050 rajveer 99
    private static class EntityByCategory extends SerialSerialKeyCreator<Object, Object, Object>{
100
 
101
    	public EntityByCategory(ClassCatalog classCatalog, Class primaryKeyClass, Class dataClass, Class indexKeyClass) {
102
			super(classCatalog, primaryKeyClass, dataClass, indexKeyClass);
103
		}
104
 
105
		@Override
106
		public Object createSecondaryKey(Object primaryKeyInput, Object valueInput) {
107
            Entity entity = (Entity) valueInput;
108
            return entity.getCategoryID();
109
		}
110
    }
111
 
112
 
113
    /**
114
     * Return the storage environment for the database.
115
     */
116
    public final Environment getEnvironment() {
117
        return env;
118
    }
119
 
120
    /**
121
     * Return the class catalog.
122
     */
123
    public final StoredClassCatalog getClassCatalog() {
124
 
125
        return javaCatalog;
126
    }
127
 
128
    /**
1153 rajveer 129
     * Return the definition storage container.
1050 rajveer 130
     */
131
    public final Database getDefinitionDatabase() {
132
 
133
        return definitionDb;
134
    }
135
 
136
    /**
1153 rajveer 137
     * @return Return the entity storage container.
1050 rajveer 138
     */
2275 rajveer 139
    public final Database getHelpdocDatabase() {
140
 
141
        return helpdocDb;
142
    }
143
 
144
    /**
145
     * @return Return the entity storage container.
146
     */
1050 rajveer 147
    public final Database getEntityDatabase() {
148
 
149
        return entityDb;
150
    }
151
 
1153 rajveer 152
    /**
153
     * 
154
     * @return
155
     */
1050 rajveer 156
    public final SecondaryDatabase getCategoryByCategoryDatabase() {
157
 
158
        return entityByCategoryDb;
159
    }
160
 
161
 
162
    /**
1153 rajveer 163
     * @return Return the entity metadata storage container.
1050 rajveer 164
     */
165
    public final Database getEntityMetadataDatabase() {
166
 
167
        return entityMetadataDb;
168
    }
169
 
170
    /**
1153 rajveer 171
     * @return Return the objects storage container.
1050 rajveer 172
     */
173
    public final Database getDataObjectDatabase() {
174
 
175
        return dataObjectDb;
176
    }
177
 
178
 
179
 
180
    /**
181
     * Close all databases and the environment.
182
     */
183
    public void close()
184
        throws DatabaseException {
185
 
186
    	entityByCategoryDb.close();
187
 
188
        entityDb.close();
189
        entityMetadataDb.close();
2275 rajveer 190
        helpdocDb.close();
1050 rajveer 191
        dataObjectDb.close();
192
        definitionDb.close();
193
 
194
        // And don't forget to close the catalog and the environment.
195
        javaCatalog.close();
196
        env.close();
197
    }
198
}