01: /*-
02: * See the file LICENSE for redistribution information.
03: *
04: * Copyright (c) 2000,2008 Oracle. All rights reserved.
05: *
06: * $Id: TupleSerialMarshalledKeyCreator.java,v 1.25.2.2 2008/01/07 15:14:06 cwl Exp $
07: */
08:
09: package com.sleepycat.bind.serial;
10:
11: import com.sleepycat.bind.tuple.MarshalledTupleKeyEntity;
12: import com.sleepycat.bind.tuple.TupleInput;
13: import com.sleepycat.bind.tuple.TupleOutput;
14:
15: /**
16: * A concrete key creator that works in conjunction with a {@link
17: * TupleSerialMarshalledBinding}. This key creator works by calling the
18: * methods of the {@link MarshalledTupleKeyEntity} interface to create and
19: * clear the index key fields.
20: *
21: * @author Mark Hayes
22: */
23: public class TupleSerialMarshalledKeyCreator extends
24: TupleSerialKeyCreator {
25:
26: private TupleSerialMarshalledBinding binding;
27: private String keyName;
28:
29: /**
30: * Creates a tuple-serial marshalled key creator.
31: *
32: * @param binding is the binding used for the tuple-serial entity.
33: *
34: * @param keyName is the key name passed to the {@link
35: * MarshalledTupleKeyEntity#marshalSecondaryKey} method to identify the
36: * index key.
37: */
38: public TupleSerialMarshalledKeyCreator(
39: TupleSerialMarshalledBinding binding, String keyName) {
40:
41: super (binding.dataBinding);
42: this .binding = binding;
43: this .keyName = keyName;
44:
45: if (dataBinding == null) {
46: throw new NullPointerException(
47: "dataBinding may not be null");
48: }
49: }
50:
51: // javadoc is inherited
52: public boolean createSecondaryKey(TupleInput primaryKeyInput,
53: Object dataInput, TupleOutput indexKeyOutput) {
54:
55: /*
56: * The primary key is unmarshalled before marshalling the index key, to
57: * account for cases where the index key includes fields taken from the
58: * primary key.
59: */
60: MarshalledTupleKeyEntity entity = (MarshalledTupleKeyEntity) binding
61: .entryToObject(primaryKeyInput, dataInput);
62:
63: return entity.marshalSecondaryKey(keyName, indexKeyOutput);
64: }
65:
66: // javadoc is inherited
67: public Object nullifyForeignKey(Object dataInput) {
68:
69: MarshalledTupleKeyEntity entity = (MarshalledTupleKeyEntity) binding
70: .entryToObject(null, dataInput);
71:
72: return entity.nullifyForeignKey(keyName) ? dataInput : null;
73: }
74: }
|