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++ for
7 // ROCKSDB_NAMESPACE::Options.
8 
9 #include "rocksdb/table.h"
10 #include <jni.h>
11 #include "include/org_rocksdb_BlockBasedTableConfig.h"
12 #include "include/org_rocksdb_PlainTableConfig.h"
13 #include "portal.h"
14 #include "rocksdb/cache.h"
15 #include "rocksdb/filter_policy.h"
16 
17 /*
18  * Class:     org_rocksdb_PlainTableConfig
19  * Method:    newTableFactoryHandle
20  * Signature: (IIDIIBZZ)J
21  */
Java_org_rocksdb_PlainTableConfig_newTableFactoryHandle(JNIEnv *,jobject,jint jkey_size,jint jbloom_bits_per_key,jdouble jhash_table_ratio,jint jindex_sparseness,jint jhuge_page_tlb_size,jbyte jencoding_type,jboolean jfull_scan_mode,jboolean jstore_index_in_file)22 jlong Java_org_rocksdb_PlainTableConfig_newTableFactoryHandle(
23     JNIEnv * /*env*/, jobject /*jobj*/, jint jkey_size,
24     jint jbloom_bits_per_key, jdouble jhash_table_ratio, jint jindex_sparseness,
25     jint jhuge_page_tlb_size, jbyte jencoding_type, jboolean jfull_scan_mode,
26     jboolean jstore_index_in_file) {
27   ROCKSDB_NAMESPACE::PlainTableOptions options =
28       ROCKSDB_NAMESPACE::PlainTableOptions();
29   options.user_key_len = jkey_size;
30   options.bloom_bits_per_key = jbloom_bits_per_key;
31   options.hash_table_ratio = jhash_table_ratio;
32   options.index_sparseness = jindex_sparseness;
33   options.huge_page_tlb_size = jhuge_page_tlb_size;
34   options.encoding_type =
35       static_cast<ROCKSDB_NAMESPACE::EncodingType>(jencoding_type);
36   options.full_scan_mode = jfull_scan_mode;
37   options.store_index_in_file = jstore_index_in_file;
38   return reinterpret_cast<jlong>(
39       ROCKSDB_NAMESPACE::NewPlainTableFactory(options));
40 }
41 
42 /*
43  * Class:     org_rocksdb_BlockBasedTableConfig
44  * Method:    newTableFactoryHandle
45  * Signature: (ZZZZBBDBZJJJJIIIJZZZJZZIIZZBJIJI)J
46  */
Java_org_rocksdb_BlockBasedTableConfig_newTableFactoryHandle(JNIEnv *,jobject,jboolean jcache_index_and_filter_blocks,jboolean jcache_index_and_filter_blocks_with_high_priority,jboolean jpin_l0_filter_and_index_blocks_in_cache,jboolean jpin_top_level_index_and_filter,jbyte jindex_type_value,jbyte jdata_block_index_type_value,jdouble jdata_block_hash_table_util_ratio,jbyte jchecksum_type_value,jboolean jno_block_cache,jlong jblock_cache_handle,jlong jpersistent_cache_handle,jlong jblock_cache_compressed_handle,jlong jblock_size,jint jblock_size_deviation,jint jblock_restart_interval,jint jindex_block_restart_interval,jlong jmetadata_block_size,jboolean jpartition_filters,jboolean joptimize_filters_for_memory,jboolean juse_delta_encoding,jlong jfilter_policy_handle,jboolean jwhole_key_filtering,jboolean jverify_compression,jint jread_amp_bytes_per_bit,jint jformat_version,jboolean jenable_index_compression,jboolean jblock_align,jbyte jindex_shortening,jlong jblock_cache_size,jint jblock_cache_num_shard_bits,jlong jblock_cache_compressed_size,jint jblock_cache_compressed_num_shard_bits)47 jlong Java_org_rocksdb_BlockBasedTableConfig_newTableFactoryHandle(
48     JNIEnv *, jobject, jboolean jcache_index_and_filter_blocks,
49     jboolean jcache_index_and_filter_blocks_with_high_priority,
50     jboolean jpin_l0_filter_and_index_blocks_in_cache,
51     jboolean jpin_top_level_index_and_filter, jbyte jindex_type_value,
52     jbyte jdata_block_index_type_value,
53     jdouble jdata_block_hash_table_util_ratio, jbyte jchecksum_type_value,
54     jboolean jno_block_cache, jlong jblock_cache_handle,
55     jlong jpersistent_cache_handle, jlong jblock_cache_compressed_handle,
56     jlong jblock_size, jint jblock_size_deviation, jint jblock_restart_interval,
57     jint jindex_block_restart_interval, jlong jmetadata_block_size,
58     jboolean jpartition_filters, jboolean joptimize_filters_for_memory,
59     jboolean juse_delta_encoding, jlong jfilter_policy_handle,
60     jboolean jwhole_key_filtering, jboolean jverify_compression,
61     jint jread_amp_bytes_per_bit, jint jformat_version,
62     jboolean jenable_index_compression, jboolean jblock_align,
63     jbyte jindex_shortening, jlong jblock_cache_size,
64     jint jblock_cache_num_shard_bits, jlong jblock_cache_compressed_size,
65     jint jblock_cache_compressed_num_shard_bits) {
66   ROCKSDB_NAMESPACE::BlockBasedTableOptions options;
67   options.cache_index_and_filter_blocks =
68       static_cast<bool>(jcache_index_and_filter_blocks);
69   options.cache_index_and_filter_blocks_with_high_priority =
70       static_cast<bool>(jcache_index_and_filter_blocks_with_high_priority);
71   options.pin_l0_filter_and_index_blocks_in_cache =
72     static_cast<bool>(jpin_l0_filter_and_index_blocks_in_cache);
73   options.pin_top_level_index_and_filter =
74     static_cast<bool>(jpin_top_level_index_and_filter);
75   options.index_type =
76       ROCKSDB_NAMESPACE::IndexTypeJni::toCppIndexType(jindex_type_value);
77   options.data_block_index_type =
78       ROCKSDB_NAMESPACE::DataBlockIndexTypeJni::toCppDataBlockIndexType(
79           jdata_block_index_type_value);
80   options.data_block_hash_table_util_ratio =
81       static_cast<double>(jdata_block_hash_table_util_ratio);
82   options.checksum = ROCKSDB_NAMESPACE::ChecksumTypeJni::toCppChecksumType(
83       jchecksum_type_value);
84   options.no_block_cache = static_cast<bool>(jno_block_cache);
85   if (options.no_block_cache) {
86     options.block_cache = nullptr;
87   } else {
88     if (jblock_cache_handle > 0) {
89       std::shared_ptr<ROCKSDB_NAMESPACE::Cache> *pCache =
90           reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Cache> *>(
91               jblock_cache_handle);
92       options.block_cache = *pCache;
93     } else if (jblock_cache_size >= 0) {
94       if (jblock_cache_num_shard_bits > 0) {
95         options.block_cache = ROCKSDB_NAMESPACE::NewLRUCache(
96             static_cast<size_t>(jblock_cache_size),
97             static_cast<int>(jblock_cache_num_shard_bits));
98       } else {
99         options.block_cache = ROCKSDB_NAMESPACE::NewLRUCache(
100             static_cast<size_t>(jblock_cache_size));
101       }
102     } else {
103       options.no_block_cache = true;
104       options.block_cache = nullptr;
105     }
106   }
107   if (jpersistent_cache_handle > 0) {
108     std::shared_ptr<ROCKSDB_NAMESPACE::PersistentCache> *pCache =
109         reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::PersistentCache> *>(
110             jpersistent_cache_handle);
111     options.persistent_cache = *pCache;
112   }
113   if (jblock_cache_compressed_handle > 0) {
114     std::shared_ptr<ROCKSDB_NAMESPACE::Cache> *pCache =
115         reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Cache> *>(
116             jblock_cache_compressed_handle);
117     options.block_cache_compressed = *pCache;
118   } else if (jblock_cache_compressed_size > 0) {
119     if (jblock_cache_compressed_num_shard_bits > 0) {
120       options.block_cache_compressed = ROCKSDB_NAMESPACE::NewLRUCache(
121           static_cast<size_t>(jblock_cache_compressed_size),
122           static_cast<int>(jblock_cache_compressed_num_shard_bits));
123     } else {
124       options.block_cache_compressed = ROCKSDB_NAMESPACE::NewLRUCache(
125           static_cast<size_t>(jblock_cache_compressed_size));
126     }
127   }
128   options.block_size = static_cast<size_t>(jblock_size);
129   options.block_size_deviation = static_cast<int>(jblock_size_deviation);
130   options.block_restart_interval = static_cast<int>(jblock_restart_interval);
131   options.index_block_restart_interval = static_cast<int>(jindex_block_restart_interval);
132   options.metadata_block_size = static_cast<uint64_t>(jmetadata_block_size);
133   options.partition_filters = static_cast<bool>(jpartition_filters);
134   options.optimize_filters_for_memory =
135       static_cast<bool>(joptimize_filters_for_memory);
136   options.use_delta_encoding = static_cast<bool>(juse_delta_encoding);
137   if (jfilter_policy_handle > 0) {
138     std::shared_ptr<ROCKSDB_NAMESPACE::FilterPolicy> *pFilterPolicy =
139         reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::FilterPolicy> *>(
140             jfilter_policy_handle);
141     options.filter_policy = *pFilterPolicy;
142   }
143   options.whole_key_filtering = static_cast<bool>(jwhole_key_filtering);
144   options.verify_compression = static_cast<bool>(jverify_compression);
145   options.read_amp_bytes_per_bit = static_cast<uint32_t>(jread_amp_bytes_per_bit);
146   options.format_version = static_cast<uint32_t>(jformat_version);
147   options.enable_index_compression = static_cast<bool>(jenable_index_compression);
148   options.block_align = static_cast<bool>(jblock_align);
149   options.index_shortening =
150       ROCKSDB_NAMESPACE::IndexShorteningModeJni::toCppIndexShorteningMode(
151           jindex_shortening);
152 
153   return reinterpret_cast<jlong>(
154       ROCKSDB_NAMESPACE::NewBlockBasedTableFactory(options));
155 }
156