Subversion Repositories SmartDukaan

Rev

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