Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Bitmap type DUMP/RESTORE command support #2535

Merged
merged 5 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/storage/rdb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
#include "rdb_listpack.h"
#include "rdb_ziplist.h"
#include "rdb_zipmap.h"
#include "storage/redis_metadata.h"
#include "time_util.h"
#include "types/redis_bitmap.h"
#include "types/redis_bitmap_string.h"
#include "types/redis_hash.h"
#include "types/redis_list.h"
#include "types/redis_set.h"
Expand Down Expand Up @@ -402,7 +405,7 @@ StatusOr<int> RDB::loadRdbType() {
}

StatusOr<RedisObjValue> RDB::loadRdbObject(int type, [[maybe_unused]] const std::string &key) {
if (type == RDBTypeString) {
if (type == RDBTypeString || type == RDBTypeBitmap) {
git-hulk marked this conversation as resolved.
Show resolved Hide resolved
auto value = GET_OR_RET(LoadStringObject());
return value;
} else if (type == RDBTypeSet || type == RDBTypeSetIntSet || type == RDBTypeSetListPack) {
Expand Down Expand Up @@ -457,7 +460,7 @@ StatusOr<RedisObjValue> RDB::loadRdbObject(int type, [[maybe_unused]] const std:
Status RDB::saveRdbObject(engine::Context &ctx, int type, const std::string &key, const RedisObjValue &obj,
uint64_t ttl_ms) {
rocksdb::Status db_status;
if (type == RDBTypeString) {
if (type == RDBTypeString || type == RDBTypeBitmap) {
const auto &value = std::get<std::string>(obj);
redis::String string_db(storage_, ns_);
uint64_t expire_ms = 0;
Expand Down Expand Up @@ -728,6 +731,8 @@ Status RDB::SaveObjectType(const RedisType type) {
robj_type = RDBTypeSet;
} else if (type == kRedisZSet) {
robj_type = RDBTypeZSet2;
} else if (type == kRedisBitmap) {
robj_type = RDBTypeBitmap;
git-hulk marked this conversation as resolved.
Show resolved Hide resolved
} else {
LOG(WARNING) << "Invalid or Not supported object type: " << type;
return {Status::NotOK, "Invalid or Not supported object type"};
Expand Down Expand Up @@ -781,6 +786,16 @@ Status RDB::SaveObject(const std::string &key, const RedisType type) {
}

return SaveHashObject(field_values);
} else if (type == kRedisBitmap) {
std::string value;
redis::Bitmap bitmap_db(storage_, ns_);
Config *config = storage_->GetConfig();
uint32_t max_btos_size = static_cast<uint32_t>(config->max_bitmap_to_string_mb) * MiB;
auto s = bitmap_db.GetString(ctx, key, max_btos_size, &value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@git-hulk would this meet the same issue like lsb to msb?

if (!s.ok() && !s.IsNotFound()) {
return {Status::RedisExecErr, s.ToString()};
}
return SaveStringObject(value);
} else {
LOG(WARNING) << "Invalid or Not supported object type: " << type;
return {Status::NotOK, "Invalid or Not supported object type"};
Expand Down
3 changes: 2 additions & 1 deletion src/storage/rdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ constexpr const int RDBTypeSet = 2;
constexpr const int RDBTypeZSet = 3;
constexpr const int RDBTypeHash = 4;
constexpr const int RDBTypeZSet2 = 5;
constexpr const int RDBTypeBitmap = 6;
// NOTE: when adding new Redis object type, update LoadObjectType.

// Redis object encoding
Expand Down Expand Up @@ -150,7 +151,7 @@ class RDB {

/*0-5 is the basic type of Redis objects and 9-21 is the encoding type of Redis objects.
Redis allow basic is 0-7 and 6/7 is for the module type which we don't support here.*/
static bool isObjectType(int type) { return (type >= 0 && type <= 5) || (type >= 9 && type <= 21); };
static bool isObjectType(int type) { return (type >= 0 && type <= 6) || (type >= 9 && type <= 21); };
static bool isEmptyRedisObject(const RedisObjValue &value);
static int rdbEncodeInteger(long long value, unsigned char *enc);
Status rdbSaveBinaryDoubleValue(double val);
Expand Down
18 changes: 18 additions & 0 deletions tests/gocase/unit/dump/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,21 @@ func TestDump_Set(t *testing.T) {
require.NoError(t, rdb.RestoreReplace(ctx, restoredKey, 0, serialized).Err())
require.ElementsMatch(t, members, rdb.SMembers(ctx, restoredKey).Val())
}

func TestDump_Bitset(t *testing.T) {
srv := util.StartServer(t, map[string]string{})
defer srv.Close()

ctx := context.Background()
rdb := srv.NewClient()
defer func() { require.NoError(t, rdb.Close()) }()

key := "bitsetKey1"
require.NoError(t, rdb.SetBit(ctx, key, 1, 1).Err())
serialized, err := rdb.Dump(ctx, key).Result()
require.NoError(t, err)

restoredKey := fmt.Sprintf("restore_%s", key)
require.NoError(t, rdb.RestoreReplace(ctx, restoredKey, 0, serialized).Err())
require.Equal(t, rdb.Get(ctx, key).Val(), rdb.Get(ctx, restoredKey).Val())
}
Loading