/
LevelDB.java
373 lines (322 loc) · 11.4 KB
/
LevelDB.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.spark.util.kvstore;
import java.io.File;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicReference;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import org.fusesource.leveldbjni.JniDBFactory;
import org.iq80.leveldb.DB;
import org.iq80.leveldb.Options;
import org.iq80.leveldb.WriteBatch;
import org.apache.spark.annotation.Private;
/**
* Implementation of KVStore that uses LevelDB as the underlying data store.
*/
@Private
public class LevelDB implements KVStore {
@VisibleForTesting
static final long STORE_VERSION = 1L;
@VisibleForTesting
static final byte[] STORE_VERSION_KEY = "__version__".getBytes(UTF_8);
/** DB key where app metadata is stored. */
private static final byte[] METADATA_KEY = "__meta__".getBytes(UTF_8);
/** DB key where type aliases are stored. */
private static final byte[] TYPE_ALIASES_KEY = "__types__".getBytes(UTF_8);
final AtomicReference<DB> _db;
final KVStoreSerializer serializer;
/**
* Keep a mapping of class names to a shorter, unique ID managed by the store. This serves two
* purposes: make the keys stored on disk shorter, and spread out the keys, since class names
* will often have a long, redundant prefix (think "org.apache.spark.").
*/
private final ConcurrentMap<String, byte[]> typeAliases;
private final ConcurrentMap<Class<?>, LevelDBTypeInfo> types;
/**
* Trying to close a JNI LevelDB handle with a closed DB causes JVM crashes. This is used to
* ensure that all iterators are correctly closed before LevelDB is closed. Use soft reference
* to ensure that the iterator can be GCed, when it is only referenced here.
*/
private final ConcurrentLinkedQueue<SoftReference<LevelDBIterator<?>>> iteratorTracker;
public LevelDB(File path) throws Exception {
this(path, new KVStoreSerializer());
}
public LevelDB(File path, KVStoreSerializer serializer) throws Exception {
this.serializer = serializer;
this.types = new ConcurrentHashMap<>();
Options options = new Options();
options.createIfMissing(true);
this._db = new AtomicReference<>(JniDBFactory.factory.open(path, options));
byte[] versionData = db().get(STORE_VERSION_KEY);
if (versionData != null) {
long version = serializer.deserializeLong(versionData);
if (version != STORE_VERSION) {
close();
throw new UnsupportedStoreVersionException();
}
} else {
db().put(STORE_VERSION_KEY, serializer.serialize(STORE_VERSION));
}
Map<String, byte[]> aliases;
try {
aliases = get(TYPE_ALIASES_KEY, TypeAliases.class).aliases;
} catch (NoSuchElementException e) {
aliases = new HashMap<>();
}
typeAliases = new ConcurrentHashMap<>(aliases);
iteratorTracker = new ConcurrentLinkedQueue<>();
}
@Override
public <T> T getMetadata(Class<T> klass) throws Exception {
try {
return get(METADATA_KEY, klass);
} catch (NoSuchElementException nsee) {
return null;
}
}
@Override
public void setMetadata(Object value) throws Exception {
if (value != null) {
put(METADATA_KEY, value);
} else {
db().delete(METADATA_KEY);
}
}
<T> T get(byte[] key, Class<T> klass) throws Exception {
byte[] data = db().get(key);
if (data == null) {
throw new NoSuchElementException(new String(key, UTF_8));
}
return serializer.deserialize(data, klass);
}
private void put(byte[] key, Object value) throws Exception {
Preconditions.checkArgument(value != null, "Null values are not allowed.");
db().put(key, serializer.serialize(value));
}
@Override
public <T> T read(Class<T> klass, Object naturalKey) throws Exception {
Preconditions.checkArgument(naturalKey != null, "Null keys are not allowed.");
byte[] key = getTypeInfo(klass).naturalIndex().start(null, naturalKey);
return get(key, klass);
}
@Override
public void write(Object value) throws Exception {
Preconditions.checkArgument(value != null, "Null values are not allowed.");
LevelDBTypeInfo ti = getTypeInfo(value.getClass());
try (WriteBatch batch = db().createWriteBatch()) {
byte[] data = serializer.serialize(value);
synchronized (ti) {
Object existing;
try {
existing = get(ti.naturalIndex().entityKey(null, value), value.getClass());
} catch (NoSuchElementException e) {
existing = null;
}
PrefixCache cache = new PrefixCache(value);
byte[] naturalKey = ti.naturalIndex().toKey(ti.naturalIndex().getValue(value));
for (LevelDBTypeInfo.Index idx : ti.indices()) {
byte[] prefix = cache.getPrefix(idx);
idx.add(batch, value, existing, data, naturalKey, prefix);
}
db().write(batch);
}
}
}
@Override
public void delete(Class<?> type, Object naturalKey) throws Exception {
Preconditions.checkArgument(naturalKey != null, "Null keys are not allowed.");
try (WriteBatch batch = db().createWriteBatch()) {
LevelDBTypeInfo ti = getTypeInfo(type);
byte[] key = ti.naturalIndex().start(null, naturalKey);
synchronized (ti) {
byte[] data = db().get(key);
if (data != null) {
Object existing = serializer.deserialize(data, type);
PrefixCache cache = new PrefixCache(existing);
byte[] keyBytes = ti.naturalIndex().toKey(ti.naturalIndex().getValue(existing));
for (LevelDBTypeInfo.Index idx : ti.indices()) {
idx.remove(batch, existing, keyBytes, cache.getPrefix(idx));
}
db().write(batch);
}
}
} catch (NoSuchElementException nse) {
// Ignore.
}
}
@Override
public <T> KVStoreView<T> view(Class<T> type) throws Exception {
return new KVStoreView<T>() {
@Override
public Iterator<T> iterator() {
try {
LevelDBIterator<T> it = new LevelDBIterator<>(type, LevelDB.this, this);
iteratorTracker.add(new SoftReference<>(it));
return it;
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
};
}
@Override
public <T> boolean removeAllByIndexValues(
Class<T> klass,
String index,
Collection<?> indexValues) throws Exception {
LevelDBTypeInfo.Index naturalIndex = getTypeInfo(klass).naturalIndex();
boolean removed = false;
KVStoreView<T> view = view(klass).index(index);
for (Object indexValue : indexValues) {
for (T value: view.first(indexValue).last(indexValue)) {
Object itemKey = naturalIndex.getValue(value);
delete(klass, itemKey);
removed = true;
}
}
return removed;
}
@Override
public long count(Class<?> type) throws Exception {
LevelDBTypeInfo.Index idx = getTypeInfo(type).naturalIndex();
return idx.getCount(idx.end(null));
}
@Override
public long count(Class<?> type, String index, Object indexedValue) throws Exception {
LevelDBTypeInfo.Index idx = getTypeInfo(type).index(index);
return idx.getCount(idx.end(null, indexedValue));
}
@Override
public void close() throws IOException {
synchronized (this._db) {
DB _db = this._db.getAndSet(null);
if (_db == null) {
return;
}
try {
if (iteratorTracker != null) {
for (SoftReference<LevelDBIterator<?>> ref: iteratorTracker) {
LevelDBIterator<?> it = ref.get();
if (it != null) {
it.close();
}
}
}
_db.close();
} catch (IOException ioe) {
throw ioe;
} catch (Exception e) {
throw new IOException(e.getMessage(), e);
}
}
}
/**
* Closes the given iterator if the DB is still open. Trying to close a JNI LevelDB handle
* with a closed DB can cause JVM crashes, so this ensures that situation does not happen.
*/
void closeIterator(LevelDBIterator<?> it) throws IOException {
notifyIteratorClosed(it);
synchronized (this._db) {
DB _db = this._db.get();
if (_db != null) {
it.close();
}
}
}
/**
* Remove iterator from iterator tracker. `LevelDBIterator` calls it to notify
* iterator is closed.
*/
void notifyIteratorClosed(LevelDBIterator<?> it) {
iteratorTracker.removeIf(ref -> it.equals(ref.get()));
}
/** Returns metadata about indices for the given type. */
LevelDBTypeInfo getTypeInfo(Class<?> type) throws Exception {
LevelDBTypeInfo ti = types.get(type);
if (ti == null) {
LevelDBTypeInfo tmp = new LevelDBTypeInfo(this, type, getTypeAlias(type));
ti = types.putIfAbsent(type, tmp);
if (ti == null) {
ti = tmp;
}
}
return ti;
}
/**
* Try to avoid use-after close since that has the tendency of crashing the JVM. This doesn't
* prevent methods that retrieved the instance from using it after close, but hopefully will
* catch most cases; otherwise, we'll need some kind of locking.
*/
DB db() {
DB _db = this._db.get();
if (_db == null) {
throw new IllegalStateException("DB is closed.");
}
return _db;
}
private byte[] getTypeAlias(Class<?> klass) throws Exception {
byte[] alias = typeAliases.get(klass.getName());
if (alias == null) {
synchronized (typeAliases) {
byte[] tmp = String.valueOf(typeAliases.size()).getBytes(UTF_8);
alias = typeAliases.putIfAbsent(klass.getName(), tmp);
if (alias == null) {
alias = tmp;
put(TYPE_ALIASES_KEY, new TypeAliases(typeAliases));
}
}
}
return alias;
}
/** Needs to be public for Jackson. */
public static class TypeAliases {
public Map<String, byte[]> aliases;
TypeAliases(Map<String, byte[]> aliases) {
this.aliases = aliases;
}
TypeAliases() {
this(null);
}
}
private static class PrefixCache {
private final Object entity;
private final Map<LevelDBTypeInfo.Index, byte[]> prefixes;
PrefixCache(Object entity) {
this.entity = entity;
this.prefixes = new HashMap<>();
}
byte[] getPrefix(LevelDBTypeInfo.Index idx) throws Exception {
byte[] prefix = null;
if (idx.isChild()) {
prefix = prefixes.get(idx.parent());
if (prefix == null) {
prefix = idx.parent().childPrefix(idx.parent().getValue(entity));
prefixes.put(idx.parent(), prefix);
}
}
return prefix;
}
}
}