KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > bind > serial > TupleSerialKeyCreator


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

8
9 package com.sleepycat.bind.serial;
10
11 import com.sleepycat.bind.tuple.TupleBase;
12 import com.sleepycat.bind.tuple.TupleInput;
13 import com.sleepycat.bind.tuple.TupleOutput;
14 import com.sleepycat.je.DatabaseEntry;
15 import com.sleepycat.je.DatabaseException;
16 import com.sleepycat.je.ForeignKeyNullifier;
17 import com.sleepycat.je.SecondaryDatabase;
18 import com.sleepycat.je.SecondaryKeyCreator;
19
20 /**
21  * A abstract key creator that uses a tuple key and a serial data entry. This
22  * class takes care of serializing and deserializing the data entry, and
23  * converting the key entry to/from {@link TupleInput} and {@link TupleOutput}
24  * objects.
25  * The following abstract method must be implemented by a concrete subclass
26  * to create the index key using these objects
27  * <ul>
28  * <li> {@link #createSecondaryKey(TupleInput,Object,TupleOutput)} </li>
29  * </ul>
30  * <!-- begin JE only -->
31  * <p>If {@link com.sleepycat.je.ForeignKeyDeleteAction#NULLIFY} was
32  * specified when opening the secondary database, the following method must be
33  * overridden to nullify the foreign index key. If NULLIFY was not specified,
34  * this method need not be overridden.</p>
35  * <ul>
36  * <li> {@link #nullifyForeignKey(Object)} </li>
37  * </ul>
38  * <!-- end JE only -->
39  *
40  * @author Mark Hayes
41  */

42 public abstract class TupleSerialKeyCreator extends TupleBase
43     implements SecondaryKeyCreator, ForeignKeyNullifier {
44
45     protected SerialBinding dataBinding;
46
47     /**
48      * Creates a tuple-serial key creator.
49      *
50      * @param classCatalog is the catalog to hold shared class information and
51      * for a database should be a {@link StoredClassCatalog}.
52      *
53      * @param dataClass is the data base class.
54      */

55     public TupleSerialKeyCreator(ClassCatalog classCatalog, Class JavaDoc dataClass) {
56
57         this(new SerialBinding(classCatalog, dataClass));
58     }
59
60     /**
61      * Creates a tuple-serial key creator.
62      *
63      * @param dataBinding is the data binding.
64      */

65     public TupleSerialKeyCreator(SerialBinding dataBinding) {
66
67         this.dataBinding = dataBinding;
68     }
69
70     // javadoc is inherited
71
public boolean createSecondaryKey(SecondaryDatabase db,
72                                       DatabaseEntry primaryKeyEntry,
73                                       DatabaseEntry dataEntry,
74                                       DatabaseEntry indexKeyEntry)
75         throws DatabaseException {
76
77         TupleOutput output = getTupleOutput(null);
78         TupleInput primaryKeyInput = entryToInput(primaryKeyEntry);
79         Object JavaDoc dataInput = dataBinding.entryToObject(dataEntry);
80         if (createSecondaryKey(primaryKeyInput, dataInput, output)) {
81             outputToEntry(output, indexKeyEntry);
82             return true;
83         } else {
84             return false;
85         }
86     }
87
88     // javadoc is inherited
89
public boolean nullifyForeignKey(SecondaryDatabase db,
90                                      DatabaseEntry dataEntry)
91         throws DatabaseException {
92
93         Object JavaDoc data = dataBinding.entryToObject(dataEntry);
94         data = nullifyForeignKey(data);
95         if (data != null) {
96             dataBinding.objectToEntry(data, dataEntry);
97             return true;
98         } else {
99             return false;
100         }
101     }
102
103     /**
104      * Creates the index key entry from primary key tuple entry and
105      * deserialized data entry.
106      *
107      * @param primaryKeyInput is the {@link TupleInput} for the primary key
108      * entry, or null if no primary key entry is used to construct the index
109      * key.
110      *
111      * @param dataInput is the deserialized data entry, or null if no data
112      * entry is used to construct the index key.
113      *
114      * @param indexKeyOutput is the destination index key tuple. For index
115      * keys which are optionally present, no tuple entry should be output to
116      * indicate that the key is not present or null.
117      *
118      * @return true if a key was created, or false to indicate that the key is
119      * not present.
120      */

121     public abstract boolean createSecondaryKey(TupleInput primaryKeyInput,
122                                                Object JavaDoc dataInput,
123                                                TupleOutput indexKeyOutput);
124
125     /**
126      * Clears the index key in the deserialized data entry.
127      *
128      * <p>On entry the data parameter contains the index key to be cleared. It
129      * should be changed by this method such that {@link #createSecondaryKey}
130      * will return false. Other fields in the data object should remain
131      * unchanged.</p>
132      *
133      * @param data is the source and destination deserialized data
134      * entry.
135      *
136      * @return the destination data object, or null to indicate that the
137      * key is not present and no change is necessary. The data returned may
138      * be the same object passed as the data parameter or a newly created
139      * object.
140      */

141     public Object JavaDoc nullifyForeignKey(Object JavaDoc data) {
142
143         return null;
144     }
145 }
146
Popular Tags