checkpoint.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // This file implements the "bridge" between Java and C++ and enables
  7. // calling c++ ROCKSDB_NAMESPACE::Checkpoint methods from Java side.
  8. #include "rocksdb/utilities/checkpoint.h"
  9. #include <jni.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string>
  13. #include "include/org_rocksdb_Checkpoint.h"
  14. #include "rocksdb/db.h"
  15. #include "rocksjni/cplusplus_to_java_convert.h"
  16. #include "rocksjni/portal.h"
  17. /*
  18. * Class: org_rocksdb_Checkpoint
  19. * Method: newCheckpoint
  20. * Signature: (J)J
  21. */
  22. jlong Java_org_rocksdb_Checkpoint_newCheckpoint(JNIEnv* /*env*/,
  23. jclass /*jclazz*/,
  24. jlong jdb_handle) {
  25. auto* db = reinterpret_cast<ROCKSDB_NAMESPACE::DB*>(jdb_handle);
  26. ROCKSDB_NAMESPACE::Checkpoint* checkpoint;
  27. ROCKSDB_NAMESPACE::Checkpoint::Create(db, &checkpoint);
  28. return GET_CPLUSPLUS_POINTER(checkpoint);
  29. }
  30. /*
  31. * Class: org_rocksdb_Checkpoint
  32. * Method: dispose
  33. * Signature: (J)V
  34. */
  35. void Java_org_rocksdb_Checkpoint_disposeInternalJni(JNIEnv* /*env*/,
  36. jclass /*jobj*/,
  37. jlong jhandle) {
  38. auto* checkpoint = reinterpret_cast<ROCKSDB_NAMESPACE::Checkpoint*>(jhandle);
  39. assert(checkpoint != nullptr);
  40. delete checkpoint;
  41. }
  42. /*
  43. * Class: org_rocksdb_Checkpoint
  44. * Method: createCheckpoint
  45. * Signature: (JLjava/lang/String;)V
  46. */
  47. void Java_org_rocksdb_Checkpoint_createCheckpoint(JNIEnv* env, jclass /*jcls*/,
  48. jlong jcheckpoint_handle,
  49. jstring jcheckpoint_path) {
  50. const char* checkpoint_path = env->GetStringUTFChars(jcheckpoint_path, 0);
  51. if (checkpoint_path == nullptr) {
  52. // exception thrown: OutOfMemoryError
  53. return;
  54. }
  55. auto* checkpoint =
  56. reinterpret_cast<ROCKSDB_NAMESPACE::Checkpoint*>(jcheckpoint_handle);
  57. ROCKSDB_NAMESPACE::Status s = checkpoint->CreateCheckpoint(checkpoint_path);
  58. env->ReleaseStringUTFChars(jcheckpoint_path, checkpoint_path);
  59. if (!s.ok()) {
  60. ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s);
  61. }
  62. }
  63. /*
  64. * Class: org_rocksdb_Checkpoint
  65. * Method: exportColumnFamily
  66. * Signature: (JJLjava/lang/String;)Lorg/rocksdb/ExportImportFilesMetaData;
  67. */
  68. jlong Java_org_rocksdb_Checkpoint_exportColumnFamily(
  69. JNIEnv* env, jobject /*jobj*/, jlong jcheckpoint_handle,
  70. jlong jcolumn_family_handle, jstring jexport_path) {
  71. const char* export_path = env->GetStringUTFChars(jexport_path, 0);
  72. if (export_path == nullptr) {
  73. // exception thrown: OutOfMemoryError
  74. return 0;
  75. }
  76. auto* checkpoint =
  77. reinterpret_cast<ROCKSDB_NAMESPACE::Checkpoint*>(jcheckpoint_handle);
  78. auto* column_family_handle =
  79. reinterpret_cast<ROCKSDB_NAMESPACE::ColumnFamilyHandle*>(
  80. jcolumn_family_handle);
  81. ROCKSDB_NAMESPACE::ExportImportFilesMetaData* metadata = nullptr;
  82. ROCKSDB_NAMESPACE::Status s = checkpoint->ExportColumnFamily(
  83. column_family_handle, export_path, &metadata);
  84. env->ReleaseStringUTFChars(jexport_path, export_path);
  85. if (!s.ok()) {
  86. ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s);
  87. }
  88. return GET_CPLUSPLUS_POINTER(metadata);
  89. }