01: /*-
02: * See the file LICENSE for redistribution information.
03: *
04: * Copyright (c) 2000,2008 Oracle. All rights reserved.
05: *
06: * $Id: TupleTupleMarshalledKeyCreator.java,v 1.27.2.3 2008/01/07 15:14:06 cwl Exp $
07: */
08:
09: package com.sleepycat.bind.tuple;
10:
11: /**
12: * A concrete key creator that works in conjunction with a {@link
13: * TupleTupleMarshalledBinding}. This key creator works by calling the
14: * methods of the {@link MarshalledTupleKeyEntity} interface to create and
15: * clear the index key.
16: *
17: * <p>Note that a marshalled tuple key creator is somewhat less efficient
18: * than a non-marshalled key tuple creator because more conversions are
19: * needed. A marshalled key creator must convert the entry to an object in
20: * order to create the key, while an unmarshalled key creator does not.</p>
21: *
22: * @author Mark Hayes
23: */
24: public class TupleTupleMarshalledKeyCreator extends
25: TupleTupleKeyCreator {
26:
27: private String keyName;
28: private TupleTupleMarshalledBinding binding;
29:
30: /**
31: * Creates a tuple-tuple marshalled key creator.
32: *
33: * @param binding is the binding used for the tuple-tuple entity.
34: *
35: * @param keyName is the key name passed to the {@link
36: * MarshalledTupleKeyEntity#marshalSecondaryKey} method to identify the
37: * index key.
38: */
39: public TupleTupleMarshalledKeyCreator(
40: TupleTupleMarshalledBinding binding, String keyName) {
41:
42: this .binding = binding;
43: this .keyName = keyName;
44: }
45:
46: // javadoc is inherited
47: public boolean createSecondaryKey(TupleInput primaryKeyInput,
48: TupleInput dataInput, TupleOutput indexKeyOutput) {
49:
50: /* The primary key is unmarshalled before marshalling the index key, to
51: * account for cases where the index key includes fields taken from the
52: * primary key.
53: */
54: MarshalledTupleKeyEntity entity = (MarshalledTupleKeyEntity) binding
55: .entryToObject(primaryKeyInput, dataInput);
56:
57: return entity.marshalSecondaryKey(keyName, indexKeyOutput);
58: }
59:
60: // javadoc is inherited
61: public boolean nullifyForeignKey(TupleInput dataInput,
62: TupleOutput dataOutput) {
63:
64: MarshalledTupleKeyEntity entity = (MarshalledTupleKeyEntity) binding
65: .entryToObject(null, dataInput);
66: if (entity.nullifyForeignKey(keyName)) {
67: binding.objectToData(entity, dataOutput);
68: return true;
69: } else {
70: return false;
71: }
72: }
73: }
|