Skip to content

Commit 43a58cd

Browse files
Improves API for BFInfo with args
1 parent aa44fbc commit 43a58cd

File tree

2 files changed

+59
-42
lines changed

2 files changed

+59
-42
lines changed

probabilistic.go

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ type probabilisticCmdable interface {
1111
BFCard(ctx context.Context, key string) *IntCmd
1212
BFExists(ctx context.Context, key string, element interface{}) *BoolCmd
1313
BFInfo(ctx context.Context, key string) *BFInfoCmd
14-
BFInfoArg(ctx context.Context, key string, option BFInfoArgs) *BFInfoCmd
14+
BFInfoCapacity(ctx context.Context, key string) *BFInfoCmd
15+
BFInfoSize(ctx context.Context, key string) *BFInfoCmd
16+
BFInfoFilters(ctx context.Context, key string) *BFInfoCmd
17+
BFInfoItems(ctx context.Context, key string) *BFInfoCmd
18+
BFInfoExpansion(ctx context.Context, key string) *BFInfoCmd
1519
BFInsert(ctx context.Context, key string, options *BFInsertOptions, elements ...interface{}) *BoolSliceCmd
1620
BFMAdd(ctx context.Context, key string, elements ...interface{}) *BoolSliceCmd
1721
BFMExists(ctx context.Context, key string, elements ...interface{}) *BoolSliceCmd
@@ -94,32 +98,6 @@ type CFInsertOptions struct {
9498
NoCreate bool
9599
}
96100

97-
type BFInfoArgs int
98-
99-
const (
100-
BFCAPACITY BFInfoArgs = iota
101-
BFSIZE
102-
BFFILTERS
103-
BFITEMS
104-
BFEXPANSION
105-
)
106-
107-
func (b BFInfoArgs) String() string {
108-
switch b {
109-
case BFCAPACITY:
110-
return "capacity"
111-
case BFSIZE:
112-
return "size"
113-
case BFFILTERS:
114-
return "filters"
115-
case BFITEMS:
116-
return "elements"
117-
case BFEXPANSION:
118-
return "expansion"
119-
}
120-
return ""
121-
}
122-
123101
// -------------------------------------------
124102
// Bloom filter commands
125103
//-------------------------------------------
@@ -195,11 +173,11 @@ func (c cmdable) BFInfo(ctx context.Context, key string) *BFInfoCmd {
195173
}
196174

197175
type BFInfo struct {
198-
Capacity int64
199-
Size int64
200-
NumFilters int64
201-
NumItemsInserted int64
202-
ExpansionRate int64
176+
Capacity int64
177+
Size int64
178+
Filters int64
179+
ItemsInserted int64
180+
ExpansionRate int64
203181
}
204182

205183
type BFInfoCmd struct {
@@ -253,9 +231,9 @@ func (cmd *BFInfoCmd) readReply(rd *proto.Reader) (err error) {
253231
case "Size":
254232
result.Size, err = rd.ReadInt()
255233
case "Number of filters":
256-
result.NumFilters, err = rd.ReadInt()
234+
result.Filters, err = rd.ReadInt()
257235
case "Number of items inserted":
258-
result.NumItemsInserted, err = rd.ReadInt()
236+
result.ItemsInserted, err = rd.ReadInt()
259237
case "Expansion rate":
260238
result.ExpansionRate, err = rd.ReadInt()
261239
default:
@@ -271,8 +249,28 @@ func (cmd *BFInfoCmd) readReply(rd *proto.Reader) (err error) {
271249
return nil
272250
}
273251

274-
func (c cmdable) BFInfoArg(ctx context.Context, key string, option BFInfoArgs) *BFInfoCmd {
275-
args := []interface{}{"bf.info", key, option.String()}
252+
func (c cmdable) BFInfoCapacity(ctx context.Context, key string) *BFInfoCmd {
253+
return c.bFInfoArg(ctx, key, "capacity")
254+
}
255+
256+
func (c cmdable) BFInfoSize(ctx context.Context, key string) *BFInfoCmd {
257+
return c.bFInfoArg(ctx, key, "size")
258+
}
259+
260+
func (c cmdable) BFInfoFilters(ctx context.Context, key string) *BFInfoCmd {
261+
return c.bFInfoArg(ctx, key, "filters")
262+
}
263+
264+
func (c cmdable) BFInfoItems(ctx context.Context, key string) *BFInfoCmd {
265+
return c.bFInfoArg(ctx, key, "items")
266+
}
267+
268+
func (c cmdable) BFInfoExpansion(ctx context.Context, key string) *BFInfoCmd {
269+
return c.bFInfoArg(ctx, key, "expansion")
270+
}
271+
272+
func (c cmdable) bFInfoArg(ctx context.Context, key, option string) *BFInfoCmd {
273+
args := []interface{}{"bf.info", key, option}
276274
cmd := NewBFInfoCmd(ctx, args...)
277275
_ = c(ctx, cmd)
278276
return cmd

probabilistic_test.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"math"
99
)
1010

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

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

3333
Expect(err).NotTo(HaveOccurred())
3434
Expect(resultInfo).To(BeAssignableToTypeOf(redis.BFInfo{}))
35-
Expect(resultInfo.NumItemsInserted).To(BeEquivalentTo(int64(1)))
35+
Expect(resultInfo.ItemsInserted).To(BeEquivalentTo(int64(1)))
3636
})
3737

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

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

84-
result, err := client.BFInfoArg(ctx, "testbf1", redis.BFCAPACITY).Result()
84+
result, err := client.BFInfoCapacity(ctx, "testbf1").Result()
8585
Expect(err).NotTo(HaveOccurred())
8686
Expect(result.Capacity).To(BeEquivalentTo(int64(2000)))
87+
88+
result, err = client.BFInfoItems(ctx, "testbf1").Result()
89+
Expect(err).NotTo(HaveOccurred())
90+
Expect(result.ItemsInserted).To(BeEquivalentTo(int64(0)))
91+
92+
result, err = client.BFInfoSize(ctx, "testbf1").Result()
93+
Expect(err).NotTo(HaveOccurred())
94+
Expect(result.Size).To(BeEquivalentTo(int64(4056)))
95+
96+
err = client.BFReserveExpansion(ctx, "testbf2", 0.001, 2000, 3).Err()
97+
Expect(err).NotTo(HaveOccurred())
98+
99+
result, err = client.BFInfoFilters(ctx, "testbf2").Result()
100+
Expect(err).NotTo(HaveOccurred())
101+
Expect(result.Filters).To(BeEquivalentTo(int64(1)))
102+
103+
result, err = client.BFInfoExpansion(ctx, "testbf2").Result()
104+
Expect(err).NotTo(HaveOccurred())
105+
Expect(result.ExpansionRate).To(BeEquivalentTo(int64(3)))
87106
})
88107

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

133152
Expect(err).NotTo(HaveOccurred())
134153
Expect(resultInfo).To(BeAssignableToTypeOf(redis.BFInfo{}))
135-
Expect(resultInfo.NumItemsInserted).To(BeEquivalentTo(int64(3)))
154+
Expect(resultInfo.ItemsInserted).To(BeEquivalentTo(int64(3)))
136155
})
137156

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

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

0 commit comments

Comments
 (0)