kyotocabinet-java 0.1.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/.document +5 -0
  2. data/Gemfile +15 -0
  3. data/LICENSE.txt +20 -0
  4. data/README.rdoc +19 -0
  5. data/Rakefile +62 -0
  6. data/VERSION +1 -0
  7. data/ext/kyotocabinet-java/.gitignore +2 -0
  8. data/ext/kyotocabinet-java/COPYING +674 -0
  9. data/ext/kyotocabinet-java/Cursor.java +268 -0
  10. data/ext/kyotocabinet-java/DB.java +650 -0
  11. data/ext/kyotocabinet-java/Error.java +257 -0
  12. data/ext/kyotocabinet-java/FileProcessor.java +42 -0
  13. data/ext/kyotocabinet-java/Loader.java +60 -0
  14. data/ext/kyotocabinet-java/Makefile.in +246 -0
  15. data/ext/kyotocabinet-java/MapReduce.java +127 -0
  16. data/ext/kyotocabinet-java/README +26 -0
  17. data/ext/kyotocabinet-java/Test.java +1494 -0
  18. data/ext/kyotocabinet-java/Utility.java +131 -0
  19. data/ext/kyotocabinet-java/VCmakefile +150 -0
  20. data/ext/kyotocabinet-java/ValueIterator.java +49 -0
  21. data/ext/kyotocabinet-java/Visitor.java +56 -0
  22. data/ext/kyotocabinet-java/configure.in +310 -0
  23. data/ext/kyotocabinet-java/doc/allclasses-frame.html +26 -0
  24. data/ext/kyotocabinet-java/doc/allclasses-noframe.html +26 -0
  25. data/ext/kyotocabinet-java/doc/constant-values.html +344 -0
  26. data/ext/kyotocabinet-java/doc/index-all.html +848 -0
  27. data/ext/kyotocabinet-java/doc/index.html +30 -0
  28. data/ext/kyotocabinet-java/doc/kyotocabinet/Cursor.html +711 -0
  29. data/ext/kyotocabinet-java/doc/kyotocabinet/DB.html +1674 -0
  30. data/ext/kyotocabinet-java/doc/kyotocabinet/Error.html +604 -0
  31. data/ext/kyotocabinet-java/doc/kyotocabinet/FileProcessor.html +210 -0
  32. data/ext/kyotocabinet-java/doc/kyotocabinet/MapReduce.html +461 -0
  33. data/ext/kyotocabinet-java/doc/kyotocabinet/Test.html +220 -0
  34. data/ext/kyotocabinet-java/doc/kyotocabinet/Utility.html +413 -0
  35. data/ext/kyotocabinet-java/doc/kyotocabinet/ValueIterator.html +221 -0
  36. data/ext/kyotocabinet-java/doc/kyotocabinet/Visitor.html +283 -0
  37. data/ext/kyotocabinet-java/doc/kyotocabinet/package-frame.html +33 -0
  38. data/ext/kyotocabinet-java/doc/kyotocabinet/package-summary.html +199 -0
  39. data/ext/kyotocabinet-java/doc/kyotocabinet/package-tree.html +141 -0
  40. data/ext/kyotocabinet-java/doc/overview-summary.html +285 -0
  41. data/ext/kyotocabinet-java/doc/overview-tree.html +145 -0
  42. data/ext/kyotocabinet-java/doc/package-list +1 -0
  43. data/ext/kyotocabinet-java/doc/resources/background.gif +0 -0
  44. data/ext/kyotocabinet-java/doc/resources/tab.gif +0 -0
  45. data/ext/kyotocabinet-java/doc/resources/titlebar.gif +0 -0
  46. data/ext/kyotocabinet-java/doc/resources/titlebar_end.gif +0 -0
  47. data/ext/kyotocabinet-java/doc/serialized-form.html +141 -0
  48. data/ext/kyotocabinet-java/doc/stylesheet.css +474 -0
  49. data/ext/kyotocabinet-java/example/KCDBEX1.java +44 -0
  50. data/ext/kyotocabinet-java/example/KCDBEX2.java +44 -0
  51. data/ext/kyotocabinet-java/example/Makefile +59 -0
  52. data/ext/kyotocabinet-java/kyotocabinet.cc +2187 -0
  53. data/ext/kyotocabinet-java/kyotocabinet_Cursor.h +157 -0
  54. data/ext/kyotocabinet-java/kyotocabinet_DB.h +353 -0
  55. data/ext/kyotocabinet-java/kyotocabinet_Error.h +49 -0
  56. data/ext/kyotocabinet-java/kyotocabinet_Error_XBROKEN.h +41 -0
  57. data/ext/kyotocabinet-java/kyotocabinet_Error_XDUPREC.h +41 -0
  58. data/ext/kyotocabinet-java/kyotocabinet_Error_XINVALID.h +41 -0
  59. data/ext/kyotocabinet-java/kyotocabinet_Error_XLOGIC.h +41 -0
  60. data/ext/kyotocabinet-java/kyotocabinet_Error_XMISC.h +41 -0
  61. data/ext/kyotocabinet-java/kyotocabinet_Error_XNOIMPL.h +41 -0
  62. data/ext/kyotocabinet-java/kyotocabinet_Error_XNOPERM.h +41 -0
  63. data/ext/kyotocabinet-java/kyotocabinet_Error_XNOREC.h +41 -0
  64. data/ext/kyotocabinet-java/kyotocabinet_Error_XNOREPOS.h +41 -0
  65. data/ext/kyotocabinet-java/kyotocabinet_Error_XSUCCESS.h +41 -0
  66. data/ext/kyotocabinet-java/kyotocabinet_Error_XSYSTEM.h +41 -0
  67. data/ext/kyotocabinet-java/kyotocabinet_MapReduce.h +33 -0
  68. data/ext/kyotocabinet-java/kyotocabinet_Utility.h +101 -0
  69. data/ext/kyotocabinet-java/kyotocabinet_ValueIterator.h +21 -0
  70. data/ext/kyotocabinet-java/overview.html +177 -0
  71. data/kyotocabinet-java.gemspec +129 -0
  72. data/lib/kyotocabinet-java.rb +141 -0
  73. data/lib/kyotocabinet-java/version.rb +5 -0
  74. metadata +222 -0
@@ -0,0 +1,44 @@
1
+ import kyotocabinet.*;
2
+
3
+ public class KCDBEX1 {
4
+ public static void main(String[] args) {
5
+
6
+ // create the object
7
+ DB db = new DB();
8
+
9
+ // open the database
10
+ if (!db.open("casket.kch", DB.OWRITER | DB.OCREATE)){
11
+ System.err.println("open error: " + db.error());
12
+ }
13
+
14
+ // store records
15
+ if (!db.set("foo", "hop") ||
16
+ !db.set("bar", "step") ||
17
+ !db.set("baz", "jump")){
18
+ System.err.println("set error: " + db.error());
19
+ }
20
+
21
+ // retrieve records
22
+ String value = db.get("foo");
23
+ if (value != null){
24
+ System.out.println(value);
25
+ } else {
26
+ System.err.println("set error: " + db.error());
27
+ }
28
+
29
+ // traverse records
30
+ Cursor cur = db.cursor();
31
+ cur.jump();
32
+ String[] rec;
33
+ while ((rec = cur.get_str(true)) != null) {
34
+ System.out.println(rec[0] + ":" + rec[1]);
35
+ }
36
+ cur.disable();
37
+
38
+ // close the database
39
+ if(!db.close()){
40
+ System.err.println("close error: " + db.error());
41
+ }
42
+
43
+ }
44
+ }
@@ -0,0 +1,44 @@
1
+ import kyotocabinet.*;
2
+
3
+ public class KCDBEX2 {
4
+ public static void main(String[] args) {
5
+
6
+ // create the object
7
+ DB db = new DB();
8
+
9
+ // open the database
10
+ if (!db.open("casket.kch", DB.OREADER)) {
11
+ System.err.println("open error: " + db.error());
12
+ }
13
+
14
+ // define the visitor
15
+ class VisitorImpl implements Visitor {
16
+ public byte[] visit_full(byte[] key, byte[] value) {
17
+ System.out.println(new String(key) + ":" + new String(value));
18
+ return NOP;
19
+ }
20
+ public byte[] visit_empty(byte[] key) {
21
+ System.err.println(new String(key) + " is missing");
22
+ return NOP;
23
+ }
24
+ }
25
+ Visitor visitor = new VisitorImpl();
26
+
27
+ // retrieve a record with visitor
28
+ if (!db.accept("foo".getBytes(), visitor, false) ||
29
+ !db.accept("dummy".getBytes(), visitor, false)) {
30
+ System.err.println("accept error: " + db.error());
31
+ }
32
+
33
+ // traverse records with visitor
34
+ if (!db.iterate(visitor, false)) {
35
+ System.err.println("iterate error: " + db.error());
36
+ }
37
+
38
+ // close the database
39
+ if(!db.close()){
40
+ System.err.println("close error: " + db.error());
41
+ }
42
+
43
+ }
44
+ }
@@ -0,0 +1,59 @@
1
+ # Makefile for sample programs of Kyoto Cabinet
2
+
3
+
4
+
5
+ #================================================================
6
+ # Setting Variables
7
+ #================================================================
8
+
9
+
10
+ # Generic settings
11
+ SHELL = /bin/sh
12
+
13
+ # Targets
14
+ MYCLASSES = KCDBEX1.class KCDBEX2.class
15
+
16
+ # Building binaries
17
+ JAVAC = javac
18
+ JAVACFLAGS = -cp .:../kyotocabinet.jar
19
+ JAVARUN = java
20
+ JAVARUNFLAGS = -cp .:../kyotocabinet.jar -Djava.library.path=.:..:/usr/local/lib
21
+
22
+
23
+
24
+ #================================================================
25
+ # Suffix rules
26
+ #================================================================
27
+
28
+
29
+ .SUFFIXES :
30
+ .SUFFIXES : .java .class
31
+
32
+ .java.class :
33
+ $(JAVAC) $(JAVACFLAGS) $<
34
+
35
+
36
+
37
+ #================================================================
38
+ # Actions
39
+ #================================================================
40
+
41
+
42
+ all : $(MYCLASSES)
43
+
44
+
45
+ run :
46
+ rm -rf casket*
47
+ $(JAVARUN) $(JAVARUNFLAGS) KCDBEX1
48
+ $(JAVARUN) $(JAVARUNFLAGS) KCDBEX2
49
+
50
+
51
+ clean :
52
+ rm -rf $(MYCLASSES) *.class casket* *~
53
+
54
+
55
+ .PHONY : all clean
56
+
57
+
58
+
59
+ # END OF FILE
@@ -0,0 +1,2187 @@
1
+ /*************************************************************************************************
2
+ * Java binding of Kyoto Cabinet.
3
+ * Copyright (C) 2009-2011 FAL Labs
4
+ * This file is part of Kyoto Cabinet.
5
+ * This program is free software: you can redistribute it and/or modify it under the terms of
6
+ * the GNU General Public License as published by the Free Software Foundation, either version
7
+ * 3 of the License, or any later version.
8
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
+ * See the GNU General Public License for more details.
11
+ * You should have received a copy of the GNU General Public License along with this program.
12
+ * If not, see <http://www.gnu.org/licenses/>.
13
+ *************************************************************************************************/
14
+
15
+
16
+ #include <kcpolydb.h>
17
+ #include <kcdbext.h>
18
+ #include <jni.h>
19
+
20
+ #include "kyotocabinet_Utility.h"
21
+ #include "kyotocabinet_Error.h"
22
+ #include "kyotocabinet_Cursor.h"
23
+ #include "kyotocabinet_DB.h"
24
+ #include "kyotocabinet_MapReduce.h"
25
+ #include "kyotocabinet_ValueIterator.h"
26
+
27
+ #define P_OBJ "java/lang/Object"
28
+ #define L_OBJ "L" P_OBJ ";"
29
+ #define P_STR "java/lang/String"
30
+ #define L_STR "L" P_STR ";"
31
+ #define P_ERR "kyotocabinet/Error"
32
+ #define L_ERR "L" P_ERR ";"
33
+ #define P_VIS "kyotocabinet/Visitor"
34
+ #define L_VIS "L" P_VIS ";"
35
+ #define P_FPROC "kyotocabinet/FileProcessor"
36
+ #define L_FPROC "L" P_FPROC ";"
37
+ #define P_CUR "kyotocabinet/Cursor"
38
+ #define L_CUR "L" P_CUR ";"
39
+ #define P_DB "kyotocabinet/DB"
40
+ #define L_DB "L" P_DB ";"
41
+ #define P_MR "kyotocabinet/MapReduce"
42
+ #define L_MR "L" P_MR ";"
43
+ #define P_VITER "kyotocabinet/ValueIterator"
44
+ #define L_VITER "L" P_VITER ";"
45
+
46
+ namespace kc = kyotocabinet;
47
+
48
+
49
+ /* precedent type declaration */
50
+ class SoftString;
51
+ class SoftArray;
52
+ class CursorBurrow;
53
+ class SoftCursor;
54
+ class SoftVisitor;
55
+ class SoftFileProcessor;
56
+ class SoftMapReduce;
57
+ typedef std::map<std::string, std::string> StringMap;
58
+ typedef std::vector<std::string> StringVector;
59
+
60
+
61
+ /* function prototypes */
62
+ static void throwruntime(JNIEnv* env, const char* message);
63
+ static void throwoutmem(JNIEnv* env);
64
+ static void throwillarg(JNIEnv* env);
65
+ static jstring newstring(JNIEnv* env, const char* str);
66
+ static jbyteArray newarray(JNIEnv* env, const char* buf, size_t size);
67
+ static jobject maptojhash(JNIEnv* env, const StringMap* map);
68
+ static jobject vectortojlist(JNIEnv* env, const StringVector* vec);
69
+ static kc::PolyDB* getdbcore(JNIEnv* env, jobject jdb);
70
+ static void throwdberror(JNIEnv* env, jobject jdb);
71
+ static SoftCursor* getcurcore(JNIEnv* env, jobject jcur);
72
+ static jobject getcurdb(JNIEnv* env, jobject jcur);
73
+
74
+
75
+ /* global variables */
76
+ const char* const p_err_children[] = {
77
+ P_ERR "$XSUCCESS",
78
+ P_ERR "$XNOIMPL",
79
+ P_ERR "$XINVALID",
80
+ P_ERR "$XNOREPOS",
81
+ P_ERR "$XNOPERM",
82
+ P_ERR "$XBROKEN",
83
+ P_ERR "$XDUPREC",
84
+ P_ERR "$XNOREC",
85
+ P_ERR "$XLOGIC",
86
+ P_ERR "$XSYSTEM",
87
+ P_ERR "$XMISC",
88
+ };
89
+ jbyteArray obj_vis_nop;
90
+ jbyteArray obj_vis_remove;
91
+
92
+
93
+ /**
94
+ * Generic options.
95
+ */
96
+ enum GenericOption {
97
+ GEXCEPTIONAL = 1 << 0,
98
+ GCONCURRENT = 1 << 1
99
+ };
100
+
101
+
102
+ /**
103
+ * Wrapper to treat a Java string as a C++ string.
104
+ */
105
+ class SoftString {
106
+ public:
107
+ explicit SoftString(JNIEnv* env, jstring jstr) :
108
+ env_(env), jstr_(jstr), str_(NULL), copied_(false) {
109
+ if (jstr) {
110
+ str_ = env_->GetStringUTFChars(jstr_, &copied_);
111
+ if (!str_) {
112
+ throwoutmem(env);
113
+ throw std::bad_alloc();
114
+ }
115
+ } else {
116
+ str_ = NULL;
117
+ }
118
+ }
119
+ ~SoftString() {
120
+ if (copied_) env_->ReleaseStringUTFChars(jstr_, str_);
121
+ }
122
+ const char* str() {
123
+ return str_;
124
+ }
125
+ private:
126
+ JNIEnv* env_;
127
+ jstring jstr_;
128
+ const char* str_;
129
+ jboolean copied_;
130
+ };
131
+
132
+
133
+ /**
134
+ * Wrapper to treat a Java byte array as a C++ byte array.
135
+ */
136
+ class SoftArray {
137
+ public:
138
+ explicit SoftArray(JNIEnv* env, jbyteArray jary) :
139
+ env_(env), jary_(jary), ary_(NULL), size_(0), copied_(false) {
140
+ if (jary) {
141
+ ary_ = env_->GetByteArrayElements(jary, &copied_);
142
+ if (!ary_) {
143
+ throwoutmem(env);
144
+ throw std::bad_alloc();
145
+ }
146
+ size_ = env_->GetArrayLength(jary);
147
+ } else {
148
+ ary_ = NULL;
149
+ }
150
+ }
151
+ ~SoftArray() {
152
+ if (copied_) env_->ReleaseByteArrayElements(jary_, ary_, JNI_ABORT);
153
+ }
154
+ const char* ptr() {
155
+ return (const char*)ary_;
156
+ }
157
+ size_t size() {
158
+ return size_;
159
+ }
160
+ private:
161
+ JNIEnv* env_;
162
+ jbyteArray jary_;
163
+ jbyte* ary_;
164
+ size_t size_;
165
+ jboolean copied_;
166
+ };
167
+
168
+
169
+ /**
170
+ * Burrow of cursors no longer in use.
171
+ */
172
+ class CursorBurrow {
173
+ private:
174
+ typedef std::vector<kc::PolyDB::Cursor*> CursorList;
175
+ public:
176
+ explicit CursorBurrow() : mlock_(), dcurs_() {}
177
+ ~CursorBurrow() {
178
+ sweap();
179
+ }
180
+ void sweap() {
181
+ kc::ScopedSpinLock lock(&mlock_);
182
+ if (dcurs_.size() > 0) {
183
+ CursorList::iterator dit = dcurs_.begin();
184
+ CursorList::iterator ditend = dcurs_.end();
185
+ while (dit != ditend) {
186
+ kc::PolyDB::Cursor* cur = *dit;
187
+ delete cur;
188
+ dit++;
189
+ }
190
+ dcurs_.clear();
191
+ }
192
+ }
193
+ void deposit(kc::PolyDB::Cursor* cur) {
194
+ kc::ScopedSpinLock lock(&mlock_);
195
+ dcurs_.push_back(cur);
196
+ }
197
+ private:
198
+ kc::SpinLock mlock_;
199
+ CursorList dcurs_;
200
+ } g_curbur;
201
+
202
+
203
+ /**
204
+ * Wrapper of a cursor.
205
+ */
206
+ class SoftCursor {
207
+ public:
208
+ explicit SoftCursor(kc::PolyDB* db) : cur_(NULL) {
209
+ cur_ = db->cursor();
210
+ }
211
+ ~SoftCursor() {
212
+ if (cur_) g_curbur.deposit(cur_);
213
+ }
214
+ kc::PolyDB::Cursor* cur() {
215
+ return cur_;
216
+ }
217
+ void disable() {
218
+ delete cur_;
219
+ cur_ = NULL;
220
+ }
221
+ private:
222
+ kc::PolyDB::Cursor* cur_;
223
+ };
224
+
225
+
226
+ /**
227
+ * Wrapper of a visitor.
228
+ */
229
+ class SoftVisitor : public kc::PolyDB::Visitor {
230
+ public:
231
+ explicit SoftVisitor(JNIEnv* env, jobject jvisitor, bool writable) :
232
+ env_(env), jvisitor_(jvisitor), writable_(writable),
233
+ cls_vis_(0), id_vis_visit_full_(0), id_vis_visit_empty_(0),
234
+ jrv_(NULL), rv_(NULL), jex_(NULL) {
235
+ cls_vis_ = env->GetObjectClass(jvisitor_);
236
+ id_vis_visit_full_ = env->GetMethodID(cls_vis_, "visit_full", "([B[B)[B");
237
+ id_vis_visit_empty_ = env->GetMethodID(cls_vis_, "visit_empty", "([B)[B");
238
+ }
239
+ ~SoftVisitor() {
240
+ cleanup();
241
+ }
242
+ jthrowable exception() {
243
+ return jex_;
244
+ }
245
+ private:
246
+ const char* visit_full(const char* kbuf, size_t ksiz,
247
+ const char* vbuf, size_t vsiz, size_t* sp) {
248
+ cleanup();
249
+ jbyteArray jkey = newarray(env_, kbuf, ksiz);
250
+ jbyteArray jvalue = newarray(env_, vbuf, vsiz);
251
+ jbyteArray jrv = (jbyteArray)env_->CallObjectMethod(jvisitor_, id_vis_visit_full_,
252
+ jkey, jvalue);
253
+ jthrowable jex = env_->ExceptionOccurred();
254
+ if (jex) {
255
+ if (jex_) env_->DeleteLocalRef(jex_);
256
+ jex_ = jex;
257
+ env_->ExceptionClear();
258
+ }
259
+ env_->DeleteLocalRef(jkey);
260
+ env_->DeleteLocalRef(jvalue);
261
+ if (!jrv) return NOP;
262
+ if (env_->IsSameObject(jrv, obj_vis_nop)) {
263
+ env_->DeleteLocalRef(jrv);
264
+ return NOP;
265
+ }
266
+ if (!writable_) {
267
+ env_->DeleteLocalRef(jrv);
268
+ throwruntime(env_, "confliction with the read-only parameter");
269
+ jex = env_->ExceptionOccurred();
270
+ if (jex) {
271
+ if (jex_) env_->DeleteLocalRef(jex_);
272
+ jex_ = jex;
273
+ env_->ExceptionClear();
274
+ }
275
+ return NOP;
276
+ }
277
+ if (env_->IsSameObject(jrv, obj_vis_remove)) {
278
+ env_->DeleteLocalRef(jrv);
279
+ return REMOVE;
280
+ }
281
+ jrv_ = jrv;
282
+ rv_ = new SoftArray(env_, jrv);
283
+ *sp = rv_->size();
284
+ return rv_->ptr();
285
+ }
286
+ const char* visit_empty(const char* kbuf, size_t ksiz, size_t* sp) {
287
+ cleanup();
288
+ jbyteArray jkey = newarray(env_, kbuf, ksiz);
289
+ jbyteArray jrv = (jbyteArray)env_->CallObjectMethod(jvisitor_, id_vis_visit_empty_, jkey);
290
+ jthrowable jex = env_->ExceptionOccurred();
291
+ if (jex) {
292
+ if (jex_) env_->DeleteLocalRef(jex_);
293
+ jex_ = jex;
294
+ env_->ExceptionClear();
295
+ }
296
+ env_->DeleteLocalRef(jkey);
297
+ if (!jrv) return NOP;
298
+ if (env_->IsSameObject(jrv, obj_vis_nop)) {
299
+ env_->DeleteLocalRef(jrv);
300
+ return NOP;
301
+ }
302
+ if (!writable_) {
303
+ env_->DeleteLocalRef(jrv);
304
+ throwruntime(env_, "confliction with the read-only parameter");
305
+ jex = env_->ExceptionOccurred();
306
+ if (jex) {
307
+ if (jex_) env_->DeleteLocalRef(jex_);
308
+ jex_ = jex;
309
+ env_->ExceptionClear();
310
+ }
311
+ return NOP;
312
+ }
313
+ if (env_->IsSameObject(jrv, obj_vis_remove)) {
314
+ env_->DeleteLocalRef(jrv);
315
+ return REMOVE;
316
+ }
317
+ jrv_ = jrv;
318
+ rv_ = new SoftArray(env_, jrv);
319
+ *sp = rv_->size();
320
+ return rv_->ptr();
321
+ }
322
+ void cleanup() {
323
+ delete rv_;
324
+ rv_ = NULL;
325
+ if (jrv_) {
326
+ env_->DeleteLocalRef(jrv_);
327
+ jrv_ = NULL;
328
+ }
329
+ }
330
+ JNIEnv* env_;
331
+ jobject jvisitor_;
332
+ bool writable_;
333
+ jclass cls_vis_;
334
+ jmethodID id_vis_visit_full_;
335
+ jmethodID id_vis_visit_empty_;
336
+ jbyteArray jrv_;
337
+ SoftArray* rv_;
338
+ jthrowable jex_;
339
+ };
340
+
341
+
342
+ /**
343
+ * Wrapper of a file processor.
344
+ */
345
+ class SoftFileProcessor : public kc::PolyDB::FileProcessor {
346
+ public:
347
+ explicit SoftFileProcessor(JNIEnv* env, jobject jproc) :
348
+ env_(env), jproc_(jproc), cls_fproc_(0), id_fproc_process_(0), jex_(NULL) {
349
+ cls_fproc_ = env->GetObjectClass(jproc_);
350
+ id_fproc_process_ = env->GetMethodID(cls_fproc_, "process", "(" L_STR "JJ)Z");
351
+ }
352
+ jthrowable exception() {
353
+ return jex_;
354
+ }
355
+ private:
356
+ bool process(const std::string& path, int64_t count, int64_t size) {
357
+ jstring jpath = newstring(env_, path.c_str());
358
+ bool rv = env_->CallBooleanMethod(jproc_, id_fproc_process_, jpath, count, size);
359
+ jthrowable jex = env_->ExceptionOccurred();
360
+ if (jex) {
361
+ jex_ = jex;
362
+ env_->ExceptionClear();
363
+ }
364
+ env_->DeleteLocalRef(jpath);
365
+ return rv;
366
+ }
367
+ JNIEnv* env_;
368
+ jobject jproc_;
369
+ jclass cls_fproc_;
370
+ jmethodID id_fproc_process_;
371
+ jthrowable jex_;
372
+ };
373
+
374
+
375
+ /**
376
+ * Wrapper of a MapReduce framework.
377
+ */
378
+ class SoftMapReduce : public kc::MapReduce {
379
+ public:
380
+ explicit SoftMapReduce(JNIEnv* env, jobject jmr) :
381
+ env_(env), jmr_(jmr), cls_mr_(0), id_mr_map_(0), id_mr_reduce_(0),
382
+ id_mr_preproc_(0), id_mr_midproc_(0), id_mr_postproc_(0),
383
+ cls_viter_(0), id_viter_init_(0), jex_(NULL) {
384
+ cls_mr_ = env->GetObjectClass(jmr_);
385
+ id_mr_map_ = env->GetMethodID(cls_mr_, "map", "([B[B)Z");
386
+ id_mr_reduce_ = env->GetMethodID(cls_mr_, "reduce", "([B" L_VITER ")Z");
387
+ id_mr_preproc_ = env->GetMethodID(cls_mr_, "preprocess", "()Z");
388
+ id_mr_midproc_ = env->GetMethodID(cls_mr_, "midprocess", "()Z");
389
+ id_mr_postproc_ = env->GetMethodID(cls_mr_, "postprocess", "()Z");
390
+ cls_viter_ = env->FindClass(P_VITER);
391
+ id_viter_init_ = env->GetMethodID(cls_viter_, "<init>", "()V");
392
+ jfieldID id_mr_ptr = env->GetFieldID(cls_mr_, "ptr_", "J");
393
+ env->SetLongField(jmr_, id_mr_ptr, (intptr_t)this);
394
+ }
395
+ jthrowable exception() {
396
+ return jex_;
397
+ }
398
+ bool emit_public(const char* kbuf, size_t ksiz, const char* vbuf, size_t vsiz) {
399
+ return emit(kbuf, ksiz, vbuf, vsiz);
400
+ }
401
+ private:
402
+ bool map(const char* kbuf, size_t ksiz, const char* vbuf, size_t vsiz) {
403
+ jbyteArray jkey = newarray(env_, kbuf, ksiz);
404
+ jbyteArray jvalue = newarray(env_, vbuf, vsiz);
405
+ bool rv = env_->CallBooleanMethod(jmr_, id_mr_map_, jkey, jvalue);
406
+ env_->DeleteLocalRef(jkey);
407
+ env_->DeleteLocalRef(jvalue);
408
+ jthrowable jex = env_->ExceptionOccurred();
409
+ if (jex) {
410
+ if (jex_) env_->DeleteLocalRef(jex_);
411
+ jex_ = jex;
412
+ env_->ExceptionClear();
413
+ }
414
+ return rv;
415
+ }
416
+ bool reduce(const char* kbuf, size_t ksiz, ValueIterator* iter) {
417
+ jbyteArray jkey = newarray(env_, kbuf, ksiz);
418
+ jobject jviter = env_->NewObject(cls_viter_, id_viter_init_);
419
+ jfieldID id_viter_ptr = env_->GetFieldID(cls_viter_, "ptr_", "J");
420
+ env_->SetLongField(jviter, id_viter_ptr, (intptr_t)iter);
421
+ bool rv = env_->CallBooleanMethod(jmr_, id_mr_reduce_, jkey, jviter);
422
+ env_->DeleteLocalRef(jviter);
423
+ env_->DeleteLocalRef(jkey);
424
+ jthrowable jex = env_->ExceptionOccurred();
425
+ if (jex) {
426
+ if (jex_) env_->DeleteLocalRef(jex_);
427
+ jex_ = jex;
428
+ env_->ExceptionClear();
429
+ }
430
+ return rv;
431
+ }
432
+ bool preprocess() {
433
+ bool rv = env_->CallBooleanMethod(jmr_, id_mr_preproc_);
434
+ jthrowable jex = env_->ExceptionOccurred();
435
+ if (jex) {
436
+ if (jex_) env_->DeleteLocalRef(jex_);
437
+ jex_ = jex;
438
+ env_->ExceptionClear();
439
+ }
440
+ return rv;
441
+ }
442
+ bool midprocess() {
443
+ bool rv = env_->CallBooleanMethod(jmr_, id_mr_midproc_);
444
+ jthrowable jex = env_->ExceptionOccurred();
445
+ if (jex) {
446
+ if (jex_) env_->DeleteLocalRef(jex_);
447
+ jex_ = jex;
448
+ env_->ExceptionClear();
449
+ }
450
+ return rv;
451
+ }
452
+ bool postprocess() {
453
+ bool rv = env_->CallBooleanMethod(jmr_, id_mr_postproc_);
454
+ jthrowable jex = env_->ExceptionOccurred();
455
+ if (jex) {
456
+ if (jex_) env_->DeleteLocalRef(jex_);
457
+ jex_ = jex;
458
+ env_->ExceptionClear();
459
+ }
460
+ return rv;
461
+ }
462
+ bool log(const char* name, const char* message) {
463
+ return true;
464
+ }
465
+ private:
466
+ JNIEnv* env_;
467
+ jobject jmr_;
468
+ jclass cls_mr_;
469
+ jmethodID id_mr_map_;
470
+ jmethodID id_mr_reduce_;
471
+ jmethodID id_mr_preproc_;
472
+ jmethodID id_mr_midproc_;
473
+ jmethodID id_mr_postproc_;
474
+ jclass cls_viter_;
475
+ jmethodID id_viter_init_;
476
+ jthrowable jex_;
477
+ };
478
+
479
+
480
+ /**
481
+ * Throw a runtime error.
482
+ */
483
+ static void throwruntime(JNIEnv* env, const char* message) {
484
+ jclass cls = env->FindClass("java/lang/RuntimeException");
485
+ env->ThrowNew(cls, message);
486
+ }
487
+
488
+
489
+ /**
490
+ * Throw the out-of-memory error.
491
+ */
492
+ static void throwoutmem(JNIEnv* env) {
493
+ jclass cls = env->FindClass("java/lang/OutOfMemoryError");
494
+ env->ThrowNew(cls, "out of memory");
495
+ }
496
+
497
+
498
+ /**
499
+ * Throw the illegal argument exception.
500
+ */
501
+ static void throwillarg(JNIEnv* env) {
502
+ jclass cls = env->FindClass("java/lang/IllegalArgumentException");
503
+ env->ThrowNew(cls, "illegal argument");
504
+ }
505
+
506
+
507
+ /**
508
+ * Create a new string.
509
+ */
510
+ static jstring newstring(JNIEnv* env, const char* str) {
511
+ jstring jstr = env->NewStringUTF(str);
512
+ if (!jstr) {
513
+ throwoutmem(env);
514
+ throw std::bad_alloc();
515
+ }
516
+ return jstr;
517
+ }
518
+
519
+
520
+ /**
521
+ * Create a new byte array.
522
+ */
523
+ static jbyteArray newarray(JNIEnv* env, const char* buf, size_t size) {
524
+ jbyteArray jbuf = env->NewByteArray(size);
525
+ if (!jbuf) {
526
+ throwoutmem(env);
527
+ throw std::bad_alloc();
528
+ }
529
+ env->SetByteArrayRegion(jbuf, 0, size, (jbyte *)buf);
530
+ return jbuf;
531
+ }
532
+
533
+
534
+ /**
535
+ * Convert an internal map to a Java map.
536
+ */
537
+ static jobject maptojhash(JNIEnv* env, const StringMap* map) {
538
+ jclass cls_hm = env->FindClass("java/util/HashMap");
539
+ jmethodID id_hm_init = env->GetMethodID(cls_hm, "<init>", "(I)V");
540
+ jmethodID id_hm_put = env->GetMethodID(cls_hm, "put", "(" L_OBJ L_OBJ ")" L_OBJ);
541
+ jobject jhash = env->NewObject(cls_hm, id_hm_init, map->size() * 2 + 1);
542
+ StringMap::const_iterator it = map->begin();
543
+ StringMap::const_iterator itend = map->end();
544
+ while (it != itend) {
545
+ jstring jkey = newstring(env, it->first.c_str());
546
+ jstring jvalue = newstring(env, it->second.c_str());
547
+ env->CallObjectMethod(jhash, id_hm_put, jkey, jvalue);
548
+ env->DeleteLocalRef(jkey);
549
+ env->DeleteLocalRef(jvalue);
550
+ it++;
551
+ }
552
+ return jhash;
553
+ }
554
+
555
+
556
+ /**
557
+ * Convert an internal vector to a Java list.
558
+ */
559
+ static jobject vectortojlist(JNIEnv* env, const StringVector* vec) {
560
+ jclass cls_al = env->FindClass("java/util/ArrayList");
561
+ jmethodID id_al_init = env->GetMethodID(cls_al, "<init>", "(I)V");
562
+ jmethodID id_al_add = env->GetMethodID(cls_al, "add", "(" L_OBJ ")Z");
563
+ jobject jlist = env->NewObject(cls_al, id_al_init, vec->size());
564
+ StringVector::const_iterator it = vec->begin();
565
+ StringVector::const_iterator itend = vec->end();
566
+ while (it != itend) {
567
+ jstring jstr = newstring(env, it->c_str());
568
+ env->CallObjectMethod(jlist, id_al_add, jstr);
569
+ env->DeleteLocalRef(jstr);
570
+ it++;
571
+ }
572
+ return jlist;
573
+ }
574
+
575
+
576
+ /**
577
+ * Convert the pointer to the internal data of a database object.
578
+ */
579
+ static kc::PolyDB* getdbcore(JNIEnv* env, jobject jdb) {
580
+ jclass cls_db = env->GetObjectClass(jdb);
581
+ jfieldID id_db_ptr = env->GetFieldID(cls_db, "ptr_", "J");
582
+ return (kc::PolyDB*)(intptr_t)env->GetLongField(jdb, id_db_ptr);
583
+ }
584
+
585
+
586
+ /**
587
+ * Throw the exception of an error code.
588
+ */
589
+ static void throwdberror(JNIEnv* env, jobject jdb) {
590
+ jclass cls_db = env->GetObjectClass(jdb);
591
+ jfieldID id_db_exbits = env->GetFieldID(cls_db, "exbits_", "I");
592
+ uint32_t exbits = env->GetLongField(jdb, id_db_exbits);
593
+ if (exbits == 0) return;
594
+ kc::PolyDB* db = getdbcore(env, jdb);
595
+ kc::PolyDB::Error err = db->error();
596
+ uint32_t code = err.code();
597
+ if (exbits & (1 << code)) {
598
+ jclass cls = env->FindClass(p_err_children[code]);
599
+ std::string expr = kc::strprintf("%u: %s", code, err.message());
600
+ env->ThrowNew(cls, expr.c_str());
601
+ }
602
+ }
603
+
604
+
605
+ /**
606
+ * Convert the pointer to the internal data of a cursor object.
607
+ */
608
+ static SoftCursor* getcurcore(JNIEnv* env, jobject jcur) {
609
+ jclass cls_cur = env->GetObjectClass(jcur);
610
+ jfieldID id_cur_ptr = env->GetFieldID(cls_cur, "ptr_", "J");
611
+ return (SoftCursor*)(intptr_t)env->GetLongField(jcur, id_cur_ptr);
612
+ }
613
+
614
+
615
+ /**
616
+ * Get the inner database object of a cursor object.
617
+ */
618
+ static jobject getcurdb(JNIEnv* env, jobject jcur) {
619
+ jclass cls_cur = env->GetObjectClass(jcur);
620
+ jfieldID id_cur_db = env->GetFieldID(cls_cur, "db_", L_DB);
621
+ return env->GetObjectField(jcur, id_cur_db);
622
+ }
623
+
624
+
625
+ /**
626
+ * Implementation of init_visitor_NOP.
627
+ */
628
+ JNIEXPORT jbyteArray JNICALL Java_kyotocabinet_Utility_init_1visitor_1NOP
629
+ (JNIEnv* env, jclass cls) {
630
+ try {
631
+ char buf[kc::NUMBUFSIZ];
632
+ size_t size = std::sprintf(buf, "[NOP]");
633
+ obj_vis_nop = (jbyteArray)env->NewGlobalRef((jobject)newarray(env, buf, size));
634
+ return obj_vis_nop;
635
+ } catch (std::exception& e) {
636
+ return NULL;
637
+ }
638
+ }
639
+
640
+
641
+ /**
642
+ * Implementation of init_visitor_REMOVE.
643
+ */
644
+ JNIEXPORT jbyteArray JNICALL Java_kyotocabinet_Utility_init_1visitor_1REMOVE
645
+ (JNIEnv* env, jclass cls) {
646
+ try {
647
+ char buf[kc::NUMBUFSIZ];
648
+ size_t size = std::sprintf(buf, "[REMOVE]");
649
+ obj_vis_remove = (jbyteArray)env->NewGlobalRef((jobject)newarray(env, buf, size));
650
+ return obj_vis_remove;
651
+ } catch (std::exception& e) {
652
+ return NULL;
653
+ }
654
+ }
655
+
656
+
657
+ /**
658
+ * Implementation of atoi.
659
+ */
660
+ JNIEXPORT jlong JNICALL Java_kyotocabinet_Utility_atoi
661
+ (JNIEnv* env, jclass cls, jstring jstr) {
662
+ try {
663
+ if (!jstr) {
664
+ throwillarg(env);
665
+ return 0;
666
+ }
667
+ SoftString str(env, jstr);
668
+ return kc::atoi(str.str());
669
+ } catch (std::exception& e) {
670
+ return 0;
671
+ }
672
+ }
673
+
674
+
675
+ /**
676
+ * Implementation of atoix.
677
+ */
678
+ JNIEXPORT jlong JNICALL Java_kyotocabinet_Utility_atoix
679
+ (JNIEnv* env, jclass cls, jstring jstr) {
680
+ try {
681
+ if (!jstr) {
682
+ throwillarg(env);
683
+ return 0;
684
+ }
685
+ SoftString str(env, jstr);
686
+ return kc::atoix(str.str());
687
+ } catch (std::exception& e) {
688
+ return 0;
689
+ }
690
+ }
691
+
692
+
693
+ /**
694
+ * Implementation of atof.
695
+ */
696
+ JNIEXPORT jdouble JNICALL Java_kyotocabinet_Utility_atof
697
+ (JNIEnv* env, jclass cls, jstring jstr) {
698
+ try {
699
+ if (!jstr) {
700
+ throwillarg(env);
701
+ return 0;
702
+ }
703
+ SoftString str(env, jstr);
704
+ return kc::atof(str.str());
705
+ } catch (std::exception& e) {
706
+ return 0;
707
+ }
708
+ }
709
+
710
+
711
+ /**
712
+ * Implementation of hash_murmur.
713
+ */
714
+ JNIEXPORT jlong JNICALL Java_kyotocabinet_Utility_hash_1murmur
715
+ (JNIEnv* env, jclass cls, jbyteArray jary) {
716
+ try {
717
+ if (!jary) {
718
+ throwillarg(env);
719
+ return 0;
720
+ }
721
+ SoftArray ary(env, jary);
722
+ return kc::hashmurmur(ary.ptr(), ary.size());
723
+ } catch (std::exception& e) {
724
+ return 0;
725
+ }
726
+ }
727
+
728
+
729
+ /**
730
+ * Implementation of hash_fnv.
731
+ */
732
+ JNIEXPORT jlong JNICALL Java_kyotocabinet_Utility_hash_1fnv
733
+ (JNIEnv* env, jclass cls, jbyteArray jary) {
734
+ try {
735
+ if (!jary) {
736
+ throwillarg(env);
737
+ return 0;
738
+ }
739
+ SoftArray ary(env, jary);
740
+ return kc::hashfnv(ary.ptr(), ary.size());
741
+ } catch (std::exception& e) {
742
+ return 0;
743
+ }
744
+ }
745
+
746
+
747
+ /**
748
+ * Implementation of levdist.
749
+ */
750
+ JNIEXPORT jlong JNICALL Java_kyotocabinet_Utility_levdist___3B_3B
751
+ (JNIEnv* env, jclass cls, jbyteArray ja, jbyteArray jb) {
752
+ try {
753
+ if (!ja || !jb) {
754
+ throwillarg(env);
755
+ return 0;
756
+ }
757
+ SoftArray aary(env, ja);
758
+ SoftArray bary(env, jb);
759
+ return kc::memdist(aary.ptr(), aary.size(), bary.ptr(), bary.size());
760
+ } catch (std::exception& e) {
761
+ return 0;
762
+ }
763
+ }
764
+
765
+
766
+ /**
767
+ * Implementation of levdist.
768
+ */
769
+ JNIEXPORT jlong JNICALL Java_kyotocabinet_Utility_levdist__Ljava_lang_String_2Ljava_lang_String_2
770
+ (JNIEnv* env, jclass cls, jstring ja, jstring jb) {
771
+ try {
772
+ if (!ja || !jb) {
773
+ throwillarg(env);
774
+ return 0;
775
+ }
776
+ SoftString astr(env, ja);
777
+ SoftString bstr(env, jb);
778
+ return kc::strutfdist(astr.str(), bstr.str());
779
+ } catch (std::exception& e) {
780
+ return 0;
781
+ }
782
+ }
783
+
784
+
785
+ /**
786
+ * Implementation of remove_recursively.
787
+ */
788
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_Utility_remove_1files_1recursively
789
+ (JNIEnv* env, jclass cls, jstring jpath) {
790
+ if (!jpath) {
791
+ throwillarg(env);
792
+ return false;
793
+ }
794
+ SoftString path(env, jpath);
795
+ return kc::File::remove_recursively(path.str());
796
+ }
797
+
798
+
799
+ /**
800
+ * Implementation of version.
801
+ */
802
+ JNIEXPORT jstring JNICALL Java_kyotocabinet_Utility_version
803
+ (JNIEnv* env, jclass cls) {
804
+ try {
805
+ return newstring(env, kc::VERSION);
806
+ } catch (std::exception& e) {
807
+ return NULL;
808
+ }
809
+ }
810
+
811
+
812
+ /**
813
+ * Implementation of codename.
814
+ */
815
+ JNIEXPORT jstring JNICALL Java_kyotocabinet_Error_codename
816
+ (JNIEnv* env, jclass cls, jint code) {
817
+ try {
818
+ return newstring(env, kc::PolyDB::Error::codename((kyotocabinet::PolyDB::Error::Code)code));
819
+ } catch (std::exception& e) {
820
+ return NULL;
821
+ }
822
+ }
823
+
824
+
825
+ /**
826
+ * Implementation of disable.
827
+ */
828
+ JNIEXPORT void JNICALL Java_kyotocabinet_Cursor_disable
829
+ (JNIEnv* env, jobject jself) {
830
+ try {
831
+ SoftCursor* cur = getcurcore(env, jself);
832
+ kc::PolyDB::Cursor* icur = cur->cur();
833
+ if (!icur) return;
834
+ cur->disable();
835
+ } catch (std::exception& e) {}
836
+ }
837
+
838
+
839
+ /**
840
+ * Implementation of accept.
841
+ */
842
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_Cursor_accept
843
+ (JNIEnv* env, jobject jself, jobject jvisitor, jboolean writable, jboolean step) {
844
+ try {
845
+ if (!jvisitor) {
846
+ throwillarg(env);
847
+ return false;
848
+ }
849
+ SoftCursor* cur = getcurcore(env, jself);
850
+ kc::PolyDB::Cursor* icur = cur->cur();
851
+ if (!icur) return false;
852
+ SoftVisitor visitor(env, jvisitor, writable);
853
+ bool rv = icur->accept(&visitor, writable, step);
854
+ jthrowable jex = visitor.exception();
855
+ if (jex) {
856
+ env->Throw(jex);
857
+ return false;
858
+ }
859
+ if (rv) return true;
860
+ throwdberror(env, getcurdb(env, jself));
861
+ return false;
862
+ } catch (std::exception& e) {
863
+ return false;
864
+ }
865
+ }
866
+
867
+
868
+ /**
869
+ * Implementation of set_value.
870
+ */
871
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_Cursor_set_1value
872
+ (JNIEnv* env, jobject jself, jbyteArray jvalue, jboolean step) {
873
+ try {
874
+ if (!jvalue) {
875
+ throwillarg(env);
876
+ return false;
877
+ }
878
+ SoftCursor* cur = getcurcore(env, jself);
879
+ kc::PolyDB::Cursor* icur = cur->cur();
880
+ if (!icur) return false;
881
+ SoftArray value(env, jvalue);
882
+ bool rv = icur->set_value(value.ptr(), value.size(), step);
883
+ if (rv) return true;
884
+ throwdberror(env, getcurdb(env, jself));
885
+ return false;
886
+ } catch (std::exception& e) {
887
+ return false;
888
+ }
889
+ }
890
+
891
+
892
+ /**
893
+ * Implementation of remove.
894
+ */
895
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_Cursor_remove
896
+ (JNIEnv* env, jobject jself) {
897
+ try {
898
+ SoftCursor* cur = getcurcore(env, jself);
899
+ kc::PolyDB::Cursor* icur = cur->cur();
900
+ if (!icur) return false;
901
+ bool rv = icur->remove();
902
+ if (rv) return true;
903
+ throwdberror(env, getcurdb(env, jself));
904
+ return false;
905
+ } catch (std::exception& e) {
906
+ return false;
907
+ }
908
+ }
909
+
910
+
911
+ /**
912
+ * Implementation of get_key.
913
+ */
914
+ JNIEXPORT jbyteArray JNICALL Java_kyotocabinet_Cursor_get_1key
915
+ (JNIEnv* env, jobject jself, jboolean step) {
916
+ try {
917
+ SoftCursor* cur = getcurcore(env, jself);
918
+ kc::PolyDB::Cursor* icur = cur->cur();
919
+ if (!icur) return NULL;
920
+ size_t ksiz;
921
+ char* kbuf = icur->get_key(&ksiz, step);
922
+ if (!kbuf) {
923
+ throwdberror(env, getcurdb(env, jself));
924
+ return NULL;
925
+ }
926
+ jbyteArray jkey = newarray(env, kbuf, ksiz);
927
+ delete[] kbuf;
928
+ return jkey;
929
+ } catch (std::exception& e) {
930
+ return NULL;
931
+ }
932
+ }
933
+
934
+
935
+ /**
936
+ * Implementation of get_value.
937
+ */
938
+ JNIEXPORT jbyteArray JNICALL Java_kyotocabinet_Cursor_get_1value
939
+ (JNIEnv* env, jobject jself, jboolean step) {
940
+ try {
941
+ SoftCursor* cur = getcurcore(env, jself);
942
+ kc::PolyDB::Cursor* icur = cur->cur();
943
+ if (!icur) return NULL;
944
+ size_t vsiz;
945
+ char* vbuf = icur->get_value(&vsiz, step);
946
+ if (!vbuf) {
947
+ throwdberror(env, getcurdb(env, jself));
948
+ return NULL;
949
+ }
950
+ jbyteArray jvalue = newarray(env, vbuf, vsiz);
951
+ delete[] vbuf;
952
+ return jvalue;
953
+ } catch (std::exception& e) {
954
+ return NULL;
955
+ }
956
+ }
957
+
958
+
959
+ /**
960
+ * Implementation of get.
961
+ */
962
+ JNIEXPORT jobjectArray JNICALL Java_kyotocabinet_Cursor_get
963
+ (JNIEnv* env, jobject jself, jboolean step) {
964
+ try {
965
+ SoftCursor* cur = getcurcore(env, jself);
966
+ kc::PolyDB::Cursor* icur = cur->cur();
967
+ if (!icur) return NULL;
968
+ const char* vbuf;
969
+ size_t ksiz, vsiz;
970
+ char* kbuf = icur->get(&ksiz, &vbuf, &vsiz, step);
971
+ if (!kbuf) {
972
+ throwdberror(env, getcurdb(env, jself));
973
+ return NULL;
974
+ }
975
+ jbyteArray jkey = newarray(env, kbuf, ksiz);
976
+ jbyteArray jvalue = newarray(env, vbuf, vsiz);
977
+ delete[] kbuf;
978
+ jclass cls_byteary = env->FindClass("[B");
979
+ jobjectArray jrec = env->NewObjectArray(2, cls_byteary, NULL);
980
+ env->SetObjectArrayElement(jrec, 0, jkey);
981
+ env->SetObjectArrayElement(jrec, 1, jvalue);
982
+ env->DeleteLocalRef(jkey);
983
+ env->DeleteLocalRef(jvalue);
984
+ return jrec;
985
+ } catch (std::exception& e) {
986
+ return NULL;
987
+ }
988
+ }
989
+
990
+
991
+ /**
992
+ * Implementation of seize.
993
+ */
994
+ JNIEXPORT jobjectArray JNICALL Java_kyotocabinet_Cursor_seize
995
+ (JNIEnv* env, jobject jself) {
996
+ try {
997
+ SoftCursor* cur = getcurcore(env, jself);
998
+ kc::PolyDB::Cursor* icur = cur->cur();
999
+ if (!icur) return NULL;
1000
+ const char* vbuf;
1001
+ size_t ksiz, vsiz;
1002
+ char* kbuf = icur->seize(&ksiz, &vbuf, &vsiz);
1003
+ if (!kbuf) {
1004
+ throwdberror(env, getcurdb(env, jself));
1005
+ return NULL;
1006
+ }
1007
+ jbyteArray jkey = newarray(env, kbuf, ksiz);
1008
+ jbyteArray jvalue = newarray(env, vbuf, vsiz);
1009
+ delete[] kbuf;
1010
+ jclass cls_byteary = env->FindClass("[B");
1011
+ jobjectArray jrec = env->NewObjectArray(2, cls_byteary, NULL);
1012
+ env->SetObjectArrayElement(jrec, 0, jkey);
1013
+ env->SetObjectArrayElement(jrec, 1, jvalue);
1014
+ env->DeleteLocalRef(jkey);
1015
+ env->DeleteLocalRef(jvalue);
1016
+ return jrec;
1017
+ } catch (std::exception& e) {
1018
+ return NULL;
1019
+ }
1020
+ }
1021
+
1022
+
1023
+ /**
1024
+ * Implementation of jump.
1025
+ */
1026
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_Cursor_jump__
1027
+ (JNIEnv* env, jobject jself) {
1028
+ try {
1029
+ SoftCursor* cur = getcurcore(env, jself);
1030
+ kc::PolyDB::Cursor* icur = cur->cur();
1031
+ if (!icur) return false;
1032
+ bool rv = icur->jump();
1033
+ if (rv) return true;
1034
+ throwdberror(env, getcurdb(env, jself));
1035
+ return false;
1036
+ } catch (std::exception& e) {
1037
+ return false;
1038
+ }
1039
+ }
1040
+
1041
+
1042
+ /**
1043
+ * Implementation of jump.
1044
+ */
1045
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_Cursor_jump___3B
1046
+ (JNIEnv* env, jobject jself, jbyteArray jkey) {
1047
+ try {
1048
+ if (!jkey) {
1049
+ throwillarg(env);
1050
+ return false;
1051
+ }
1052
+ SoftCursor* cur = getcurcore(env, jself);
1053
+ kc::PolyDB::Cursor* icur = cur->cur();
1054
+ if (!icur) return false;
1055
+ SoftArray key(env, jkey);
1056
+ bool rv = icur->jump(key.ptr(), key.size());
1057
+ if (rv) return true;
1058
+ throwdberror(env, getcurdb(env, jself));
1059
+ return false;
1060
+ } catch (std::exception& e) {
1061
+ return false;
1062
+ }
1063
+ }
1064
+
1065
+
1066
+ /**
1067
+ * Implementation of jump_back.
1068
+ */
1069
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_Cursor_jump_1back__
1070
+ (JNIEnv* env, jobject jself) {
1071
+ try {
1072
+ SoftCursor* cur = getcurcore(env, jself);
1073
+ kc::PolyDB::Cursor* icur = cur->cur();
1074
+ if (!icur) return false;
1075
+ bool rv = icur->jump_back();
1076
+ if (rv) return true;
1077
+ throwdberror(env, getcurdb(env, jself));
1078
+ return false;
1079
+ } catch (std::exception& e) {
1080
+ return false;
1081
+ }
1082
+ }
1083
+
1084
+
1085
+ /**
1086
+ * Implementation of jump_back.
1087
+ */
1088
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_Cursor_jump_1back___3B
1089
+ (JNIEnv* env, jobject jself, jbyteArray jkey) {
1090
+ try {
1091
+ if (!jkey) {
1092
+ throwillarg(env);
1093
+ return false;
1094
+ }
1095
+ SoftCursor* cur = getcurcore(env, jself);
1096
+ kc::PolyDB::Cursor* icur = cur->cur();
1097
+ if (!icur) return false;
1098
+ SoftArray key(env, jkey);
1099
+ bool rv = icur->jump_back(key.ptr(), key.size());
1100
+ if (rv) return true;
1101
+ throwdberror(env, getcurdb(env, jself));
1102
+ return false;
1103
+ } catch (std::exception& e) {
1104
+ return false;
1105
+ }
1106
+ }
1107
+
1108
+
1109
+ /**
1110
+ * Implementation of step.
1111
+ */
1112
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_Cursor_step
1113
+ (JNIEnv* env, jobject jself) {
1114
+ try {
1115
+ SoftCursor* cur = getcurcore(env, jself);
1116
+ kc::PolyDB::Cursor* icur = cur->cur();
1117
+ if (!icur) return false;
1118
+ bool rv = icur->step();
1119
+ if (rv) return true;
1120
+ throwdberror(env, getcurdb(env, jself));
1121
+ return false;
1122
+ } catch (std::exception& e) {
1123
+ return false;
1124
+ }
1125
+ }
1126
+
1127
+
1128
+ /**
1129
+ * Implementation of step_back.
1130
+ */
1131
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_Cursor_step_1back
1132
+ (JNIEnv* env, jobject jself) {
1133
+ try {
1134
+ SoftCursor* cur = getcurcore(env, jself);
1135
+ kc::PolyDB::Cursor* icur = cur->cur();
1136
+ if (!icur) return false;
1137
+ bool rv = icur->step_back();
1138
+ if (rv) return true;
1139
+ throwdberror(env, getcurdb(env, jself));
1140
+ return false;
1141
+ } catch (std::exception& e) {
1142
+ return false;
1143
+ }
1144
+ }
1145
+
1146
+
1147
+ /**
1148
+ * Implementation of error.
1149
+ */
1150
+ JNIEXPORT jthrowable JNICALL Java_kyotocabinet_Cursor_error
1151
+ (JNIEnv* env, jobject jself) {
1152
+ try {
1153
+ jclass cls_err = env->FindClass(P_ERR);
1154
+ jmethodID id_err_init = env->GetMethodID(cls_err, "<init>", "(I" L_STR ")V");
1155
+ SoftCursor* cur = getcurcore(env, jself);
1156
+ kc::PolyDB::Cursor* icur = cur->cur();
1157
+ if (!icur) return NULL;
1158
+ kc::PolyDB::Error err = cur->cur()->error();
1159
+ jstring jmessage = newstring(env, err.message());
1160
+ jobject jerr = env->NewObject(cls_err, id_err_init, err.code(), jmessage);
1161
+ env->DeleteLocalRef(jmessage);
1162
+ return (jthrowable)jerr;
1163
+ } catch (std::exception& e) {
1164
+ return NULL;
1165
+ }
1166
+ }
1167
+
1168
+
1169
+ /**
1170
+ * Implementation of db.
1171
+ */
1172
+ JNIEXPORT jobject JNICALL Java_kyotocabinet_Cursor_db
1173
+ (JNIEnv* env, jobject jself) {
1174
+ try {
1175
+ return getcurdb(env, jself);
1176
+ } catch (std::exception& e) {
1177
+ return NULL;
1178
+ }
1179
+ }
1180
+
1181
+
1182
+ /**
1183
+ * Implementation of initialize.
1184
+ */
1185
+ JNIEXPORT void JNICALL Java_kyotocabinet_Cursor_initialize
1186
+ (JNIEnv* env, jobject jself, jobject jdb) {
1187
+ try {
1188
+ if (!jdb) {
1189
+ throwillarg(env);
1190
+ return;
1191
+ }
1192
+ jclass cls_cur = env->GetObjectClass(jself);
1193
+ jfieldID id_cur_ptr = env->GetFieldID(cls_cur, "ptr_", "J");
1194
+ jfieldID id_cur_db = env->GetFieldID(cls_cur, "db_", L_DB);
1195
+ kc::PolyDB* db = getdbcore(env, jdb);
1196
+ g_curbur.sweap();
1197
+ SoftCursor* cur = new SoftCursor(db);
1198
+ env->SetLongField(jself, id_cur_ptr, (intptr_t)cur);
1199
+ env->SetObjectField(jself, id_cur_db, jdb);
1200
+ } catch (std::exception& e) {}
1201
+ }
1202
+
1203
+
1204
+ /**
1205
+ * Implementation of destruct.
1206
+ */
1207
+ JNIEXPORT void JNICALL Java_kyotocabinet_Cursor_destruct
1208
+ (JNIEnv* env, jobject jself) {
1209
+ try {
1210
+ SoftCursor* cur = getcurcore(env, jself);
1211
+ delete cur;
1212
+ } catch (std::exception& e) {}
1213
+ }
1214
+
1215
+
1216
+ /**
1217
+ * Implementation of error.
1218
+ */
1219
+ JNIEXPORT jthrowable JNICALL Java_kyotocabinet_DB_error
1220
+ (JNIEnv* env, jobject jself) {
1221
+ try {
1222
+ jclass cls_err = env->FindClass(P_ERR);
1223
+ jmethodID id_err_init = env->GetMethodID(cls_err, "<init>", "(I" L_STR ")V");
1224
+ kc::PolyDB* db = getdbcore(env, jself);
1225
+ kc::PolyDB::Error err = db->error();
1226
+ jstring jmessage = newstring(env, err.message());
1227
+ jobject jerr = env->NewObject(cls_err, id_err_init, err.code(), jmessage);
1228
+ env->DeleteLocalRef(jmessage);
1229
+ return (jthrowable)jerr;
1230
+ } catch (std::exception& e) {
1231
+ return NULL;
1232
+ }
1233
+ }
1234
+
1235
+
1236
+ /**
1237
+ * Implementation of open.
1238
+ */
1239
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_open
1240
+ (JNIEnv* env, jobject jself, jstring jpath, jint mode) {
1241
+ try {
1242
+ if (!jpath) {
1243
+ throwillarg(env);
1244
+ return false;
1245
+ }
1246
+ kc::PolyDB* db = getdbcore(env, jself);
1247
+ SoftString path(env, jpath);
1248
+ bool rv = db->open(path.str(), mode);
1249
+ if (rv) return true;
1250
+ throwdberror(env, jself);
1251
+ return false;
1252
+ } catch (std::exception& e) {
1253
+ return false;
1254
+ }
1255
+ }
1256
+
1257
+
1258
+ /**
1259
+ * Implementation of close.
1260
+ */
1261
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_close
1262
+ (JNIEnv* env, jobject jself) {
1263
+ try {
1264
+ kc::PolyDB* db = getdbcore(env, jself);
1265
+ g_curbur.sweap();
1266
+ bool rv = db->close();
1267
+ if (rv) return true;
1268
+ throwdberror(env, jself);
1269
+ return false;
1270
+ } catch (std::exception& e) {
1271
+ return false;
1272
+ }
1273
+ }
1274
+
1275
+
1276
+ /**
1277
+ * Implementation of accept.
1278
+ */
1279
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_accept
1280
+ (JNIEnv* env, jobject jself, jbyteArray jkey, jobject jvisitor, jboolean writable) {
1281
+ try {
1282
+ if (!jkey || !jvisitor) {
1283
+ throwillarg(env);
1284
+ return false;
1285
+ }
1286
+ kc::PolyDB* db = getdbcore(env, jself);
1287
+ SoftArray key(env, jkey);
1288
+ SoftVisitor visitor(env, jvisitor, writable);
1289
+ bool rv = db->accept(key.ptr(), key.size(), &visitor, writable);
1290
+ jthrowable jex = visitor.exception();
1291
+ if (jex) {
1292
+ env->Throw(jex);
1293
+ return false;
1294
+ }
1295
+ if (rv) return true;
1296
+ throwdberror(env, jself);
1297
+ return false;
1298
+ } catch (std::exception& e) {
1299
+ return false;
1300
+ }
1301
+ }
1302
+
1303
+
1304
+ /**
1305
+ * Implementation of accept_bulk.
1306
+ */
1307
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_accept_1bulk
1308
+ (JNIEnv* env, jobject jself, jobjectArray jkeys, jobject jvisitor, jboolean writable) {
1309
+ try {
1310
+ if (!jkeys || !jvisitor) {
1311
+ throwillarg(env);
1312
+ return false;
1313
+ }
1314
+ kc::PolyDB* db = getdbcore(env, jself);
1315
+ size_t knum = env->GetArrayLength(jkeys);
1316
+ StringVector keys;
1317
+ keys.reserve(knum);
1318
+ for (size_t i = 0; i < knum; i++) {
1319
+ jbyteArray jkey = (jbyteArray)env->GetObjectArrayElement(jkeys, i);
1320
+ if (jkey) {
1321
+ SoftArray key(env, jkey);
1322
+ keys.push_back(std::string(key.ptr(), key.size()));
1323
+ }
1324
+ env->DeleteLocalRef(jkey);
1325
+ }
1326
+ SoftVisitor visitor(env, jvisitor, writable);
1327
+ bool rv = db->accept_bulk(keys, &visitor, writable);
1328
+ jthrowable jex = visitor.exception();
1329
+ if (jex) {
1330
+ env->Throw(jex);
1331
+ return false;
1332
+ }
1333
+ if (rv) return true;
1334
+ throwdberror(env, jself);
1335
+ return false;
1336
+ } catch (std::exception& e) {
1337
+ return false;
1338
+ }
1339
+ }
1340
+
1341
+
1342
+ /**
1343
+ * Implementation of iterate.
1344
+ */
1345
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_iterate
1346
+ (JNIEnv* env, jobject jself, jobject jvisitor, jboolean writable) {
1347
+ try {
1348
+ if (!jvisitor) {
1349
+ throwillarg(env);
1350
+ return false;
1351
+ }
1352
+ kc::PolyDB* db = getdbcore(env, jself);
1353
+ SoftVisitor visitor(env, jvisitor, writable);
1354
+ bool rv = db->iterate(&visitor, writable);
1355
+ jthrowable jex = visitor.exception();
1356
+ if (jex) {
1357
+ env->Throw(jex);
1358
+ return false;
1359
+ }
1360
+ if (rv) return true;
1361
+ throwdberror(env, jself);
1362
+ return false;
1363
+ } catch (std::exception& e) {
1364
+ return false;
1365
+ }
1366
+ }
1367
+
1368
+
1369
+ /**
1370
+ * Implementation of set.
1371
+ */
1372
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_set
1373
+ (JNIEnv* env, jobject jself, jbyteArray jkey, jbyteArray jvalue) {
1374
+ try {
1375
+ if (!jkey || !jvalue) {
1376
+ throwillarg(env);
1377
+ return false;
1378
+ }
1379
+ kc::PolyDB* db = getdbcore(env, jself);
1380
+ SoftArray key(env, jkey);
1381
+ SoftArray value(env, jvalue);
1382
+ bool rv = db->set(key.ptr(), key.size(), value.ptr(), value.size());
1383
+ if (rv) return true;
1384
+ throwdberror(env, jself);
1385
+ return false;
1386
+ } catch (std::exception& e) {
1387
+ return false;
1388
+ }
1389
+ }
1390
+
1391
+
1392
+ /**
1393
+ * Implementation of add.
1394
+ */
1395
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_add
1396
+ (JNIEnv* env, jobject jself, jbyteArray jkey, jbyteArray jvalue) {
1397
+ try {
1398
+ if (!jkey || !jvalue) {
1399
+ throwillarg(env);
1400
+ return false;
1401
+ }
1402
+ kc::PolyDB* db = getdbcore(env, jself);
1403
+ SoftArray key(env, jkey);
1404
+ SoftArray value(env, jvalue);
1405
+ bool rv = db->add(key.ptr(), key.size(), value.ptr(), value.size());
1406
+ if (rv) return true;
1407
+ throwdberror(env, jself);
1408
+ return false;
1409
+ } catch (std::exception& e) {
1410
+ return false;
1411
+ }
1412
+ }
1413
+
1414
+
1415
+ /**
1416
+ * Implementation of replace.
1417
+ */
1418
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_replace
1419
+ (JNIEnv* env, jobject jself, jbyteArray jkey, jbyteArray jvalue) {
1420
+ try {
1421
+ if (!jkey || !jvalue) {
1422
+ throwillarg(env);
1423
+ return false;
1424
+ }
1425
+ kc::PolyDB* db = getdbcore(env, jself);
1426
+ SoftArray key(env, jkey);
1427
+ SoftArray value(env, jvalue);
1428
+ bool rv = db->replace(key.ptr(), key.size(), value.ptr(), value.size());
1429
+ if (rv) return true;
1430
+ throwdberror(env, jself);
1431
+ return false;
1432
+ } catch (std::exception& e) {
1433
+ return false;
1434
+ }
1435
+ }
1436
+
1437
+
1438
+ /**
1439
+ * Implementation of append.
1440
+ */
1441
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_append
1442
+ (JNIEnv* env, jobject jself, jbyteArray jkey, jbyteArray jvalue) {
1443
+ try {
1444
+ if (!jkey || !jvalue) {
1445
+ throwillarg(env);
1446
+ return false;
1447
+ }
1448
+ kc::PolyDB* db = getdbcore(env, jself);
1449
+ SoftArray key(env, jkey);
1450
+ SoftArray value(env, jvalue);
1451
+ bool rv = db->append(key.ptr(), key.size(), value.ptr(), value.size());
1452
+ if (rv) return true;
1453
+ throwdberror(env, jself);
1454
+ return false;
1455
+ } catch (std::exception& e) {
1456
+ return false;
1457
+ }
1458
+ }
1459
+
1460
+
1461
+ /**
1462
+ * Implementation of increment.
1463
+ */
1464
+ JNIEXPORT jlong JNICALL Java_kyotocabinet_DB_increment
1465
+ (JNIEnv* env, jobject jself, jbyteArray jkey, jlong num, jlong orig) {
1466
+ try {
1467
+ if (!jkey) {
1468
+ throwillarg(env);
1469
+ return false;
1470
+ }
1471
+ kc::PolyDB* db = getdbcore(env, jself);
1472
+ SoftArray key(env, jkey);
1473
+ num = db->increment(key.ptr(), key.size(), num, orig);
1474
+ if (num == kc::INT64MIN) throwdberror(env, jself);
1475
+ return num;
1476
+ } catch (std::exception& e) {
1477
+ return 0;
1478
+ }
1479
+ }
1480
+
1481
+
1482
+ /**
1483
+ * Implementation of increment_double.
1484
+ */
1485
+ JNIEXPORT jdouble JNICALL Java_kyotocabinet_DB_increment_1double
1486
+ (JNIEnv* env, jobject jself, jbyteArray jkey, jdouble num, jdouble orig) {
1487
+ try {
1488
+ if (!jkey) {
1489
+ throwillarg(env);
1490
+ return false;
1491
+ }
1492
+ kc::PolyDB* db = getdbcore(env, jself);
1493
+ SoftArray key(env, jkey);
1494
+ num = db->increment_double(key.ptr(), key.size(), num, orig);
1495
+ if (kc::chknan(num)) throwdberror(env, jself);
1496
+ return num;
1497
+ } catch (std::exception& e) {
1498
+ return 0;
1499
+ }
1500
+ }
1501
+
1502
+
1503
+ /**
1504
+ * Implementation of cas.
1505
+ */
1506
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_cas
1507
+ (JNIEnv* env, jobject jself, jbyteArray jkey, jbyteArray joval, jbyteArray jnval) {
1508
+ try {
1509
+ if (!jkey) {
1510
+ throwillarg(env);
1511
+ return false;
1512
+ }
1513
+ kc::PolyDB* db = getdbcore(env, jself);
1514
+ SoftArray key(env, jkey);
1515
+ SoftArray oval(env, joval);
1516
+ SoftArray nval(env, jnval);
1517
+ bool rv = db->cas(key.ptr(), key.size(), oval.ptr(), oval.size(), nval.ptr(), nval.size());
1518
+ if (rv) return true;
1519
+ throwdberror(env, jself);
1520
+ return false;
1521
+ } catch (std::exception& e) {
1522
+ return false;
1523
+ }
1524
+ }
1525
+
1526
+
1527
+ /**
1528
+ * Implementation of remove.
1529
+ */
1530
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_remove
1531
+ (JNIEnv* env, jobject jself, jbyteArray jkey) {
1532
+ try {
1533
+ if (!jkey) {
1534
+ throwillarg(env);
1535
+ return false;
1536
+ }
1537
+ kc::PolyDB* db = getdbcore(env, jself);
1538
+ SoftArray key(env, jkey);
1539
+ bool rv = db->remove(key.ptr(), key.size());
1540
+ if (rv) return true;
1541
+ throwdberror(env, jself);
1542
+ return false;
1543
+ } catch (std::exception& e) {
1544
+ return false;
1545
+ }
1546
+ }
1547
+
1548
+
1549
+ /**
1550
+ * Implementation of get.
1551
+ */
1552
+ JNIEXPORT jbyteArray JNICALL Java_kyotocabinet_DB_get
1553
+ (JNIEnv* env, jobject jself, jbyteArray jkey) {
1554
+ try {
1555
+ if (!jkey) {
1556
+ throwillarg(env);
1557
+ return NULL;
1558
+ }
1559
+ kc::PolyDB* db = getdbcore(env, jself);
1560
+ SoftArray key(env, jkey);
1561
+ size_t vsiz;
1562
+ char* vbuf = db->get(key.ptr(), key.size(), &vsiz);
1563
+ if (!vbuf) {
1564
+ throwdberror(env, jself);
1565
+ return NULL;
1566
+ }
1567
+ jbyteArray jvalue = newarray(env, vbuf, vsiz);
1568
+ delete[] vbuf;
1569
+ return jvalue;
1570
+ } catch (std::exception& e) {
1571
+ return NULL;
1572
+ }
1573
+ }
1574
+
1575
+
1576
+ /**
1577
+ * Implementation of check.
1578
+ */
1579
+ JNIEXPORT jint JNICALL Java_kyotocabinet_DB_check
1580
+ (JNIEnv* env, jobject jself, jbyteArray jkey) {
1581
+ try {
1582
+ if (!jkey) {
1583
+ throwillarg(env);
1584
+ return -1;
1585
+ }
1586
+ kc::PolyDB* db = getdbcore(env, jself);
1587
+ SoftArray key(env, jkey);
1588
+ int32_t vsiz = db->check(key.ptr(), key.size());
1589
+ if (vsiz < 0) {
1590
+ throwdberror(env, jself);
1591
+ return -1;
1592
+ }
1593
+ return vsiz;
1594
+ } catch (std::exception& e) {
1595
+ return -1;
1596
+ }
1597
+ }
1598
+
1599
+
1600
+ /**
1601
+ * Implementation of seize.
1602
+ */
1603
+ JNIEXPORT jbyteArray JNICALL Java_kyotocabinet_DB_seize
1604
+ (JNIEnv* env, jobject jself, jbyteArray jkey) {
1605
+ try {
1606
+ if (!jkey) {
1607
+ throwillarg(env);
1608
+ return NULL;
1609
+ }
1610
+ kc::PolyDB* db = getdbcore(env, jself);
1611
+ SoftArray key(env, jkey);
1612
+ size_t vsiz;
1613
+ char* vbuf = db->seize(key.ptr(), key.size(), &vsiz);
1614
+ if (!vbuf) {
1615
+ throwdberror(env, jself);
1616
+ return NULL;
1617
+ }
1618
+ jbyteArray jvalue = newarray(env, vbuf, vsiz);
1619
+ delete[] vbuf;
1620
+ return jvalue;
1621
+ } catch (std::exception& e) {
1622
+ return NULL;
1623
+ }
1624
+ }
1625
+
1626
+
1627
+ /**
1628
+ * Implementation of set_bulk.
1629
+ */
1630
+ JNIEXPORT jlong JNICALL Java_kyotocabinet_DB_set_1bulk
1631
+ (JNIEnv* env, jobject jself, jobjectArray jrecs, jboolean atomic) {
1632
+ try {
1633
+ if (!jrecs) {
1634
+ throwillarg(env);
1635
+ return -1;
1636
+ }
1637
+ kc::PolyDB* db = getdbcore(env, jself);
1638
+ size_t rnum = env->GetArrayLength(jrecs);
1639
+ StringMap recs;
1640
+ for (size_t i = 0; i + 1 < rnum; i += 2) {
1641
+ jbyteArray jkey = (jbyteArray)env->GetObjectArrayElement(jrecs, i);
1642
+ jbyteArray jvalue = (jbyteArray)env->GetObjectArrayElement(jrecs, i + 1);
1643
+ if (jkey && jvalue) {
1644
+ SoftArray key(env, jkey);
1645
+ SoftArray value(env, jvalue);
1646
+ recs[std::string(key.ptr(), key.size())] = std::string(value.ptr(), value.size());
1647
+ }
1648
+ env->DeleteLocalRef(jkey);
1649
+ env->DeleteLocalRef(jvalue);
1650
+ }
1651
+ int64_t rv = db->set_bulk(recs, atomic);
1652
+ if (rv < 0) {
1653
+ throwdberror(env, jself);
1654
+ return -1;
1655
+ }
1656
+ return rv;
1657
+ } catch (std::exception& e) {
1658
+ return -1;
1659
+ }
1660
+ }
1661
+
1662
+
1663
+ /**
1664
+ * Implementation of remove_bulk.
1665
+ */
1666
+ JNIEXPORT jlong JNICALL Java_kyotocabinet_DB_remove_1bulk
1667
+ (JNIEnv* env, jobject jself, jobjectArray jkeys, jboolean atomic) {
1668
+ try {
1669
+ if (!jkeys) {
1670
+ throwillarg(env);
1671
+ return -1;
1672
+ }
1673
+ kc::PolyDB* db = getdbcore(env, jself);
1674
+ size_t knum = env->GetArrayLength(jkeys);
1675
+ StringVector keys;
1676
+ keys.reserve(knum);
1677
+ for (size_t i = 0; i < knum; i++) {
1678
+ jbyteArray jkey = (jbyteArray)env->GetObjectArrayElement(jkeys, i);
1679
+ if (jkey) {
1680
+ SoftArray key(env, jkey);
1681
+ keys.push_back(std::string(key.ptr(), key.size()));
1682
+ }
1683
+ env->DeleteLocalRef(jkey);
1684
+ }
1685
+ int64_t rv = db->remove_bulk(keys, atomic);
1686
+ if (rv < 0) {
1687
+ throwdberror(env, jself);
1688
+ return -1;
1689
+ }
1690
+ return rv;
1691
+ } catch (std::exception& e) {
1692
+ return (jlong)NULL;
1693
+ }
1694
+ }
1695
+
1696
+
1697
+ /**
1698
+ * Implementation of get_bulk.
1699
+ */
1700
+ JNIEXPORT jobjectArray JNICALL Java_kyotocabinet_DB_get_1bulk
1701
+ (JNIEnv* env, jobject jself, jobjectArray jkeys, jboolean atomic) {
1702
+ try {
1703
+ if (!jkeys) {
1704
+ throwillarg(env);
1705
+ return NULL;
1706
+ }
1707
+ kc::PolyDB* db = getdbcore(env, jself);
1708
+ size_t knum = env->GetArrayLength(jkeys);
1709
+ StringVector keys;
1710
+ keys.reserve(knum);
1711
+ for (size_t i = 0; i < knum; i++) {
1712
+ jbyteArray jkey = (jbyteArray)env->GetObjectArrayElement(jkeys, i);
1713
+ if (jkey) {
1714
+ SoftArray key(env, jkey);
1715
+ keys.push_back(std::string(key.ptr(), key.size()));
1716
+ }
1717
+ env->DeleteLocalRef(jkey);
1718
+ }
1719
+ StringMap recs;
1720
+ if (db->get_bulk(keys, &recs, atomic) < 0) {
1721
+ throwdberror(env, jself);
1722
+ return NULL;
1723
+ }
1724
+ size_t rnum = recs.size();
1725
+ jclass cls_byteary = env->FindClass("[B");
1726
+ jobjectArray jrec = env->NewObjectArray(rnum * 2, cls_byteary, NULL);
1727
+ StringMap::iterator it = recs.begin();
1728
+ StringMap::iterator itend = recs.end();
1729
+ size_t ridx = 0;
1730
+ while (it != itend) {
1731
+ jbyteArray jkey = newarray(env, it->first.data(), it->first.size());
1732
+ jbyteArray jvalue = newarray(env, it->second.data(), it->second.size());
1733
+ env->SetObjectArrayElement(jrec, ridx++, jkey);
1734
+ env->SetObjectArrayElement(jrec, ridx++, jvalue);
1735
+ env->DeleteLocalRef(jkey);
1736
+ env->DeleteLocalRef(jvalue);
1737
+ it++;
1738
+ }
1739
+ return jrec;
1740
+ } catch (std::exception& e) {
1741
+ return NULL;
1742
+ }
1743
+ }
1744
+
1745
+
1746
+ /**
1747
+ * Implementation of clear.
1748
+ */
1749
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_clear
1750
+ (JNIEnv* env, jobject jself) {
1751
+ try {
1752
+ kc::PolyDB* db = getdbcore(env, jself);
1753
+ bool rv = db->clear();
1754
+ if (rv) return true;
1755
+ throwdberror(env, jself);
1756
+ return false;
1757
+ } catch (std::exception& e) {
1758
+ return false;
1759
+ }
1760
+ }
1761
+
1762
+
1763
+ /**
1764
+ * Implementation of synchronize.
1765
+ */
1766
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_synchronize
1767
+ (JNIEnv* env, jobject jself, jboolean hard, jobject jproc) {
1768
+ try {
1769
+ kc::PolyDB* db = getdbcore(env, jself);
1770
+ if (!jproc) return db->synchronize(hard);
1771
+ SoftFileProcessor proc(env, jproc);
1772
+ bool rv = db->synchronize(hard, &proc);
1773
+ jthrowable jex = proc.exception();
1774
+ if (jex) {
1775
+ env->Throw(jex);
1776
+ return false;
1777
+ }
1778
+ if (rv) return true;
1779
+ throwdberror(env, jself);
1780
+ return false;
1781
+ } catch (std::exception& e) {
1782
+ return false;
1783
+ }
1784
+ }
1785
+
1786
+
1787
+ /**
1788
+ * Implementation of occupy.
1789
+ */
1790
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_occupy
1791
+ (JNIEnv* env, jobject jself, jboolean writable, jobject jproc) {
1792
+ try {
1793
+ kc::PolyDB* db = getdbcore(env, jself);
1794
+ if (!jproc) return db->occupy(writable);
1795
+ SoftFileProcessor proc(env, jproc);
1796
+ bool rv = db->occupy(writable, &proc);
1797
+ jthrowable jex = proc.exception();
1798
+ if (jex) {
1799
+ env->Throw(jex);
1800
+ return false;
1801
+ }
1802
+ if (rv) return true;
1803
+ throwdberror(env, jself);
1804
+ return false;
1805
+ } catch (std::exception& e) {
1806
+ return false;
1807
+ }
1808
+ }
1809
+
1810
+
1811
+ /**
1812
+ * Implementation of copy.
1813
+ */
1814
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_copy
1815
+ (JNIEnv* env, jobject jself, jstring jdest) {
1816
+ try {
1817
+ if (!jdest) {
1818
+ throwillarg(env);
1819
+ return false;
1820
+ }
1821
+ kc::PolyDB* db = getdbcore(env, jself);
1822
+ SoftString dest(env, jdest);
1823
+ bool rv = db->copy(dest.str());
1824
+ if (rv) return true;
1825
+ throwdberror(env, jself);
1826
+ return false;
1827
+ } catch (std::exception& e) {
1828
+ return false;
1829
+ }
1830
+ }
1831
+
1832
+
1833
+ /**
1834
+ * Implementation of begin_trnasaction.
1835
+ */
1836
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_begin_1transaction
1837
+ (JNIEnv* env, jobject jself, jboolean hard) {
1838
+ try {
1839
+ kc::PolyDB* db = getdbcore(env, jself);
1840
+ bool rv = db->begin_transaction(hard);
1841
+ if (rv) return true;
1842
+ throwdberror(env, jself);
1843
+ return false;
1844
+ } catch (std::exception& e) {
1845
+ return false;
1846
+ }
1847
+ }
1848
+
1849
+
1850
+ /**
1851
+ * Implementation of end_transaction.
1852
+ */
1853
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_end_1transaction
1854
+ (JNIEnv* env, jobject jself, jboolean commit) {
1855
+ try {
1856
+ kc::PolyDB* db = getdbcore(env, jself);
1857
+ bool rv = db->end_transaction(commit);
1858
+ if (rv) return true;
1859
+ throwdberror(env, jself);
1860
+ return false;
1861
+ } catch (std::exception& e) {
1862
+ return false;
1863
+ }
1864
+ }
1865
+
1866
+
1867
+ /**
1868
+ * Implementation of dump_snapshot.
1869
+ */
1870
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_dump_1snapshot
1871
+ (JNIEnv* env, jobject jself, jstring jdest) {
1872
+ try {
1873
+ if (!jdest) {
1874
+ throwillarg(env);
1875
+ return false;
1876
+ }
1877
+ kc::PolyDB* db = getdbcore(env, jself);
1878
+ SoftString dest(env, jdest);
1879
+ bool rv = db->dump_snapshot(dest.str());
1880
+ if (rv) return true;
1881
+ throwdberror(env, jself);
1882
+ return false;
1883
+ } catch (std::exception& e) {
1884
+ return false;
1885
+ }
1886
+ }
1887
+
1888
+
1889
+ /**
1890
+ * Implementation of load_snapshot.
1891
+ */
1892
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_load_1snapshot
1893
+ (JNIEnv* env, jobject jself, jstring jsrc) {
1894
+ try {
1895
+ if (!jsrc) {
1896
+ throwillarg(env);
1897
+ return false;
1898
+ }
1899
+ kc::PolyDB* db = getdbcore(env, jself);
1900
+ SoftString src(env, jsrc);
1901
+ bool rv = db->load_snapshot(src.str());
1902
+ if (rv) return true;
1903
+ throwdberror(env, jself);
1904
+ return false;
1905
+ } catch (std::exception& e) {
1906
+ return false;
1907
+ }
1908
+ }
1909
+
1910
+
1911
+ /**
1912
+ * Implementation of count.
1913
+ */
1914
+ JNIEXPORT jlong JNICALL Java_kyotocabinet_DB_count
1915
+ (JNIEnv* env, jobject jself) {
1916
+ try {
1917
+ kc::PolyDB* db = getdbcore(env, jself);
1918
+ int64_t count = db->count();
1919
+ if (count < 0) throwdberror(env, jself);
1920
+ return count;
1921
+ } catch (std::exception& e) {
1922
+ return 0;
1923
+ }
1924
+ }
1925
+
1926
+
1927
+ /**
1928
+ * Implementation of size.
1929
+ */
1930
+ JNIEXPORT jlong JNICALL Java_kyotocabinet_DB_size
1931
+ (JNIEnv* env, jobject jself) {
1932
+ try {
1933
+ kc::PolyDB* db = getdbcore(env, jself);
1934
+ int64_t size = db->size();
1935
+ if (size < 0) throwdberror(env, jself);
1936
+ return size;
1937
+ } catch (std::exception& e) {
1938
+ return 0;
1939
+ }
1940
+ }
1941
+
1942
+
1943
+ /**
1944
+ * Implementation of path.
1945
+ */
1946
+ JNIEXPORT jstring JNICALL Java_kyotocabinet_DB_path
1947
+ (JNIEnv* env, jobject jself) {
1948
+ try {
1949
+ kc::PolyDB* db = getdbcore(env, jself);
1950
+ std::string path = db->path().c_str();
1951
+ if (path.empty()) {
1952
+ throwdberror(env, jself);
1953
+ return NULL;
1954
+ }
1955
+ return newstring(env, path.c_str());
1956
+ } catch (std::exception& e) {
1957
+ return NULL;
1958
+ }
1959
+ }
1960
+
1961
+
1962
+ /**
1963
+ * Implementation of status.
1964
+ */
1965
+ JNIEXPORT jobject JNICALL Java_kyotocabinet_DB_status
1966
+ (JNIEnv* env, jobject jself) {
1967
+ try {
1968
+ kc::PolyDB* db = getdbcore(env, jself);
1969
+ StringMap status;
1970
+ if (!db->status(&status)) {
1971
+ throwdberror(env, jself);
1972
+ return NULL;
1973
+ }
1974
+ return maptojhash(env, &status);
1975
+ } catch (std::exception& e) {
1976
+ return NULL;
1977
+ }
1978
+ }
1979
+
1980
+
1981
+ /*
1982
+ * Implementation of match_prefix.
1983
+ */
1984
+ JNIEXPORT jobject JNICALL Java_kyotocabinet_DB_match_1prefix
1985
+ (JNIEnv* env, jobject jself, jstring jprefix, jlong max) {
1986
+ try {
1987
+ kc::PolyDB* db = getdbcore(env, jself);
1988
+ SoftString prefix(env, jprefix);
1989
+ StringVector keys;
1990
+ if (db->match_prefix(prefix.str(), &keys, max) == -1) {
1991
+ throwdberror(env, jself);
1992
+ return NULL;
1993
+ }
1994
+ return vectortojlist(env, &keys);
1995
+ } catch (std::exception& e) {
1996
+ return NULL;
1997
+ }
1998
+ }
1999
+
2000
+
2001
+ /*
2002
+ * Implementation of match_regex.
2003
+ */
2004
+ JNIEXPORT jobject JNICALL Java_kyotocabinet_DB_match_1regex
2005
+ (JNIEnv* env, jobject jself, jstring jregex, jlong max) {
2006
+ try {
2007
+ kc::PolyDB* db = getdbcore(env, jself);
2008
+ SoftString regex(env, jregex);
2009
+ StringVector keys;
2010
+ if (db->match_regex(regex.str(), &keys, max) == -1) {
2011
+ throwdberror(env, jself);
2012
+ return NULL;
2013
+ }
2014
+ return vectortojlist(env, &keys);
2015
+ } catch (std::exception& e) {
2016
+ return NULL;
2017
+ }
2018
+ }
2019
+
2020
+
2021
+ /*
2022
+ * Implementation of match_similar.
2023
+ */
2024
+ JNIEXPORT jobject JNICALL Java_kyotocabinet_DB_match_1similar
2025
+ (JNIEnv* env, jobject jself, jstring jorigin, jlong range, jboolean utf, jlong max) {
2026
+ try {
2027
+ kc::PolyDB* db = getdbcore(env, jself);
2028
+ SoftString origin(env, jorigin);
2029
+ StringVector keys;
2030
+ if (db->match_similar(origin.str(), range, utf, &keys, max) == -1) {
2031
+ throwdberror(env, jself);
2032
+ return NULL;
2033
+ }
2034
+ return vectortojlist(env, &keys);
2035
+ } catch (std::exception& e) {
2036
+ return NULL;
2037
+ }
2038
+ }
2039
+
2040
+
2041
+ /**
2042
+ * Implementation of merge.
2043
+ */
2044
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_DB_merge
2045
+ (JNIEnv* env, jobject jself, jobjectArray jsrcary, jint mode) {
2046
+ try {
2047
+ kc::PolyDB* db = getdbcore(env, jself);
2048
+ size_t srcnum = env->GetArrayLength(jsrcary);
2049
+ if (srcnum < 1) return true;
2050
+ kc::BasicDB** srcary = new kc::BasicDB*[srcnum];
2051
+ for (size_t i = 0; i < srcnum; i++) {
2052
+ jobject jsrcdb = env->GetObjectArrayElement(jsrcary, i);
2053
+ if (!jsrcdb) {
2054
+ delete[] srcary;
2055
+ return false;
2056
+ }
2057
+ srcary[i] = getdbcore(env, jsrcdb);
2058
+ }
2059
+ bool rv = db->merge(srcary, srcnum, (kc::PolyDB::MergeMode)mode);
2060
+ delete[] srcary;
2061
+ if (rv) return true;
2062
+ throwdberror(env, jself);
2063
+ return false;
2064
+ } catch (std::exception& e) {
2065
+ return false;
2066
+ }
2067
+ }
2068
+
2069
+
2070
+ /**
2071
+ * Implementation of cursor.
2072
+ */
2073
+ JNIEXPORT jobject JNICALL Java_kyotocabinet_DB_cursor
2074
+ (JNIEnv* env, jobject jself) {
2075
+ try {
2076
+ jclass cls_cur = env->FindClass(P_CUR);
2077
+ jmethodID id_cur_init = env->GetMethodID(cls_cur, "<init>", "(" L_DB ")V");
2078
+ return env->NewObject(cls_cur, id_cur_init, jself);
2079
+ } catch (std::exception& e) {
2080
+ return NULL;
2081
+ }
2082
+ }
2083
+
2084
+
2085
+ /**
2086
+ * Implementation of initialize.
2087
+ */
2088
+ JNIEXPORT void JNICALL Java_kyotocabinet_DB_initialize
2089
+ (JNIEnv* env, jobject jself, jint opts) {
2090
+ try {
2091
+ jclass cls_db = env->GetObjectClass(jself);
2092
+ jfieldID id_db_ptr = env->GetFieldID(cls_db, "ptr_", "J");
2093
+ jfieldID id_db_exbits = env->GetFieldID(cls_db, "exbits_", "I");
2094
+ kc::PolyDB* db = new kc::PolyDB();
2095
+ int32_t exbits = 0;
2096
+ if (opts & GEXCEPTIONAL) {
2097
+ exbits |= 1 << kc::PolyDB::Error::NOIMPL;
2098
+ exbits |= 1 << kc::PolyDB::Error::INVALID;
2099
+ exbits |= 1 << kc::PolyDB::Error::NOREPOS;
2100
+ exbits |= 1 << kc::PolyDB::Error::NOPERM;
2101
+ exbits |= 1 << kc::PolyDB::Error::BROKEN;
2102
+ exbits |= 1 << kc::PolyDB::Error::SYSTEM;
2103
+ exbits |= 1 << kc::PolyDB::Error::MISC;
2104
+ }
2105
+ env->SetLongField(jself, id_db_ptr, (intptr_t)db);
2106
+ env->SetIntField(jself, id_db_exbits, exbits);
2107
+ } catch (std::exception& e) {}
2108
+ }
2109
+
2110
+
2111
+ /**
2112
+ * Implementation of destruct.
2113
+ */
2114
+ JNIEXPORT void JNICALL Java_kyotocabinet_DB_destruct
2115
+ (JNIEnv* env, jobject jself) {
2116
+ try {
2117
+ kc::PolyDB* db = getdbcore(env, jself);
2118
+ delete db;
2119
+ } catch (std::exception& e) {}
2120
+ }
2121
+
2122
+
2123
+ /**
2124
+ * Implementation of execute.
2125
+ */
2126
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_MapReduce_execute
2127
+ (JNIEnv* env, jobject jself, jobject jdb, jstring jtmppath, jint opts) {
2128
+ try {
2129
+ SoftMapReduce mr(env, jself);
2130
+ kc::PolyDB* db = getdbcore(env, jdb);
2131
+ SoftString* tmppath = jtmppath ? new SoftString(env, jtmppath) : NULL;
2132
+ bool rv = mr.execute(db, tmppath ? tmppath->str() : "", opts);
2133
+ delete tmppath;
2134
+ jthrowable jex = mr.exception();
2135
+ if (jex) {
2136
+ env->Throw(jex);
2137
+ return false;
2138
+ }
2139
+ if (rv) return true;
2140
+ throwdberror(env, jdb);
2141
+ return false;
2142
+ } catch (std::exception& e) {
2143
+ return false;
2144
+ }
2145
+ }
2146
+
2147
+
2148
+ /**
2149
+ * Implementation of emit.
2150
+ */
2151
+ JNIEXPORT jboolean JNICALL Java_kyotocabinet_MapReduce_emit
2152
+ (JNIEnv* env, jobject jself, jbyteArray jkey, jbyteArray jvalue) {
2153
+ try {
2154
+ jclass cls_mr = env->GetObjectClass(jself);
2155
+ jfieldID id_mr_ptr = env->GetFieldID(cls_mr, "ptr_", "J");
2156
+ SoftMapReduce* mr = (SoftMapReduce*)(intptr_t)env->GetLongField(jself, id_mr_ptr);
2157
+ SoftArray key(env, jkey);
2158
+ SoftArray value(env, jvalue);
2159
+ return mr->emit_public(key.ptr(), key.size(), value.ptr(), value.size());
2160
+ } catch (std::exception& e) {
2161
+ return false;
2162
+ }
2163
+ }
2164
+
2165
+
2166
+ /**
2167
+ * Implementation of next.
2168
+ */
2169
+ JNIEXPORT jbyteArray JNICALL Java_kyotocabinet_ValueIterator_next
2170
+ (JNIEnv* env, jobject jself) {
2171
+ try {
2172
+ jclass cls_viter = env->GetObjectClass(jself);
2173
+ jfieldID id_viter_ptr = env->GetFieldID(cls_viter, "ptr_", "J");
2174
+ kc::MapReduce::ValueIterator* viter =
2175
+ (kc::MapReduce::ValueIterator*)(intptr_t)env->GetLongField(jself, id_viter_ptr);
2176
+ size_t vsiz;
2177
+ const char* vbuf = viter->next(&vsiz);
2178
+ if (vbuf) return newarray(env, vbuf, vsiz);
2179
+ return NULL;
2180
+ } catch (std::exception& e) {
2181
+ return NULL;
2182
+ }
2183
+ }
2184
+
2185
+
2186
+
2187
+ // END OF FILE