-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcom_github_hf_leveldb_implementation_NativeLevelDB.cc
More file actions
358 lines (245 loc) · 10.6 KB
/
com_github_hf_leveldb_implementation_NativeLevelDB.cc
File metadata and controls
358 lines (245 loc) · 10.6 KB
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
/*
* Stojan Dimitrovski
*
* Copyright (c) 2014, Stojan Dimitrovski <sdimitrovski@gmail.com>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "com_github_hf_leveldb_implementation_NativeLevelDB.h"
#include <iostream>
#include "leveldb/db.h"
#include "leveldb/write_batch.h"
#include "leveldb/env.h"
#include "leveldb/cache.h"
#include <android/log.h>
// Redirects leveldb's logging to the Android logger.
class AndroidLogger : public leveldb::Logger {
public:
virtual void Logv(const char* format, va_list ap) {
__android_log_vprint(ANDROID_LOG_INFO, "com.github.hf.leveldb:N", format, ap);
}
};
// Holds references to heap-allocated native objects so that they can be
// closed in Java_com_github_hf_leveldb_implementation_NativeLevelDB_nclose.
class NDBHolder {
public:
NDBHolder(leveldb::DB* ldb, AndroidLogger* llogger, leveldb::Cache* lcache) : db(ldb), logger(llogger), cache(lcache) {}
leveldb::DB* db;
AndroidLogger* logger;
leveldb::Cache* cache;
};
// Throws the appropriate Java exception for the given status. Make sure you
// check IsNotFound() and similar possible non-exception statuses before calling
// this. Please release all Java references before calling this.
void throwExceptionFromStatus(JNIEnv *env, leveldb::Status &status) {
if (status.ok()) {
return;
}
if (status.IsIOError()) {
jclass ioExceptionClass = env->FindClass("com/github/hf/leveldb/exception/LevelDBIOException");
env->ThrowNew(ioExceptionClass, status.ToString().data());
} else if (status.IsCorruption()) {
jclass corruptionExceptionClass = env->FindClass("com/github/hf/leveldb/exception/LevelDBCorruptionException");
env->ThrowNew(corruptionExceptionClass, status.ToString().data());
} else if (status.IsNotFound()) {
jclass notFoundExceptionClass = env->FindClass("com/github/hf/leveldb/exception/LevelDBNotFoundException");
env->ThrowNew(notFoundExceptionClass, status.ToString().data());
} else {
jclass exceptionClass = env->FindClass("com/github/hf/leveldb/exception/LevelDBException");
env->ThrowNew(exceptionClass, status.ToString().data());
}
}
JNIEXPORT jlong JNICALL Java_com_github_hf_leveldb_implementation_NativeLevelDB_nopen
(JNIEnv *env, jclass cself, jboolean createIfMissing, jboolean paranoidChecks, jboolean reuseLogs, jboolean exceptionIfExists, jint cacheSize, jint blockSize, jint writeBufferSize, jint maxOpenFiles, jstring path) {
const char *nativePath = env->GetStringUTFChars(path, 0);
leveldb::DB *db;
AndroidLogger* logger = new AndroidLogger();
leveldb::Cache* cache = NULL;
if (cacheSize != 0) {
cache = leveldb::NewLRUCache((size_t) cacheSize);
}
leveldb::Options options;
options.create_if_missing = createIfMissing == JNI_TRUE;
options.info_log = logger;
options.paranoid_checks = paranoidChecks == JNI_TRUE;
options.reuse_logs = reuseLogs == JNI_TRUE;
options.error_if_exists = exceptionIfExists == JNI_TRUE;
if (cache != NULL) {
options.block_cache = cache;
}
if (blockSize > 0) {
options.block_size = (size_t) blockSize;
}
if (writeBufferSize > 0) {
options.write_buffer_size = (size_t) writeBufferSize;
}
if (maxOpenFiles > 0) {
options.max_open_files = (int) maxOpenFiles;
}
leveldb::Status status = leveldb::DB::Open(options, nativePath, &db);
env->ReleaseStringUTFChars(path, nativePath);
if (status.ok()) {
NDBHolder* holder = new NDBHolder(db, logger, cache);
return (jlong) holder;
} else {
delete logger;
delete cache;
}
throwExceptionFromStatus(env, status);
return 0;
}
JNIEXPORT void JNICALL Java_com_github_hf_leveldb_implementation_NativeLevelDB_nclose
(JNIEnv *env, jclass cself, jlong ndb) {
if (ndb != 0) {
NDBHolder* holder = (NDBHolder*) ndb;
delete holder->db;
delete holder->cache;
delete holder->logger;
delete holder;
}
}
JNIEXPORT void JNICALL Java_com_github_hf_leveldb_implementation_NativeLevelDB_nput
(JNIEnv *env, jclass cself, jlong ndb, jboolean sync, jbyteArray key, jbyteArray value) {
NDBHolder* holder = (NDBHolder*) ndb;
leveldb::DB* db = holder->db;
leveldb::WriteOptions writeOptions;
writeOptions.sync = sync == JNI_TRUE;
const char* keyData = (char*) env->GetByteArrayElements(key, 0);
const char* valueData = (char*) env->GetByteArrayElements(value, 0);
leveldb::Slice keySlice (keyData, (size_t) env->GetArrayLength(key));
leveldb::Slice valueSlice (valueData, (size_t) env->GetArrayLength(value));
leveldb::Status status = db->Put(writeOptions, keySlice, valueSlice);
env->ReleaseByteArrayElements(key, (jbyte*) keyData, 0);
env->ReleaseByteArrayElements(value, (jbyte*) valueData, 0);
throwExceptionFromStatus(env, status);
}
JNIEXPORT void JNICALL Java_com_github_hf_leveldb_implementation_NativeLevelDB_nwrite
(JNIEnv *env, jclass cself, jlong ndb, jboolean sync, jlong nwb) {
NDBHolder* holder = (NDBHolder*) ndb;
leveldb::DB* db = holder->db;
leveldb::WriteOptions options;
options.sync = sync == JNI_TRUE;
leveldb::WriteBatch* wb = (leveldb::WriteBatch*) nwb;
leveldb::Status status = db->Write(options, wb);
throwExceptionFromStatus(env, status);
}
JNIEXPORT jbyteArray JNICALL Java_com_github_hf_leveldb_implementation_NativeLevelDB_nget
(JNIEnv *env, jclass cself, jlong ndb, jbyteArray key, jlong nsnapshot) {
NDBHolder* holder = (NDBHolder*) ndb;
leveldb::DB* db = holder->db;
leveldb::ReadOptions readOptions;
readOptions.snapshot = (leveldb::Snapshot*) nsnapshot;
const char* keyData = (char*) env->GetByteArrayElements(key, 0);
leveldb::Slice keySlice (keyData, env->GetArrayLength(key));
std::string value;
leveldb::Status status = db->Get(readOptions, keySlice, &value);
env->ReleaseByteArrayElements(key, (jbyte*) keyData, 0);
if (status.ok()) {
if (value.length() < 1) {
return 0;
}
jbyteArray retval = env->NewByteArray(value.length());
env->SetByteArrayRegion(retval, 0, value.length(), (jbyte*) value.data());
return retval;
} else if (status.IsNotFound()) {
return 0;
}
throwExceptionFromStatus(env, status);
return 0;
}
JNIEXPORT void JNICALL Java_com_github_hf_leveldb_implementation_NativeLevelDB_ndelete
(JNIEnv *env, jclass cself, jlong ndb, jboolean sync, jbyteArray key) {
NDBHolder* holder = (NDBHolder*) ndb;
leveldb::DB* db = holder->db;
const char* keyData = (char*) env->GetByteArrayElements(key, 0);
leveldb::Slice keySlice (keyData, (size_t) env->GetArrayLength(key));
leveldb::WriteOptions writeOptions;
writeOptions.sync = sync == JNI_TRUE;
leveldb::Status status = db->Delete(writeOptions, keySlice);
env->ReleaseByteArrayElements(key, (jbyte*) keyData, 0);
throwExceptionFromStatus(env, status);
}
JNIEXPORT jbyteArray JNICALL Java_com_github_hf_leveldb_implementation_NativeLevelDB_ngetProperty
(JNIEnv *env, jclass cself, jlong ndb, jbyteArray key) {
NDBHolder* holder = (NDBHolder*) ndb;
leveldb::DB* db = holder->db;
const char* keyData = (char*) env->GetByteArrayElements(key, 0);
leveldb::Slice keySlice (keyData, (size_t) env->GetArrayLength(key));
leveldb::ReadOptions readOptions;
std::string value;
bool ok = db->GetProperty(keySlice, &value);
env->ReleaseByteArrayElements(key, (jbyte*) keyData, 0);
if (ok) {
if (value.length() < 1) {
return 0;
}
jbyteArray retval = env->NewByteArray(value.length());
env->SetByteArrayRegion(retval, 0, value.length(), (jbyte*) value.data());
return retval;
}
return 0;
}
JNIEXPORT void JNICALL Java_com_github_hf_leveldb_implementation_NativeLevelDB_ndestroy
(JNIEnv *env, jclass cself, jstring path) {
const char *nativePath = env->GetStringUTFChars(path, 0);
leveldb::Status status = leveldb::DestroyDB(nativePath, leveldb::Options());
env->ReleaseStringUTFChars(path, nativePath);
throwExceptionFromStatus(env, status);
}
JNIEXPORT void JNICALL Java_com_github_hf_leveldb_implementation_NativeLevelDB_nrepair
(JNIEnv *env, jclass cself, jstring path) {
const char *nativePath = env->GetStringUTFChars(path, 0);
leveldb::Status status = leveldb::RepairDB(nativePath, leveldb::Options());
env->ReleaseStringUTFChars(path, nativePath);
throwExceptionFromStatus(env, status);
}
JNIEXPORT jlong JNICALL Java_com_github_hf_leveldb_implementation_NativeLevelDB_niterate
(JNIEnv *env, jclass cself, jlong ndb, jboolean fillCache, jlong nsnapshot) {
NDBHolder* holder = (NDBHolder*) ndb;
leveldb::DB* db = holder->db;
leveldb::ReadOptions options;
options.snapshot = (leveldb::Snapshot*) nsnapshot;
options.fill_cache = (bool) fillCache;
leveldb::Iterator* it = db->NewIterator(options);
return (jlong) it;
}
JNIEXPORT jlong JNICALL Java_com_github_hf_leveldb_implementation_NativeLevelDB_nsnapshot
(JNIEnv *env, jclass cself, jlong ndb) {
NDBHolder* holder = (NDBHolder*) ndb;
leveldb::DB* db = holder->db;
return (jlong) db->GetSnapshot();
}
JNIEXPORT void JNICALL Java_com_github_hf_leveldb_implementation_NativeLevelDB_nreleaseSnapshot
(JNIEnv *env, jclass cself, jlong ndb, jlong nsnapshot) {
if (nsnapshot == 0) {
return;
}
NDBHolder* holder = (NDBHolder*) ndb;
leveldb::DB* db = holder->db;
db->ReleaseSnapshot((leveldb::Snapshot*) nsnapshot);
}