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