Commit a73383e 1 parent 8e81caf commit a73383e Copy full SHA for a73383e
File tree 4 files changed +17
-16
lines changed
4 files changed +17
-16
lines changed Original file line number Diff line number Diff line change @@ -21,9 +21,10 @@ public static void main(String[] args) {
21
21
String db_path = args [0 ];
22
22
23
23
System .out .println ("RocksDBSample" );
24
+ RocksDB db = null ;
24
25
25
26
try {
26
- RocksDB db = RocksDB .open (db_path );
27
+ db = RocksDB .open (db_path );
27
28
db .put ("hello" .getBytes (), "world" .getBytes ());
28
29
byte [] value = db .get ("hello" .getBytes ());
29
30
System .out .format ("Get('hello') = %s\n " ,
@@ -67,13 +68,11 @@ public static void main(String[] args) {
67
68
assert (len == RocksDB .NOT_FOUND );
68
69
len = db .get (testKey , enoughArray );
69
70
assert (len == testValue .length );
70
- try {
71
- db .close ();
72
- } catch (IOException e ) {
73
- System .err .println (e );
74
- }
75
71
} catch (RocksDBException e ) {
76
72
System .err .println (e );
77
73
}
74
+ if (db != null ) {
75
+ db .close ();
76
+ }
78
77
}
79
78
}
Original file line number Diff line number Diff line change 16
16
* All methods of this class could potentially throw RocksDBException, which
17
17
* indicates sth wrong at the rocksdb library side and the call failed.
18
18
*/
19
- public class RocksDB implements Closeable {
19
+ public class RocksDB {
20
20
public static final int NOT_FOUND = -1 ;
21
21
/**
22
22
* The factory constructor of RocksDB that opens a RocksDB instance given
@@ -33,8 +33,8 @@ public static RocksDB open(String path) throws RocksDBException {
33
33
return db ;
34
34
}
35
35
36
- @ Override public void close () throws IOException {
37
- if (nativeHandle != 0 ) {
36
+ public synchronized void close () {
37
+ if (nativeHandle_ != 0 ) {
38
38
close0 ();
39
39
}
40
40
}
@@ -80,11 +80,15 @@ public byte[] get(byte[] key) throws RocksDBException {
80
80
return get (key , key .length );
81
81
}
82
82
83
+ @ Override protected void finalize () {
84
+ close ();
85
+ }
86
+
83
87
/**
84
88
* Private constructor.
85
89
*/
86
90
private RocksDB () {
87
- nativeHandle = - 1 ;
91
+ nativeHandle_ = 0 ;
88
92
}
89
93
90
94
// native methods
@@ -99,5 +103,5 @@ private native byte[] get(
99
103
byte [] key , int keyLen ) throws RocksDBException ;
100
104
private native void close0 ();
101
105
102
- private long nativeHandle ;
106
+ private long nativeHandle_ ;
103
107
}
Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ class RocksDBJni {
29
29
// that stores the pointer to rocksdb::DB.
30
30
static jfieldID getHandleFieldID (JNIEnv* env) {
31
31
static jfieldID fid = env->GetFieldID (
32
- getJClass (env), " nativeHandle " , " J" );
32
+ getJClass (env), " nativeHandle_ " , " J" );
33
33
assert (fid != nullptr );
34
34
return fid;
35
35
}
Original file line number Diff line number Diff line change @@ -147,6 +147,7 @@ jint Java_org_rocksdb_RocksDB_get___3BI_3BI(
147
147
env->ReleaseByteArrayElements (jvalue, value, JNI_ABORT);
148
148
return kNotFound ;
149
149
} else if (!s.ok ()) {
150
+ env->ReleaseByteArrayElements (jvalue, value, JNI_ABORT);
150
151
// Here since we are throwing a Java exception from c++ side.
151
152
// As a result, c++ does not know calling this function will in fact
152
153
// throwing an exception. As a result, the execution flow will
@@ -164,10 +165,7 @@ jint Java_org_rocksdb_RocksDB_get___3BI_3BI(
164
165
165
166
memcpy (value, cvalue.c_str (), length);
166
167
env->ReleaseByteArrayElements (jvalue, value, JNI_COMMIT);
167
- if (cvalue_len > length) {
168
- return static_cast <jint>(cvalue_len);
169
- }
170
- return length;
168
+ return static_cast <jint>(cvalue_len);
171
169
}
172
170
173
171
/*
You can’t perform that action at this time.
0 commit comments