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

Cli supports zset data structures (#249) #310

Merged
merged 9 commits into from
Jul 30, 2024
1 change: 1 addition & 0 deletions cmd/client/app.go
Original file line number Diff line number Diff line change
@@ -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
@@ -2,9 +2,10 @@ package main

import (
"fmt"
"os"

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

func main() {
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
Copy link
Member

Choose a reason for hiding this comment

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

We do not recommend creating consts files for a zset, but please add appropriate comments to the file if necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To avoid hardcoding strings like 'key' in the codebase,not only for zset other data structures like set,hash, can use for.

Copy link
Member

Choose a reason for hiding this comment

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

I know this, so please add comments to it, for example for zset data structures

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm sorry, I misunderstood you before. I'll go and modify my code later.

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
@@ -2,6 +2,7 @@ package client

import (
"fmt"

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

import (
"fmt"

"github.com/desertbit/grumble"
)

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{
@@ -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
@@ -2,6 +2,7 @@ package client

import (
"fmt"

"github.com/desertbit/grumble"
)

10 changes: 1 addition & 9 deletions cmd/client/string.go
Original file line number Diff line number Diff line change
@@ -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")
Loading
Loading