Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 16 additions & 1 deletion src/storage/rdb/rdb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "types/redis_bitmap.h"
#include "types/redis_bitmap_string.h"
#include "types/redis_hash.h"
#include "types/redis_json.h"
#include "types/redis_list.h"
#include "types/redis_set.h"
#include "types/redis_sortedint.h"
Expand Down Expand Up @@ -723,7 +724,7 @@ Status RDB::Dump(const std::string &key, const RedisType type) {

Status RDB::SaveObjectType(const RedisType type) {
int robj_type = -1;
if (type == kRedisString || type == kRedisBitmap) {
if (type == kRedisString || type == kRedisBitmap || type == kRedisJson) {
robj_type = RDBTypeString;
} else if (type == kRedisHash) {
robj_type = RDBTypeHash;
Expand Down Expand Up @@ -798,6 +799,20 @@ Status RDB::SaveObject(const std::string &key, const RedisType type) {
return {Status::RedisExecErr, s.ToString()};
}
return SaveStringObject(value);
} else if (type == kRedisJson) {
redis::Json json_db(storage_, ns_);
JsonValue value;
auto s = json_db.Get(ctx, key, {}, &value);
if (!s.ok() && !s.IsNotFound()) {
return {Status::RedisExecErr, s.ToString()};
}
std::string json_str;
Config *config = storage_->GetConfig();
auto dump_status = value.Dump(&json_str, config->json_max_nesting_depth);
if (!dump_status.IsOK()) {
return {Status::RedisExecErr, dump_status.Msg()};
}
return SaveStringObject(json_str);
} else if (type == kRedisSortedint) {
redis::Sortedint sortedint_db(storage_, ns_);
std::vector<uint64_t> ids;
Expand Down
29 changes: 29 additions & 0 deletions tests/gocase/unit/dump/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,32 @@ func TestDump_SortedInt(t *testing.T) {
expectedMembers := []string{"5", "12", "23", "89", "100"}
require.ElementsMatch(t, expectedMembers, members)
}

func TestDump_JSON(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 := "test_json_key"

jsonData := `{"name":"Alice","age":30,"city":"NYC"}`
require.NoError(t, rdb.Do(ctx, "JSON.SET", key, "$", jsonData).Err())

require.EqualValues(t, "ReJSON-RL", rdb.Type(ctx, key).Val())

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.EqualValues(t, "string", rdb.Type(ctx, restoredKey).Val())

content := rdb.Get(ctx, restoredKey).Val()
require.Contains(t, content, "Alice")
require.Contains(t, content, "30")
require.Contains(t, content, "NYC")
}
Loading