Skip to content

Commit 8ed4ab2

Browse files
feat(debug): add parse_key to debug tool (#7640)
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 000000000000000000000b6467726170682e74797065000000000000000001 {d} Key: UID: 1, Attr: 0-dgraph.type, Data key This tells you that the key 000000000000000000000b6467726170682e74797065000000000000000001 is for the predicate `0-dgraph.type` and the UID `1`. Docs PR: dgraph-io/dgraph-docs#636 Co-authored-by: Aman Mangal <aman@dgraph.io>
1 parent 9858dbc commit 8ed4ab2

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

dgraph/cmd/debug/run.go

+31
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type flagOptions struct {
7070
noKeys bool
7171
key x.Sensitive
7272
onlySummary bool
73+
parseKey string
7374

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

@@ -949,6 +951,35 @@ func run() {
949951
}
950952
}()
951953

954+
if opt.parseKey != "" {
955+
k, err := hex.DecodeString(opt.parseKey)
956+
if err != nil {
957+
log.Fatalf("error while decoding hex key: %v\n", err)
958+
}
959+
pk, err := x.Parse(k)
960+
if err != nil {
961+
log.Fatalf("error while parsing key: %v\n", err)
962+
}
963+
if pk.IsData() {
964+
fmt.Printf("{d}")
965+
}
966+
if pk.IsIndex() {
967+
fmt.Printf("{i}")
968+
}
969+
if pk.IsCountOrCountRev() {
970+
fmt.Printf("{c}")
971+
}
972+
if pk.IsSchema() {
973+
fmt.Printf("{s}")
974+
}
975+
if pk.IsReverse() {
976+
fmt.Printf("{r}")
977+
}
978+
fmt.Printf(" Key: %+v\n", pk)
979+
return
980+
}
981+
982+
var err error
952983
dir := opt.pdir
953984
isWal := false
954985
if len(dir) == 0 {

0 commit comments

Comments
 (0)