Skip to content

Commit

Permalink
feat(debug): add parse_key to debug tool
Browse files Browse the repository at this point in the history
This adds a `dgraph debug --parse_key` flag that can return the ParsedKey
struct of a hex key. This is useful if there's a lone key that you want parse
without having to have a p directory around. This flag does not need a p
directory, just the hex key string.

Example:

    $ dgraph debug --parse_key 00000375726c000000000000000001
    Key: {ByteType:0 Attr:url Uid:1 HasStartUid:false StartUid:0 Term: Count:0 bytePrefix:0}

This tells you that the key 00000375726c000000000000000001 is for the
predicate `url` and the UID `0x1`.
  • Loading branch information
mangalaman93 committed Aug 21, 2023
1 parent 28ac6c2 commit 6e44a98
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions dgraph/cmd/debug/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type flagOptions struct {
noKeys bool
key x.Sensitive
onlySummary bool
parseKey string

// Options related to the WAL.
wdir string
Expand Down Expand Up @@ -114,6 +115,7 @@ func init() {
flag.StringVarP(&opt.wsetSnapshot, "snap", "s", "",
"Set snapshot term,index,readts to this. Value must be comma-separated list containing"+
" the value for these vars in that order.")
flag.StringVar(&opt.parseKey, "parse_key", "", "Parse hex key.")
ee.RegisterEncFlag(flag)
}

Expand Down Expand Up @@ -937,6 +939,35 @@ func run() {
}
}()

if opt.parseKey != "" {
k, err := hex.DecodeString(opt.parseKey)
if err != nil {
log.Fatalf("Error while decoding hex key: %v\n", err)
}
pk, err := x.Parse(k)
if err != nil {
log.Fatalf("Error while parsing key: %v\n", err)
}
if pk.IsData() {
fmt.Printf("{d}")
}
if pk.IsIndex() {
fmt.Printf("{i}")
}
if pk.IsCountOrCountRev() {
fmt.Printf("{c}")
}
if pk.IsSchema() {
fmt.Printf("{s}")
}
if pk.IsReverse() {
fmt.Printf("{r}")
}
fmt.Printf(" Key: %+v\n", pk)
return
}

var err error
dir := opt.pdir
isWal := false
if len(dir) == 0 {
Expand Down

0 comments on commit 6e44a98

Please sign in to comment.