KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > DatabaseConfig


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: DatabaseConfig.java,v 1.34 2006/10/30 21:14:11 bostic Exp $
7  */

8
9 package com.sleepycat.je;
10
11 import java.util.Arrays JavaDoc;
12 import java.util.Comparator JavaDoc;
13
14 import com.sleepycat.je.dbi.DatabaseImpl;
15
16 /**
17  * Javadoc for this public class is generated
18  * via the doc templates in the doc_src directory.
19  */

20 public class DatabaseConfig implements Cloneable JavaDoc {
21
22     /*
23      * An instance created using the default constructor is initialized with
24      * the system's default settings.
25      */

26     public static final DatabaseConfig DEFAULT = new DatabaseConfig();
27
28     private boolean allowCreate = false;
29     private boolean exclusiveCreate = false;
30     private boolean transactional = false;
31     private boolean readOnly = false;
32     private boolean duplicatesAllowed = false;
33     private boolean deferredWrite = false;
34
35     /* User defined Btree and duplicate comparison functions, if specified.*/
36     private int nodeMax;
37     private int nodeMaxDupTree;
38     private Comparator JavaDoc btreeComparator = null;
39     private Comparator JavaDoc duplicateComparator = null;
40     private boolean btreeComparatorByClassName = false;
41     private boolean duplicateComparatorByClassName = false;
42     private boolean overrideBtreeComparator = false;
43     private boolean overrideDupComparator = false;
44     private boolean useExistingConfig = false;
45
46     /**
47      * Javadoc for this public method is generated via
48      * the doc templates in the doc_src directory.
49      */

50     public DatabaseConfig() {
51     }
52
53     /**
54      * Javadoc for this public method is generated via
55      * the doc templates in the doc_src directory.
56      */

57     public void setAllowCreate(boolean allowCreate) {
58         this.allowCreate = allowCreate;
59     }
60
61     /**
62      * Javadoc for this public method is generated via
63      * the doc templates in the doc_src directory.
64      */

65     public boolean getAllowCreate() {
66         return allowCreate;
67     }
68
69     /**
70      * Javadoc for this public method is generated via
71      * the doc templates in the doc_src directory.
72      */

73     public void setExclusiveCreate(boolean exclusiveCreate) {
74         this.exclusiveCreate = exclusiveCreate;
75     }
76
77     /**
78      * Javadoc for this public method is generated via
79      * the doc templates in the doc_src directory.
80      */

81     public boolean getExclusiveCreate() {
82         return exclusiveCreate;
83     }
84
85     /**
86      * Javadoc for this public method is generated via
87      * the doc templates in the doc_src directory.
88      */

89     public void setSortedDuplicates(boolean duplicatesAllowed) {
90         this.duplicatesAllowed = duplicatesAllowed;
91     }
92
93     /**
94      * Javadoc for this public method is generated via
95      * the doc templates in the doc_src directory.
96      */

97     public boolean getSortedDuplicates() {
98         return duplicatesAllowed;
99     }
100
101     /**
102      * Javadoc for this public method is generated via
103      * the doc templates in the doc_src directory.
104      */

105     public void setTransactional(boolean transactional) {
106         this.transactional = transactional;
107     }
108     /**
109      * Javadoc for this public method is generated via
110      * the doc templates in the doc_src directory.
111      */

112     public boolean getTransactional() {
113         return transactional;
114     }
115
116     /**
117      * Javadoc for this public method is generated via
118      * the doc templates in the doc_src directory.
119      */

120     public void setReadOnly(boolean readOnly) {
121         this.readOnly = readOnly;
122     }
123
124     /**
125      * Javadoc for this public method is generated via
126      * the doc templates in the doc_src directory.
127      */

128     public boolean getReadOnly() {
129         return readOnly;
130     }
131
132     /**
133      * Javadoc for this public method is generated via
134      * the doc templates in the doc_src directory.
135      */

136     public void setNodeMaxEntries(int nodeMaxEntries) {
137     this.nodeMax = nodeMaxEntries;
138     }
139
140     /**
141      * Javadoc for this public method is generated via
142      * the doc templates in the doc_src directory.
143      */

144     public void setNodeMaxDupTreeEntries(int nodeMaxDupTreeEntries) {
145     this.nodeMaxDupTree = nodeMaxDupTreeEntries;
146     }
147
148     /**
149      * Javadoc for this public method is generated via
150      * the doc templates in the doc_src directory.
151      */

152     public int getNodeMaxEntries() {
153     return nodeMax;
154     }
155
156     /**
157      * Javadoc for this public method is generated via
158      * the doc templates in the doc_src directory.
159      */

160     public int getNodeMaxDupTreeEntries() {
161     return nodeMaxDupTree;
162     }
163
164     /**
165      * Javadoc for this public method is generated via
166      * the doc templates in the doc_src directory.
167      */

168     public void setBtreeComparator(Comparator JavaDoc btreeComparator) {
169         /* Note: comparator may be null */
170         this.btreeComparator = validateComparator(btreeComparator, "Btree");
171         this.btreeComparatorByClassName = false;
172     }
173
174     /**
175      * Javadoc for this public method is generated via
176      * the doc templates in the doc_src directory.
177      */

178     public void setBtreeComparator(Class JavaDoc btreeComparator) {
179         /* Note: comparator may be null */
180         this.btreeComparator = validateComparator(btreeComparator, "Btree");
181         this.btreeComparatorByClassName = true;
182     }
183
184     /**
185      * Javadoc for this public method is generated via
186      * the doc templates in the doc_src directory.
187      */

188     public Comparator JavaDoc getBtreeComparator() {
189         return btreeComparator;
190     }
191
192     /**
193      * @return whether Comparator is set by class name, not by serializable
194      * Comparator object.
195      */

196     public boolean getBtreeComparatorByClassName() {
197         return btreeComparatorByClassName;
198     }
199
200     /**
201      * Javadoc for this public method is generated via
202      * the doc templates in the doc_src directory.
203      */

204     public void setOverrideBtreeComparator(boolean override) {
205         overrideBtreeComparator = override;
206     }
207
208     /**
209      * Javadoc for this public method is generated via
210      * the doc templates in the doc_src directory.
211      */

212     public boolean getOverrideBtreeComparator() {
213         return overrideBtreeComparator;
214     }
215
216     /**
217      * Javadoc for this public method is generated via
218      * the doc templates in the doc_src directory.
219      */

220     public void setDuplicateComparator(Comparator JavaDoc duplicateComparator) {
221         /* Note: comparator may be null */
222         this.duplicateComparator =
223         validateComparator(duplicateComparator, "Duplicate");
224         this.duplicateComparatorByClassName = false;
225     }
226
227     /**
228      * Javadoc for this public method is generated via
229      * the doc templates in the doc_src directory.
230      */

231     public void setDuplicateComparator(Class JavaDoc duplicateComparator) {
232         /* Note: comparator may be null */
233         this.duplicateComparator =
234         validateComparator(duplicateComparator, "Duplicate");
235         this.duplicateComparatorByClassName = true;
236     }
237
238     /**
239      * Javadoc for this public method is generated via
240      * the doc templates in the doc_src directory.
241      */

242     public Comparator JavaDoc getDuplicateComparator() {
243         return duplicateComparator;
244     }
245
246     /**
247      * @return whether Comparator is set by class name, not by serializable
248      * Comparator object.
249      */

250     public boolean getDuplicateComparatorByClassName() {
251         return duplicateComparatorByClassName;
252     }
253
254     /**
255      * Javadoc for this public method is generated via
256      * the doc templates in the doc_src directory.
257      */

258     public void setOverrideDuplicateComparator(boolean override) {
259         overrideDupComparator = override;
260     }
261
262     /**
263      * Javadoc for this public method is generated via
264      * the doc templates in the doc_src directory.
265      */

266     public boolean getOverrideDuplicateComparator() {
267         return overrideDupComparator;
268     }
269
270     /**
271      * Javadoc for this public method is generated via
272      * the doc templates in the doc_src directory.
273      */

274     public void setDeferredWrite(boolean deferredWrite) {
275         this.deferredWrite = deferredWrite;
276     }
277
278     /**
279      * Javadoc for this public method is generated via
280      * the doc templates in the doc_src directory.
281      */

282     public boolean getDeferredWrite() {
283         return deferredWrite;
284     }
285
286     /**
287      * Used to set the comparator when filling in a configuration from an
288      * existing database.
289      */

290     void setBtreeComparatorInternal(Comparator JavaDoc comparator,
291                                     boolean byClassName) {
292         btreeComparator = comparator;
293         btreeComparatorByClassName = byClassName;
294     }
295
296     /**
297      * Used to set the comparator when filling in a configuration from an
298      * existing database.
299      */

300     void setDuplicateComparatorInternal(Comparator JavaDoc comparator,
301                                         boolean byClassName) {
302         duplicateComparator = comparator;
303         duplicateComparatorByClassName = byClassName;
304     }
305
306     /**
307      * For utilities, to avoid having to know the configuration of a database.
308      */

309     void setUseExistingConfig(boolean useExistingConfig) {
310         this.useExistingConfig = useExistingConfig;
311     }
312
313     /**
314      * For utilities, to avoid having to know the configuration of a database.
315      */

316     boolean getUseExistingConfig() {
317         return useExistingConfig;
318     }
319
320     /**
321      * Javadoc for this public method is generated via
322      * the doc templates in the doc_src directory.
323      */

324     public DatabaseConfig cloneConfig() {
325         try {
326             return (DatabaseConfig) super.clone();
327         } catch (CloneNotSupportedException JavaDoc willNeverOccur) {
328             return null;
329         }
330     }
331
332     /*
333      * For JCA Database handle caching.
334      */

335     void validate(DatabaseConfig config)
336     throws DatabaseException {
337
338     if (config == null) {
339         config = DatabaseConfig.DEFAULT;
340     }
341
342     boolean txnMatch = (config.transactional == transactional);
343     boolean roMatch = (config.readOnly == readOnly);
344     boolean sdMatch = (config.duplicatesAllowed == duplicatesAllowed);
345         boolean dwMatch = (config.getDeferredWrite() == deferredWrite);
346     boolean btCmpMatch = true;
347         if (config.overrideBtreeComparator) {
348             if (btreeComparator == null) {
349                 btCmpMatch = (config.btreeComparator == null);
350             } else if (config.btreeComparatorByClassName !=
351                        btreeComparatorByClassName) {
352                 btCmpMatch = false;
353             } else if (btreeComparatorByClassName) {
354                 btCmpMatch = btreeComparator.getClass() ==
355                  config.btreeComparator.getClass();
356             } else {
357                 btCmpMatch = Arrays.equals
358                     (DatabaseImpl.objectToBytes
359                         (btreeComparator, "Btree"),
360                      DatabaseImpl.objectToBytes
361                         (config.btreeComparator, "Btree"));
362             }
363         }
364     boolean dtCmpMatch = true;
365         if (config.overrideDupComparator) {
366             if (duplicateComparator == null) {
367                 dtCmpMatch = (config.duplicateComparator == null);
368             } else if (config.duplicateComparatorByClassName !=
369                        duplicateComparatorByClassName) {
370                 dtCmpMatch = false;
371             } else if (duplicateComparatorByClassName) {
372                 dtCmpMatch = duplicateComparator.getClass() ==
373                  config.duplicateComparator.getClass();
374             } else {
375                 dtCmpMatch = Arrays.equals
376                     (DatabaseImpl.objectToBytes
377                         (duplicateComparator, "Duplicate"),
378                      DatabaseImpl.objectToBytes
379                         (config.duplicateComparator, "Duplicate"));
380             }
381         }
382
383     if (txnMatch &&
384         roMatch &&
385         sdMatch &&
386             dwMatch &&
387         btCmpMatch &&
388         dtCmpMatch) {
389         return;
390     } else {
391         String JavaDoc message =
392         genDatabaseConfigMismatchMessage
393         (config, txnMatch, roMatch, sdMatch, dwMatch,
394                  btCmpMatch, dtCmpMatch);
395         throw new DatabaseException(message);
396     }
397     }
398
399     private String JavaDoc genDatabaseConfigMismatchMessage(DatabaseConfig config,
400                                                     boolean txnMatch,
401                                                     boolean roMatch,
402                                                     boolean sdMatch,
403                                                     boolean dwMatch,
404                                                     boolean btCmpMatch,
405                                                     boolean dtCmpMatch) {
406     StringBuffer JavaDoc ret = new StringBuffer JavaDoc
407         ("The following DatabaseConfig parameters for the\n" +
408          "cached Database do not match the parameters for the\n" +
409          "requested Database:\n");
410     if (!txnMatch) {
411         ret.append(" Transactional\n");
412     }
413         
414     if (!roMatch) {
415         ret.append(" Read-Only\n");
416     }
417         
418     if (!sdMatch) {
419         ret.append(" Sorted Duplicates\n");
420     }
421         
422         if (!dwMatch) {
423             ret.append(" Deferred Write");
424         }
425
426     if (!btCmpMatch) {
427         ret.append(" Btree Comparator\n");
428     }
429         
430     if (!dtCmpMatch) {
431         ret.append(" Duplicate Comparator\n");
432     }
433
434     return ret.toString();
435     }
436
437     /**
438      * Check that this comparator can be serialized by JE.
439      */

440     private Comparator JavaDoc validateComparator(Comparator JavaDoc comparator, String JavaDoc type)
441         throws IllegalArgumentException JavaDoc {
442
443     if (comparator == null) {
444         return null;
445     }
446
447         try {
448         return DatabaseImpl.instantiateComparator(comparator, type);
449         } catch (DatabaseException e) {
450             throw new IllegalArgumentException JavaDoc
451         (type +
452          " comparator is not valid: " +
453          e.getMessage() +
454          "\nThe comparator object must be serializable.");
455         }
456     }
457
458     /**
459      * Check that this comparator class can be instantiated by JE.
460      */

461     private Comparator JavaDoc validateComparator(Class JavaDoc comparator, String JavaDoc type)
462         throws IllegalArgumentException JavaDoc {
463
464     if (comparator == null) {
465         return null;
466     }
467
468         if (!Comparator JavaDoc.class.isAssignableFrom(comparator)) {
469             throw new IllegalArgumentException JavaDoc
470                 (comparator.getName() +
471                  " is is not valid as a " + type +
472                  " comparator because it does not " +
473                  " implement java.util.Comparator.");
474         }
475
476         try {
477         return DatabaseImpl.instantiateComparator(comparator, type);
478         } catch (DatabaseException e) {
479             throw new IllegalArgumentException JavaDoc
480         (type +
481          " comparator is not valid: " +
482          e.getMessage() +
483          "\nPerhaps you have not implemented a zero-parameter " +
484          "constructor for the comparator or the comparator class " +
485          "cannot be found.");
486         }
487     }
488
489     /**
490      * Check that this database configuration is valid for a new, non-existant
491      * database.
492      */

493     void validateForNewDb()
494         throws DatabaseException {
495
496         if (readOnly) {
497             throw new DatabaseException
498         ("DatabaseConfig.setReadOnly() must be set to false " +
499          "when creating a Database");
500         }
501
502         if (transactional && deferredWrite) {
503             throw new DatabaseException("deferredWrite mode is not yet " +
504                                         "supported for transactional " +
505                                         "databases");
506         }
507     }
508 }
509
Popular Tags