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

ft: Add support for PFDEBUG command #1216

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
WrongTypeErr = "-WRONGTYPE Operation against a key holding the wrong kind of value"
WrongTypeHllErr = "-WRONGTYPE Key is not a valid HyperLogLog string value."
InvalidHllErr = "-INVALIDOBJ Corrupted HLL object detected"
HllEncodingErr = "HLL encoding is not sparse"
WorkerNotFoundErr = "worker with ID %s not found"
JSONPathValueTypeErr = "-WRONGTYPE wrong type of path value - expected string but found integer"
HashValueNotIntegerErr = "hash value is not an integer"
Expand Down
8 changes: 8 additions & 0 deletions internal/eval/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,13 @@ var (
Arity: -2,
KeySpecs: KeySpecs{BeginIndex: 1},
}
pfDebugMeta = DiceCmdMeta{
Name: "PFDEBUG",
Info: `PFDEBUG subcommand key`,
Eval: evalPFDEBUG,
Arity: -3,
KeySpecs: KeySpecs{BeginIndex: 1},
}
pfCountCmdMeta = DiceCmdMeta{
Name: "PFCOUNT",
Info: `PFCOUNT key [key ...]
Expand Down Expand Up @@ -1188,6 +1195,7 @@ func init() {
DiceCmds["PFADD"] = pfAddCmdMeta
DiceCmds["PFCOUNT"] = pfCountCmdMeta
DiceCmds["PFMERGE"] = pfMergeCmdMeta
DiceCmds["PFDEBUG"] = pfDebugMeta
DiceCmds["PING"] = pingCmdMeta
DiceCmds["PTTL"] = pttlCmdMeta
DiceCmds["QUNWATCH"] = qUnwatchCmdMeta
Expand Down
87 changes: 87 additions & 0 deletions internal/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package eval
import (
"bytes"
"crypto/rand"
"encoding/base64"

"errors"
"fmt"
Expand All @@ -12,6 +13,7 @@ import (
"math"
"math/big"
"math/bits"
"reflect"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -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")
Copy link
Collaborator

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.

Copy link
Author

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

}

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()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a way we can avoid usage of unsafe pointers?

Copy link
Author

Choose a reason for hiding this comment

The 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":
Copy link
Collaborator

Choose a reason for hiding this comment

The 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" {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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")
Expand Down