@@ -93,8 +93,9 @@ uint32_t SegmentSubKeyIndexForBit(uint32_t bit_offset) {
9393 return (bit_offset / kBitmapSegmentBits ) * kBitmapSegmentBytes ;
9494}
9595
96- rocksdb::Status Bitmap::GetMetadata (const Slice &ns_key, BitmapMetadata *metadata, std::string *raw_value) {
97- auto s = GetRawMetadata (ns_key, raw_value);
96+ rocksdb::Status Bitmap::GetMetadata (Database::GetOptions get_options, const Slice &ns_key, BitmapMetadata *metadata,
97+ std::string *raw_value) {
98+ auto s = GetRawMetadata (get_options, ns_key, raw_value);
9899 if (!s.ok ()) return s;
99100
100101 Slice slice = *raw_value;
@@ -107,15 +108,15 @@ rocksdb::Status Bitmap::GetBit(const Slice &user_key, uint32_t bit_offset, bool
107108 std::string ns_key = AppendNamespacePrefix (user_key);
108109
109110 BitmapMetadata metadata (false );
110- rocksdb::Status s = GetMetadata (ns_key, &metadata, &raw_value);
111+ LatestSnapShot ss (storage_);
112+ rocksdb::Status s = GetMetadata (GetOptions{.snapshot = ss.GetSnapShot ()}, ns_key, &metadata, &raw_value);
111113 if (!s.ok ()) return s.IsNotFound () ? rocksdb::Status::OK () : s;
112114
113115 if (metadata.Type () == kRedisString ) {
114116 redis::BitmapString bitmap_string_db (storage_, namespace_);
115117 return bitmap_string_db.GetBit (raw_value, bit_offset, bit);
116118 }
117119
118- LatestSnapShot ss (storage_);
119120 rocksdb::ReadOptions read_options;
120121 read_options.snapshot = ss.GetSnapShot ();
121122 rocksdb::PinnableSlice value;
@@ -142,7 +143,8 @@ rocksdb::Status Bitmap::GetString(const Slice &user_key, const uint32_t max_btos
142143 std::string ns_key = AppendNamespacePrefix (user_key);
143144
144145 BitmapMetadata metadata (false );
145- rocksdb::Status s = GetMetadata (ns_key, &metadata, &raw_value);
146+ LatestSnapShot ss (storage_);
147+ rocksdb::Status s = GetMetadata (GetOptions{.snapshot = ss.GetSnapShot ()}, ns_key, &metadata, &raw_value);
146148 if (!s.ok ()) return s;
147149 if (metadata.size > max_btos_size) {
148150 return rocksdb::Status::Aborted (kErrBitmapStringOutOfRange );
@@ -152,7 +154,6 @@ rocksdb::Status Bitmap::GetString(const Slice &user_key, const uint32_t max_btos
152154 std::string prefix_key = InternalKey (ns_key, " " , metadata.version , storage_->IsSlotIdEncoded ()).Encode ();
153155
154156 rocksdb::ReadOptions read_options = storage_->DefaultScanOptions ();
155- LatestSnapShot ss (storage_);
156157 read_options.snapshot = ss.GetSnapShot ();
157158
158159 auto iter = util::UniqueIterator (storage_, read_options);
@@ -184,7 +185,7 @@ rocksdb::Status Bitmap::SetBit(const Slice &user_key, uint32_t bit_offset, bool
184185
185186 LockGuard guard (storage_->GetLockManager (), ns_key);
186187 BitmapMetadata metadata;
187- rocksdb::Status s = GetMetadata (ns_key, &metadata, &raw_value);
188+ rocksdb::Status s = GetMetadata (GetOptions{}, ns_key, &metadata, &raw_value);
188189 if (!s.ok () && !s.IsNotFound ()) return s;
189190
190191 if (metadata.Type () == kRedisString ) {
@@ -228,7 +229,8 @@ rocksdb::Status Bitmap::BitCount(const Slice &user_key, int64_t start, int64_t s
228229 std::string ns_key = AppendNamespacePrefix (user_key);
229230
230231 BitmapMetadata metadata (false );
231- rocksdb::Status s = GetMetadata (ns_key, &metadata, &raw_value);
232+ std::optional<LatestSnapShot> ss (storage_);
233+ rocksdb::Status s = GetMetadata (GetOptions{.snapshot = ss->GetSnapShot ()}, ns_key, &metadata, &raw_value);
232234 if (!s.ok ()) return s.IsNotFound () ? rocksdb::Status::OK () : s;
233235
234236 /* Convert negative indexes */
@@ -237,6 +239,9 @@ rocksdb::Status Bitmap::BitCount(const Slice &user_key, int64_t start, int64_t s
237239 }
238240
239241 if (metadata.Type () == kRedisString ) {
242+ // Release snapshot ahead for performance, this requires
243+ // `bitmap_string_db` doesn't get anything.
244+ ss = std::nullopt ;
240245 redis::BitmapString bitmap_string_db (storage_, namespace_);
241246 return bitmap_string_db.BitCount (raw_value, start, stop, is_bit_index, cnt);
242247 }
@@ -257,9 +262,8 @@ rocksdb::Status Bitmap::BitCount(const Slice &user_key, int64_t start, int64_t s
257262 auto u_start = static_cast <uint32_t >(start_byte);
258263 auto u_stop = static_cast <uint32_t >(stop_byte);
259264
260- LatestSnapShot ss (storage_);
261265 rocksdb::ReadOptions read_options;
262- read_options.snapshot = ss. GetSnapShot ();
266+ read_options.snapshot = ss-> GetSnapShot ();
263267 uint32_t start_index = u_start / kBitmapSegmentBytes ;
264268 uint32_t stop_index = u_stop / kBitmapSegmentBytes ;
265269 // Don't use multi get to prevent large range query, and take too much memory
@@ -308,14 +312,16 @@ rocksdb::Status Bitmap::BitPos(const Slice &user_key, bool bit, int64_t start, i
308312 std::string ns_key = AppendNamespacePrefix (user_key);
309313
310314 BitmapMetadata metadata (false );
311- rocksdb::Status s = GetMetadata (ns_key, &metadata, &raw_value);
315+ std::optional<LatestSnapShot> ss (storage_);
316+ rocksdb::Status s = GetMetadata (GetOptions{.snapshot = ss->GetSnapShot ()}, ns_key, &metadata, &raw_value);
312317 if (!s.ok () && !s.IsNotFound ()) return s;
313318 if (s.IsNotFound ()) {
314319 *pos = bit ? -1 : 0 ;
315320 return rocksdb::Status::OK ();
316321 }
317322
318323 if (metadata.Type () == kRedisString ) {
324+ ss = std::nullopt ;
319325 redis::BitmapString bitmap_string_db (storage_, namespace_);
320326 return bitmap_string_db.BitPos (raw_value, bit, start, stop, stop_given, pos);
321327 }
@@ -335,9 +341,8 @@ rocksdb::Status Bitmap::BitPos(const Slice &user_key, bool bit, int64_t start, i
335341 return -1 ;
336342 };
337343
338- LatestSnapShot ss (storage_);
339344 rocksdb::ReadOptions read_options;
340- read_options.snapshot = ss. GetSnapShot ();
345+ read_options.snapshot = ss-> GetSnapShot ();
341346 uint32_t start_index = u_start / kBitmapSegmentBytes ;
342347 uint32_t stop_index = u_stop / kBitmapSegmentBytes ;
343348 // Don't use multi get to prevent large range query, and take too much memory
@@ -417,7 +422,7 @@ rocksdb::Status Bitmap::BitOp(BitOpFlags op_flag, const std::string &op_name, co
417422 for (const auto &op_key : op_keys) {
418423 BitmapMetadata metadata (false );
419424 std::string ns_op_key = AppendNamespacePrefix (op_key);
420- auto s = GetMetadata (ns_op_key, &metadata, &raw_value);
425+ auto s = GetMetadata (GetOptions{}, ns_op_key, &metadata, &raw_value);
421426 if (!s.ok ()) {
422427 if (s.IsNotFound ()) {
423428 continue ;
@@ -769,7 +774,8 @@ rocksdb::Status Bitmap::bitfield(const Slice &user_key, const std::vector<Bitfie
769774
770775 BitmapMetadata metadata;
771776 std::string raw_value;
772- auto s = GetMetadata (ns_key, &metadata, &raw_value);
777+ // TODO(mwish): maintain snapshot for read-only bitfield.
778+ auto s = GetMetadata (GetOptions{}, ns_key, &metadata, &raw_value);
773779 if (!s.ok () && !s.IsNotFound ()) {
774780 return s;
775781 }
0 commit comments