Skip to content

Commit

Permalink
Complete the hash data service
Browse files Browse the repository at this point in the history
  • Loading branch information
qishenonly committed Jan 26, 2024
1 parent ce0f026 commit d50b12d
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/redis/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
173 changes: 173 additions & 0 deletions lib/redis/hash.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis

import (
"encoding/binary"
"github.com/ByteStorage/FlyDB/structure"
"github.com/tidwall/redcon"
)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit d50b12d

Please sign in to comment.