001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: TestStore.java,v 1.40.2.3 2008/01/07 15:14:24 cwl Exp $
007: */
008:
009: package com.sleepycat.collections.test;
010:
011: import java.io.IOException;
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: import com.sleepycat.bind.EntityBinding;
016: import com.sleepycat.bind.EntryBinding;
017: import com.sleepycat.bind.RecordNumberBinding;
018: import com.sleepycat.collections.CurrentTransaction;
019: import com.sleepycat.compat.DbCompat;
020: import com.sleepycat.je.Database;
021: import com.sleepycat.je.DatabaseException;
022: import com.sleepycat.je.Environment;
023: import com.sleepycat.je.SecondaryConfig;
024:
025: /**
026: * @author Mark Hayes
027: */
028: class TestStore {
029:
030: static final TestKeyCreator BYTE_EXTRACTOR = new TestKeyCreator(
031: false);
032: static final TestKeyCreator RECNO_EXTRACTOR = new TestKeyCreator(
033: true);
034: static final EntryBinding VALUE_BINDING = new TestDataBinding();
035: static final EntryBinding BYTE_KEY_BINDING = VALUE_BINDING;
036: static final EntryBinding RECNO_KEY_BINDING = new RecordNumberBinding();
037: static final EntityBinding BYTE_ENTITY_BINDING = new TestEntityBinding(
038: false);
039: static final EntityBinding RECNO_ENTITY_BINDING = new TestEntityBinding(
040: true);
041: static final TestKeyAssigner BYTE_KEY_ASSIGNER = new TestKeyAssigner(
042: false);
043: static final TestKeyAssigner RECNO_KEY_ASSIGNER = new TestKeyAssigner(
044: true);
045:
046: static final TestStore BTREE_UNIQ;
047: static final TestStore BTREE_DUP;
048: static final TestStore BTREE_DUPSORT;
049: static final TestStore BTREE_RECNUM;
050: static final TestStore HASH_UNIQ;
051: static final TestStore HASH_DUP;
052: static final TestStore HASH_DUPSORT;
053: static final TestStore QUEUE;
054: static final TestStore RECNO;
055: static final TestStore RECNO_RENUM;
056:
057: static final TestStore[] ALL;
058: static {
059: List list = new ArrayList();
060: SecondaryConfig config;
061:
062: config = new SecondaryConfig();
063: DbCompat.setTypeBtree(config);
064: BTREE_UNIQ = new TestStore("btree-uniq", config);
065: BTREE_UNIQ.indexOf = BTREE_UNIQ;
066: list.add(BTREE_UNIQ);
067:
068: if (DbCompat.INSERTION_ORDERED_DUPLICATES) {
069: config = new SecondaryConfig();
070: DbCompat.setTypeBtree(config);
071: DbCompat.setUnsortedDuplicates(config, true);
072: BTREE_DUP = new TestStore("btree-dup", config);
073: BTREE_DUP.indexOf = null; // indexes must use sorted dups
074: list.add(BTREE_DUP);
075: } else {
076: BTREE_DUP = null;
077: }
078:
079: config = new SecondaryConfig();
080: DbCompat.setTypeBtree(config);
081: DbCompat.setSortedDuplicates(config, true);
082: BTREE_DUPSORT = new TestStore("btree-dupsort", config);
083: BTREE_DUPSORT.indexOf = BTREE_UNIQ;
084: list.add(BTREE_DUPSORT);
085:
086: if (DbCompat.BTREE_RECNUM_METHOD) {
087: config = new SecondaryConfig();
088: DbCompat.setTypeBtree(config);
089: DbCompat.setBtreeRecordNumbers(config, true);
090: BTREE_RECNUM = new TestStore("btree-recnum", config);
091: BTREE_RECNUM.indexOf = BTREE_RECNUM;
092: list.add(BTREE_RECNUM);
093: } else {
094: BTREE_RECNUM = null;
095: }
096:
097: if (DbCompat.HASH_METHOD) {
098: config = new SecondaryConfig();
099: DbCompat.setTypeHash(config);
100: HASH_UNIQ = new TestStore("hash-uniq", config);
101: HASH_UNIQ.indexOf = HASH_UNIQ;
102: list.add(HASH_UNIQ);
103:
104: if (DbCompat.INSERTION_ORDERED_DUPLICATES) {
105: config = new SecondaryConfig();
106: DbCompat.setTypeHash(config);
107: DbCompat.setUnsortedDuplicates(config, true);
108: HASH_DUP = new TestStore("hash-dup", config);
109: HASH_DUP.indexOf = null; // indexes must use sorted dups
110: list.add(HASH_DUP);
111: } else {
112: HASH_DUP = null;
113: }
114:
115: config = new SecondaryConfig();
116: DbCompat.setTypeHash(config);
117: DbCompat.setSortedDuplicates(config, true);
118: HASH_DUPSORT = new TestStore("hash-dupsort", config);
119: HASH_DUPSORT.indexOf = HASH_UNIQ;
120: list.add(HASH_DUPSORT);
121: } else {
122: HASH_UNIQ = null;
123: HASH_DUP = null;
124: HASH_DUPSORT = null;
125: }
126:
127: if (DbCompat.QUEUE_METHOD) {
128: config = new SecondaryConfig();
129: DbCompat.setTypeQueue(config);
130: QUEUE = new TestStore("queue", config);
131: QUEUE.indexOf = QUEUE;
132: list.add(QUEUE);
133: } else {
134: QUEUE = null;
135: }
136:
137: if (DbCompat.RECNO_METHOD) {
138: config = new SecondaryConfig();
139: DbCompat.setTypeRecno(config);
140: RECNO = new TestStore("recno", config);
141: RECNO.indexOf = RECNO;
142: list.add(RECNO);
143:
144: config = new SecondaryConfig();
145: DbCompat.setTypeRecno(config);
146: DbCompat.setRenumbering(config, true);
147: RECNO_RENUM = new TestStore("recno-renum", config);
148: RECNO_RENUM.indexOf = null; // indexes must have stable keys
149: list.add(RECNO_RENUM);
150: } else {
151: RECNO = null;
152: RECNO_RENUM = null;
153: }
154:
155: ALL = new TestStore[list.size()];
156: list.toArray(ALL);
157: }
158:
159: private String name;
160: private SecondaryConfig config;
161: private TestStore indexOf;
162: private boolean isRecNumFormat;
163:
164: private TestStore(String name, SecondaryConfig config) {
165:
166: this .name = name;
167: this .config = config;
168:
169: isRecNumFormat = isQueueOrRecno()
170: || (DbCompat.isTypeBtree(config) && DbCompat
171: .getBtreeRecordNumbers(config));
172: }
173:
174: EntryBinding getValueBinding() {
175:
176: return VALUE_BINDING;
177: }
178:
179: EntryBinding getKeyBinding() {
180:
181: return isRecNumFormat ? RECNO_KEY_BINDING : BYTE_KEY_BINDING;
182: }
183:
184: EntityBinding getEntityBinding() {
185:
186: return isRecNumFormat ? RECNO_ENTITY_BINDING
187: : BYTE_ENTITY_BINDING;
188: }
189:
190: TestKeyAssigner getKeyAssigner() {
191:
192: if (isQueueOrRecno()) {
193: return null;
194: } else {
195: if (isRecNumFormat) {
196: return RECNO_KEY_ASSIGNER;
197: } else {
198: return BYTE_KEY_ASSIGNER;
199: }
200: }
201: }
202:
203: String getName() {
204:
205: return name;
206: }
207:
208: boolean isOrdered() {
209:
210: return !DbCompat.isTypeHash(config);
211: }
212:
213: boolean isQueueOrRecno() {
214:
215: return DbCompat.isTypeQueue(config)
216: || DbCompat.isTypeRecno(config);
217: }
218:
219: boolean areDuplicatesAllowed() {
220:
221: return DbCompat.getSortedDuplicates(config)
222: || DbCompat.getUnsortedDuplicates(config);
223: }
224:
225: boolean hasRecNumAccess() {
226:
227: return isRecNumFormat;
228: }
229:
230: boolean areKeysRenumbered() {
231:
232: return hasRecNumAccess()
233: && (DbCompat.isTypeBtree(config) || DbCompat
234: .getRenumbering(config));
235: }
236:
237: TestStore getIndexOf() {
238:
239: return DbCompat.SECONDARIES ? indexOf : null;
240: }
241:
242: Database open(Environment env, String fileName) throws IOException,
243: DatabaseException {
244:
245: int fixedLen = (isQueueOrRecno() ? 1 : 0);
246: return openDb(env, fileName, fixedLen, null);
247: }
248:
249: Database openIndex(Database primary, String fileName)
250: throws IOException, DatabaseException {
251:
252: int fixedLen = (isQueueOrRecno() ? 4 : 0);
253: config.setKeyCreator(isRecNumFormat ? RECNO_EXTRACTOR
254: : BYTE_EXTRACTOR);
255: Environment env = primary.getEnvironment();
256: return openDb(env, fileName, fixedLen, primary);
257: }
258:
259: private Database openDb(Environment env, String fileName,
260: int fixedLen, Database primary) throws IOException,
261: DatabaseException {
262:
263: if (fixedLen > 0) {
264: DbCompat.setRecordLength(config, fixedLen);
265: DbCompat.setRecordPad(config, 0);
266: } else {
267: DbCompat.setRecordLength(config, 0);
268: }
269: config.setAllowCreate(true);
270: DbCompat.setReadUncommitted(config, true);
271: config
272: .setTransactional(CurrentTransaction.getInstance(env) != null);
273: if (primary != null) {
274: return DbCompat.openSecondaryDatabase(env, null, fileName,
275: null, primary, config);
276: } else {
277: return DbCompat.openDatabase(env, null, fileName, null,
278: config);
279: }
280: }
281: }
|