1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002, 2013 Oracle and/or its affiliates.  All rights reserved.
5  *
6  */
7 
8 package com.sleepycat.persist;
9 
10 import com.sleepycat.db.DatabaseException;
11 import com.sleepycat.db.Environment; // for javadoc
12 import com.sleepycat.persist.evolve.IncompatibleClassException;
13 import com.sleepycat.persist.evolve.Mutations;
14 import com.sleepycat.persist.model.AnnotationModel;
15 import com.sleepycat.persist.model.EntityModel;
16 import com.sleepycat.persist.raw.RawStore; // for javadoc
17 
18 /**
19  * Configuration properties used with an {@link EntityStore} or {@link
20  * RawStore}.
21  *
22  * <p>{@code StoreConfig} objects are thread-safe.  Multiple threads may safely
23  * call the methods of a shared {@code StoreConfig} object.</p>
24  *
25  * <p>See the {@link <a href="package-summary.html#example">package
26  * summary example</a>} for an example of using a {@code StoreConfig}.</p>
27  *
28  * @author Mark Hayes
29  */
30 public class StoreConfig implements Cloneable {
31 
32     /**
33      * The default store configuration containing properties as if the
34      * configuration were constructed and not modified.
35      */
36     public static final StoreConfig DEFAULT = new StoreConfig();
37 
38     private boolean allowCreate;
39     private boolean exclusiveCreate;
40     private boolean transactional;
41     private boolean readOnly;
42     private boolean secondaryBulkLoad;
43     private EntityModel model;
44     private Mutations mutations;
45     private DatabaseNamer databaseNamer = DatabaseNamer.DEFAULT;
46 
47     /**
48      * Creates an entity store configuration object with default properties.
49      */
StoreConfig()50     public StoreConfig() {
51     }
52 
53     /**
54      * Returns a shallow copy of the configuration.
55      *
56      * @deprecated As of JE 4.0.13, replaced by {@link
57      * StoreConfig#clone()}.</p>
58      */
cloneConfig()59     public StoreConfig cloneConfig() {
60         try {
61             return (StoreConfig) super.clone();
62         } catch (CloneNotSupportedException cannotHappen) {
63             return null;
64         }
65     }
66 
67     /**
68      * Returns a shallow copy of the configuration.
69      */
70     @Override
clone()71     public StoreConfig clone() {
72         try {
73             return (StoreConfig) super.clone();
74         } catch (CloneNotSupportedException cannotHappen) {
75             return null;
76         }
77     }
78 
79     /**
80      * Specifies whether creation of a new store is allowed.  By default this
81      * property is false.
82      *
83      * <p>If this property is false and the internal store metadata database
84      * does not exist, {@link DatabaseException} will be thrown when the store
85      * is opened.</p>
86      */
setAllowCreate(boolean allowCreate)87     public StoreConfig setAllowCreate(boolean allowCreate) {
88         setAllowCreateVoid(allowCreate);
89         return this;
90     }
91 
92     /**
93      * @hidden
94      * The void return setter for use by Bean editors.
95      */
setAllowCreateVoid(boolean allowCreate)96     public void setAllowCreateVoid(boolean allowCreate) {
97         this.allowCreate = allowCreate;
98     }
99 
100     /**
101      * Returns whether creation of a new store is allowed.
102      */
getAllowCreate()103     public boolean getAllowCreate() {
104         return allowCreate;
105     }
106 
107     /**
108      * Specifies whether opening an existing store is prohibited.  By default
109      * this property is false.
110      *
111      * <p>If this property is true and the internal store metadata database
112      * already exists, {@link DatabaseException} will be thrown when the store
113      * is opened.</p>
114      */
setExclusiveCreate(boolean exclusiveCreate)115     public StoreConfig setExclusiveCreate(boolean exclusiveCreate) {
116         setExclusiveCreateVoid(exclusiveCreate);
117         return this;
118     }
119 
120     /**
121      * @hidden
122      * The void return setter for use by Bean editors.
123      */
setExclusiveCreateVoid(boolean exclusiveCreate)124     public void setExclusiveCreateVoid(boolean exclusiveCreate) {
125         this.exclusiveCreate = exclusiveCreate;
126     }
127 
128     /**
129      * Returns whether opening an existing store is prohibited.
130      */
getExclusiveCreate()131     public boolean getExclusiveCreate() {
132         return exclusiveCreate;
133     }
134 
135     /**
136      * Sets the transactional configuration property.  By default this property
137      * is false.
138      *
139      * <p>This property is true to open all store indices for transactional
140      * access.  True may not be specified if the environment is not also
141      * transactional.</p>
142      */
setTransactional(boolean transactional)143     public StoreConfig setTransactional(boolean transactional) {
144         setTransactionalVoid(transactional);
145         return this;
146     }
147 
148     /**
149      * @hidden
150      * The void return setter for use by Bean editors.
151      */
setTransactionalVoid(boolean transactional)152     public void setTransactionalVoid(boolean transactional) {
153         this.transactional = transactional;
154     }
155 
156     /**
157      * Returns the transactional configuration property.
158      */
getTransactional()159     public boolean getTransactional() {
160         return transactional;
161     }
162 
163     /**
164      * Sets the read-only configuration property.  By default this property is
165      * false.
166      *
167      * <p>This property is true to open all store indices for read-only access,
168      * or false to open them for read-write access.  False may not be specified
169      * if the environment is read-only.</p>
170      */
setReadOnly(boolean readOnly)171     public StoreConfig setReadOnly(boolean readOnly) {
172         setReadOnlyVoid(readOnly);
173         return this;
174     }
175 
176     /**
177      * @hidden
178      * The void return setter for use by Bean editors.
179      */
setReadOnlyVoid(boolean readOnly)180     public void setReadOnlyVoid(boolean readOnly) {
181         this.readOnly = readOnly;
182     }
183 
184     /**
185      * Returns the read-only configuration property.
186      */
getReadOnly()187     public boolean getReadOnly() {
188         return readOnly;
189     }
190 
191 
192     /**
193      * Sets the bulk-load-secondaries configuration property.  By default this
194      * property is false.
195      *
196      * <p>This property is true to cause the initial creation of secondary
197      * indices to be performed as a bulk load.  If this property is true and
198      * {@link EntityStore#getSecondaryIndex EntityStore.getSecondaryIndex} has
199      * never been called for a secondary index, that secondary index will not
200      * be created or written as records are written to the primary index.  In
201      * addition, if that secondary index defines a foreign key constraint, the
202      * constraint will not be enforced.</p>
203      *
204      * <p>The secondary index will be populated later when the {@code
205      * getSecondaryIndex} method is called for the first time for that index,
206      * or when the store is closed and re-opened with this property set to
207      * false and the primary index is obtained.  In either case, the secondary
208      * index is populated by reading through the entire primary index and
209      * adding records to the secondary index as needed.  While populating the
210      * secondary, foreign key constraints will be enforced and an exception is
211      * thrown if a constraint is violated.</p>
212      *
213      * <p>When loading a primary index along with secondary indexes from a
214      * large input data set, configuring a bulk load of the secondary indexes
215      * is sometimes more performant than updating the secondary indexes each
216      * time the primary index is updated.  The absence of foreign key
217      * constraints during the load also provides more flexibility.</p>
218      */
setSecondaryBulkLoad(boolean secondaryBulkLoad)219     public StoreConfig setSecondaryBulkLoad(boolean secondaryBulkLoad) {
220         setSecondaryBulkLoadVoid(secondaryBulkLoad);
221         return this;
222     }
223 
224     /**
225      * @hidden
226      * The void return setter for use by Bean editors.
227      */
setSecondaryBulkLoadVoid(boolean secondaryBulkLoad)228     public void setSecondaryBulkLoadVoid(boolean secondaryBulkLoad) {
229         this.secondaryBulkLoad = secondaryBulkLoad;
230     }
231 
232     /**
233      * Returns the bulk-load-secondaries configuration property.
234      */
getSecondaryBulkLoad()235     public boolean getSecondaryBulkLoad() {
236         return secondaryBulkLoad;
237     }
238 
239     /**
240      * Sets the entity model that defines entity classes and index keys.
241      *
242      * <p>If null is specified or this method is not called, an {@link
243      * AnnotationModel} instance is used by default.</p>
244      */
setModel(EntityModel model)245     public StoreConfig setModel(EntityModel model) {
246         setModelVoid(model);
247         return this;
248     }
249 
250     /**
251      * @hidden
252      * The void return setter for use by Bean editors.
253      */
setModelVoid(EntityModel model)254     public void setModelVoid(EntityModel model) {
255         this.model = model;
256     }
257 
258     /**
259      * Returns the entity model that defines entity classes and index keys.
260      */
getModel()261     public EntityModel getModel() {
262         return model;
263     }
264 
265     /**
266      * Configures mutations for performing lazy evolution of stored instances.
267      * Existing mutations for this store are not cleared, so the mutations
268      * required are only those changes that have been made since the store was
269      * last opened.  Some new mutations may override existing specifications,
270      * and some may be supplemental.
271      *
272      * <p>If null is specified and the store already exists, the previously
273      * specified mutations are used.  The mutations are stored persistently in
274      * serialized form.</p>
275      *
276      * <p>Mutations must be available to handle all changes to classes that are
277      * incompatible with the class definitions known to this store.  See {@link
278      * Mutations} and {@link com.sleepycat.persist.evolve Class Evolution} for
279      * more information.</p>
280      *
281      * <p>If an incompatible class change has been made and mutations are not
282      * available for handling the change, {@link IncompatibleClassException}
283      * will be thrown when creating an {@link EntityStore}.</p>
284      */
setMutations(Mutations mutations)285     public StoreConfig setMutations(Mutations mutations) {
286         setMutationsVoid(mutations);
287         return this;
288     }
289 
290     /**
291      * @hidden
292      * The void return setter for use by Bean editors.
293      */
setMutationsVoid(Mutations mutations)294     public void setMutationsVoid(Mutations mutations) {
295         this.mutations = mutations;
296     }
297 
298     /**
299      * Returns the configured mutations for performing lazy evolution of stored
300      * instances.
301      */
getMutations()302     public Mutations getMutations() {
303         return mutations;
304     }
305 
306     /**
307      * Specifies the object reponsible for naming of files and databases.
308      *
309      * By default this property is {@link DatabaseNamer#DEFAULT}.
310      *
311      * @throws NullPointerException if a null parameter value is passed.
312      */
setDatabaseNamer(DatabaseNamer databaseNamer)313     public StoreConfig setDatabaseNamer(DatabaseNamer databaseNamer) {
314         setDatabaseNamerVoid(databaseNamer);
315         return this;
316     }
317 
318     /**
319      * @hidden
320      * The void return setter for use by Bean editors.
321      */
setDatabaseNamerVoid(DatabaseNamer databaseNamer)322     public void setDatabaseNamerVoid(DatabaseNamer databaseNamer) {
323         if (databaseNamer == null) {
324             throw new NullPointerException();
325         }
326         this.databaseNamer = databaseNamer;
327     }
328 
329     /**
330      * Returns the object reponsible for naming of files and databases.
331      */
getDatabaseNamer()332     public DatabaseNamer getDatabaseNamer() {
333         return databaseNamer;
334     }
335 }
336