Skip to content

Commit

Permalink
Improves API for BFInfo with args
Browse files Browse the repository at this point in the history
  • Loading branch information
elena-kolevska committed Jul 22, 2023
1 parent aa44fbc commit 43a58cd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 42 deletions.
70 changes: 34 additions & 36 deletions probabilistic.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ type probabilisticCmdable interface {
BFCard(ctx context.Context, key string) *IntCmd
BFExists(ctx context.Context, key string, element interface{}) *BoolCmd
BFInfo(ctx context.Context, key string) *BFInfoCmd
BFInfoArg(ctx context.Context, key string, option BFInfoArgs) *BFInfoCmd
BFInfoCapacity(ctx context.Context, key string) *BFInfoCmd
BFInfoSize(ctx context.Context, key string) *BFInfoCmd
BFInfoFilters(ctx context.Context, key string) *BFInfoCmd
BFInfoItems(ctx context.Context, key string) *BFInfoCmd
BFInfoExpansion(ctx context.Context, key string) *BFInfoCmd
BFInsert(ctx context.Context, key string, options *BFInsertOptions, elements ...interface{}) *BoolSliceCmd
BFMAdd(ctx context.Context, key string, elements ...interface{}) *BoolSliceCmd
BFMExists(ctx context.Context, key string, elements ...interface{}) *BoolSliceCmd
Expand Down Expand Up @@ -94,32 +98,6 @@ type CFInsertOptions struct {
NoCreate bool
}

type BFInfoArgs int

const (
BFCAPACITY BFInfoArgs = iota
BFSIZE
BFFILTERS
BFITEMS
BFEXPANSION
)

func (b BFInfoArgs) String() string {
switch b {
case BFCAPACITY:
return "capacity"
case BFSIZE:
return "size"
case BFFILTERS:
return "filters"
case BFITEMS:
return "elements"
case BFEXPANSION:
return "expansion"
}
return ""
}

// -------------------------------------------
// Bloom filter commands
//-------------------------------------------
Expand Down Expand Up @@ -195,11 +173,11 @@ func (c cmdable) BFInfo(ctx context.Context, key string) *BFInfoCmd {
}

type BFInfo struct {
Capacity int64
Size int64
NumFilters int64
NumItemsInserted int64
ExpansionRate int64
Capacity int64
Size int64
Filters int64
ItemsInserted int64
ExpansionRate int64
}

type BFInfoCmd struct {
Expand Down Expand Up @@ -253,9 +231,9 @@ func (cmd *BFInfoCmd) readReply(rd *proto.Reader) (err error) {
case "Size":
result.Size, err = rd.ReadInt()
case "Number of filters":
result.NumFilters, err = rd.ReadInt()
result.Filters, err = rd.ReadInt()
case "Number of items inserted":
result.NumItemsInserted, err = rd.ReadInt()
result.ItemsInserted, err = rd.ReadInt()
case "Expansion rate":
result.ExpansionRate, err = rd.ReadInt()
default:
Expand All @@ -271,8 +249,28 @@ func (cmd *BFInfoCmd) readReply(rd *proto.Reader) (err error) {
return nil
}

func (c cmdable) BFInfoArg(ctx context.Context, key string, option BFInfoArgs) *BFInfoCmd {
args := []interface{}{"bf.info", key, option.String()}
func (c cmdable) BFInfoCapacity(ctx context.Context, key string) *BFInfoCmd {
return c.bFInfoArg(ctx, key, "capacity")
}

func (c cmdable) BFInfoSize(ctx context.Context, key string) *BFInfoCmd {
return c.bFInfoArg(ctx, key, "size")
}

func (c cmdable) BFInfoFilters(ctx context.Context, key string) *BFInfoCmd {
return c.bFInfoArg(ctx, key, "filters")
}

func (c cmdable) BFInfoItems(ctx context.Context, key string) *BFInfoCmd {
return c.bFInfoArg(ctx, key, "items")
}

func (c cmdable) BFInfoExpansion(ctx context.Context, key string) *BFInfoCmd {
return c.bFInfoArg(ctx, key, "expansion")
}

func (c cmdable) bFInfoArg(ctx context.Context, key, option string) *BFInfoCmd {
args := []interface{}{"bf.info", key, option}
cmd := NewBFInfoCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
Expand Down
31 changes: 25 additions & 6 deletions probabilistic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"math"
)

var _ = FDescribe("Probabilistic commands", Label("probabilistic"), func() {
var _ = Describe("Probabilistic commands", Label("probabilistic"), func() {
ctx := context.TODO()
var client *redis.Client

Expand All @@ -32,7 +32,7 @@ var _ = FDescribe("Probabilistic commands", Label("probabilistic"), func() {

Expect(err).NotTo(HaveOccurred())
Expect(resultInfo).To(BeAssignableToTypeOf(redis.BFInfo{}))
Expect(resultInfo.NumItemsInserted).To(BeEquivalentTo(int64(1)))
Expect(resultInfo.ItemsInserted).To(BeEquivalentTo(int64(1)))
})

It("should BFCard", Label("bloom", "bfcard"), func() {
Expand Down Expand Up @@ -77,13 +77,32 @@ var _ = FDescribe("Probabilistic commands", Label("probabilistic"), func() {
Expect(result.Capacity).To(BeEquivalentTo(int64(2000)))
})

It("should BFInfoArg", Label("bloom", "bfinfoarg"), func() {
It("should BFInfoCapacity, BFInfoSize, BFInfoFilters, BFInfoItems, BFInfoExpansion, ", Label("bloom", "bfinfocapacity", "bfinfosize", "bfinfofilters", "bfinfoitems", "bfinfoexpansion"), func() {
err := client.BFReserve(ctx, "testbf1", 0.001, 2000).Err()
Expect(err).NotTo(HaveOccurred())

result, err := client.BFInfoArg(ctx, "testbf1", redis.BFCAPACITY).Result()
result, err := client.BFInfoCapacity(ctx, "testbf1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(result.Capacity).To(BeEquivalentTo(int64(2000)))

result, err = client.BFInfoItems(ctx, "testbf1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(result.ItemsInserted).To(BeEquivalentTo(int64(0)))

result, err = client.BFInfoSize(ctx, "testbf1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(result.Size).To(BeEquivalentTo(int64(4056)))

err = client.BFReserveExpansion(ctx, "testbf2", 0.001, 2000, 3).Err()
Expect(err).NotTo(HaveOccurred())

result, err = client.BFInfoFilters(ctx, "testbf2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(result.Filters).To(BeEquivalentTo(int64(1)))

result, err = client.BFInfoExpansion(ctx, "testbf2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(result.ExpansionRate).To(BeEquivalentTo(int64(3)))
})

It("should BFInsert", Label("bloom", "bfinsert"), func() {
Expand Down Expand Up @@ -132,7 +151,7 @@ var _ = FDescribe("Probabilistic commands", Label("probabilistic"), func() {

Expect(err).NotTo(HaveOccurred())
Expect(resultInfo).To(BeAssignableToTypeOf(redis.BFInfo{}))
Expect(resultInfo.NumItemsInserted).To(BeEquivalentTo(int64(3)))
Expect(resultInfo.ItemsInserted).To(BeEquivalentTo(int64(3)))
})

It("should BFMExists", Label("bloom", "bfmexists"), func() {
Expand Down Expand Up @@ -611,7 +630,7 @@ var _ = FDescribe("Probabilistic commands", Label("probabilistic"), func() {
Expect(info.Compression).To(BeEquivalentTo(int64(2000)))
})

FIt("should TDigestMerge", Label("tdigest", "tmerge"), func() {
It("should TDigestMerge", Label("tdigest", "tmerge"), func() {
err := client.TDigestCreate(ctx, "tdigest1").Err()
Expect(err).NotTo(HaveOccurred())
err = client.TDigestAdd(ctx, "tdigest1", 10, 20, 30, 40, 50, 60, 70, 80, 90, 100).Err()
Expand Down

0 comments on commit 43a58cd

Please sign in to comment.