1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2000, 2010 Oracle and/or its affiliates.  All rights reserved.
5  *
6  */
7 
8 package com.sleepycat.collections;
9 
10 import com.sleepycat.bind.EntryBinding;
11 import com.sleepycat.bind.serial.ClassCatalog;
12 import com.sleepycat.bind.serial.TupleSerialMarshalledBinding;
13 import com.sleepycat.bind.serial.TupleSerialMarshalledKeyCreator;
14 import com.sleepycat.bind.tuple.MarshalledTupleEntry; // for javadoc
15 import com.sleepycat.bind.tuple.MarshalledTupleKeyEntity;
16 import com.sleepycat.bind.tuple.TupleBinding;
17 import com.sleepycat.bind.tuple.TupleMarshalledBinding;
18 import com.sleepycat.je.Database;
19 
20 /**
21  * Creates stored collections having tuple keys and serialized entity values.
22  * The entity classes must be Serializable and must implement the
23  * MarshalledTupleKeyEntity interfaces.  The key classes must either implement
24  * the MarshalledTupleEntry interface or be one of the Java primitive type
25  * classes.  Underlying binding objects are created automatically.
26  *
27  * @author Mark Hayes
28  */
29 public class TupleSerialFactory {
30 
31     private ClassCatalog catalog;
32 
33     /**
34      * Creates a tuple-serial factory for given environment and class catalog.
35      */
TupleSerialFactory(ClassCatalog catalog)36     public TupleSerialFactory(ClassCatalog catalog) {
37 
38         this.catalog = catalog;
39     }
40 
41     /**
42      * Returns the class catalog associated with this factory.
43      */
getCatalog()44     public final ClassCatalog getCatalog() {
45 
46         return catalog;
47     }
48 
49     /**
50      * Creates a map from a previously opened Database object.
51      *
52      * @param db the previously opened Database object.
53      *
54      * @param keyClass is the class used for map keys.  It must implement the
55      * {@link MarshalledTupleEntry} interface or be one of the Java primitive
56      * type classes.
57      *
58      * @param valueBaseClass the base class of the entity values for this
59      * store.  It must implement the  {@link MarshalledTupleKeyEntity}
60      * interface.
61      *
62      * @param writeAllowed is true to create a read-write collection or false
63      * to create a read-only collection.
64      */
65     public <K, V extends MarshalledTupleKeyEntity> StoredMap<K, V>
newMap(Database db, Class<K> keyClass, Class<V> valueBaseClass, boolean writeAllowed)66         newMap(Database db,
67                Class<K> keyClass,
68                Class<V> valueBaseClass,
69                boolean writeAllowed) {
70 
71         return new StoredMap<K, V>(db,
72                         getKeyBinding(keyClass),
73                         getEntityBinding(valueBaseClass),
74                         writeAllowed);
75     }
76 
77     /**
78      * Creates a sorted map from a previously opened Database object.
79      *
80      * @param db the previously opened Database object.
81      *
82      * @param keyClass is the class used for map keys.  It must implement the
83      * {@link MarshalledTupleEntry} interface or be one of the Java primitive
84      * type classes.
85      *
86      * @param valueBaseClass the base class of the entity values for this
87      * store.  It must implement the  {@link MarshalledTupleKeyEntity}
88      * interface.
89      *
90      * @param writeAllowed is true to create a read-write collection or false
91      * to create a read-only collection.
92      */
93     public <K, V extends MarshalledTupleKeyEntity> StoredSortedMap<K, V>
newSortedMap(Database db, Class<K> keyClass, Class<V> valueBaseClass, boolean writeAllowed)94         newSortedMap(Database db,
95                      Class<K> keyClass,
96                      Class<V> valueBaseClass,
97                      boolean writeAllowed) {
98 
99         return new StoredSortedMap(db,
100                         getKeyBinding(keyClass),
101                         getEntityBinding(valueBaseClass),
102                         writeAllowed);
103     }
104 
105     /**
106      * Creates a <code>SecondaryKeyCreator</code> object for use in configuring
107      * a <code>SecondaryDatabase</code>.  The returned object implements
108      * the {@link com.sleepycat.je.SecondaryKeyCreator} interface.
109      *
110      * @param valueBaseClass the base class of the entity values for this
111      * store.  It must implement the  {@link MarshalledTupleKeyEntity}
112      * interface.
113      *
114      * @param keyName is the key name passed to the {@link
115      * MarshalledTupleKeyEntity#marshalSecondaryKey} method to identify the
116      * secondary key.
117      */
118     public <V extends MarshalledTupleKeyEntity>
119         TupleSerialMarshalledKeyCreator<V>
getKeyCreator(Class<V> valueBaseClass, String keyName)120         getKeyCreator(Class<V> valueBaseClass, String keyName) {
121 
122         return new TupleSerialMarshalledKeyCreator<V>
123             (getEntityBinding(valueBaseClass), keyName);
124     }
125 
126     public <V extends MarshalledTupleKeyEntity>
127         TupleSerialMarshalledBinding<V>
getEntityBinding(Class<V> baseClass)128         getEntityBinding(Class<V> baseClass) {
129 
130         return new TupleSerialMarshalledBinding<V>(catalog, baseClass);
131     }
132 
getKeyBinding(Class<K> keyClass)133     private <K> EntryBinding<K> getKeyBinding(Class<K> keyClass) {
134 
135         EntryBinding<K> binding = TupleBinding.getPrimitiveBinding(keyClass);
136         if (binding == null) {
137 
138             /*
139              * Cannot use type param <K> here because it does not implement
140              * MarshalledTupleEntry if it is a primitive class.
141              */
142             binding = new TupleMarshalledBinding(keyClass);
143         }
144         return binding;
145     }
146 }
147