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