summaryrefslogtreecommitdiff
path: root/lang/java/src/com/sleepycat/bind/tuple/TupleBase.java
blob: 62cfc75daf74c53516b9557c02f343889fbfd603 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2000, 2015 Oracle and/or its affiliates.  All rights reserved.
 *
 */

package com.sleepycat.bind.tuple;

import com.sleepycat.db.DatabaseEntry;

/**
 * A base class for tuple bindings and tuple key creators that provides control
 * over the allocation of the output buffer.
 *
 * <p>Tuple bindings and key creators append data to a {@link TupleOutput}
 * instance, which is also a {@link com.sleepycat.util.FastOutputStream}
 * instance.  This object has a byte array buffer that is resized when it is
 * full.  The reallocation of this buffer can be a performance factor for
 * some applications using large objects.  To manage this issue, the {@link
 * #setTupleBufferSize} method may be used to control the initial size of the
 * buffer, and the {@link #getTupleOutput} method may be overridden by
 * subclasses to take over creation of the TupleOutput object.</p>
 */
public class TupleBase<E> {

    private int outputBufferSize;

    /**
     * Initializes the initial output buffer size to zero.
     *
     * <p>Unless {@link #setTupleBufferSize} is called, the default {@link
     * com.sleepycat.util.FastOutputStream#DEFAULT_INIT_SIZE} size will be
     * used.</p>
     */
    public TupleBase() {
        outputBufferSize = 0;
    }

    /**
     * Sets the initial byte size of the output buffer that is allocated by the
     * default implementation of {@link #getTupleOutput}.
     *
     * <p>If this property is zero (the default), the default {@link
     * com.sleepycat.util.FastOutputStream#DEFAULT_INIT_SIZE} size is used.</p>
     *
     * @param byteSize the initial byte size of the output buffer, or zero to
     * use the default size.
     */
    public void setTupleBufferSize(int byteSize) {
        outputBufferSize = byteSize;
    }

    /**
     * Returns the initial byte size of the output buffer.
     *
     * @return the initial byte size of the output buffer.
     *
     * @see #setTupleBufferSize
     */
    public int getTupleBufferSize() {
        return outputBufferSize;
    }

    /**
     * Returns an empty TupleOutput instance that will be used by the tuple
     * binding or key creator.
     *
     * <p>The default implementation of this method creates a new TupleOutput
     * with an initial buffer size that can be changed using the {@link
     * #setTupleBufferSize} method.</p>
     *
     * <p>This method may be overridden to return a TupleOutput instance.  For
     * example, an instance per thread could be created and returned by this
     * method.  If a TupleOutput instance is reused, be sure to call its
     * {@link com.sleepycat.util.FastOutputStream#reset} method before each
     * use.</p>
     *
     * @param object is the object to be written to the tuple output, and may
     * be used by subclasses to determine the size of the output buffer.
     *
     * @return an empty TupleOutput instance.
     *
     * @see #setTupleBufferSize
     */
    protected TupleOutput getTupleOutput(E object) {
        int byteSize = getTupleBufferSize();
        if (byteSize != 0) {
            return new TupleOutput(new byte[byteSize]);
        } else {
            return new TupleOutput();
        }
    }

    /**
     * Utility method to set the data in a entry buffer to the data in a tuple
     * output object.
     *
     * @param output is the source tuple output object.
     *
     * @param entry is the destination entry buffer.
     */
    public static void outputToEntry(TupleOutput output, DatabaseEntry entry) {

        entry.setData(output.getBufferBytes(), output.getBufferOffset(),
                      output.getBufferLength());
    }

    /**
     * Utility method to set the data in a entry buffer to the data in a tuple
     * input object.
     *
     * @param input is the source tuple input object.
     *
     * @param entry is the destination entry buffer.
     */
    public static void inputToEntry(TupleInput input, DatabaseEntry entry) {

        entry.setData(input.getBufferBytes(), input.getBufferOffset(),
                      input.getBufferLength());
    }

    /**
     * Utility method to create a new tuple input object for reading the data
     * from a given buffer.  If an existing input is reused, it is reset before
     * returning it.
     *
     * @param entry is the source entry buffer.
     *
     * @return the new tuple input object.
     */
    public static TupleInput entryToInput(DatabaseEntry entry) {

        return new TupleInput(entry.getData(), entry.getOffset(),
                              entry.getSize());
    }

    /**
     * Utility method for use by bindings to create a tuple output object.
     *
     * @return a new tuple output object.
     *
     * @deprecated replaced by {@link #getTupleOutput}
     */
    public static TupleOutput newOutput() {

        return new TupleOutput();
    }

    /**
     * Utility method for use by bindings to create a tuple output object
     * with a specific starting size.
     *
     * @return a new tuple output object.
     *
     * @deprecated replaced by {@link #getTupleOutput}
     */
    public static TupleOutput newOutput(byte[] buffer) {

        return new TupleOutput(buffer);
    }
}