portal.h 27.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright (c) 2014, Facebook, Inc.  All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.

// This file is designed for caching those frequently used IDs and provide
// efficient portal (i.e, a set of static functions) to access java code
// from c++.

#ifndef JAVA_ROCKSJNI_PORTAL_H_
#define JAVA_ROCKSJNI_PORTAL_H_

#include <jni.h>
14
#include <limits>
15
#include <string>
16
#include <vector>
17

18
#include "rocksdb/db.h"
A
Ankit Gupta 已提交
19
#include "rocksdb/filter_policy.h"
20
#include "rocksdb/status.h"
21
#include "rocksdb/utilities/backupable_db.h"
22
#include "rocksdb/utilities/write_batch_with_index.h"
23
#include "rocksjni/comparatorjnicallback.h"
A
Adam Retter 已提交
24
#include "rocksjni/writebatchhandlerjnicallback.h"
25 26 27

namespace rocksdb {

28 29 30 31 32 33 34
// detect if jlong overflows size_t
inline Status check_if_jlong_fits_size_t(const jlong& jvalue) {
  Status s = Status::OK();
  if (static_cast<uint64_t>(jvalue) > std::numeric_limits<size_t>::max()) {
    s = Status::InvalidArgument(Slice("jlong overflows 32 bit value."));
  }
  return s;
35 36
}

37 38 39 40 41
// The portal class for org.rocksdb.RocksDB
class RocksDBJni {
 public:
  // Get the java class id of org.rocksdb.RocksDB.
  static jclass getJClass(JNIEnv* env) {
42
    jclass jclazz = env->FindClass("org/rocksdb/RocksDB");
43 44 45 46 47 48 49
    assert(jclazz != nullptr);
    return jclazz;
  }

  // Get the field id of the member variable of org.rocksdb.RocksDB
  // that stores the pointer to rocksdb::DB.
  static jfieldID getHandleFieldID(JNIEnv* env) {
50
    static jfieldID fid = env->GetFieldID(
51
        getJClass(env), "nativeHandle_", "J");
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    assert(fid != nullptr);
    return fid;
  }

  // Get the pointer to rocksdb::DB of the specified org.rocksdb.RocksDB.
  static rocksdb::DB* getHandle(JNIEnv* env, jobject jdb) {
    return reinterpret_cast<rocksdb::DB*>(
        env->GetLongField(jdb, getHandleFieldID(env)));
  }

  // Pass the rocksdb::DB pointer to the java side.
  static void setHandle(JNIEnv* env, jobject jdb, rocksdb::DB* db) {
    env->SetLongField(
        jdb, getHandleFieldID(env),
        reinterpret_cast<jlong>(db));
  }
};

// The portal class for org.rocksdb.RocksDBException
class RocksDBExceptionJni {
 public:
  // Get the jclass of org.rocksdb.RocksDBException
  static jclass getJClass(JNIEnv* env) {
75
    jclass jclazz = env->FindClass("org/rocksdb/RocksDBException");
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    assert(jclazz != nullptr);
    return jclazz;
  }

  // Create and throw a java exception by converting the input
  // Status to an RocksDBException.
  //
  // In case s.ok() is true, then this function will not throw any
  // exception.
  static void ThrowNew(JNIEnv* env, Status s) {
    if (s.ok()) {
      return;
    }
    jstring msg = env->NewStringUTF(s.ToString().c_str());
    // get the constructor id of org.rocksdb.RocksDBException
91
    static jmethodID mid = env->GetMethodID(
92 93 94 95 96 97 98
        getJClass(env), "<init>", "(Ljava/lang/String;)V");
    assert(mid != nullptr);

    env->Throw((jthrowable)env->NewObject(getJClass(env), mid, msg));
  }
};

99 100 101 102
class OptionsJni {
 public:
  // Get the java class id of org.rocksdb.Options.
  static jclass getJClass(JNIEnv* env) {
103
    jclass jclazz = env->FindClass("org/rocksdb/Options");
104 105 106 107 108 109 110
    assert(jclazz != nullptr);
    return jclazz;
  }

  // Get the field id of the member variable of org.rocksdb.Options
  // that stores the pointer to rocksdb::Options
  static jfieldID getHandleFieldID(JNIEnv* env) {
111
    static jfieldID fid = env->GetFieldID(
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        getJClass(env), "nativeHandle_", "J");
    assert(fid != nullptr);
    return fid;
  }

  // Get the pointer to rocksdb::Options
  static rocksdb::Options* getHandle(JNIEnv* env, jobject jobj) {
    return reinterpret_cast<rocksdb::Options*>(
        env->GetLongField(jobj, getHandleFieldID(env)));
  }

  // Pass the rocksdb::Options pointer to the java side.
  static void setHandle(JNIEnv* env, jobject jobj, rocksdb::Options* op) {
    env->SetLongField(
        jobj, getHandleFieldID(env),
        reinterpret_cast<jlong>(op));
  }
};

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
class DBOptionsJni {
 public:
  // Get the java class id of org.rocksdb.DBOptions.
  static jclass getJClass(JNIEnv* env) {
    jclass jclazz = env->FindClass("org/rocksdb/DBOptions");
    assert(jclazz != nullptr);
    return jclazz;
  }

  // Get the field id of the member variable of org.rocksdb.DBOptions
  // that stores the pointer to rocksdb::DBOptions
  static jfieldID getHandleFieldID(JNIEnv* env) {
    static jfieldID fid = env->GetFieldID(
        getJClass(env), "nativeHandle_", "J");
    assert(fid != nullptr);
    return fid;
  }

  // Get the pointer to rocksdb::DBOptions
  static rocksdb::DBOptions* getHandle(JNIEnv* env, jobject jobj) {
    return reinterpret_cast<rocksdb::DBOptions*>(
        env->GetLongField(jobj, getHandleFieldID(env)));
  }

  // Pass the rocksdb::DBOptions pointer to the java side.
  static void setHandle(JNIEnv* env, jobject jobj, rocksdb::DBOptions* op) {
    env->SetLongField(
        jobj, getHandleFieldID(env),
        reinterpret_cast<jlong>(op));
  }
};

163 164 165 166 167 168 169 170 171 172 173 174 175
class ColumnFamilyDescriptorJni {
 public:
  // Get the java class id of org.rocksdb.ColumnFamilyDescriptor
  static jclass getColumnFamilyDescriptorClass(JNIEnv* env) {
    jclass jclazz = env->FindClass("org/rocksdb/ColumnFamilyDescriptor");
    assert(jclazz != nullptr);
    return jclazz;
  }

  // Get the java method id of columnFamilyName
  static jmethodID getColumnFamilyNameMethod(JNIEnv* env) {
    static jmethodID mid = env->GetMethodID(
        getColumnFamilyDescriptorClass(env),
176
        "columnFamilyName", "()[B");
177 178 179 180 181 182 183 184 185 186 187 188 189 190
    assert(mid != nullptr);
    return mid;
  }

  // Get the java method id of columnFamilyOptions
  static jmethodID getColumnFamilyOptionsMethod(JNIEnv* env) {
    static jmethodID mid = env->GetMethodID(
        getColumnFamilyDescriptorClass(env),
        "columnFamilyOptions", "()Lorg/rocksdb/ColumnFamilyOptions;");
    assert(mid != nullptr);
    return mid;
  }
};

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
class ColumnFamilyOptionsJni {
 public:
  // Get the java class id of org.rocksdb.ColumnFamilyOptions.
  static jclass getJClass(JNIEnv* env) {
    jclass jclazz = env->FindClass("org/rocksdb/ColumnFamilyOptions");
    assert(jclazz != nullptr);
    return jclazz;
  }

  // Get the field id of the member variable of org.rocksdb.DBOptions
  // that stores the pointer to rocksdb::ColumnFamilyOptions
  static jfieldID getHandleFieldID(JNIEnv* env) {
    static jfieldID fid = env->GetFieldID(
        getJClass(env), "nativeHandle_", "J");
    assert(fid != nullptr);
    return fid;
  }

  // Get the pointer to rocksdb::ColumnFamilyOptions
  static rocksdb::ColumnFamilyOptions* getHandle(JNIEnv* env, jobject jobj) {
    return reinterpret_cast<rocksdb::ColumnFamilyOptions*>(
        env->GetLongField(jobj, getHandleFieldID(env)));
  }

  // Pass the rocksdb::ColumnFamilyOptions pointer to the java side.
  static void setHandle(JNIEnv* env, jobject jobj,
      rocksdb::ColumnFamilyOptions* op) {
    env->SetLongField(
        jobj, getHandleFieldID(env),
        reinterpret_cast<jlong>(op));
  }
};

224 225 226 227
class WriteOptionsJni {
 public:
  // Get the java class id of org.rocksdb.WriteOptions.
  static jclass getJClass(JNIEnv* env) {
228
    jclass jclazz = env->FindClass("org/rocksdb/WriteOptions");
229 230 231 232 233 234 235
    assert(jclazz != nullptr);
    return jclazz;
  }

  // Get the field id of the member variable of org.rocksdb.WriteOptions
  // that stores the pointer to rocksdb::WriteOptions
  static jfieldID getHandleFieldID(JNIEnv* env) {
236
    static jfieldID fid = env->GetFieldID(
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
        getJClass(env), "nativeHandle_", "J");
    assert(fid != nullptr);
    return fid;
  }

  // Get the pointer to rocksdb::WriteOptions
  static rocksdb::WriteOptions* getHandle(JNIEnv* env, jobject jobj) {
    return reinterpret_cast<rocksdb::WriteOptions*>(
        env->GetLongField(jobj, getHandleFieldID(env)));
  }

  // Pass the rocksdb::WriteOptions pointer to the java side.
  static void setHandle(JNIEnv* env, jobject jobj, rocksdb::WriteOptions* op) {
    env->SetLongField(
        jobj, getHandleFieldID(env),
        reinterpret_cast<jlong>(op));
  }
};

256 257 258 259 260

class ReadOptionsJni {
 public:
  // Get the java class id of org.rocksdb.ReadOptions.
  static jclass getJClass(JNIEnv* env) {
261
    jclass jclazz = env->FindClass("org/rocksdb/ReadOptions");
262 263 264 265 266 267 268
    assert(jclazz != nullptr);
    return jclazz;
  }

  // Get the field id of the member variable of org.rocksdb.ReadOptions
  // that stores the pointer to rocksdb::ReadOptions
  static jfieldID getHandleFieldID(JNIEnv* env) {
269
    static jfieldID fid = env->GetFieldID(
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
        getJClass(env), "nativeHandle_", "J");
    assert(fid != nullptr);
    return fid;
  }

  // Get the pointer to rocksdb::ReadOptions
  static rocksdb::ReadOptions* getHandle(JNIEnv* env, jobject jobj) {
    return reinterpret_cast<rocksdb::ReadOptions*>(
        env->GetLongField(jobj, getHandleFieldID(env)));
  }

  // Pass the rocksdb::ReadOptions pointer to the java side.
  static void setHandle(JNIEnv* env, jobject jobj,
                        rocksdb::ReadOptions* op) {
    env->SetLongField(
        jobj, getHandleFieldID(env),
        reinterpret_cast<jlong>(op));
  }
};


291 292 293
class WriteBatchJni {
 public:
  static jclass getJClass(JNIEnv* env) {
294
    jclass jclazz = env->FindClass("org/rocksdb/WriteBatch");
295 296 297 298 299
    assert(jclazz != nullptr);
    return jclazz;
  }

  static jfieldID getHandleFieldID(JNIEnv* env) {
300
    static jfieldID fid = env->GetFieldID(
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
        getJClass(env), "nativeHandle_", "J");
    assert(fid != nullptr);
    return fid;
  }

  // Get the pointer to rocksdb::WriteBatch of the specified
  // org.rocksdb.WriteBatch.
  static rocksdb::WriteBatch* getHandle(JNIEnv* env, jobject jwb) {
    return reinterpret_cast<rocksdb::WriteBatch*>(
        env->GetLongField(jwb, getHandleFieldID(env)));
  }

  // Pass the rocksdb::WriteBatch pointer to the java side.
  static void setHandle(JNIEnv* env, jobject jwb, rocksdb::WriteBatch* wb) {
    env->SetLongField(
        jwb, getHandleFieldID(env),
        reinterpret_cast<jlong>(wb));
  }
};
A
Ankit Gupta 已提交
320

A
Adam Retter 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
class WriteBatchHandlerJni {
 public:
  static jclass getJClass(JNIEnv* env) {
    jclass jclazz = env->FindClass("org/rocksdb/WriteBatch$Handler");
    assert(jclazz != nullptr);
    return jclazz;
  }

  static jfieldID getHandleFieldID(JNIEnv* env) {
    static jfieldID fid = env->GetFieldID(
        getJClass(env), "nativeHandle_", "J");
    assert(fid != nullptr);
    return fid;
  }

  // Get the java method `put` of org.rocksdb.WriteBatch.Handler.
  static jmethodID getPutMethodId(JNIEnv* env) {
    static jmethodID mid = env->GetMethodID(
        getJClass(env), "put", "([B[B)V");
    assert(mid != nullptr);
    return mid;
  }

  // Get the java method `merge` of org.rocksdb.WriteBatch.Handler.
  static jmethodID getMergeMethodId(JNIEnv* env) {
    static jmethodID mid = env->GetMethodID(
        getJClass(env), "merge", "([B[B)V");
    assert(mid != nullptr);
    return mid;
  }

  // Get the java method `delete` of org.rocksdb.WriteBatch.Handler.
  static jmethodID getDeleteMethodId(JNIEnv* env) {
    static jmethodID mid = env->GetMethodID(
        getJClass(env), "delete", "([B)V");
    assert(mid != nullptr);
    return mid;
  }

  // Get the java method `logData` of org.rocksdb.WriteBatch.Handler.
  static jmethodID getLogDataMethodId(JNIEnv* env) {
    static jmethodID mid = env->GetMethodID(
        getJClass(env), "logData", "([B)V");
    assert(mid != nullptr);
    return mid;
  }

  // Get the java method `shouldContinue` of org.rocksdb.WriteBatch.Handler.
  static jmethodID getContinueMethodId(JNIEnv* env) {
    static jmethodID mid = env->GetMethodID(
        getJClass(env), "shouldContinue", "()Z");
    assert(mid != nullptr);
    return mid;
  }

  // Get the pointer to rocksdb::WriteBatchHandlerJniCallback of the specified
  // org.rocksdb.WriteBatchHandler.
  static rocksdb::WriteBatchHandlerJniCallback* getHandle(
      JNIEnv* env, jobject jobj) {
    return reinterpret_cast<rocksdb::WriteBatchHandlerJniCallback*>(
        env->GetLongField(jobj, getHandleFieldID(env)));
  }

  // Pass the rocksdb::WriteBatchHandlerJniCallback pointer to the java side.
  static void setHandle(
      JNIEnv* env, jobject jobj,
      const rocksdb::WriteBatchHandlerJniCallback* op) {
    env->SetLongField(
        jobj, getHandleFieldID(env),
        reinterpret_cast<jlong>(op));
  }
};

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
class WriteBatchWithIndexJni {
 public:
  static jclass getJClass(JNIEnv* env) {
    jclass jclazz = env->FindClass("org/rocksdb/WriteBatchWithIndex");
    assert(jclazz != nullptr);
    return jclazz;
  }

  static jfieldID getHandleFieldID(JNIEnv* env) {
    static jfieldID fid = env->GetFieldID(
        getJClass(env), "nativeHandle_", "J");
    assert(fid != nullptr);
    return fid;
  }

  // Get the pointer to rocksdb::WriteBatchWithIndex of the specified
  // org.rocksdb.WriteBatchWithIndex.
  static rocksdb::WriteBatchWithIndex* getHandle(JNIEnv* env, jobject jwbwi) {
    return reinterpret_cast<rocksdb::WriteBatchWithIndex*>(
        env->GetLongField(jwbwi, getHandleFieldID(env)));
  }

  // Pass the rocksdb::WriteBatchWithIndex pointer to the java side.
  static void setHandle(JNIEnv* env, jobject jwbwi,
      rocksdb::WriteBatchWithIndex* wbwi) {
    env->SetLongField(
        jwbwi, getHandleFieldID(env),
        reinterpret_cast<jlong>(wbwi));
  }
};

A
Ankit Gupta 已提交
425 426 427
class HistogramDataJni {
 public:
  static jmethodID getConstructorMethodId(JNIEnv* env, jclass jclazz) {
428
    static jmethodID mid = env->GetMethodID(jclazz, "<init>", "(DDDDD)V");
A
Ankit Gupta 已提交
429 430
    assert(mid != nullptr);
    return mid;
A
Ankit Gupta 已提交
431 432
  }
};
A
Ankit Gupta 已提交
433

434 435 436 437
class BackupableDBOptionsJni {
 public:
  // Get the java class id of org.rocksdb.BackupableDBOptions.
  static jclass getJClass(JNIEnv* env) {
438
    jclass jclazz = env->FindClass("org/rocksdb/BackupableDBOptions");
439 440 441 442 443 444 445
    assert(jclazz != nullptr);
    return jclazz;
  }

  // Get the field id of the member variable of org.rocksdb.BackupableDBOptions
  // that stores the pointer to rocksdb::BackupableDBOptions
  static jfieldID getHandleFieldID(JNIEnv* env) {
446
    static jfieldID fid = env->GetFieldID(
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
        getJClass(env), "nativeHandle_", "J");
    assert(fid != nullptr);
    return fid;
  }

  // Get the pointer to rocksdb::BackupableDBOptions
  static rocksdb::BackupableDBOptions* getHandle(JNIEnv* env, jobject jobj) {
    return reinterpret_cast<rocksdb::BackupableDBOptions*>(
        env->GetLongField(jobj, getHandleFieldID(env)));
  }

  // Pass the rocksdb::BackupableDBOptions pointer to the java side.
  static void setHandle(
      JNIEnv* env, jobject jobj, rocksdb::BackupableDBOptions* op) {
    env->SetLongField(
        jobj, getHandleFieldID(env),
        reinterpret_cast<jlong>(op));
A
Ankit Gupta 已提交
464 465
  }
};
A
Ankit Gupta 已提交
466 467 468 469 470

class IteratorJni {
 public:
  // Get the java class id of org.rocksdb.Iteartor.
  static jclass getJClass(JNIEnv* env) {
471
    jclass jclazz = env->FindClass("org/rocksdb/RocksIterator");
A
Ankit Gupta 已提交
472 473 474 475 476
    assert(jclazz != nullptr);
    return jclazz;
  }

  // Get the field id of the member variable of org.rocksdb.Iterator
A
Add doc  
Ankit Gupta 已提交
477
  // that stores the pointer to rocksdb::Iterator.
A
Ankit Gupta 已提交
478
  static jfieldID getHandleFieldID(JNIEnv* env) {
479
    static jfieldID fid = env->GetFieldID(
A
Ankit Gupta 已提交
480 481 482 483 484
        getJClass(env), "nativeHandle_", "J");
    assert(fid != nullptr);
    return fid;
  }

A
Add doc  
Ankit Gupta 已提交
485
  // Get the pointer to rocksdb::Iterator.
A
Ankit Gupta 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498
  static rocksdb::Iterator* getHandle(JNIEnv* env, jobject jobj) {
    return reinterpret_cast<rocksdb::Iterator*>(
        env->GetLongField(jobj, getHandleFieldID(env)));
  }

  // Pass the rocksdb::Iterator pointer to the java side.
  static void setHandle(
      JNIEnv* env, jobject jobj, rocksdb::Iterator* op) {
    env->SetLongField(
        jobj, getHandleFieldID(env),
        reinterpret_cast<jlong>(op));
  }
};
A
Ankit Gupta 已提交
499 500 501

class FilterJni {
 public:
A
Fix doc  
Ankit Gupta 已提交
502
  // Get the java class id of org.rocksdb.FilterPolicy.
A
Ankit Gupta 已提交
503
  static jclass getJClass(JNIEnv* env) {
504
    jclass jclazz = env->FindClass("org/rocksdb/Filter");
A
Ankit Gupta 已提交
505 506 507 508 509
    assert(jclazz != nullptr);
    return jclazz;
  }

  // Get the field id of the member variable of org.rocksdb.Filter
A
Fix doc  
Ankit Gupta 已提交
510
  // that stores the pointer to rocksdb::FilterPolicy.
A
Ankit Gupta 已提交
511
  static jfieldID getHandleFieldID(JNIEnv* env) {
512
    static jfieldID fid = env->GetFieldID(
A
Ankit Gupta 已提交
513 514 515 516 517
        getJClass(env), "nativeHandle_", "J");
    assert(fid != nullptr);
    return fid;
  }

A
Fix doc  
Ankit Gupta 已提交
518
  // Get the pointer to rocksdb::FilterPolicy.
519 520 521 522
  static std::shared_ptr<rocksdb::FilterPolicy>* getHandle(
      JNIEnv* env, jobject jobj) {
    return reinterpret_cast
        <std::shared_ptr<rocksdb::FilterPolicy> *>(
A
Ankit Gupta 已提交
523 524 525
        env->GetLongField(jobj, getHandleFieldID(env)));
  }

A
Fix doc  
Ankit Gupta 已提交
526
  // Pass the rocksdb::FilterPolicy pointer to the java side.
A
Ankit Gupta 已提交
527
  static void setHandle(
528
      JNIEnv* env, jobject jobj, std::shared_ptr<rocksdb::FilterPolicy>* op) {
A
Ankit Gupta 已提交
529 530 531
    env->SetLongField(
        jobj, getHandleFieldID(env),
        reinterpret_cast<jlong>(op));
F
fyrz 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
  }
};

class ColumnFamilyHandleJni {
 public:
  // Get the java class id of org.rocksdb.ColumnFamilyHandle.
  static jclass getJClass(JNIEnv* env) {
    jclass jclazz = env->FindClass("org/rocksdb/ColumnFamilyHandle");
    assert(jclazz != nullptr);
    return jclazz;
  }

  // Get the field id of the member variable of org.rocksdb.ColumnFamilyHandle.
  // that stores the pointer to rocksdb::ColumnFamilyHandle.
  static jfieldID getHandleFieldID(JNIEnv* env) {
    static jfieldID fid = env->GetFieldID(
        getJClass(env), "nativeHandle_", "J");
    assert(fid != nullptr);
    return fid;
  }

  // Get the pointer to rocksdb::ColumnFamilyHandle.
  static rocksdb::ColumnFamilyHandle* getHandle(JNIEnv* env, jobject jobj) {
    return reinterpret_cast<rocksdb::ColumnFamilyHandle*>(
        env->GetLongField(jobj, getHandleFieldID(env)));
  }

  // Pass the rocksdb::ColumnFamilyHandle pointer to the java side.
  static void setHandle(
      JNIEnv* env, jobject jobj, const rocksdb::ColumnFamilyHandle* op) {
    env->SetLongField(
        jobj, getHandleFieldID(env),
        reinterpret_cast<jlong>(op));
A
Ankit Gupta 已提交
565 566
  }
};
A
Ankit Gupta 已提交
567

F
fyrz 已提交
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
class FlushOptionsJni {
 public:
    // Get the java class id of org.rocksdb.FlushOptions.
    static jclass getJClass(JNIEnv* env) {
      jclass jclazz = env->FindClass("org/rocksdb/FlushOptions");
      assert(jclazz != nullptr);
      return jclazz;
    }

    // Get the field id of the member variable of org.rocksdb.FlushOptions
    // that stores the pointer to rocksdb::FlushOptions.
    static jfieldID getHandleFieldID(JNIEnv* env) {
      static jfieldID fid = env->GetFieldID(
          getJClass(env), "nativeHandle_", "J");
      assert(fid != nullptr);
      return fid;
    }

    // Pass the FlushOptions pointer to the java side.
    static void setHandle(
      JNIEnv* env, jobject jobj,
      const rocksdb::FlushOptions* op) {
      env->SetLongField(
          jobj, getHandleFieldID(env),
          reinterpret_cast<jlong>(op));
    }
};

596
class ComparatorOptionsJni {
597
 public:
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
    // Get the java class id of org.rocksdb.ComparatorOptions.
    static jclass getJClass(JNIEnv* env) {
      jclass jclazz = env->FindClass("org/rocksdb/ComparatorOptions");
      assert(jclazz != nullptr);
      return jclazz;
    }

    // Get the field id of the member variable of org.rocksdb.ComparatorOptions
    // that stores the pointer to rocksdb::ComparatorJniCallbackOptions.
    static jfieldID getHandleFieldID(JNIEnv* env) {
      static jfieldID fid = env->GetFieldID(
          getJClass(env), "nativeHandle_", "J");
      assert(fid != nullptr);
      return fid;
    }

    // Pass the ComparatorJniCallbackOptions pointer to the java side.
    static void setHandle(
616 617
      JNIEnv* env, jobject jobj,
      const rocksdb::ComparatorJniCallbackOptions* op) {
618 619 620 621 622 623
      env->SetLongField(
          jobj, getHandleFieldID(env),
          reinterpret_cast<jlong>(op));
    }
};

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
class AbstractComparatorJni {
 public:
  // Get the java class id of org.rocksdb.Comparator.
  static jclass getJClass(JNIEnv* env) {
    jclass jclazz = env->FindClass("org/rocksdb/AbstractComparator");
    assert(jclazz != nullptr);
    return jclazz;
  }

  // Get the field id of the member variable of org.rocksdb.Comparator
  // that stores the pointer to rocksdb::Comparator.
  static jfieldID getHandleFieldID(JNIEnv* env) {
    static jfieldID fid = env->GetFieldID(
        getJClass(env), "nativeHandle_", "J");
    assert(fid != nullptr);
    return fid;
  }

  // Get the java method `name` of org.rocksdb.Comparator.
  static jmethodID getNameMethodId(JNIEnv* env) {
    static jmethodID mid = env->GetMethodID(
        getJClass(env), "name", "()Ljava/lang/String;");
    assert(mid != nullptr);
    return mid;
  }

  // Get the java method `compare` of org.rocksdb.Comparator.
  static jmethodID getCompareMethodId(JNIEnv* env) {
652 653 654
    static jmethodID mid = env->GetMethodID(getJClass(env),
      "compare",
      "(Lorg/rocksdb/AbstractSlice;Lorg/rocksdb/AbstractSlice;)I");
655 656 657 658 659 660
    assert(mid != nullptr);
    return mid;
  }

  // Get the java method `findShortestSeparator` of org.rocksdb.Comparator.
  static jmethodID getFindShortestSeparatorMethodId(JNIEnv* env) {
661 662 663
    static jmethodID mid = env->GetMethodID(getJClass(env),
      "findShortestSeparator",
      "(Ljava/lang/String;Lorg/rocksdb/AbstractSlice;)Ljava/lang/String;");
664 665 666 667 668 669
    assert(mid != nullptr);
    return mid;
  }

  // Get the java method `findShortSuccessor` of org.rocksdb.Comparator.
  static jmethodID getFindShortSuccessorMethodId(JNIEnv* env) {
670 671 672
    static jmethodID mid = env->GetMethodID(getJClass(env),
      "findShortSuccessor",
      "(Ljava/lang/String;)Ljava/lang/String;");
673 674 675 676 677
    assert(mid != nullptr);
    return mid;
  }

  // Get the pointer to ComparatorJniCallback.
678 679
  static rocksdb::BaseComparatorJniCallback* getHandle(
    JNIEnv* env, jobject jobj) {
680 681 682 683 684 685
    return reinterpret_cast<rocksdb::BaseComparatorJniCallback*>(
        env->GetLongField(jobj, getHandleFieldID(env)));
  }

  // Pass the ComparatorJniCallback pointer to the java side.
  static void setHandle(
686
    JNIEnv* env, jobject jobj, const rocksdb::BaseComparatorJniCallback* op) {
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
    env->SetLongField(
        jobj, getHandleFieldID(env),
        reinterpret_cast<jlong>(op));
  }
};

class AbstractSliceJni {
 public:
  // Get the java class id of org.rocksdb.Slice.
  static jclass getJClass(JNIEnv* env) {
    jclass jclazz = env->FindClass("org/rocksdb/AbstractSlice");
    assert(jclazz != nullptr);
    return jclazz;
  }

  // Get the field id of the member variable of org.rocksdb.Slice
  // that stores the pointer to rocksdb::Slice.
  static jfieldID getHandleFieldID(JNIEnv* env) {
    static jfieldID fid = env->GetFieldID(
        getJClass(env), "nativeHandle_", "J");
    assert(fid != nullptr);
    return fid;
  }

  // Get the pointer to Slice.
  static rocksdb::Slice* getHandle(JNIEnv* env, jobject jobj) {
    return reinterpret_cast<rocksdb::Slice*>(
        env->GetLongField(jobj, getHandleFieldID(env)));
  }

  // Pass the Slice pointer to the java side.
  static void setHandle(
      JNIEnv* env, jobject jobj, const rocksdb::Slice* op) {
    env->SetLongField(
        jobj, getHandleFieldID(env),
        reinterpret_cast<jlong>(op));
  }
};

class SliceJni {
 public:
  // Get the java class id of org.rocksdb.Slice.
  static jclass getJClass(JNIEnv* env) {
    jclass jclazz = env->FindClass("org/rocksdb/Slice");
    assert(jclazz != nullptr);
    return jclazz;
  }

  static jobject construct0(JNIEnv* env) {
    static jmethodID mid = env->GetMethodID(getJClass(env), "<init>", "()V");
    assert(mid != nullptr);
    return env->NewObject(getJClass(env), mid);
  }
};

class DirectSliceJni {
 public:
  // Get the java class id of org.rocksdb.DirectSlice.
  static jclass getJClass(JNIEnv* env) {
    jclass jclazz = env->FindClass("org/rocksdb/DirectSlice");
    assert(jclazz != nullptr);
    return jclazz;
  }

  static jobject construct0(JNIEnv* env) {
    static jmethodID mid = env->GetMethodID(getJClass(env), "<init>", "()V");
    assert(mid != nullptr);
    return env->NewObject(getJClass(env), mid);
  }
};

A
Ankit Gupta 已提交
758 759 760 761
class ListJni {
 public:
  // Get the java class id of java.util.List.
  static jclass getListClass(JNIEnv* env) {
762
    jclass jclazz = env->FindClass("java/util/List");
A
Ankit Gupta 已提交
763 764 765
    assert(jclazz != nullptr);
    return jclazz;
  }
A
Ankit Gupta 已提交
766

A
Ankit Gupta 已提交
767 768
  // Get the java class id of java.util.ArrayList.
  static jclass getArrayListClass(JNIEnv* env) {
769
    jclass jclazz = env->FindClass("java/util/ArrayList");
A
Ankit Gupta 已提交
770 771 772 773 774 775
    assert(jclazz != nullptr);
    return jclazz;
  }

  // Get the java class id of java.util.Iterator.
  static jclass getIteratorClass(JNIEnv* env) {
776
    jclass jclazz = env->FindClass("java/util/Iterator");
A
Ankit Gupta 已提交
777 778 779
    assert(jclazz != nullptr);
    return jclazz;
  }
A
Ankit Gupta 已提交
780

A
Ankit Gupta 已提交
781 782
  // Get the java method id of java.util.List.iterator().
  static jmethodID getIteratorMethod(JNIEnv* env) {
783
    static jmethodID mid = env->GetMethodID(
A
Ankit Gupta 已提交
784 785 786 787
        getListClass(env), "iterator", "()Ljava/util/Iterator;");
    assert(mid != nullptr);
    return mid;
  }
A
Ankit Gupta 已提交
788

A
Ankit Gupta 已提交
789 790
  // Get the java method id of java.util.Iterator.hasNext().
  static jmethodID getHasNextMethod(JNIEnv* env) {
791
    static jmethodID mid = env->GetMethodID(
A
Ankit Gupta 已提交
792 793 794 795
        getIteratorClass(env), "hasNext", "()Z");
    assert(mid != nullptr);
    return mid;
  }
A
Ankit Gupta 已提交
796

A
Ankit Gupta 已提交
797 798
  // Get the java method id of java.util.Iterator.next().
  static jmethodID getNextMethod(JNIEnv* env) {
799
    static jmethodID mid = env->GetMethodID(
A
Ankit Gupta 已提交
800 801 802 803
        getIteratorClass(env), "next", "()Ljava/lang/Object;");
    assert(mid != nullptr);
    return mid;
  }
A
Ankit Gupta 已提交
804

A
Ankit Gupta 已提交
805 806
  // Get the java method id of arrayList constructor.
  static jmethodID getArrayListConstructorMethodId(JNIEnv* env, jclass jclazz) {
807
    static jmethodID mid = env->GetMethodID(
A
Ankit Gupta 已提交
808 809 810 811
        jclazz, "<init>", "(I)V");
    assert(mid != nullptr);
    return mid;
  }
A
Ankit Gupta 已提交
812

A
Ankit Gupta 已提交
813 814
  // Get the java method id of java.util.List.add().
  static jmethodID getListAddMethodId(JNIEnv* env) {
815
    static jmethodID mid = env->GetMethodID(
A
Ankit Gupta 已提交
816 817 818 819 820
        getListClass(env), "add", "(Ljava/lang/Object;)Z");
    assert(mid != nullptr);
    return mid;
  }
};
821

822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
class BackupInfoJni {
 public:
  // Get the java class id of org.rocksdb.BackupInfo.
  static jclass getJClass(JNIEnv* env) {
    jclass jclazz = env->FindClass("org/rocksdb/BackupInfo");
    assert(jclazz != nullptr);
    return jclazz;
  }

  static jobject construct0(JNIEnv* env, uint32_t backup_id, int64_t timestamp,
      uint64_t size, uint32_t number_files) {
    static jmethodID mid = env->GetMethodID(getJClass(env), "<init>",
        "(IJJI)V");
    assert(mid != nullptr);
    return env->NewObject(getJClass(env), mid,
        backup_id, timestamp, size, number_files);
  }
};

class BackupInfoListJni {
 public:
  static jobject getBackupInfo(JNIEnv* env,
      std::vector<BackupInfo> backup_infos) {
    jclass jclazz = env->FindClass("java/util/ArrayList");
    jmethodID mid = rocksdb::ListJni::getArrayListConstructorMethodId(
        env, jclazz);
    jobject jbackup_info_handle_list = env->NewObject(jclazz, mid,
        backup_infos.size());
    // insert in java list
    for (std::vector<rocksdb::BackupInfo>::size_type i = 0;
        i != backup_infos.size(); i++) {
      rocksdb::BackupInfo backup_info = backup_infos[i];
      jobject obj = rocksdb::BackupInfoJni::construct0(env,
          backup_info.backup_id,
          backup_info.timestamp,
          backup_info.size,
          backup_info.number_files);
      env->CallBooleanMethod(jbackup_info_handle_list,
          rocksdb::ListJni::getListAddMethodId(env), obj);
    }
    return jbackup_info_handle_list;
  }
};

866
class JniUtil {
867
 public:
868 869 870 871 872 873 874 875 876 877 878 879
    /**
     * Copies a jstring to a std::string
     * and releases the original jstring
     */
    static std::string copyString(JNIEnv* env, jstring js) {
      const char *utf = env->GetStringUTFChars(js, NULL);
      std::string name(utf);
      env->ReleaseStringUTFChars(js, utf);
      return name;
    }
};

880 881
}  // namespace rocksdb
#endif  // JAVA_ROCKSJNI_PORTAL_H_