Skip to content

Commit 04cf5d8

Browse files
committed
base: add ParseInternalKV
1 parent f2194a9 commit 04cf5d8

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

internal/base/internal.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -585,19 +585,35 @@ func ParseInternalKey(s string) InternalKey {
585585
seqNum := ParseSeqNum(x[2])
586586
return MakeInternalKey([]byte(ukey), seqNum, kind)
587587
}
588-
x := strings.FieldsFunc(s, func(c rune) bool { return c == '#' || c == ',' })
589-
if len(x) != 3 {
590-
panic(fmt.Sprintf("invalid key internal %q", s))
588+
sep1 := strings.Index(s, "#")
589+
sep2 := strings.Index(s, ",")
590+
if sep1 == -1 || sep2 == -1 || sep2 < sep1 {
591+
panic(fmt.Sprintf("invalid internal key %q", s))
591592
}
592-
userKey := []byte(x[0])
593-
seqNum := ParseSeqNum(x[1])
594-
kind, ok := kindsMap[x[2]]
593+
594+
userKey := []byte(s[:sep1])
595+
seqNum := ParseSeqNum(s[sep1+1 : sep2])
596+
kind, ok := kindsMap[s[sep2+1:]]
595597
if !ok {
596-
panic(fmt.Sprintf("unknown kind: %q", x[2]))
598+
panic(fmt.Sprintf("unknown kind: %q", s[sep2+1:]))
597599
}
598600
return MakeInternalKey(userKey, seqNum, kind)
599601
}
600602

603+
// ParseInternalKV parses the string representation of an internal KV. The
604+
// format is "<user-key>#<seq-num>,<kind>:value". The value is encoded in-place.
605+
func ParseInternalKV(s string) InternalKV {
606+
// Cut the key at the first ":".
607+
sepIdx := strings.Index(s, ":")
608+
if sepIdx == -1 {
609+
panic(fmt.Sprintf("invalid KV %q", s))
610+
}
611+
keyStr := strings.TrimSpace(s[:sepIdx])
612+
valStr := strings.TrimSpace(s[sepIdx+1:])
613+
key := ParseInternalKey(keyStr)
614+
return MakeInternalKV(key, []byte(valStr))
615+
}
616+
601617
// ParseInternalKeyRange parses a string of the form:
602618
//
603619
// [<user-key>#<seq-num>,<kind>-<user-key>#<seq-num>,<kind>]

sstable/test_utils.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,27 +165,23 @@ func ParseTestKVsAndSpans(input string, bv *blobtest.Values) (_ []ParsedKVOrSpan
165165

166166
var kv ParsedKVOrSpan
167167
line, kv.ForceObsolete = strings.CutPrefix(line, "force-obsolete:")
168-
// Cut the key at the first ":".
169-
sepIdx := strings.Index(line, ":")
170-
if sepIdx == -1 {
171-
return nil, errors.Newf("KV format is \"[force-obsolete:] <key>:<value>\": %q", line)
172-
}
173-
keyStr := strings.TrimSpace(line[:sepIdx])
174-
valStr := strings.TrimSpace(line[sepIdx+1:])
175-
kv.Key = base.ParseInternalKey(keyStr)
168+
internalKV := base.ParseInternalKV(line)
169+
kv.Key = internalKV.K
170+
kv.Value = internalKV.InPlaceValue()
176171

177172
if kv.ForceObsolete && kv.Key.Kind() == InternalKeyKindRangeDelete {
178173
return nil, errors.Errorf("force-obsolete is not allowed for RANGEDEL")
179174
}
180175

181-
if blobtest.IsBlobHandle(valStr) {
176+
if blobtest.IsBlobHandle(string(kv.Value)) {
182177
if bv == nil {
183178
return nil, errors.Errorf("test not set up to support blob handles")
184179
}
185-
handle, remaining, err := bv.ParseInlineHandle(valStr)
180+
handle, remaining, err := bv.ParseInlineHandle(string(kv.Value))
186181
if err != nil {
187182
return nil, errors.Wrapf(err, "parsing blob handle")
188183
}
184+
kv.Value = nil
189185
kv.BlobHandle = handle
190186
if remaining != "" {
191187
p := strparse.MakeParser("=", remaining)
@@ -196,8 +192,6 @@ func ParseTestKVsAndSpans(input string, bv *blobtest.Values) (_ []ParsedKVOrSpan
196192
return nil, errors.Newf("unexpected trailing input %q", p.Remaining())
197193
}
198194
}
199-
} else {
200-
kv.Value = []byte(valStr)
201195
}
202196
result = append(result, kv)
203197
}

0 commit comments

Comments
 (0)