Subversion Repositories SmartDukaan

Rev

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