Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / lang / java / src / com / sleepycat / bind / tuple / TupleTupleKeyCreator.java
1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2000, 2012 Oracle and/or its affiliates.  All rights reserved.
5  *
6  */
7
8 package com.sleepycat.bind.tuple;
9
10 import com.sleepycat.db.DatabaseEntry;
11 import com.sleepycat.db.ForeignKeyNullifier;
12 import com.sleepycat.db.SecondaryDatabase;
13 import com.sleepycat.db.SecondaryKeyCreator;
14
15 /**
16  * An abstract key creator that uses a tuple key and a tuple data entry. This
17  * class takes care of converting the key and data entry to/from {@link
18  * TupleInput} and {@link TupleOutput} objects.
19  * The following abstract method must be implemented by a concrete subclass
20  * to create the index key using these objects
21  * <ul>
22  * <li> {@link #createSecondaryKey(TupleInput,TupleInput,TupleOutput)} </li>
23  * </ul>
24  * <p>If {@link com.sleepycat.db.ForeignKeyDeleteAction#NULLIFY} was
25  * specified when opening the secondary database, the following method must be
26  * overridden to nullify the foreign index key.  If NULLIFY was not specified,
27  * this method need not be overridden.</p>
28  * <ul>
29  * <li> {@link #nullifyForeignKey(TupleInput,TupleOutput)} </li>
30  * </ul>
31  * <p>If {@link com.sleepycat.db.ForeignKeyDeleteAction#NULLIFY} was
32  * specified when creating the secondary, this method is called when the
33  * entity for this foreign key is deleted.  If NULLIFY was not specified,
34  * this method will not be called and may always return false.</p>
35  *
36  * @author Mark Hayes
37  */
38 public abstract class TupleTupleKeyCreator<E> extends TupleBase<E>
39     implements SecondaryKeyCreator, ForeignKeyNullifier {
40
41     /**
42      * Creates a tuple-tuple key creator.
43      */
44     public TupleTupleKeyCreator() {
45     }
46
47     // javadoc is inherited
48     public boolean createSecondaryKey(SecondaryDatabase db,
49                                       DatabaseEntry primaryKeyEntry,
50                                       DatabaseEntry dataEntry,
51                                       DatabaseEntry indexKeyEntry) {
52         TupleOutput output = getTupleOutput(null);
53         TupleInput primaryKeyInput = entryToInput(primaryKeyEntry);
54         TupleInput dataInput = entryToInput(dataEntry);
55         if (createSecondaryKey(primaryKeyInput, dataInput, output)) {
56             outputToEntry(output, indexKeyEntry);
57             return true;
58         } else {
59             return false;
60         }
61     }
62
63     // javadoc is inherited
64     public boolean nullifyForeignKey(SecondaryDatabase db,
65                                      DatabaseEntry dataEntry) {
66         TupleOutput output = getTupleOutput(null);
67         if (nullifyForeignKey(entryToInput(dataEntry), output)) {
68             outputToEntry(output, dataEntry);
69             return true;
70         } else {
71             return false;
72         }
73     }
74
75     /**
76      * Creates the index key from primary key tuple and data tuple.
77      *
78      * @param primaryKeyInput is the {@link TupleInput} for the primary key
79      * entry.
80      *
81      * @param dataInput is the {@link TupleInput} for the data entry.
82      *
83      * @param indexKeyOutput is the destination index key tuple.
84      *
85      * @return true if a key was created, or false to indicate that the key is
86      * not present.
87      */
88     public abstract boolean createSecondaryKey(TupleInput primaryKeyInput,
89                                                TupleInput dataInput,
90                                                TupleOutput indexKeyOutput);
91
92     /**
93      * Clears the index key in the tuple data entry.  The dataInput should be
94      * read and then written to the dataOutput, clearing the index key in the
95      * process.
96      *
97      * <p>The secondary key should be output or removed by this method such
98      * that {@link #createSecondaryKey} will return false.  Other fields in the
99      * data object should remain unchanged.</p>
100      *
101      * @param dataInput is the {@link TupleInput} for the data entry.
102      *
103      * @param dataOutput is the destination {@link TupleOutput}.
104      *
105      * @return true if the key was cleared, or false to indicate that the key
106      * is not present and no change is necessary.
107      */
108     public boolean nullifyForeignKey(TupleInput dataInput,
109                                      TupleOutput dataOutput) {
110
111         return false;
112     }
113 }