portal.h 9.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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>
#include "rocksdb/db.h"
A
Ankit Gupta 已提交
15
#include "rocksdb/filter_policy.h"
16
#include "utilities/backupable_db.h"
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

namespace rocksdb {

// The portal class for org.rocksdb.RocksDB
class RocksDBJni {
 public:
  // Get the java class id of org.rocksdb.RocksDB.
  static jclass getJClass(JNIEnv* env) {
    static jclass jclazz = env->FindClass("org/rocksdb/RocksDB");
    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) {
    static jfieldID fid = env->GetFieldID(
34
        getJClass(env), "nativeHandle_", "J");
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    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) {
    static jclass jclazz = env->FindClass("org/rocksdb/RocksDBException");
    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
    static jmethodID mid = env->GetMethodID(
        getJClass(env), "<init>", "(Ljava/lang/String;)V");
    assert(mid != nullptr);

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

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
class OptionsJni {
 public:
  // Get the java class id of org.rocksdb.Options.
  static jclass getJClass(JNIEnv* env) {
    static jclass jclazz = env->FindClass("org/rocksdb/Options");
    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) {
    static jfieldID fid = env->GetFieldID(
        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));
  }
};

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
class WriteOptionsJni {
 public:
  // Get the java class id of org.rocksdb.WriteOptions.
  static jclass getJClass(JNIEnv* env) {
    static jclass jclazz = env->FindClass("org/rocksdb/WriteOptions");
    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) {
    static jfieldID fid = env->GetFieldID(
        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));
  }
};

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

class ReadOptionsJni {
 public:
  // Get the java class id of org.rocksdb.ReadOptions.
  static jclass getJClass(JNIEnv* env) {
    static jclass jclazz = env->FindClass("org/rocksdb/ReadOptions");
    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) {
    static jfieldID fid = env->GetFieldID(
        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));
  }
};


181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
class WriteBatchJni {
 public:
  static jclass getJClass(JNIEnv* env) {
    static jclass jclazz = env->FindClass("org/rocksdb/WriteBatch");
    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::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 已提交
210 211 212 213 214 215 216 217

class HistogramDataJni {
 public:
  static jmethodID getConstructorMethodId(JNIEnv* env, jclass jclazz) {
    static jmethodID mid = env->GetMethodID(
        jclazz, "<init>", "(DDDDD)V");
    assert(mid != nullptr);
    return mid;
A
Ankit Gupta 已提交
218 219
  }
};
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
class BackupableDBOptionsJni {
 public:
  // Get the java class id of org.rocksdb.BackupableDBOptions.
  static jclass getJClass(JNIEnv* env) {
    static jclass jclazz = env->FindClass("org/rocksdb/BackupableDBOptions");
    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) {
    static jfieldID fid = env->GetFieldID(
        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 已提交
250 251
  }
};
A
Ankit Gupta 已提交
252 253 254 255 256 257 258 259 260 261 262

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

  // Get the field id of the member variable of org.rocksdb.Iterator
A
Add doc  
Ankit Gupta 已提交
263
  // that stores the pointer to rocksdb::Iterator.
A
Ankit Gupta 已提交
264 265 266 267 268 269 270
  static jfieldID getHandleFieldID(JNIEnv* env) {
    static jfieldID fid = env->GetFieldID(
        getJClass(env), "nativeHandle_", "J");
    assert(fid != nullptr);
    return fid;
  }

A
Add doc  
Ankit Gupta 已提交
271
  // Get the pointer to rocksdb::Iterator.
A
Ankit Gupta 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284
  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 已提交
285 286 287

class FilterJni {
 public:
A
Fix doc  
Ankit Gupta 已提交
288
  // Get the java class id of org.rocksdb.FilterPolicy.
A
Ankit Gupta 已提交
289 290 291 292 293 294 295
  static jclass getJClass(JNIEnv* env) {
    static jclass jclazz = env->FindClass("org/rocksdb/Filter");
    assert(jclazz != nullptr);
    return jclazz;
  }

  // Get the field id of the member variable of org.rocksdb.Filter
A
Fix doc  
Ankit Gupta 已提交
296
  // that stores the pointer to rocksdb::FilterPolicy.
A
Ankit Gupta 已提交
297 298 299 300 301 302 303
  static jfieldID getHandleFieldID(JNIEnv* env) {
    static jfieldID fid = env->GetFieldID(
        getJClass(env), "nativeHandle_", "J");
    assert(fid != nullptr);
    return fid;
  }

A
Fix doc  
Ankit Gupta 已提交
304
  // Get the pointer to rocksdb::FilterPolicy.
A
Ankit Gupta 已提交
305 306 307 308 309
  static rocksdb::FilterPolicy* getHandle(JNIEnv* env, jobject jobj) {
    return reinterpret_cast<rocksdb::FilterPolicy*>(
        env->GetLongField(jobj, getHandleFieldID(env)));
  }

A
Fix doc  
Ankit Gupta 已提交
310
  // Pass the rocksdb::FilterPolicy pointer to the java side.
A
Ankit Gupta 已提交
311 312 313 314 315 316 317
  static void setHandle(
      JNIEnv* env, jobject jobj, const rocksdb::FilterPolicy* op) {
    env->SetLongField(
        jobj, getHandleFieldID(env),
        reinterpret_cast<jlong>(op));
  }
};
318 319
}  // namespace rocksdb
#endif  // JAVA_ROCKSJNI_PORTAL_H_