• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt/router/db-4.8.30/java/src/com/sleepycat/bind/tuple/
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
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 */
24public class TupleTupleMarshalledKeyCreator<E extends
25    MarshalledTupleEntry & MarshalledTupleKeyEntity>
26    extends TupleTupleKeyCreator<E> {
27
28    private String keyName;
29    private TupleTupleMarshalledBinding<E> binding;
30
31    /**
32     * Creates a tuple-tuple marshalled key creator.
33     *
34     * @param binding is the binding used for the tuple-tuple entity.
35     *
36     * @param keyName is the key name passed to the {@link
37     * MarshalledTupleKeyEntity#marshalSecondaryKey} method to identify the
38     * index key.
39     */
40    public TupleTupleMarshalledKeyCreator(TupleTupleMarshalledBinding<E>
41                                          binding,
42                                          String keyName) {
43
44        this.binding = binding;
45        this.keyName = keyName;
46    }
47
48    // javadoc is inherited
49    public boolean createSecondaryKey(TupleInput primaryKeyInput,
50                                      TupleInput dataInput,
51                                      TupleOutput indexKeyOutput) {
52
53        /* The primary key is unmarshalled before marshalling the index key, to
54         * account for cases where the index key includes fields taken from the
55         * primary key.
56         */
57        E entity = binding.entryToObject(primaryKeyInput, dataInput);
58        return entity.marshalSecondaryKey(keyName, indexKeyOutput);
59    }
60
61    // javadoc is inherited
62    public boolean nullifyForeignKey(TupleInput dataInput,
63                                     TupleOutput dataOutput) {
64
65        E entity = binding.entryToObject(null, dataInput);
66        if (entity.nullifyForeignKey(keyName)) {
67            binding.objectToData(entity, dataOutput);
68            return true;
69        } else {
70            return false;
71        }
72    }
73}
74