8000 db: don't require quotes around level number in options section · cockroachdb/pebble@22a51f0 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 22a51f0

Browse files
committed
db: don't require quotes around level number in options section
The current format requires per-level sections to look like `[Level "1"]`. The quotes can be a pain to escape, especially when this is passed through the `--store` command line. This change makes the parsing more tolerant to allow but not require quotes, so that `[Level 1]` also works.
1 parent ccd438d commit 22a51f0

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

options.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"fmt"
1010
"io"
11+
"regexp"
1112
"runtime"
1213
"strconv"
1314
"strings"
@@ -1969,15 +1970,11 @@ func (o *Options) Parse(s string, hooks *ParseHooks) error {
19691970
return err
19701971

19711972
case strings.HasPrefix(section, "Level "):
1972-
var index int
1973-
if n, err := fmt.Sscanf(section, `Level "%d"`, &index); err != nil {
1974-
return err
1975-
} else if n != 1 {
1976-
if hooks != nil && hooks.SkipUnknown != nil && hooks.SkipUnknown(section, value) {
1977-
return nil
1978-
}
1973+
m := regexp.MustCompile(`Level\s*"?(\d+)"?\s*$`).FindStringSubmatch(section)
1974+
if m == nil {
19791975
return errors.Errorf("pebble: unknown section: %q", errors.Safe(section))
19801976
}
1977+
index, _ := strconv.Atoi(m[1])
19811978

19821979
if len(o.Levels) <= index {
19831980
newLevels := make([]LevelOptions, index+1)

options_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,36 @@ func TestOptionsParse(t *testing.T) {
319319
}
320320
}
321321

322+
func TestOptionsParseLevelNoQuotes(t *testing.T) {
323+
withQuotes := `
324+
[Options]
325+
[Level "1"]
326+
block_restart_interval=8
327+
block_size=10
328+
[Level "6"]
329+
block_restart_interval=8
330+
block_size=10
331+
`
332+
withoutQuotes := `
333+
[Options]
334+
[Level 1]
335+
block_restart_interval=8
336+
block_size=10
337+
[Level 6]
338+
block_restart_interval=8
339+
block_size=10
340+
`
341+
o1 := &Options{}
342+
require.NoError(t, o1.Parse(withQuotes, nil))
343+
o1.EnsureDefaults()
344+
345+
o2 := &Options{}
346+
require.NoError(t, o2.Parse(withoutQuotes, nil))
347+
o2.EnsureDefaults()
348+
349+
require.Equal(t, o1.String(), o2.String())
350+
}
351+
322352
func TestOptionsParseComparerOverwrite(t *testing.T) {
323353
// Test that an unrecognized comparer in the OPTIONS file does not nil out
324354
// the Comparer field.

0 commit comments

Comments
 (0)
0