2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 2000, 2012 Oracle and/or its affiliates. All rights reserved.
8 package com.sleepycat.bind.tuple;
10 import com.sleepycat.db.DatabaseEntry;
11 import com.sleepycat.db.ForeignKeyNullifier;
12 import com.sleepycat.db.SecondaryDatabase;
13 import com.sleepycat.db.SecondaryKeyCreator;
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
22 * <li> {@link #createSecondaryKey(TupleInput,TupleInput,TupleOutput)} </li>
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>
29 * <li> {@link #nullifyForeignKey(TupleInput,TupleOutput)} </li>
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>
38 public abstract class TupleTupleKeyCreator<E> extends TupleBase<E>
39 implements SecondaryKeyCreator, ForeignKeyNullifier {
42 * Creates a tuple-tuple key creator.
44 public TupleTupleKeyCreator() {
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);
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);
76 * Creates the index key from primary key tuple and data tuple.
78 * @param primaryKeyInput is the {@link TupleInput} for the primary key
81 * @param dataInput is the {@link TupleInput} for the data entry.
83 * @param indexKeyOutput is the destination index key tuple.
85 * @return true if a key was created, or false to indicate that the key is
88 public abstract boolean createSecondaryKey(TupleInput primaryKeyInput,
90 TupleOutput indexKeyOutput);
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
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>
101 * @param dataInput is the {@link TupleInput} for the data entry.
103 * @param dataOutput is the destination {@link TupleOutput}.
105 * @return true if the key was cleared, or false to indicate that the key
106 * is not present and no change is necessary.
108 public boolean nullifyForeignKey(TupleInput dataInput,
109 TupleOutput dataOutput) {