This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
TableConfig.java
304 lines (260 loc) · 12.8 KB
/
TableConfig.java
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package com.indeed.mph;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.Serializable;
/**
* Configuration for TableWriter (serialized and loaded automatically for TableReader).
*
* Fluent API constructor allows you to set the following fields:
*
* keySerializer: A SmartSerializer applied to the keys (required).
*
* valueSerializer: A SmartSerializer applied to the values
* (optional, without which values are not stored).
*
* offsetStorage: The hash function maps keys to hash buckets, which
* we then need to map to offsets in the data file. By default,
* the most compact storage representation is chosen
* automatically, but you can manually override:
* - FIXED: if the entries are all a fixed size, we don't need to store offsets
* - INDEXED: offsets are just a flat table indexed by hash bucket
* - SELECTED: offsets are represented as a bit-vector of all bytes in the data
* file, and we use a Rank/Select algorithm to quickly map from hash bucket
* to corresponding starting offset
* In general, if you have many small entries SELECTED will be
* better, but INDEXED is better if individual entries are large.
*
* keyStorage: EXPLICIT by default, but can be set to IMPLICIT
* (along with specifying a signatureWidth) to omit the keys from
* table at the expense of allowing false positives. In many
* cases, you know you will only be querying existing keys so
* there's no reason to store them.
*
* rangeChecking: if not NONE, keeps track of the minimum and
* maximum keys in the table to shortcut lookups and further
* reduce false positives when using IMPLICIT keyStorage.
*
* signatureWidth: The number of bits per key to use in a bloom
* filter (required for IMPLICIT keyStorage).
*
* maxHeapUsage: If positive, the limit beyond which offsets are
* mmapped instead of being stored directly in the heap. By
* default we always store offsets in the heap.
*
* maxDataHeapUsage: If positive, the limit beyond which data is
* mmapped instead of being stored directly in the heap. By
* default we never store data in the heap.
*
* debugDuplicateKeys: If true, asks TableWriter to attempt to
* determine which keys were duplicated on failure to build the
* hash function.
*
* @param <K> key type
* @param <V> value type
*
* @author alexs
*/
public class TableConfig<K, V> implements Serializable {
public static final long DEFAULT_SHARD_SIZE = 64 * 1024 * 1024;
private static final long serialVersionUID = 927763169;
private final SmartSerializer<? super K> keySerializer;
private final SmartSerializer<? super V> valueSerializer;
private final LinearDiophantineEquation entrySizeEq;
private final KeyValidator<K, V> keyValidator;
private final KeyStorage keyStorage;
private final OffsetStorage offsetStorage;
private final RangeChecking rangeChecking;
private final int signatureWidth;
private final long maxHeapUsage;
private final long maxDataHeapUsage;
private final long tempShardSize;
private final boolean debugDuplicateKeys;
TableConfig(@Nullable final SmartSerializer<? super K> keySerializer,
@Nullable final SmartSerializer<? super V> valueSerializer,
@Nullable final KeyValidator<K, V> keyValidator,
final KeyStorage keyStorage,
final OffsetStorage offsetStorage,
final RangeChecking rangeChecking,
final int signatureWidth,
final long maxHeapUsage,
final long maxDataHeapUsage,
final long tempShardSize,
final boolean debugDuplicateKeys) {
this.keySerializer = keySerializer;
this.valueSerializer = valueSerializer;
this.keyValidator = keyValidator;
this.keyStorage = keyStorage;
this.offsetStorage = offsetStorage;
this.rangeChecking = rangeChecking;
this.signatureWidth = signatureWidth;
this.maxHeapUsage = maxHeapUsage;
this.maxDataHeapUsage = maxDataHeapUsage;
this.tempShardSize = tempShardSize;
this.debugDuplicateKeys = debugDuplicateKeys;
final LinearDiophantineEquation valueSizeEq = valueSerializer == null ?
LinearDiophantineEquation.constantValue(0L) :
valueSerializer.size() == null ? LinearDiophantineEquation.multipleOf(1L) : valueSerializer.size();
final LinearDiophantineEquation keySizeEq =
(KeyStorage.IMPLICIT.equals(keyStorage) || keySerializer == null) ?
LinearDiophantineEquation.constantValue(0L) : keySerializer.size();
this.entrySizeEq =
(keySizeEq == null ? LinearDiophantineEquation.multipleOf(1L) : keySizeEq).add(valueSizeEq);
}
public TableConfig() {
this(null, null, new EqualKeyValidator<>(), KeyStorage.EXPLICIT, OffsetStorage.AUTOMATIC, RangeChecking.NONE, 0, 0, 0, DEFAULT_SHARD_SIZE, false);
}
public SmartSerializer<? super K> getKeySerializer() {
return keySerializer;
}
public SmartSerializer<? super V> getValueSerializer() {
return valueSerializer;
}
public KeyValidator<K, V> getKeyValidator() {
return keyValidator;
}
public KeyStorage getKeyStorage() {
return keyStorage;
}
public OffsetStorage getOffsetStorage() {
return offsetStorage;
}
public RangeChecking getRangeChecking() {
return rangeChecking;
}
public int getSignatureWidth() {
return signatureWidth;
}
public long getMaxHeapUsage() {
return maxHeapUsage;
}
public long getMaxDataHeapUsage() {
return maxDataHeapUsage;
}
public long getTempShardSize() {
return tempShardSize;
}
public boolean getDebugDuplicateKeys() {
return debugDuplicateKeys;
}
public LinearDiophantineEquation getEntrySize() {
return entrySizeEq;
}
public long sizeOf(final K key, final V value) throws IOException {
return (TableConfig.KeyStorage.IMPLICIT.equals(getKeyStorage()) ? 0 : getKeySerializer().sizeOf(key)) +
(getValueSerializer() == null ? 0 : getValueSerializer().sizeOf(value));
}
// We add in an extra 1*n to ensure that every compressed offset
// is unique.
// TODO: consider checking case-by-case if this is needed
public long compressOffset(final long offset, final long n) {
return entrySizeEq.solveForNth(offset, n) + n;
}
public long decompressOffset(final long value, final long n) {
return entrySizeEq.applyNth(value - n, n);
}
public OffsetStorage chooseBestOffsetStorage(final long numEntries, final long dataSize) {
if (entrySizeEq.isConstant()) {
return OffsetStorage.FIXED;
}
final long indexedSize = getIndexedOffsetSize(numEntries, dataSize);
final long selectedSize = getSelectedOffsetSize(numEntries, dataSize);
return indexedSize <= selectedSize ? OffsetStorage.INDEXED : OffsetStorage.SELECTED;
}
public long getIndexedOffsetSize(final long numEntries, final long dataSize) {
return numEntries * bytesPerOffset(numEntries, dataSize);
}
public long getSelectedOffsetSize(final long numEntries, final long dataSize) {
final long maxValue = compressOffset(dataSize, numEntries);
return ((maxValue * 3L) / 64L);
}
public int bytesPerOffset(final long numEntries, final long dataSize) {
return bytesPerLong(dataSize);
}
public static int bytesPerLong(final long maxValue) { // currently handle only power of 2 bytes
if (maxValue < Short.MAX_VALUE) {
return 2;
}
if (maxValue < Integer.MAX_VALUE) {
return 4;
}
return 8;
}
public boolean isValid() {
return keySerializer != null &&
(!OffsetStorage.FIXED.equals(offsetStorage) || entrySizeEq.isConstant());
}
public K readKey(final DataInput in) throws IOException {
return (K) ((TableConfig.KeyStorage.IMPLICIT.equals(keyStorage) || keySerializer == null) ? null :
keySerializer.read(in));
}
public V readValue(final DataInput in) throws IOException {
return (V) ((valueSerializer == null) ? null : valueSerializer.read(in));
}
public void write(final K k, final V v, final DataOutput out) throws IOException {
if (!KeyStorage.IMPLICIT.equals(keyStorage)) {
keySerializer.write(k, out);
}
if (valueSerializer != null) {
valueSerializer.write(v, out);
}
}
public TableConfig<K, V> withKeySerializer(final SmartSerializer<? super K> serializer) {
return new TableConfig<K,V>(serializer, valueSerializer, keyValidator, keyStorage, offsetStorage, rangeChecking, signatureWidth, maxHeapUsage, maxDataHeapUsage, tempShardSize, debugDuplicateKeys);
}
public TableConfig<K, V> withValueSerializer(final SmartSerializer<? super V> serializer) {
return new TableConfig<K,V>(keySerializer, serializer, keyValidator, keyStorage, offsetStorage, rangeChecking, signatureWidth, maxHeapUsage, maxDataHeapUsage, tempShardSize, debugDuplicateKeys);
}
public TableConfig<K, V> withKeyValidator(final KeyValidator<K, V> validator) {
return new TableConfig<K,V>(keySerializer, valueSerializer, validator, keyStorage, offsetStorage, rangeChecking, signatureWidth, maxHeapUsage, maxDataHeapUsage, tempShardSize, debugDuplicateKeys);
}
public TableConfig<K, V> withKeyStorage(final KeyStorage storage) {
return new TableConfig<K,V>(keySerializer, valueSerializer, keyValidator, storage, offsetStorage, rangeChecking, signatureWidth, maxHeapUsage, maxDataHeapUsage, tempShardSize, debugDuplicateKeys);
}
public TableConfig<K, V> withOffsetStorage(final OffsetStorage storage) {
return new TableConfig<K,V>(keySerializer, valueSerializer, keyValidator, keyStorage, storage, rangeChecking, signatureWidth, maxHeapUsage, maxDataHeapUsage, tempShardSize, debugDuplicateKeys);
}
public TableConfig<K, V> withRangeChecking(final RangeChecking rangeCheck) {
return new TableConfig<K,V>(keySerializer, valueSerializer, keyValidator, keyStorage, offsetStorage, rangeCheck, signatureWidth, maxHeapUsage, maxDataHeapUsage, tempShardSize, debugDuplicateKeys);
}
public TableConfig<K, V> withSignatureWidth(final int width) {
return new TableConfig<K,V>(keySerializer, valueSerializer, keyValidator, keyStorage, offsetStorage, rangeChecking, width, maxHeapUsage, maxDataHeapUsage, tempShardSize, debugDuplicateKeys);
}
public TableConfig<K, V> withMaxHeapUsage(final long maxHeap) {
return new TableConfig<K,V>(keySerializer, valueSerializer, keyValidator, keyStorage, offsetStorage, rangeChecking, signatureWidth, maxHeap, maxDataHeapUsage, tempShardSize, debugDuplicateKeys);
}
public TableConfig<K, V> withMaxDataHeapUsage(final long maxDataHeap) {
return new TableConfig<K,V>(keySerializer, valueSerializer, keyValidator, keyStorage, offsetStorage, rangeChecking, signatureWidth, maxHeapUsage, maxDataHeap, tempShardSize, debugDuplicateKeys);
}
public TableConfig<K, V> withTempShardSize(final long shardSize) {
return new TableConfig<K,V>(keySerializer, valueSerializer, keyValidator, keyStorage, offsetStorage, rangeChecking, signatureWidth, maxHeapUsage, maxDataHeapUsage, shardSize, debugDuplicateKeys);
}
public TableConfig<K, V> withDebugDuplicateKeys(final boolean debugDupKeys) {
return new TableConfig<K,V>(keySerializer, valueSerializer, keyValidator, keyStorage, offsetStorage, rangeChecking, signatureWidth, maxHeapUsage, maxDataHeapUsage, tempShardSize, debugDupKeys);
}
public String toString() {
return "[TableConfig keys: " + keySerializer + " values: " + valueSerializer +
" keyStorage: " + keyStorage + " offsetStorage: " + offsetStorage +
" rangeChecking: " + rangeChecking +
" validator: " + keyValidator + " signatureWidth: " + signatureWidth +
" maxHeapUsage: " + maxHeapUsage + " maxDataHeapUsage: " + maxDataHeapUsage +
" entrySize: " + entrySizeEq + " debugDupKeys: " + debugDuplicateKeys + "]";
}
public enum KeyStorage {
EXPLICIT, // default
IMPLICIT // don't actually store the keys
}
public enum OffsetStorage {
AUTOMATIC, // choose optimal storage
INDEXED, // an indexed array of offsets per hash
SELECTED, // a rank-select lookup per hash
FIXED // fixed size entries
}
public enum RangeChecking {
MIN_AND_MAX,
NONE,
AUTOMATIC,
}
}