Skip to content

Commit

Permalink
Merge pull request #310 from halalala222/cli-support-zet
Browse files Browse the repository at this point in the history
Cli supports zset data structures (#249)
  • Loading branch information
qishenonly authored Jul 30, 2024
2 parents 4a36750 + 1afd4cc commit 145f2e0
Show file tree
Hide file tree
Showing 18 changed files with 1,240 additions and 97 deletions.
1 change: 1 addition & 0 deletions cmd/client/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ func init() {
\/__/ \/__/ \/__/ \/__/
`}, "\r\n"))
})
App.OnClosing(close)
register(App)
}
3 changes: 2 additions & 1 deletion cmd/client/cli/flydb-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"fmt"
"os"

"github.com/ByteStorage/FlyDB/cmd/client"
"github.com/desertbit/grumble"
"os"
)

func main() {
Expand Down
39 changes: 39 additions & 0 deletions cmd/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package client

import (
"fmt"

"github.com/ByteStorage/FlyDB/db/grpc/client"
)

var (
Addr string
cliClient *client.Client
)

func newClient() *client.Client {
var err error
if cliClient != nil {
return cliClient
}

if cliClient, err = client.NewClient(Addr); err != nil {
fmt.Println("new client error: ", err)
}

return cliClient
}

func close() error {
if cliClient == nil {
return nil
}

if err := cliClient.Close(); err != nil {
return err
}

cliClient = nil

return nil
}
37 changes: 37 additions & 0 deletions cmd/client/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package client

import "math"

const (
CommonKeyArg = "key"
CommonMemberArg = "member"
CommonMembersArg = "members"
CommonValueArg = "value"

CommonDefaultEmptyString = ""

ZSetScoreArg = "score"
ZSetStartArg = "start"
ZSetEndArg = "end"
ZSetIncrByArg = "incrBy"
ZSetMinArg = "min"
ZSetMaxArg = "max"

ZSetDefaultScore = 0
ZSetDefaultRangeStart = 0
ZSetDefaultRangeEnd = math.MaxInt
ZSetDefaultIncrBy = 0
ZSetDefaultMin = 0
ZSetDefaultMax = math.MaxInt

ZSetDefaultKeyHelp = "the key of the zset"
ZSetDefaultMemberHelp = "the member of the zset"
ZSetDefaultMembersHelp = "the members of the zset, separated by space, e.g. member1 member2 member3"
ZSetDefaultValueHelp = "the value of the zset"
ZSetDefaultScoreHelp = "the score of the zset"
ZSetDefaultStartHelp = "the start index of the zset"
ZSetDefaultEndHelp = "the end index of the zset"
ZSetDefaultIncrByHelp = "the increment value of the zset"
ZSetDefaultMinHelp = "the min score of the zset"
ZSetDefaultMaxHelp = "the max score of the zset"
)
1 change: 1 addition & 0 deletions cmd/client/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"fmt"

"github.com/desertbit/grumble"
"github.com/pkg/errors"
)
Expand Down
1 change: 1 addition & 0 deletions cmd/client/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"fmt"

"github.com/desertbit/grumble"
)

Expand Down
118 changes: 117 additions & 1 deletion cmd/client/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package client

import "github.com/desertbit/grumble"
import (
"github.com/desertbit/grumble"
)

func register(app *grumble.App) {
app.AddCommand(&grumble.Command{
Expand Down Expand Up @@ -545,4 +547,118 @@ func register(app *grumble.App) {
},
})

app.AddCommand(&grumble.Command{
Name: "ZAdd",
Help: "Add a member to a sorted set, or update its score if it already exists",
Run: ZSetAdd,
Args: func(a *grumble.Args) {
a.String(CommonKeyArg, ZSetDefaultKeyHelp, grumble.Default(CommonDefaultEmptyString))
a.String(CommonMemberArg, ZSetDefaultMemberHelp, grumble.Default(CommonDefaultEmptyString))
a.Int(ZSetScoreArg, ZSetDefaultScoreHelp, grumble.Default(ZSetDefaultScore))
a.String(CommonValueArg, ZSetDefaultValueHelp, grumble.Default(CommonDefaultEmptyString))
},
})

app.AddCommand(&grumble.Command{
Name: "ZRem",
Help: "Remove a member from a sorted set",
Run: ZSetRem,
Args: func(a *grumble.Args) {
a.String(CommonKeyArg, ZSetDefaultKeyHelp, grumble.Default(CommonDefaultEmptyString))
a.String(CommonMemberArg, ZSetDefaultMemberHelp, grumble.Default(CommonDefaultEmptyString))
},
})

app.AddCommand(&grumble.Command{
Name: "ZRems",
Help: "Remove multiple members from a sorted set",
Run: ZSetRems,
Args: func(a *grumble.Args) {
a.String(CommonKeyArg, ZSetDefaultKeyHelp, grumble.Default(CommonDefaultEmptyString))
a.StringList(CommonMembersArg, ZSetDefaultMembersHelp, grumble.Default(CommonDefaultEmptyString))
},
})

app.AddCommand(&grumble.Command{
Name: "ZScore",
Help: "Get the score of a member in a sorted set",
Run: ZSetScore,
Args: func(a *grumble.Args) {
a.String(CommonKeyArg, ZSetDefaultKeyHelp, grumble.Default(CommonDefaultEmptyString))
a.String(CommonMemberArg, ZSetDefaultMemberHelp, grumble.Default(CommonDefaultEmptyString))
},
})

app.AddCommand(&grumble.Command{
Name: "ZRank",
Help: "Get the rank of a member in a sorted set",
Run: ZSetRank,
Args: func(a *grumble.Args) {
a.String(CommonKeyArg, ZSetDefaultKeyHelp, grumble.Default(CommonDefaultEmptyString))
a.String(CommonMemberArg, ZSetDefaultMemberHelp, grumble.Default(CommonDefaultEmptyString))
},
})

app.AddCommand(&grumble.Command{
Name: "ZRevRank",
Help: "Get the reverse rank of a member in a sorted set",
Run: ZSetRevRank,
Args: func(a *grumble.Args) {
a.String(CommonKeyArg, ZSetDefaultKeyHelp, grumble.Default(CommonDefaultEmptyString))
a.String(CommonMemberArg, ZSetDefaultMemberHelp, grumble.Default(CommonDefaultEmptyString))
},
})

app.AddCommand(&grumble.Command{
Name: "ZRange",
Help: "Get a range of members in a sorted set",
Run: ZSetRange,
Args: func(a *grumble.Args) {
a.String(CommonKeyArg, ZSetDefaultKeyHelp, grumble.Default(CommonDefaultEmptyString))
a.Int(ZSetStartArg, ZSetDefaultStartHelp, grumble.Default(ZSetDefaultRangeStart))
a.Int(ZSetEndArg, ZSetDefaultEndHelp, grumble.Default(ZSetDefaultRangeEnd))
},
})

app.AddCommand(&grumble.Command{
Name: "ZCount",
Help: "Count the members in a sorted set with scores within the given range",
Run: ZSetCount,
Args: func(a *grumble.Args) {
a.String(CommonKeyArg, ZSetDefaultKeyHelp, grumble.Default(CommonDefaultEmptyString))
a.Int(ZSetMinArg, ZSetDefaultMinHelp, grumble.Default(ZSetDefaultMin))
a.Int(ZSetMaxArg, ZSetDefaultMaxHelp, grumble.Default(ZSetDefaultMax))
},
})

app.AddCommand(&grumble.Command{
Name: "ZRevRange",
Help: "Get a range of members in a sorted set, in reverse order",
Run: ZSetRevRange,
Args: func(a *grumble.Args) {
a.String(CommonKeyArg, ZSetDefaultKeyHelp, grumble.Default(CommonDefaultEmptyString))
a.Int(ZSetStartArg, ZSetDefaultStartHelp, grumble.Default(ZSetDefaultRangeStart))
a.Int(ZSetEndArg, ZSetDefaultEndHelp, grumble.Default(ZSetDefaultRangeEnd))
},
})

app.AddCommand(&grumble.Command{
Name: "ZCard",
Help: "Get the number of members in a sorted set",
Run: ZSetCard,
Args: func(a *grumble.Args) {
a.String(CommonKeyArg, ZSetDefaultKeyHelp, grumble.Default(CommonDefaultEmptyString))
},
})

app.AddCommand(&grumble.Command{
Name: "ZIncrBy",
Help: "Increment the score of a member in a sorted set",
Run: ZSetIncrBy,
Args: func(a *grumble.Args) {
a.String(CommonKeyArg, ZSetDefaultKeyHelp, grumble.Default(CommonDefaultEmptyString))
a.String(CommonMemberArg, ZSetDefaultMemberHelp, grumble.Default(CommonDefaultEmptyString))
a.Int(ZSetIncrByArg, ZSetDefaultIncrByHelp, grumble.Default(ZSetDefaultIncrBy))
},
})
}
1 change: 1 addition & 0 deletions cmd/client/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"fmt"

"github.com/desertbit/grumble"
)

Expand Down
10 changes: 1 addition & 9 deletions cmd/client/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@ package client
import (
"errors"
"fmt"
"github.com/ByteStorage/FlyDB/db/grpc/client"

"github.com/desertbit/grumble"
)

var Addr string

func newClient() *client.Client {
return &client.Client{
Addr: Addr,
}
}

func stringPutData(c *grumble.Context) error {
key := c.Args.String("key")
value := c.Args.String("value")
Expand Down
Loading

0 comments on commit 145f2e0

Please sign in to comment.