Subversion Repositories SmartDukaan

Rev

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