Skip to content

Commit

Permalink
base: consolidate internal key parsing functions
Browse files Browse the repository at this point in the history
Consolidate two internal key parsing functions into one that supports
both formats. We should reduce uses of the older format over time.
  • Loading branch information
RaduBerinde committed Sep 10, 2024
1 parent 117aa7d commit e0afd55
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
48 changes: 26 additions & 22 deletions internal/base/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,23 +292,6 @@ var kindsMap = map[string]InternalKeyKind{
"DELSIZED": InternalKeyKindDeleteSized,
}

// ParseInternalKey parses the string representation of an internal key. The
// format is <user-key>.<kind>.<seq-num>. If the seq-num starts with a "b" it
// is marked as a batch-seq-num (i.e. the SeqNumBatchBit bit is set).
func ParseInternalKey(s string) InternalKey {
x := strings.Split(s, ".")
if len(x) != 3 {
panic(fmt.Sprintf("invalid internal key %q", s))
}
ukey := x[0]
kind, ok := kindsMap[x[1]]
if !ok {
panic(fmt.Sprintf("unknown kind: %q", x[1]))
}
seqNum := ParseSeqNum(x[2])
return MakeInternalKey([]byte(ukey), seqNum, kind)
}

// ParseSeqNum parses the string representation of a sequence number.
// "inf" is supported as the maximum sequence number (mainly used for exclusive
// end keys).
Expand Down Expand Up @@ -541,11 +524,32 @@ func (k prettyInternalKey) Format(s fmt.State, c rune) {
fmt.Fprintf(s, "%s#%s,%s", k.formatKey(k.UserKey), k.SeqNum(), k.Kind())
}

// ParsePrettyInternalKey parses the pretty string representation of an
// internal key. The format is <user-key>#<seq-num>,<kind>.
// TODO(radu): do we need both ParseInternalKey and ParsePrettyInternalKey?
func ParsePrettyInternalKey(s string) InternalKey {
// ParseInternalKey parses the string representation of an internal key. The
// format is <user-key>#<seq-num>,<kind>. The older format
// <user-key>.<kind>.<seq-num> is also supported (for now).
//
// If the seq-num starts with a "b" it is marked as a batch-seq-num (i.e. the
// SeqNumBatchBit bit is set).
func ParseInternalKey(s string) InternalKey {
if !strings.Contains(s, "#") {
// Parse the old format: <user-key>.<kind>.<seq-num>
// TODO(radu): get rid of this.
x := strings.Split(s, ".")
if len(x) != 3 {
panic(fmt.Sprintf("invalid internal key %q", s))
}
ukey := x[0]
kind, ok := kindsMap[x[1]]
if !ok {
panic(fmt.Sprintf("unknown kind: %q", x[1]))
}
seqNum := ParseSeqNum(x[2])
return MakeInternalKey([]byte(ukey), seqNum, kind)
}
x := strings.FieldsFunc(s, func(c rune) bool { return c == '#' || c == ',' })
if len(x) != 3 {
panic(fmt.Sprintf("invalid key internal %q", s))
}
userKey := []byte(x[0])
seqNum := ParseSeqNum(x[1])
kind, ok := kindsMap[x[2]]
Expand All @@ -565,7 +569,7 @@ func ParseInternalKeyRange(s string) (start, end InternalKey) {
if !ok1 || !ok2 || len(x) != 2 {
panic(fmt.Sprintf("invalid key range %q", s))
}
return ParsePrettyInternalKey(x[0]), ParsePrettyInternalKey(x[1])
return ParseInternalKey(x[0]), ParseInternalKey(x[1])
}

// MakeInternalKV constructs an InternalKV with the provided internal key and
Expand Down
2 changes: 1 addition & 1 deletion internal/manifest/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (p *debugParser) DiskFileNum() base.DiskFileNum {

// InternalKey parses the next token as an internal key.
func (p *debugParser) InternalKey() base.InternalKey {
return base.ParsePrettyInternalKey(p.Next())
return base.ParseInternalKey(p.Next())
}

// Errf panics with an error which includes the original string and the last
Expand Down
2 changes: 1 addition & 1 deletion sstable/colblk/data_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestDataBlock(t *testing.T) {
case "write":
for _, line := range strings.Split(td.Input, "\n") {
j := strings.IndexRune(line, ':')
ik := base.ParsePrettyInternalKey(line[:j])
ik := base.ParseInternalKey(line[:j])

kcmp := w.KeyWriter.ComparePrev(ik.UserKey)
valueString := line[j+1:]
Expand Down

0 comments on commit e0afd55

Please sign in to comment.