-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
ft: Add support for PFDEBUG command #1216
base: master
Are you sure you want to change the base?
Changes from 4 commits
2a60382
e4db0df
c0f3c6c
cbdc6f4
aa83253
a123d85
47f28e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package eval | |
import ( | ||
"bytes" | ||
"crypto/rand" | ||
"encoding/base64" | ||
|
||
"errors" | ||
"fmt" | ||
|
@@ -12,6 +13,7 @@ import ( | |
"math" | ||
"math/big" | ||
"math/bits" | ||
"reflect" | ||
"regexp" | ||
"sort" | ||
"strconv" | ||
|
@@ -4108,6 +4110,91 @@ func evalPFMERGE(args []string, store *dstore.Store) []byte { | |
return clientio.RespOK | ||
} | ||
|
||
func evalPFDEBUG(args []string, store *dstore.Store) []byte { | ||
if len(args) != 2 { | ||
return diceerrors.NewErrArity("PFDEBUG") | ||
} | ||
|
||
subcommand := strings.ToUpper(args[0]) | ||
key := args[1] | ||
|
||
obj := store.Get(key) | ||
if obj == nil { | ||
return diceerrors.NewErrWithMessage("Key does not exist") | ||
} | ||
|
||
hll, ok := obj.Value.(*hyperloglog.Sketch) | ||
if !ok { | ||
return diceerrors.NewErrWithMessage(diceerrors.WrongTypeHllErr) | ||
} | ||
|
||
sparseField := reflect.ValueOf(hll).Elem().FieldByName("sparseList") | ||
sparseFieldValue := reflect.NewAt(sparseField.Type(), unsafe.Pointer(sparseField.UnsafeAddr())).Elem() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way we can avoid usage of unsafe pointers? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one thing we can do, convert the hyperloglog structure to binary, the third byte is 0/1 depending on sparse/dense notation. I'll use that instead. |
||
|
||
var encoding string | ||
if sparseFieldValue.IsNil() { | ||
encoding = "dense" | ||
} else { | ||
encoding = "sparse" | ||
} | ||
|
||
switch subcommand { | ||
case "GETREG": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a constant to define this string in the constants file. Same for other cases. |
||
// For call to GETREG, encoding is converted from sparse to dense. | ||
// We'll merge a sparse and an empty dense HLL to get a dense HLL | ||
// with values of sparse HLL. | ||
if encoding == "sparse" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Define this string in a constant. It would also be good to be consistent with the casing of these constants. |
||
emptyDenseHLL := hyperloglog.NewNoSparse() | ||
err := hll.Merge(emptyDenseHLL) | ||
|
||
if err != nil { | ||
return diceerrors.NewErrWithMessage(diceerrors.InvalidHllErr) | ||
} | ||
} | ||
|
||
// registers := reflect.ValueOf(hll).Elem().FieldByName("regs") | ||
// registersValue := reflect.NewAt(registers.Type(), unsafe.Pointer(registers.UnsafeAddr())).Elem() | ||
data, error := hll.MarshalBinary() | ||
if error != nil { | ||
return diceerrors.NewErrWithMessage(diceerrors.InvalidHllErr) | ||
} | ||
|
||
return clientio.Encode(base64.StdEncoding.EncodeToString(data), false) | ||
|
||
case "DECODE": | ||
if encoding != "sparse" { | ||
return diceerrors.NewErrWithMessage(diceerrors.HllEncodingErr) | ||
} | ||
|
||
data, err := hll.MarshalBinary() | ||
|
||
if err != nil { | ||
return diceerrors.NewErrWithMessage(diceerrors.InvalidHllErr) | ||
} | ||
|
||
return clientio.Encode(base64.StdEncoding.EncodeToString(data), false) | ||
|
||
case "ENCODING": | ||
return clientio.Encode(encoding, false) | ||
|
||
case "TODENSE": | ||
changed := 0 | ||
if encoding == "sparse" { | ||
emptyDenseHLL := hyperloglog.NewNoSparse() | ||
err := hll.Merge(emptyDenseHLL) | ||
changed = 1 | ||
|
||
if err != nil { | ||
return diceerrors.NewErrWithMessage(diceerrors.InvalidHllErr) | ||
} | ||
} | ||
return clientio.Encode(changed, false) | ||
|
||
default: | ||
return diceerrors.NewErrWithMessage(fmt.Sprintf("Unknown PFDEBUG subcommand: %s", subcommand)) | ||
} | ||
} | ||
|
||
func evalJSONSTRLEN(args []string, store *dstore.Store) []byte { | ||
if len(args) < 1 { | ||
return diceerrors.NewErrArity("JSON.STRLEN") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should match the message returned by redis, and ideally be defined as a constant string in the constants file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, matches the message. Will define as constant