From d50b12d501aca332cbc36157c8a8d24a09a1cd55 Mon Sep 17 00:00:00 2001 From: qishenonly <1050026498@qq.com> Date: Fri, 26 Jan 2024 20:02:58 +0800 Subject: [PATCH] Complete the hash data service --- lib/redis/client.go | 13 ++++ lib/redis/hash.go | 173 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 186 insertions(+) diff --git a/lib/redis/client.go b/lib/redis/client.go index c773790..bf5c9a2 100644 --- a/lib/redis/client.go +++ b/lib/redis/client.go @@ -44,6 +44,19 @@ var FlyDBSupportCommands = map[string]CmdHandler{ "use-hash": UseHash, "hset": HSet, "hget": HGet, + "hdel": HDel, + "hdelall": HDelAll, + "hexists": HExists, + "hexpire": HExpire, + "hlen": HLen, + "hupdate": HUpdate, + "hkeys": HKeys, + "hstrlen": HStrlen, + "hmove": HMove, + "hsize": HSize, + "httl": HTTL, + "hincrby": HIncrBy, + "hdecrby": HDecrBy, } // ClientCommands is the handler for all redis commands diff --git a/lib/redis/hash.go b/lib/redis/hash.go index 51d234f..8d41e2e 100644 --- a/lib/redis/hash.go +++ b/lib/redis/hash.go @@ -1,6 +1,7 @@ package redis import ( + "encoding/binary" "github.com/ByteStorage/FlyDB/structure" "github.com/tidwall/redcon" ) @@ -36,6 +37,178 @@ func HGet(cli *FlyDBClient, args [][]byte) (interface{}, error) { return value, nil } +// HDel key field +func HDel(cli *FlyDBClient, args [][]byte) (interface{}, error) { + if len(args) != 2 { + return nil, NewWrongNumberOfArgsError("hdel") + } + + if _, err := cli.DB[1].(*structure.HashStructure).HDel(string(args[0]), args[1]); err != nil { + return nil, err + } + return redcon.SimpleString("OK"), nil +} + +// HDelAll key +func HDelAll(cli *FlyDBClient, args [][]byte) (interface{}, error) { + if len(args) != 1 { + return nil, NewWrongNumberOfArgsError("hdelall") + } + + if _, err := cli.DB[1].(*structure.HashStructure).HDelAll(string(args[0])); err != nil { + return nil, err + } + return redcon.SimpleString("OK"), nil +} + +// HExists key field +func HExists(cli *FlyDBClient, args [][]byte) (interface{}, error) { + if len(args) != 2 { + return nil, NewWrongNumberOfArgsError("hexists") + } + + var ok = 0 + res, err := cli.DB[1].(*structure.HashStructure).HExists(string(args[0]), args[1]) + if err != nil { + return nil, err + } + if res { + ok = 1 + } + return redcon.SimpleInt(ok), nil +} + +// HExpire key seconds +func HExpire(cli *FlyDBClient, args [][]byte) (interface{}, error) { + if len(args) != 2 { + return nil, NewWrongNumberOfArgsError("hexpire") + } + + if _, err := cli.DB[1].(*structure.HashStructure).HExpire(string(args[0]), + int64(binary.BigEndian.Uint64(args[1]))); err != nil { + return nil, err + } + return redcon.SimpleString("OK"), nil +} + +// HLen key +func HLen(cli *FlyDBClient, args [][]byte) (interface{}, error) { + if len(args) != 1 { + return nil, NewWrongNumberOfArgsError("hlen") + } + + length, err := cli.DB[1].(*structure.HashStructure).HLen(string(args[0])) + if err != nil { + return nil, err + } + return redcon.SimpleInt(length), nil +} + +// HUpdate key field value +func HUpdate(cli *FlyDBClient, args [][]byte) (interface{}, error) { + if len(args) != 1 { + return nil, NewWrongNumberOfArgsError("hupdate") + } + + if _, err := cli.DB[1].(*structure.HashStructure).HUpdate(string(args[0]), args[1], args[2]); err != nil { + return nil, err + } + return redcon.SimpleString("OK"), nil +} + +// HKeys key +func HKeys(cli *FlyDBClient, args [][]byte) (interface{}, error) { + if len(args) != 1 { + return nil, NewWrongNumberOfArgsError("hkeys") + } + + keys, err := cli.DB[1].(*structure.HashStructure).Keys(string(args[0])) + if err != nil { + return nil, err + } + return keys, nil +} + +// HStrlen key field +func HStrlen(cli *FlyDBClient, args [][]byte) (interface{}, error) { + if len(args) != 2 { + return nil, NewWrongNumberOfArgsError("hstrlen") + } + + length, err := cli.DB[1].(*structure.HashStructure).HStrLen(string(args[0]), args[1]) + if err != nil { + return nil, err + } + return redcon.SimpleInt(length), nil +} + +// HMove source destination field +func HMove(cli *FlyDBClient, args [][]byte) (interface{}, error) { + if len(args) != 3 { + return nil, NewWrongNumberOfArgsError("hmove") + } + + if _, err := cli.DB[1].(*structure.HashStructure).HMove(string(args[0]), string(args[1]), + int64(binary.BigEndian.Uint64(args[2]))); err != nil { + return nil, err + } + return redcon.SimpleString("OK"), nil +} + +// HSize key +func HSize(cli *FlyDBClient, args [][]byte) (interface{}, error) { + if len(args) != 1 { + return nil, NewWrongNumberOfArgsError("hsize") + } + + size, err := cli.DB[1].(*structure.HashStructure).Size(string(args[0])) + if err != nil { + return nil, err + } + return redcon.SimpleString(size), nil +} + +// HTTL key +func HTTL(cli *FlyDBClient, args [][]byte) (interface{}, error) { + if len(args) != 1 { + return nil, NewWrongNumberOfArgsError("httl") + } + + ttl, err := cli.DB[1].(*structure.HashStructure).TTL(string(args[0])) + if err != nil { + return nil, err + } + return redcon.SimpleInt(ttl), nil +} + +// HIncrBy key field increment +func HIncrBy(cli *FlyDBClient, args [][]byte) (interface{}, error) { + if len(args) != 3 { + return nil, NewWrongNumberOfArgsError("hincrby") + } + + value, err := cli.DB[1].(*structure.HashStructure).HIncrBy(string(args[0]), args[1], + int64(binary.BigEndian.Uint64(args[2]))) + if err != nil { + return nil, err + } + return redcon.SimpleInt(value), nil +} + +// HDecrBy key field decrement +func HDecrBy(cli *FlyDBClient, args [][]byte) (interface{}, error) { + if len(args) != 3 { + return nil, NewWrongNumberOfArgsError("hdecrby") + } + + value, err := cli.DB[1].(*structure.HashStructure).HDecrBy(string(args[0]), args[1], + int64(binary.BigEndian.Uint64(args[2]))) + if err != nil { + return nil, err + } + return redcon.SimpleInt(value), nil +} + // UseHash change to hash db func UseHash(cli *FlyDBClient, args [][]byte) (interface{}, error) { if len(args) != 0 {