001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2000,2008 Oracle. All rights reserved.
005: *
006: * $Id: TupleSerialFactory.java,v 1.39.2.2 2008/01/07 15:14:06 cwl Exp $
007: */
008:
009: package com.sleepycat.collections;
010:
011: import com.sleepycat.bind.EntryBinding;
012: import com.sleepycat.bind.serial.ClassCatalog;
013: import com.sleepycat.bind.serial.TupleSerialMarshalledBinding;
014: import com.sleepycat.bind.serial.TupleSerialMarshalledKeyCreator;
015: import com.sleepycat.bind.tuple.TupleBinding;
016: import com.sleepycat.bind.tuple.TupleMarshalledBinding;
017: import com.sleepycat.je.Database;
018:
019: /**
020: * Creates stored collections having tuple keys and serialized entity values.
021: * The entity classes must implement the java.io.Serializable and
022: * MarshalledTupleKeyEntity interfaces. The key classes must either implement
023: * the MarshalledTupleEntry interface or be one of the Java primitive type
024: * classes. Underlying binding objects are created automatically.
025: *
026: * @author Mark Hayes
027: */
028: public class TupleSerialFactory {
029:
030: private ClassCatalog catalog;
031:
032: /**
033: * Creates a tuple-serial factory for given environment and class catalog.
034: */
035: public TupleSerialFactory(ClassCatalog catalog) {
036:
037: this .catalog = catalog;
038: }
039:
040: /**
041: * Returns the class catalog associated with this factory.
042: */
043: public final ClassCatalog getCatalog() {
044:
045: return catalog;
046: }
047:
048: /**
049: * Creates a map from a previously opened Database object.
050: *
051: * @param db the previously opened Database object.
052: *
053: * @param keyClass is the class used for map keys. It must implement the
054: * {@link com.sleepycat.bind.tuple.MarshalledTupleEntry} interface or be
055: * one of the Java primitive type classes.
056: *
057: * @param valueBaseClass the base class of the entity values for this
058: * store. It must implement the {@link
059: * com.sleepycat.bind.tuple.MarshalledTupleKeyEntity} interface.
060: *
061: * @param writeAllowed is true to create a read-write collection or false
062: * to create a read-only collection.
063: */
064: public StoredMap newMap(Database db, Class keyClass,
065: Class valueBaseClass, boolean writeAllowed) {
066:
067: return new StoredMap(db, getKeyBinding(keyClass),
068: getEntityBinding(valueBaseClass), writeAllowed);
069: }
070:
071: /**
072: * Creates a sorted map from a previously opened Database object.
073: *
074: * @param db the previously opened Database object.
075: *
076: * @param keyClass is the class used for map keys. It must implement the
077: * {@link com.sleepycat.bind.tuple.MarshalledTupleEntry} interface or be
078: * one of the Java primitive type classes.
079: *
080: * @param valueBaseClass the base class of the entity values for this
081: * store. It must implement the {@link
082: * com.sleepycat.bind.tuple.MarshalledTupleKeyEntity} interface.
083: *
084: * @param writeAllowed is true to create a read-write collection or false
085: * to create a read-only collection.
086: */
087: public StoredSortedMap newSortedMap(Database db, Class keyClass,
088: Class valueBaseClass, boolean writeAllowed) {
089:
090: return new StoredSortedMap(db, getKeyBinding(keyClass),
091: getEntityBinding(valueBaseClass), writeAllowed);
092: }
093:
094: /**
095: * Creates a <code>SecondaryKeyCreator</code> object for use in configuring
096: * a <code>SecondaryDatabase</code>. The returned object implements
097: * the {@link com.sleepycat.je.SecondaryKeyCreator} interface.
098: *
099: * @param valueBaseClass the base class of the entity values for this
100: * store. It must implement the {@link
101: * com.sleepycat.bind.tuple.MarshalledTupleKeyEntity} interface.
102: *
103: * @param keyName is the key name passed to the {@link
104: * com.sleepycat.bind.tuple.MarshalledTupleKeyEntity#marshalSecondaryKey}
105: * method to identify the secondary key.
106: */
107: public TupleSerialMarshalledKeyCreator getKeyCreator(
108: Class valueBaseClass, String keyName) {
109:
110: return new TupleSerialMarshalledKeyCreator(
111: getEntityBinding(valueBaseClass), keyName);
112: }
113:
114: private TupleSerialMarshalledBinding getEntityBinding(
115: Class baseClass) {
116:
117: return new TupleSerialMarshalledBinding(catalog, baseClass);
118: }
119:
120: private EntryBinding getKeyBinding(Class keyClass) {
121:
122: EntryBinding binding = TupleBinding
123: .getPrimitiveBinding(keyClass);
124: if (binding == null) {
125: binding = new TupleMarshalledBinding(keyClass);
126: }
127: return binding;
128: }
129: }
|