00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package com.sleepycat.collections.test;
00011
00012 import java.io.IOException;
00013 import java.util.ArrayList;
00014 import java.util.List;
00015
00016 import com.sleepycat.bind.EntityBinding;
00017 import com.sleepycat.bind.EntryBinding;
00018 import com.sleepycat.bind.RecordNumberBinding;
00019 import com.sleepycat.collections.CurrentTransaction;
00020 import com.sleepycat.compat.DbCompat;
00021 import com.sleepycat.db.Database;
00022 import com.sleepycat.db.DatabaseException;
00023 import com.sleepycat.db.Environment;
00024 import com.sleepycat.db.SecondaryConfig;
00025
00029 class TestStore {
00030
00031 static final TestKeyCreator BYTE_EXTRACTOR = new TestKeyCreator(false);
00032 static final TestKeyCreator RECNO_EXTRACTOR = new TestKeyCreator(true);
00033 static final EntryBinding VALUE_BINDING = new TestDataBinding();
00034 static final EntryBinding BYTE_KEY_BINDING = VALUE_BINDING;
00035 static final EntryBinding RECNO_KEY_BINDING = new RecordNumberBinding();
00036 static final EntityBinding BYTE_ENTITY_BINDING =
00037 new TestEntityBinding(false);
00038 static final EntityBinding RECNO_ENTITY_BINDING =
00039 new TestEntityBinding(true);
00040 static final TestKeyAssigner BYTE_KEY_ASSIGNER =
00041 new TestKeyAssigner(false);
00042 static final TestKeyAssigner RECNO_KEY_ASSIGNER =
00043 new TestKeyAssigner(true);
00044
00045 static final TestStore BTREE_UNIQ;
00046 static final TestStore BTREE_DUP;
00047 static final TestStore BTREE_DUPSORT;
00048 static final TestStore BTREE_RECNUM;
00049 static final TestStore HASH_UNIQ;
00050 static final TestStore HASH_DUP;
00051 static final TestStore HASH_DUPSORT;
00052 static final TestStore QUEUE;
00053 static final TestStore RECNO;
00054 static final TestStore RECNO_RENUM;
00055
00056 static final TestStore[] ALL;
00057 static {
00058 List list = new ArrayList();
00059 SecondaryConfig config;
00060
00061 config = new SecondaryConfig();
00062 DbCompat.setTypeBtree(config);
00063 BTREE_UNIQ = new TestStore("btree-uniq", config);
00064 BTREE_UNIQ.indexOf = BTREE_UNIQ;
00065 list.add(BTREE_UNIQ);
00066
00067 if (DbCompat.INSERTION_ORDERED_DUPLICATES) {
00068 config = new SecondaryConfig();
00069 DbCompat.setTypeBtree(config);
00070 DbCompat.setUnsortedDuplicates(config, true);
00071 BTREE_DUP = new TestStore("btree-dup", config);
00072 BTREE_DUP.indexOf = null;
00073 list.add(BTREE_DUP);
00074 } else {
00075 BTREE_DUP = null;
00076 }
00077
00078 config = new SecondaryConfig();
00079 DbCompat.setTypeBtree(config);
00080 DbCompat.setSortedDuplicates(config, true);
00081 BTREE_DUPSORT = new TestStore("btree-dupsort", config);
00082 BTREE_DUPSORT.indexOf = BTREE_UNIQ;
00083 list.add(BTREE_DUPSORT);
00084
00085 if (DbCompat.BTREE_RECNUM_METHOD) {
00086 config = new SecondaryConfig();
00087 DbCompat.setTypeBtree(config);
00088 DbCompat.setBtreeRecordNumbers(config, true);
00089 BTREE_RECNUM = new TestStore("btree-recnum", config);
00090 BTREE_RECNUM.indexOf = BTREE_RECNUM;
00091 list.add(BTREE_RECNUM);
00092 } else {
00093 BTREE_RECNUM = null;
00094 }
00095
00096 if (DbCompat.HASH_METHOD) {
00097 config = new SecondaryConfig();
00098 DbCompat.setTypeHash(config);
00099 HASH_UNIQ = new TestStore("hash-uniq", config);
00100 HASH_UNIQ.indexOf = HASH_UNIQ;
00101 list.add(HASH_UNIQ);
00102
00103 if (DbCompat.INSERTION_ORDERED_DUPLICATES) {
00104 config = new SecondaryConfig();
00105 DbCompat.setTypeHash(config);
00106 DbCompat.setUnsortedDuplicates(config, true);
00107 HASH_DUP = new TestStore("hash-dup", config);
00108 HASH_DUP.indexOf = null;
00109 list.add(HASH_DUP);
00110 } else {
00111 HASH_DUP = null;
00112 }
00113
00114 config = new SecondaryConfig();
00115 DbCompat.setTypeHash(config);
00116 DbCompat.setSortedDuplicates(config, true);
00117 HASH_DUPSORT = new TestStore("hash-dupsort", config);
00118 HASH_DUPSORT.indexOf = HASH_UNIQ;
00119 list.add(HASH_DUPSORT);
00120 } else {
00121 HASH_UNIQ = null;
00122 HASH_DUP = null;
00123 HASH_DUPSORT = null;
00124 }
00125
00126 if (DbCompat.QUEUE_METHOD) {
00127 config = new SecondaryConfig();
00128 DbCompat.setTypeQueue(config);
00129 QUEUE = new TestStore("queue", config);
00130 QUEUE.indexOf = QUEUE;
00131 list.add(QUEUE);
00132 } else {
00133 QUEUE = null;
00134 }
00135
00136 if (DbCompat.RECNO_METHOD) {
00137 config = new SecondaryConfig();
00138 DbCompat.setTypeRecno(config);
00139 RECNO = new TestStore("recno", config);
00140 RECNO.indexOf = RECNO;
00141 list.add(RECNO);
00142
00143 config = new SecondaryConfig();
00144 DbCompat.setTypeRecno(config);
00145 DbCompat.setRenumbering(config, true);
00146 RECNO_RENUM = new TestStore("recno-renum", config);
00147 RECNO_RENUM.indexOf = null;
00148 list.add(RECNO_RENUM);
00149 } else {
00150 RECNO = null;
00151 RECNO_RENUM = null;
00152 }
00153
00154 ALL = new TestStore[list.size()];
00155 list.toArray(ALL);
00156 }
00157
00158 private String name;
00159 private SecondaryConfig config;
00160 private TestStore indexOf;
00161 private boolean isRecNumFormat;
00162
00163 private TestStore(String name, SecondaryConfig config) {
00164
00165 this.name = name;
00166 this.config = config;
00167
00168 isRecNumFormat = isQueueOrRecno() ||
00169 (DbCompat.isTypeBtree(config) &&
00170 DbCompat.getBtreeRecordNumbers(config));
00171 }
00172
00173 EntryBinding getValueBinding() {
00174
00175 return VALUE_BINDING;
00176 }
00177
00178 EntryBinding getKeyBinding() {
00179
00180 return isRecNumFormat ? RECNO_KEY_BINDING : BYTE_KEY_BINDING;
00181 }
00182
00183 EntityBinding getEntityBinding() {
00184
00185 return isRecNumFormat ? RECNO_ENTITY_BINDING : BYTE_ENTITY_BINDING;
00186 }
00187
00188 TestKeyAssigner getKeyAssigner() {
00189
00190 if (isQueueOrRecno()) {
00191 return null;
00192 } else {
00193 if (isRecNumFormat) {
00194 return RECNO_KEY_ASSIGNER;
00195 } else {
00196 return BYTE_KEY_ASSIGNER;
00197 }
00198 }
00199 }
00200
00201 String getName() {
00202
00203 return name;
00204 }
00205
00206 boolean isOrdered() {
00207
00208 return !DbCompat.isTypeHash(config);
00209 }
00210
00211 boolean isQueueOrRecno() {
00212
00213 return DbCompat.isTypeQueue(config) || DbCompat.isTypeRecno(config);
00214 }
00215
00216 boolean areDuplicatesAllowed() {
00217
00218 return DbCompat.getSortedDuplicates(config) ||
00219 DbCompat.getUnsortedDuplicates(config);
00220 }
00221
00222 boolean hasRecNumAccess() {
00223
00224 return isRecNumFormat;
00225 }
00226
00227 boolean areKeysRenumbered() {
00228
00229 return hasRecNumAccess() &&
00230 (DbCompat.isTypeBtree(config) ||
00231 DbCompat.getRenumbering(config));
00232 }
00233
00234 TestStore getIndexOf() {
00235
00236 return DbCompat.SECONDARIES ? indexOf : null;
00237 }
00238
00239 Database open(Environment env, String fileName)
00240 throws IOException, DatabaseException {
00241
00242 int fixedLen = (isQueueOrRecno() ? 1 : 0);
00243 return openDb(env, fileName, fixedLen, null);
00244 }
00245
00246 Database openIndex(Database primary, String fileName)
00247 throws IOException, DatabaseException {
00248
00249 int fixedLen = (isQueueOrRecno() ? 4 : 0);
00250 config.setKeyCreator(isRecNumFormat ? RECNO_EXTRACTOR
00251 : BYTE_EXTRACTOR);
00252 Environment env = primary.getEnvironment();
00253 return openDb(env, fileName, fixedLen, primary);
00254 }
00255
00256 private Database openDb(Environment env, String fileName, int fixedLen,
00257 Database primary)
00258 throws IOException, DatabaseException {
00259
00260 if (fixedLen > 0) {
00261 DbCompat.setRecordLength(config, fixedLen);
00262 DbCompat.setRecordPad(config, 0);
00263 } else {
00264 DbCompat.setRecordLength(config, 0);
00265 }
00266 config.setAllowCreate(true);
00267 DbCompat.setReadUncommitted(config, true);
00268 config.setTransactional(CurrentTransaction.getInstance(env) != null);
00269 if (primary != null) {
00270 return DbCompat.openSecondaryDatabase(env, null,
00271 fileName, null,
00272 primary, config);
00273 } else {
00274 return DbCompat.openDatabase(env, null,
00275 fileName, null,
00276 config);
00277 }
00278 }
00279 }