KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > bind > tuple > TupleTupleKeyCreator


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

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

42 public abstract class TupleTupleKeyCreator extends TupleBase
43     implements SecondaryKeyCreator, ForeignKeyNullifier {
44
45     /**
46      * Creates a tuple-tuple key creator.
47      */

48     public TupleTupleKeyCreator() {
49     }
50
51     // javadoc is inherited
52
public boolean createSecondaryKey(SecondaryDatabase db,
53                                       DatabaseEntry primaryKeyEntry,
54                                       DatabaseEntry dataEntry,
55                                       DatabaseEntry indexKeyEntry)
56         throws DatabaseException {
57
58         TupleOutput output = getTupleOutput(null);
59         TupleInput primaryKeyInput = entryToInput(primaryKeyEntry);
60         TupleInput dataInput = entryToInput(dataEntry);
61         if (createSecondaryKey(primaryKeyInput, dataInput, output)) {
62             outputToEntry(output, indexKeyEntry);
63             return true;
64         } else {
65             return false;
66         }
67     }
68
69     // javadoc is inherited
70
public boolean nullifyForeignKey(SecondaryDatabase db,
71                                      DatabaseEntry dataEntry)
72         throws DatabaseException {
73
74         TupleOutput output = getTupleOutput(null);
75         if (nullifyForeignKey(entryToInput(dataEntry), output)) {
76             outputToEntry(output, dataEntry);
77             return true;
78         } else {
79             return false;
80         }
81     }
82
83     /**
84      * Creates the index key from primary key tuple and data tuple.
85      *
86      * @param primaryKeyInput is the {@link TupleInput} for the primary key
87      * entry.
88      *
89      * @param dataInput is the {@link TupleInput} for the data entry.
90      *
91      * @param indexKeyOutput is the destination index key tuple.
92      *
93      * @return true if a key was created, or false to indicate that the key is
94      * not present.
95      */

96     public abstract boolean createSecondaryKey(TupleInput primaryKeyInput,
97                                                TupleInput dataInput,
98                                                TupleOutput indexKeyOutput);
99
100     /**
101      * Clears the index key in the tuple data entry. The dataInput should be
102      * read and then written to the dataOutput, clearing the index key in the
103      * process.
104      *
105      * <p>The secondary key should be output or removed by this method such
106      * that {@link #createSecondaryKey} will return false. Other fields in the
107      * data object should remain unchanged.</p>
108      *
109      * @param dataInput is the {@link TupleInput} for the data entry.
110      *
111      * @param dataOutput is the destination {@link TupleOutput}.
112      *
113      * @return true if the key was cleared, or false to indicate that the key
114      * is not present and no change is necessary.
115      */

116     public boolean nullifyForeignKey(TupleInput dataInput,
117                                      TupleOutput dataOutput) {
118
119         return false;
120     }
121 }
122
Popular Tags