1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2000,2008 Oracle.  All rights reserved.
5 *
6 * $Id: TupleSerialMarshalledKeyCreator.java,v 12.6 2008/01/08 20:58:35 bostic Exp $
7 */
8
9package com.sleepycat.bind.serial;
10
11import com.sleepycat.bind.tuple.MarshalledTupleKeyEntity;
12import com.sleepycat.bind.tuple.TupleInput;
13import 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 */
23public class TupleSerialMarshalledKeyCreator extends TupleSerialKeyCreator {
24
25    private TupleSerialMarshalledBinding binding;
26    private String keyName;
27
28    /**
29     * Creates a tuple-serial marshalled key creator.
30     *
31     * @param binding is the binding used for the tuple-serial entity.
32     *
33     * @param keyName is the key name passed to the {@link
34     * MarshalledTupleKeyEntity#marshalSecondaryKey} method to identify the
35     * index key.
36     */
37    public TupleSerialMarshalledKeyCreator(TupleSerialMarshalledBinding
38                                           binding,
39                                           String keyName) {
40
41        super(binding.dataBinding);
42        this.binding = binding;
43        this.keyName = keyName;
44
45        if (dataBinding == null) {
46            throw new NullPointerException("dataBinding may not be null");
47        }
48    }
49
50    // javadoc is inherited
51    public boolean createSecondaryKey(TupleInput primaryKeyInput,
52                                      Object dataInput,
53                                      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)
61            binding.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)
70            binding.entryToObject(null, dataInput);
71
72        return entity.nullifyForeignKey(keyName) ? dataInput : null;
73    }
74}
75