yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2013 Stanford University |
| 2 | * |
| 3 | * Permission to use, copy, modify, and distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR |
| 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | */ |
| 15 | |
Yuta HIGUCHI | b6cf583 | 2014-03-26 17:57:58 -0700 | [diff] [blame] | 16 | /* |
| 17 | * Future TODO memo. |
| 18 | * - Throwing exception is a bit expensive in Java. (Construct backtrace, etc.) |
| 19 | * Those native methods should be modified to notify conditional failure as |
| 20 | * part of return value object instead of exception to eliminate overhead. |
| 21 | * - Inner classes in JRamCloud.java should be moved out to be a separate |
| 22 | * stand alone class, to eliminate workaround 00024 signature in |
| 23 | * C methods. |
| 24 | * |
| 25 | */ |
| 26 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 27 | #include <RamCloud.h> |
| 28 | #include <TableEnumerator.h> |
| 29 | #include <Object.h> |
| 30 | #include "edu_stanford_ramcloud_JRamCloud.h" |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 31 | |
| 32 | using namespace RAMCloud; |
| 33 | |
| 34 | /// Our JRamCloud java library is packaged under "edu.stanford.ramcloud". |
| 35 | /// We will need this when using FindClass, etc. |
| 36 | #define PACKAGE_PATH "edu/stanford/ramcloud/" |
| 37 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 38 | // Java RejectRules flags bit index |
| 39 | const static int DoesntExist = 1; |
| 40 | const static int Exists = 1 << 1; |
| 41 | const static int VersionLeGiven = 1 << 2; |
| 42 | const static int VersionNeGiven = 1 << 3; |
| 43 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 44 | #define check_null(var, msg) \ |
| 45 | if (var == NULL) { \ |
| 46 | throw Exception(HERE, "JRamCloud: NULL returned: " msg "\n"); \ |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * This class provides a simple means of extracting C-style strings |
| 51 | * from a jstring and cleans up when the destructor is called. This |
| 52 | * avoids having to manually do the annoying GetStringUTFChars / |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 53 | * ReleaseStringUTFChars dance. |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 54 | */ |
| 55 | class JStringGetter { |
| 56 | public: |
| 57 | JStringGetter(JNIEnv* env, jstring jString) |
| 58 | : env(env) |
| 59 | , jString(jString) |
| 60 | , string(env->GetStringUTFChars(jString, 0)) |
| 61 | { |
| 62 | check_null(string, "GetStringUTFChars failed"); |
| 63 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 64 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 65 | ~JStringGetter() |
| 66 | { |
| 67 | if (string != NULL) |
| 68 | env->ReleaseStringUTFChars(jString, string); |
| 69 | } |
| 70 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 71 | private: |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 72 | JNIEnv* env; |
| 73 | jstring jString; |
| 74 | |
| 75 | public: |
| 76 | const char* const string; |
| 77 | }; |
| 78 | |
| 79 | /** |
| 80 | * This class provides a simple means of accessing jbyteArrays as |
| 81 | * C-style void* buffers and cleans up when the destructor is called. |
| 82 | * This avoids having to manually do the annoying GetByteArrayElements / |
| 83 | * ReleaseByteArrayElements dance. |
| 84 | */ |
| 85 | class JByteArrayGetter { |
| 86 | public: |
| 87 | JByteArrayGetter(JNIEnv* env, jbyteArray jByteArray) |
| 88 | : env(env) |
| 89 | , jByteArray(jByteArray) |
| 90 | , pointer(static_cast<void*>(env->GetByteArrayElements(jByteArray, 0))) |
| 91 | , length(env->GetArrayLength(jByteArray)) |
| 92 | { |
| 93 | check_null(pointer, "GetByteArrayElements failed"); |
| 94 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 95 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 96 | ~JByteArrayGetter() |
| 97 | { |
| 98 | if (pointer != NULL) { |
| 99 | env->ReleaseByteArrayElements(jByteArray, |
| 100 | reinterpret_cast<jbyte*>(pointer), |
| 101 | 0); |
| 102 | } |
| 103 | } |
| 104 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 105 | private: |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 106 | JNIEnv* env; |
| 107 | jbyteArray jByteArray; |
| 108 | |
| 109 | public: |
| 110 | void* const pointer; |
| 111 | const jsize length; |
| 112 | }; |
| 113 | |
| 114 | class JByteArrayReference { |
| 115 | public: |
| 116 | JByteArrayReference(JNIEnv* env, jbyteArray jByteArray) |
| 117 | : env(env) |
| 118 | , jByteArray(jByteArray) |
| 119 | , pointer(static_cast<const void*>(env->GetByteArrayElements(jByteArray, 0))) |
| 120 | , length(env->GetArrayLength(jByteArray)) |
| 121 | { |
| 122 | check_null(pointer, "GetByteArrayElements failed"); |
| 123 | } |
| 124 | |
| 125 | ~JByteArrayReference() |
| 126 | { |
| 127 | if (pointer != NULL) { |
| 128 | env->ReleaseByteArrayElements(jByteArray, |
| 129 | (jbyte*)pointer, |
| 130 | JNI_ABORT); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | private: |
| 135 | JNIEnv* env; |
| 136 | jbyteArray jByteArray; |
| 137 | |
| 138 | public: |
| 139 | const void* const pointer; |
| 140 | const jsize length; |
| 141 | }; |
| 142 | |
| 143 | static RamCloud* |
| 144 | getRamCloud(JNIEnv* env, jobject jRamCloud) |
| 145 | { |
| 146 | const static jclass cls = (jclass)env->NewGlobalRef(env->FindClass(PACKAGE_PATH "JRamCloud")); |
| 147 | const static jfieldID fieldId = env->GetFieldID(cls, "ramcloudObjectPointer", "J"); |
| 148 | return reinterpret_cast<RamCloud*>(env->GetLongField(jRamCloud, fieldId)); |
| 149 | } |
| 150 | |
| 151 | static TableEnumerator* |
| 152 | getTableEnumerator(JNIEnv* env, jobject jTableEnumerator) |
| 153 | { |
| 154 | const static jclass cls = (jclass)env->NewGlobalRef(env->FindClass(PACKAGE_PATH "JRamCloud$TableEnumerator")); |
| 155 | const static jfieldID fieldId = env->GetFieldID(cls, "tableEnumeratorObjectPointer", "J"); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 156 | return reinterpret_cast<TableEnumerator*>(env->GetLongField(jTableEnumerator, fieldId)); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static void |
| 160 | createException(JNIEnv* env, jobject jRamCloud, const char* name) |
| 161 | { |
| 162 | // Need to specify the full class name, including the package. To make it |
| 163 | // slightly more complicated, our exceptions are nested under the JRamCloud |
| 164 | // class. |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 165 | string fullName(PACKAGE_PATH "JRamCloud$"); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 166 | fullName += name; |
| 167 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 168 | jclass cls = env->FindClass(fullName.c_str()); |
| 169 | check_null(cls, "FindClass failed"); |
| 170 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 171 | env->ThrowNew(cls, ""); |
| 172 | } |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 173 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 174 | static void |
| 175 | setRejectRules(JNIEnv* env, jobject jRejectRules, RejectRules& rules) |
| 176 | { |
| 177 | const static jclass jc_RejectRules = (jclass) env->NewGlobalRef( |
| 178 | env->FindClass(PACKAGE_PATH "JRamCloud$RejectRules")); |
| 179 | static const jfieldID jf_flags = env->GetFieldID(jc_RejectRules, "flags", "I"); |
| 180 | check_null(jf_flags, "flags field ID is null"); |
| 181 | static const jfieldID jf_givenVersion = env->GetFieldID(jc_RejectRules, |
| 182 | "givenVersion", "J"); |
| 183 | check_null(jf_givenVersion, "givenVersion field ID is null"); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 184 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 185 | const jint rejectFlag = env->GetIntField(jRejectRules, jf_flags); |
| 186 | rules.doesntExist = (rejectFlag & DoesntExist) != 0; |
| 187 | rules.exists = (rejectFlag & Exists) != 0; |
| 188 | rules.versionLeGiven = (rejectFlag & VersionLeGiven) != 0; |
| 189 | rules.versionNeGiven = (rejectFlag & VersionNeGiven) != 0; |
| 190 | if (rules.versionLeGiven || rules.versionNeGiven) { |
| 191 | rules.givenVersion = env->GetLongField(jRejectRules, jf_givenVersion); |
| 192 | } |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
| 196 | * This macro is used to catch C++ exceptions and convert them into Java |
| 197 | * exceptions. Be sure to wrap the individual RamCloud:: calls in try blocks, |
| 198 | * rather than the entire methods, since doing so with functions that return |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 199 | * non-void is a bad idea with undefined(?) behaviour. |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 200 | * |
| 201 | * _returnValue is the value that should be returned from the JNI function |
| 202 | * when an exception is caught and generated in Java. As far as I can tell, |
| 203 | * the exception fires immediately upon returning from the JNI method. I |
| 204 | * don't think anything else would make sense, but the JNI docs kind of |
| 205 | * suck. |
| 206 | */ |
| 207 | #define EXCEPTION_CATCHER(_returnValue) \ |
| 208 | catch (TableDoesntExistException& e) { \ |
| 209 | createException(env, jRamCloud, "TableDoesntExistException"); \ |
| 210 | return _returnValue; \ |
| 211 | } catch (ObjectDoesntExistException& e) { \ |
| 212 | createException(env, jRamCloud, "ObjectDoesntExistException"); \ |
| 213 | return _returnValue; \ |
| 214 | } catch (ObjectExistsException& e) { \ |
| 215 | createException(env, jRamCloud, "ObjectExistsException"); \ |
| 216 | return _returnValue; \ |
| 217 | } catch (WrongVersionException& e) { \ |
| 218 | createException(env, jRamCloud, "WrongVersionException"); \ |
| 219 | return _returnValue; \ |
| 220 | } catch (RejectRulesException& e) { \ |
| 221 | createException(env, jRamCloud, "RejectRulesException"); \ |
| 222 | return _returnValue; \ |
| 223 | } catch (InvalidObjectException& e) { \ |
| 224 | createException(env, jRamCloud, "InvalidObjectException"); \ |
| 225 | return _returnValue; \ |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Class: edu_stanford_ramcloud_JRamCloud |
| 230 | * Method: connect |
| 231 | * Signature: (Ljava/lang/String;)J |
| 232 | */ |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 233 | JNIEXPORT jlong |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 234 | JNICALL Java_edu_stanford_ramcloud_JRamCloud_connect(JNIEnv *env, |
| 235 | jclass jRamCloud, |
| 236 | jstring coordinatorLocator) |
| 237 | { |
| 238 | JStringGetter locator(env, coordinatorLocator); |
| 239 | RamCloud* ramcloud = NULL; |
| 240 | try { |
| 241 | ramcloud = new RamCloud(locator.string); |
Yuta HIGUCHI | 5261772 | 2014-03-14 13:36:10 -0700 | [diff] [blame] | 242 | } EXCEPTION_CATCHER((jlong)(NULL)); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 243 | return reinterpret_cast<jlong>(ramcloud); |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Class: edu_stanford_ramcloud_JRamCloud |
| 248 | * Method: disconnect |
| 249 | * Signature: (J)V |
| 250 | */ |
| 251 | JNIEXPORT void |
| 252 | JNICALL Java_edu_stanford_ramcloud_JRamCloud_disconnect(JNIEnv *env, |
| 253 | jclass jRamCloud, |
| 254 | jlong ramcloudObjectPointer) |
| 255 | { |
| 256 | delete reinterpret_cast<RamCloud*>(ramcloudObjectPointer); |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Class: edu_stanford_ramcloud_JRamCloud |
| 261 | * Method: createTable |
| 262 | * Signature: (Ljava/lang/String;)I |
| 263 | */ |
| 264 | JNIEXPORT jlong |
| 265 | JNICALL Java_edu_stanford_ramcloud_JRamCloud_createTable__Ljava_lang_String_2(JNIEnv *env, |
| 266 | jobject jRamCloud, |
| 267 | jstring jTableName) |
| 268 | { |
| 269 | return Java_edu_stanford_ramcloud_JRamCloud_createTable__Ljava_lang_String_2I(env, |
| 270 | jRamCloud, |
| 271 | jTableName, |
| 272 | 1); |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Class: edu_stanford_ramcloud_JRamCloud |
| 277 | * Method: createTable |
| 278 | * Signature: (Ljava/lang/String;I)I |
| 279 | */ |
| 280 | JNIEXPORT jlong |
| 281 | JNICALL Java_edu_stanford_ramcloud_JRamCloud_createTable__Ljava_lang_String_2I(JNIEnv *env, |
| 282 | jobject jRamCloud, |
| 283 | jstring jTableName, |
| 284 | jint jServerSpan) |
| 285 | { |
| 286 | RamCloud* ramcloud = getRamCloud(env, jRamCloud); |
| 287 | JStringGetter tableName(env, jTableName); |
| 288 | uint64_t tableId; |
| 289 | try { |
| 290 | tableId = ramcloud->createTable(tableName.string, jServerSpan); |
| 291 | } EXCEPTION_CATCHER(-1); |
| 292 | return static_cast<jlong>(tableId); |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * Class: edu_stanford_ramcloud_JRamCloud |
| 297 | * Method: dropTable |
| 298 | * Signature: (Ljava/lang/String;)I |
| 299 | */ |
| 300 | JNIEXPORT void |
| 301 | JNICALL Java_edu_stanford_ramcloud_JRamCloud_dropTable(JNIEnv *env, |
| 302 | jobject jRamCloud, |
| 303 | jstring jTableName) |
| 304 | { |
| 305 | RamCloud* ramcloud = getRamCloud(env, jRamCloud); |
| 306 | JStringGetter tableName(env, jTableName); |
| 307 | try { |
| 308 | ramcloud->dropTable(tableName.string); |
| 309 | } EXCEPTION_CATCHER(); |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Class: edu_stanford_ramcloud_JRamCloud |
| 314 | * Method: getTableId |
| 315 | * Signature: (Ljava/lang/String;)J |
| 316 | */ |
| 317 | JNIEXPORT jlong |
| 318 | JNICALL Java_edu_stanford_ramcloud_JRamCloud_getTableId(JNIEnv *env, |
| 319 | jobject jRamCloud, |
| 320 | jstring jTableName) |
| 321 | { |
| 322 | RamCloud* ramcloud = getRamCloud(env, jRamCloud); |
| 323 | JStringGetter tableName(env, jTableName); |
| 324 | uint64_t tableId; |
| 325 | try { |
| 326 | tableId = ramcloud->getTableId(tableName.string); |
| 327 | } EXCEPTION_CATCHER(-1); |
| 328 | return tableId; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Class: edu_stanford_ramcloud_JRamCloud |
| 333 | * Method: read |
| 334 | * Signature: (J[B)LJRamCloud/Object; |
| 335 | */ |
| 336 | JNIEXPORT jobject |
| 337 | JNICALL Java_edu_stanford_ramcloud_JRamCloud_read__J_3B(JNIEnv *env, |
| 338 | jobject jRamCloud, |
| 339 | jlong jTableId, |
| 340 | jbyteArray jKey) |
| 341 | { |
| 342 | RamCloud* ramcloud = getRamCloud(env, jRamCloud); |
| 343 | JByteArrayReference key(env, jKey); |
| 344 | |
| 345 | Buffer buffer; |
| 346 | uint64_t version; |
| 347 | try { |
| 348 | ramcloud->read(jTableId, key.pointer, key.length, &buffer, NULL, &version); |
| 349 | } EXCEPTION_CATCHER(NULL); |
| 350 | |
| 351 | jbyteArray jValue = env->NewByteArray(buffer.getTotalLength()); |
| 352 | check_null(jValue, "NewByteArray failed"); |
| 353 | JByteArrayGetter value(env, jValue); |
| 354 | buffer.copy(0, buffer.getTotalLength(), value.pointer); |
| 355 | |
| 356 | // Note that using 'javap -s' on the class file will print out the method |
| 357 | // signatures (the third argument to GetMethodID). |
| 358 | const static jclass cls = (jclass)env->NewGlobalRef(env->FindClass(PACKAGE_PATH "JRamCloud$Object")); |
| 359 | check_null(cls, "FindClass failed"); |
| 360 | |
| 361 | const static jmethodID methodId = env->GetMethodID(cls, |
| 362 | "<init>", |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 363 | "([B[BJ)V"); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 364 | check_null(methodId, "GetMethodID failed"); |
| 365 | |
| 366 | return env->NewObject(cls, |
| 367 | methodId, |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 368 | jKey, |
| 369 | jValue, |
| 370 | static_cast<jlong>(version)); |
| 371 | } |
| 372 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 373 | // Workaround for javah generating incorrect signature for inner class |
| 374 | // 00024 is an escaped signature for $ character |
| 375 | #ifdef __cplusplus |
| 376 | extern "C" { |
| 377 | #endif |
| 378 | JNIEXPORT jobject JNICALL Java_edu_stanford_ramcloud_JRamCloud_read__J_3BLedu_stanford_ramcloud_JRamCloud_00024RejectRules_2 |
| 379 | (JNIEnv *, jobject, jlong, jbyteArray, jobject); |
| 380 | #ifdef __cplusplus |
| 381 | } |
| 382 | #endif |
| 383 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 384 | /* |
| 385 | * Class: edu_stanford_ramcloud_JRamCloud |
| 386 | * Method: read |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 387 | * Signature: (J[BLedu/stanford/ramcloud/JRamCloud/RejectRules;)Ledu/stanford/ramcloud/JRamCloud/Object; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 388 | */ |
| 389 | JNIEXPORT jobject |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 390 | JNICALL Java_edu_stanford_ramcloud_JRamCloud_read__J_3BLedu_stanford_ramcloud_JRamCloud_00024RejectRules_2(JNIEnv *env, |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 391 | jobject jRamCloud, |
| 392 | jlong jTableId, |
| 393 | jbyteArray jKey, |
| 394 | jobject jRejectRules) |
| 395 | { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 396 | RamCloud* ramcloud = getRamCloud(env, jRamCloud); |
| 397 | JByteArrayReference key(env, jKey); |
| 398 | |
| 399 | Buffer buffer; |
| 400 | uint64_t version; |
| 401 | RejectRules rules = {}; |
| 402 | setRejectRules(env, jRejectRules, rules); |
| 403 | |
| 404 | try { |
| 405 | ramcloud->read(jTableId, key.pointer, key.length, &buffer, &rules, &version); |
| 406 | } EXCEPTION_CATCHER(NULL); |
| 407 | |
| 408 | jbyteArray jValue = env->NewByteArray(buffer.getTotalLength()); |
| 409 | check_null(jValue, "NewByteArray failed"); |
| 410 | JByteArrayGetter value(env, jValue); |
| 411 | buffer.copy(0, buffer.getTotalLength(), value.pointer); |
| 412 | |
| 413 | // Note that using 'javap -s' on the class file will print out the method |
| 414 | // signatures (the third argument to GetMethodID). |
| 415 | const static jclass cls = (jclass)env->NewGlobalRef(env->FindClass(PACKAGE_PATH "JRamCloud$Object")); |
| 416 | check_null(cls, "FindClass failed"); |
| 417 | |
| 418 | const static jmethodID methodId = env->GetMethodID(cls, |
| 419 | "<init>", |
| 420 | "([B[BJ)V"); |
| 421 | check_null(methodId, "GetMethodID failed"); |
| 422 | |
| 423 | return env->NewObject(cls, |
| 424 | methodId, |
| 425 | jKey, |
| 426 | jValue, |
| 427 | static_cast<jlong>(version)); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Class: edu_stanford_ramcloud_JRamCloud |
| 432 | * Method: multiRead |
| 433 | * Signature: ([Ledu/stanford/ramcloud/JRamCloud$multiReadObject;I)[Ledu/stanford/ramcloud/JRamCloud$Object; |
| 434 | */ |
| 435 | JNIEXPORT jobjectArray |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 436 | JNICALL Java_edu_stanford_ramcloud_JRamCloud_multiRead(JNIEnv *env, |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 437 | jobject jRamCloud, |
Yoshi Muroi | 2c17060 | 2014-02-15 08:31:28 -0800 | [diff] [blame] | 438 | jlongArray jTableId, |
| 439 | jobjectArray jKeyData, |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 440 | jshortArray jKeyLength, |
Yoshi Muroi | 2c17060 | 2014-02-15 08:31:28 -0800 | [diff] [blame] | 441 | jint jrequestNum){ |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 442 | |
| 443 | RamCloud* ramcloud = getRamCloud(env, jRamCloud); |
Yoshi Muroi | 2c17060 | 2014-02-15 08:31:28 -0800 | [diff] [blame] | 444 | MultiReadObject objects[jrequestNum]; |
| 445 | Tub<Buffer> values[jrequestNum]; |
| 446 | jbyteArray jKey[jrequestNum]; |
| 447 | MultiReadObject* requests[jrequestNum]; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 448 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 449 | jlong tableId; |
| 450 | jshort keyLength; |
| 451 | jbyte* keyData[jrequestNum]; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 452 | |
Yoshi Muroi | 2c17060 | 2014-02-15 08:31:28 -0800 | [diff] [blame] | 453 | for (int i = 0 ; i < jrequestNum ; i++){ |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 454 | jKey[i] = (jbyteArray)env->GetObjectArrayElement(jKeyData, i); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 455 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 456 | env->GetShortArrayRegion(jKeyLength, i, 1, &keyLength); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 457 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 458 | keyData[i] = (jbyte *) malloc(keyLength); |
| 459 | env->GetByteArrayRegion(jKey[i], 0, keyLength, keyData[i]); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 460 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 461 | env->GetLongArrayRegion(jTableId, i, 1, &tableId); |
| 462 | objects[i].tableId = tableId; |
| 463 | objects[i].key = keyData[i]; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 464 | objects[i].keyLength = keyLength; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 465 | objects[i].value = &values[i]; |
| 466 | requests[i] = &objects[i]; |
| 467 | } |
| 468 | |
| 469 | try { |
Yoshi Muroi | 2c17060 | 2014-02-15 08:31:28 -0800 | [diff] [blame] | 470 | ramcloud->multiRead(requests, jrequestNum); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 471 | } EXCEPTION_CATCHER(NULL); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 472 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 473 | const static jclass jc_RcObject = (jclass)env->NewGlobalRef(env->FindClass(PACKAGE_PATH "JRamCloud$Object")); |
| 474 | check_null(jc_RcObject, "FindClass failed"); |
| 475 | const static jmethodID jm_init = env->GetMethodID(jc_RcObject, |
| 476 | "<init>", |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 477 | "([B[BJ)V"); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 478 | |
Yoshi Muroi | 2c17060 | 2014-02-15 08:31:28 -0800 | [diff] [blame] | 479 | jobjectArray outJNIArray = env->NewObjectArray(jrequestNum, jc_RcObject , NULL); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 480 | check_null(outJNIArray, "NewObjectArray failed"); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 481 | |
Yoshi Muroi | 2c17060 | 2014-02-15 08:31:28 -0800 | [diff] [blame] | 482 | for (int i = 0 ; i < jrequestNum ; i++) { |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 483 | if (objects[i].status == 0) { |
| 484 | jbyteArray jValue = env->NewByteArray(values[i].get()->getTotalLength()); |
| 485 | check_null(jValue, "NewByteArray failed"); |
| 486 | JByteArrayGetter value(env, jValue); |
| 487 | values[i].get()->copy(0, values[i].get()->getTotalLength(), value.pointer); |
Yuta HIGUCHI | 5261772 | 2014-03-14 13:36:10 -0700 | [diff] [blame] | 488 | jobject obj = env->NewObject(jc_RcObject, jm_init, jKey[i], jValue, (jlong)objects[i].version); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 489 | check_null(obj, "NewObject failed"); |
| 490 | env->SetObjectArrayElement(outJNIArray, i, obj); |
| 491 | } |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 492 | free(keyData[i]); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 493 | } |
| 494 | return outJNIArray; |
| 495 | } |
| 496 | |
| 497 | |
| 498 | /* |
| 499 | * Class: edu_stanford_ramcloud_JRamCloud |
| 500 | * Method: remove |
| 501 | * Signature: (J[B)J |
| 502 | */ |
| 503 | JNIEXPORT jlong |
| 504 | JNICALL Java_edu_stanford_ramcloud_JRamCloud_remove__J_3B(JNIEnv *env, |
| 505 | jobject jRamCloud, |
| 506 | jlong jTableId, |
| 507 | jbyteArray jKey) |
| 508 | { |
| 509 | RamCloud* ramcloud = getRamCloud(env, jRamCloud); |
| 510 | JByteArrayReference key(env, jKey); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 511 | uint64_t version = VERSION_NONEXISTENT; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 512 | try { |
| 513 | ramcloud->remove(jTableId, key.pointer, key.length, NULL, &version); |
| 514 | } EXCEPTION_CATCHER(-1); |
| 515 | return static_cast<jlong>(version); |
| 516 | } |
| 517 | |
Yuta HIGUCHI | 9402dab | 2014-01-30 19:54:31 -0800 | [diff] [blame] | 518 | |
| 519 | // Workaround for javah generating incorrect signature for inner class |
| 520 | // 00024 is an escaped signature for $ character |
| 521 | #ifdef __cplusplus |
| 522 | extern "C" { |
| 523 | #endif |
| 524 | JNIEXPORT jlong JNICALL Java_edu_stanford_ramcloud_JRamCloud_remove__J_3BLedu_stanford_ramcloud_JRamCloud_00024RejectRules_2 |
| 525 | (JNIEnv *, jobject, jlong, jbyteArray, jobject); |
| 526 | #ifdef __cplusplus |
| 527 | } |
| 528 | #endif |
| 529 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 530 | /* |
| 531 | * Class: edu_stanford_ramcloud_JRamCloud |
| 532 | * Method: remove |
Yuta HIGUCHI | 9402dab | 2014-01-30 19:54:31 -0800 | [diff] [blame] | 533 | * Signature: (J[BLedu/stanford/ramcloud/JRamCloud/$RejectRules;)J |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 534 | */ |
| 535 | JNIEXPORT jlong |
Yuta HIGUCHI | 9402dab | 2014-01-30 19:54:31 -0800 | [diff] [blame] | 536 | JNICALL Java_edu_stanford_ramcloud_JRamCloud_remove__J_3BLedu_stanford_ramcloud_JRamCloud_00024RejectRules_2(JNIEnv *env, |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 537 | jobject jRamCloud, |
| 538 | jlong jTableId, |
| 539 | jbyteArray jKey, |
| 540 | jobject jRejectRules) |
| 541 | { |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 542 | RamCloud* ramcloud = getRamCloud(env, jRamCloud); |
| 543 | JByteArrayReference key(env, jKey); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 544 | RejectRules rules = {}; |
| 545 | setRejectRules(env, jRejectRules, rules); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 546 | uint64_t version = VERSION_NONEXISTENT; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 547 | try { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 548 | ramcloud->remove(jTableId, key.pointer, key.length, &rules, &version); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 549 | } EXCEPTION_CATCHER(-1); |
| 550 | return static_cast<jlong>(version); |
| 551 | } |
| 552 | |
| 553 | /* |
| 554 | * Class: edu_stanford_ramcloud_JRamCloud |
| 555 | * Method: write |
| 556 | * Signature: (J[B[B)J |
| 557 | */ |
| 558 | JNIEXPORT jlong |
| 559 | JNICALL Java_edu_stanford_ramcloud_JRamCloud_write__J_3B_3B(JNIEnv *env, |
| 560 | jobject jRamCloud, |
| 561 | jlong jTableId, |
| 562 | jbyteArray jKey, |
| 563 | jbyteArray jValue) |
| 564 | { |
| 565 | RamCloud* ramcloud = getRamCloud(env, jRamCloud); |
| 566 | JByteArrayReference key(env, jKey); |
| 567 | JByteArrayGetter value(env, jValue); |
| 568 | uint64_t version; |
| 569 | try { |
| 570 | ramcloud->write(jTableId, |
| 571 | key.pointer, key.length, |
| 572 | value.pointer, value.length, |
| 573 | NULL, |
| 574 | &version); |
| 575 | } EXCEPTION_CATCHER(-1); |
| 576 | return static_cast<jlong>(version); |
| 577 | } |
| 578 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 579 | // Workaround for javah generating incorrect signature for inner class |
| 580 | // 00024 is an escaped signature for $ character |
| 581 | #ifdef __cplusplus |
| 582 | extern "C" { |
| 583 | #endif |
| 584 | JNIEXPORT jlong JNICALL Java_edu_stanford_ramcloud_JRamCloud_write__J_3B_3BLedu_stanford_ramcloud_JRamCloud_00024RejectRules_2 |
| 585 | (JNIEnv *, jobject, jlong, jbyteArray, jbyteArray, jobject); |
| 586 | #ifdef __cplusplus |
| 587 | } |
| 588 | #endif |
| 589 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 590 | /* |
| 591 | * Class: edu_stanford_ramcloud_JRamCloud |
| 592 | * Method: write |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 593 | * Signature: (J[B[BLedu/stanford/ramcloud/JRamCloud/RejectRules;)J |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 594 | */ |
| 595 | JNIEXPORT jlong |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 596 | JNICALL Java_edu_stanford_ramcloud_JRamCloud_write__J_3B_3BLedu_stanford_ramcloud_JRamCloud_00024RejectRules_2(JNIEnv *env, |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 597 | jobject jRamCloud, |
| 598 | jlong jTableId, |
| 599 | jbyteArray jKey, |
| 600 | jbyteArray jValue, |
| 601 | jobject jRejectRules) |
| 602 | { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 603 | return Java_edu_stanford_ramcloud_JRamCloud_writeRule(env, jRamCloud, jTableId, jKey, jValue, jRejectRules); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 604 | } |
| 605 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 606 | /* |
| 607 | * Class: edu_stanford_ramcloud_JRamCloud |
| 608 | * Method: writeRule |
| 609 | * Signature: (J[B[BLedu/stanford/ramcloud/JRamCloud/RejectRules;)J |
| 610 | */ |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 611 | JNIEXPORT jlong |
| 612 | JNICALL Java_edu_stanford_ramcloud_JRamCloud_writeRule(JNIEnv *env, |
| 613 | jobject jRamCloud, |
| 614 | jlong jTableId, |
| 615 | jbyteArray jKey, |
| 616 | jbyteArray jValue, |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 617 | jobject jRejectRules) |
| 618 | { |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 619 | RamCloud* ramcloud = getRamCloud(env, jRamCloud); |
| 620 | JByteArrayReference key(env, jKey); |
| 621 | JByteArrayGetter value(env, jValue); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 622 | RejectRules rules = {}; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 623 | setRejectRules(env, jRejectRules, rules); |
| 624 | uint64_t version; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 625 | try { |
| 626 | ramcloud->write(jTableId, |
| 627 | key.pointer, key.length, |
| 628 | value.pointer, value.length, |
| 629 | &rules, |
| 630 | &version); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 631 | } EXCEPTION_CATCHER(-1); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 632 | return static_cast<jlong> (version); |
| 633 | } |
| 634 | |
| 635 | /* |
| 636 | * Class: edu_stanford_ramcloud_JRamCloud_TableEnumerator |
| 637 | * Method: init |
| 638 | * Signature: (J)V |
| 639 | */ |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 640 | JNIEXPORT jlong JNICALL Java_edu_stanford_ramcloud_JRamCloud_00024TableEnumerator_init(JNIEnv *env, |
| 641 | jobject jTableEnumerator, |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 642 | jlong jTableId) |
| 643 | { |
| 644 | const static jclass cls = (jclass)env->NewGlobalRef(env->FindClass(PACKAGE_PATH "JRamCloud$TableEnumerator")); |
| 645 | const static jfieldID fieldId = env->GetFieldID(cls, "ramCloudObjectPointer", "J"); |
| 646 | RamCloud* ramcloud = reinterpret_cast<RamCloud*>(env->GetLongField(jTableEnumerator, fieldId)); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 647 | |
Yuta HIGUCHI | a7ec073 | 2014-03-10 16:01:06 -0700 | [diff] [blame] | 648 | return reinterpret_cast<jlong>(new TableEnumerator(*ramcloud, jTableId, false)); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Class: edu_stanford_ramcloud_JRamCloud_TableEnumerator |
| 653 | * Method: hasNext |
| 654 | * Signature: ()Z |
| 655 | */ |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 656 | JNIEXPORT jboolean JNICALL Java_edu_stanford_ramcloud_JRamCloud_00024TableEnumerator_hasNext( JNIEnv *env, |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 657 | jobject jTableEnumerator) |
| 658 | { |
| 659 | TableEnumerator* tableEnum = getTableEnumerator(env, jTableEnumerator); |
| 660 | return static_cast<jboolean>(tableEnum->hasNext()); |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * Class: edu_stanford_ramcloud_JRamCloud_TableEnumerator |
| 665 | * Method: next |
| 666 | * Signature: ()Ledu/stanford/ramcloud/JRamCloud/Object; |
| 667 | */ |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 668 | JNIEXPORT jobject JNICALL Java_edu_stanford_ramcloud_JRamCloud_00024TableEnumerator_next( JNIEnv *env, |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 669 | jobject jTableEnumerator) |
| 670 | { |
| 671 | TableEnumerator* tableEnum = getTableEnumerator(env, jTableEnumerator); |
| 672 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 673 | if (tableEnum->hasNext()) { |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 674 | uint32_t size = 0; |
| 675 | const void* buffer = 0; |
| 676 | uint64_t version = 0; |
| 677 | |
| 678 | tableEnum->next(&size, &buffer); |
| 679 | Object object(buffer, size); |
| 680 | |
| 681 | jbyteArray jKey = env->NewByteArray(object.getKeyLength()); |
| 682 | jbyteArray jValue = env->NewByteArray(object.getDataLength()); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 683 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 684 | JByteArrayGetter key(env, jKey); |
| 685 | JByteArrayGetter value(env, jValue); |
| 686 | |
| 687 | memcpy(key.pointer, object.getKey(), object.getKeyLength()); |
| 688 | memcpy(value.pointer, object.getData(), object.getDataLength()); |
| 689 | |
| 690 | version = object.getVersion(); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 691 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 692 | const static jclass cls = (jclass)env->NewGlobalRef(env->FindClass(PACKAGE_PATH "JRamCloud$Object")); |
| 693 | check_null(cls, "FindClass failed"); |
| 694 | const static jmethodID methodId = env->GetMethodID(cls, |
| 695 | "<init>", |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 696 | "([B[BJ)V"); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 697 | check_null(methodId, "GetMethodID failed"); |
| 698 | return env->NewObject(cls, |
| 699 | methodId, |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 700 | jKey, |
| 701 | jValue, |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 702 | static_cast<jlong>(version)); |
| 703 | } else { |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 704 | return NULL; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 705 | } |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | /* |
| 709 | * Class: edu_stanford_ramcloud_JRamCloud |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 710 | * Method: getTableObjects |
| 711 | * Signature: (JJ)Ledu/stanford/ramcloud/JRamCloud/TableEnumeratorObject; |
| 712 | */ |
| 713 | JNIEXPORT jobject JNICALL Java_edu_stanford_ramcloud_JRamCloud_getTableObjects(JNIEnv *env, |
| 714 | jobject jRamCloud, |
| 715 | jlong jTableId, |
| 716 | jlong jTabletNextHash){ |
| 717 | |
| 718 | RamCloud* ramcloud = getRamCloud(env, jRamCloud); |
| 719 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 720 | const static jclass jc_RcObject = (jclass)env->NewGlobalRef(env->FindClass(PACKAGE_PATH "JRamCloud$Object")); |
| 721 | check_null(jc_RcObject, "FindClass failed"); |
| 722 | const static jmethodID jm_init = env->GetMethodID(jc_RcObject, |
| 723 | "<init>", |
| 724 | "([B[BJ)V"); |
| 725 | check_null(jm_init, "GetMethodID failed"); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 726 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 727 | const static jclass jc_RcTableObject = (jclass)env->NewGlobalRef(env->FindClass(PACKAGE_PATH "JRamCloud$TableEnumeratorObject")); |
| 728 | check_null(jc_RcTableObject, "FindClass failed"); |
| 729 | const static jmethodID jm_TableEnumeratorObject_init = env->GetMethodID(jc_RcTableObject, |
| 730 | "<init>", |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 731 | "([L" PACKAGE_PATH "JRamCloud$Object;J)V"); |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 732 | check_null(jm_TableEnumeratorObject_init, "GetMethodID failed"); |
| 733 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 734 | |
| 735 | Buffer state; |
| 736 | Buffer objects; |
| 737 | bool done = false; |
| 738 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 739 | while (true) { |
Yuta HIGUCHI | a7ec073 | 2014-03-10 16:01:06 -0700 | [diff] [blame] | 740 | jTabletNextHash = ramcloud->enumerateTable(jTableId, false, jTabletNextHash, state, objects); |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 741 | if (objects.getTotalLength() > 0) { |
| 742 | break; |
| 743 | } |
| 744 | if (objects.getTotalLength() == 0 && jTabletNextHash == 0) { |
| 745 | done = true; |
| 746 | break; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 747 | } |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 748 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 749 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 750 | if (done) { |
| 751 | return env->NewObject(jc_RcTableObject, jm_TableEnumeratorObject_init, env->NewObjectArray(0, jc_RcObject , NULL), 0); |
| 752 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 753 | |
| 754 | int numOfObjects = 0; |
| 755 | uint32_t nextOffset = 0; |
| 756 | for (numOfObjects = 0; nextOffset < objects.getTotalLength() ; numOfObjects++) { |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 757 | uint32_t objectSize = *objects.getOffset<uint32_t>(nextOffset); |
| 758 | nextOffset += downCast<uint32_t>(sizeof(uint32_t)); |
| 759 | nextOffset += objectSize; |
| 760 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 761 | |
| 762 | jobjectArray outJNIArray = env->NewObjectArray(numOfObjects, jc_RcObject , NULL); |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 763 | check_null(outJNIArray, "NewObjectArray failed"); |
| 764 | |
| 765 | nextOffset = 0; |
| 766 | for (int i = 0; nextOffset < objects.getTotalLength() ;i++) { |
| 767 | uint32_t objectSize = *objects.getOffset<uint32_t>(nextOffset); |
| 768 | nextOffset += downCast<uint32_t>(sizeof(uint32_t)); |
| 769 | |
| 770 | const void* blob = objects.getRange(nextOffset, objectSize); |
| 771 | nextOffset += objectSize; |
| 772 | |
| 773 | Object object(blob, objectSize); |
| 774 | |
| 775 | jbyteArray jKey = env->NewByteArray(object.getKeyLength()); |
| 776 | jbyteArray jValue = env->NewByteArray(object.getDataLength()); |
| 777 | |
| 778 | JByteArrayGetter key(env, jKey); |
| 779 | JByteArrayGetter value(env, jValue); |
| 780 | |
| 781 | memcpy(key.pointer, object.getKey(), object.getKeyLength()); |
| 782 | memcpy(value.pointer, object.getData(), object.getDataLength()); |
| 783 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 784 | uint64_t version = object.getVersion(); |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 785 | |
| 786 | jobject obj = env->NewObject(jc_RcObject, jm_init, jKey, jValue, static_cast<jlong>(version)); |
| 787 | check_null(obj, "NewObject failed"); |
| 788 | |
| 789 | env->SetObjectArrayElement(outJNIArray, i, obj); |
| 790 | } |
| 791 | |
| 792 | return env->NewObject(jc_RcTableObject, jm_TableEnumeratorObject_init, outJNIArray, jTabletNextHash); |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /* |
| 796 | * Class: edu_stanford_ramcloud_JRamCloud |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 797 | * Method: multiWrite |
| 798 | * Signature: ([Ledu/stanford/ramcloud/JRamCloud/MultiWriteObject;)[Ledu/stanford/ramcloud/JRamCloud/MultiWriteRspObject; |
| 799 | */ |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 800 | JNIEXPORT jobjectArray JNICALL Java_edu_stanford_ramcloud_JRamCloud_multiWrite(JNIEnv *env, |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 801 | jobject jRamCloud, |
| 802 | jlongArray jTableId, |
| 803 | jobjectArray jKeyData, |
| 804 | jshortArray jKeyLength, |
| 805 | jobjectArray jValueData, |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 806 | jintArray jValueLength, |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 807 | jint jrequestNum, |
| 808 | jobjectArray jRules ) { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 809 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 810 | RamCloud* ramcloud = getRamCloud(env, jRamCloud); |
| 811 | Tub<MultiWriteObject> objects[jrequestNum]; |
| 812 | MultiWriteObject *requests[jrequestNum]; |
| 813 | RejectRules rules[jrequestNum]; |
| 814 | jbyteArray jKey[jrequestNum]; |
| 815 | jbyteArray jValue[jrequestNum]; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 816 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 817 | jlong tableId; |
| 818 | jshort keyLength; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 819 | jint valueLength; |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 820 | jbyte* keyData[jrequestNum]; |
| 821 | jbyte* valueData[jrequestNum]; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 822 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 823 | const static jclass jc_RejectRules = (jclass)env->NewGlobalRef(env->FindClass(PACKAGE_PATH "JRamCloud$RejectRules")); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 824 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 825 | const static jfieldID jf_givenVersion = env->GetFieldID(jc_RejectRules, "givenVersion", "J"); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 826 | check_null(jf_givenVersion, "givenVersion field ID is null"); |
| 827 | |
| 828 | const static jfieldID jf_flags = env->GetFieldID(jc_RejectRules, "flags", "I"); |
| 829 | check_null(jf_flags, "flags field ID is null"); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 830 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 831 | for (int i = 0; i < jrequestNum; i++) { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 832 | env->GetLongArrayRegion(jTableId, i, 1, &tableId); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 833 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 834 | env->GetShortArrayRegion(jKeyLength, i, 1, &keyLength); |
| 835 | jKey[i] = (jbyteArray)env->GetObjectArrayElement(jKeyData, i); |
| 836 | keyData[i] = (jbyte *) malloc(keyLength); |
| 837 | env->GetByteArrayRegion(jKey[i], 0, keyLength, keyData[i]); |
| 838 | |
| 839 | env->GetIntArrayRegion(jValueLength, i, 1, &valueLength); |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 840 | jValue[i] = (jbyteArray)env->GetObjectArrayElement(jValueData, i); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 841 | valueData[i] = (jbyte *) malloc(valueLength); |
| 842 | env->GetByteArrayRegion(jValue[i], 0, valueLength, valueData[i]); |
| 843 | |
| 844 | jobject jRejectRules = (jbyteArray)env->GetObjectArrayElement(jRules, i); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 845 | rules[i] = {}; |
| 846 | |
| 847 | if (jRejectRules != NULL) { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 848 | const jint flags = env->GetIntField(jRejectRules, jf_flags); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 849 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 850 | rules[i].doesntExist = (flags & DoesntExist) != 0; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 851 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 852 | rules[i].exists = (flags & Exists) != 0; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 853 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 854 | rules[i].versionLeGiven = (flags & VersionLeGiven) != 0; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 855 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 856 | rules[i].versionNeGiven = (flags & VersionNeGiven) != 0; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 857 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 858 | if (rules[i].versionLeGiven || rules[i].versionNeGiven) { |
| 859 | rules[i].givenVersion = env->GetLongField(jRejectRules, jf_givenVersion); |
| 860 | } |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 861 | } |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 862 | objects[i].construct(tableId, keyData[i], keyLength, valueData[i], valueLength, &rules[i]); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 863 | requests[i] = objects[i].get(); |
| 864 | } |
| 865 | try { |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 866 | ramcloud->multiWrite(requests, jrequestNum); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 867 | } EXCEPTION_CATCHER(NULL); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 868 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 869 | const static jclass jc_RcObject = (jclass)env->NewGlobalRef(env->FindClass(PACKAGE_PATH "JRamCloud$MultiWriteRspObject")); |
| 870 | check_null(jc_RcObject, "FindClass failed"); |
| 871 | const static jmethodID jm_init = env->GetMethodID(jc_RcObject, |
| 872 | "<init>", |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 873 | "(IJ)V"); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 874 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 875 | jobjectArray outJNIArray = env->NewObjectArray(jrequestNum, jc_RcObject , NULL); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 876 | check_null(outJNIArray, "NewObjectArray failed"); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 877 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 878 | for (int i = 0 ; i < jrequestNum ; i++) { |
| 879 | jobject obj = env->NewObject(jc_RcObject, jm_init, objects[i]->status, objects[i]->version); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 880 | check_null(obj, "NewObject failed"); |
| 881 | env->SetObjectArrayElement(outJNIArray, i, obj); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 882 | free(keyData[i]); |
| 883 | free(valueData[i]); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 884 | objects[i].destroy(); |
| 885 | } |
| 886 | return outJNIArray; |
| 887 | } |