Support Probabilistic commands with RESP 2 protocol (#3176)

* Support bloom resp 2

* Support Resp2 for BF.Info

* simplify BFInfoCmd field assignment using map-based key-to-field references
This commit is contained in:
ofekshenawa 2024-11-13 11:15:19 +02:00 committed by GitHub
parent d1b4eaed41
commit 8b1073d2d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 780 additions and 729 deletions

View File

@ -319,37 +319,69 @@ func (cmd *BFInfoCmd) Result() (BFInfo, error) {
} }
func (cmd *BFInfoCmd) readReply(rd *proto.Reader) (err error) { func (cmd *BFInfoCmd) readReply(rd *proto.Reader) (err error) {
n, err := rd.ReadMapLen() result := BFInfo{}
// Create a mapping from key names to pointers of struct fields
respMapping := map[string]*int64{
"Capacity": &result.Capacity,
"CAPACITY": &result.Capacity,
"Size": &result.Size,
"SIZE": &result.Size,
"Number of filters": &result.Filters,
"FILTERS": &result.Filters,
"Number of items inserted": &result.ItemsInserted,
"ITEMS": &result.ItemsInserted,
"Expansion rate": &result.ExpansionRate,
"EXPANSION": &result.ExpansionRate,
}
// Helper function to read and assign a value based on the key
readAndAssignValue := func(key string) error {
fieldPtr, exists := respMapping[key]
if !exists {
return fmt.Errorf("redis: BLOOM.INFO unexpected key %s", key)
}
// Read the integer and assign to the field via pointer dereferencing
val, err := rd.ReadInt()
if err != nil {
return err
}
*fieldPtr = val
return nil
}
readType, err := rd.PeekReplyType()
if err != nil { if err != nil {
return err return err
} }
var key string if len(cmd.args) > 2 && readType == proto.RespArray {
var result BFInfo n, err := rd.ReadArrayLen()
for f := 0; f < n; f++ {
key, err = rd.ReadString()
if err != nil { if err != nil {
return err return err
} }
if key, ok := cmd.args[2].(string); ok && n == 1 {
switch key { if err := readAndAssignValue(key); err != nil {
case "Capacity": return err
result.Capacity, err = rd.ReadInt() }
case "Size": } else {
result.Size, err = rd.ReadInt() return fmt.Errorf("redis: BLOOM.INFO invalid argument key type")
case "Number of filters":
result.Filters, err = rd.ReadInt()
case "Number of items inserted":
result.ItemsInserted, err = rd.ReadInt()
case "Expansion rate":
result.ExpansionRate, err = rd.ReadInt()
default:
return fmt.Errorf("redis: BLOOM.INFO unexpected key %s", key)
} }
} else {
n, err := rd.ReadMapLen()
if err != nil { if err != nil {
return err return err
} }
for i := 0; i < n; i++ {
key, err := rd.ReadString()
if err != nil {
return err
}
if err := readAndAssignValue(key); err != nil {
return err
}
}
} }
cmd.val = result cmd.val = result

File diff suppressed because it is too large Load Diff