Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BREAKING CHANGE] proposal: Builder API for Options #874

Merged
merged 3 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 8 additions & 30 deletions backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ func TestBackupRestore2(t *testing.T) {
s2Path := filepath.Join(tmpdir, "test2")
s3Path := filepath.Join(tmpdir, "test3")

opts := DefaultOptions
opts.Dir = s1Path
opts.ValueDir = s1Path
db1, err := Open(opts)
db1, err := Open(DefaultOptions(s1Path))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -166,10 +163,7 @@ func TestBackupRestore2(t *testing.T) {
}
fmt.Println("backup1 length:", backup.Len())

opts = DefaultOptions
opts.Dir = s2Path
opts.ValueDir = s2Path
db2, err := Open(opts)
db2, err := Open(DefaultOptions(s2Path))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -220,10 +214,7 @@ func TestBackupRestore2(t *testing.T) {
t.Fatal(err)
}
fmt.Println("backup2 length:", backup.Len())
opts = DefaultOptions
opts.Dir = s3Path
opts.ValueDir = s3Path
db3, err := Open(opts)
db3, err := Open(DefaultOptions(s3Path))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -296,10 +287,7 @@ func TestBackup(t *testing.T) {
}
defer os.RemoveAll(tmpdir)

opts := DefaultOptions
opts.Dir = filepath.Join(tmpdir, "backup0")
opts.ValueDir = opts.Dir
db1, err := Open(opts)
db1, err := Open(DefaultOptions(filepath.Join(tmpdir, "backup0")))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -344,15 +332,12 @@ func TestBackupRestore3(t *testing.T) {
}
defer os.RemoveAll(tmpdir)

opts := DefaultOptions
N := 1000
entries := createEntries(N)

// backup
{
opts.Dir = filepath.Join(tmpdir, "backup1")
opts.ValueDir = opts.Dir
db1, err := Open(opts)
db1, err := Open(DefaultOptions(filepath.Join(tmpdir, "backup1")))
if err != nil {
t.Fatal(err)
}
Expand All @@ -367,9 +352,7 @@ func TestBackupRestore3(t *testing.T) {
require.True(t, bb.Len() > 0)

// restore
opts.Dir = filepath.Join(tmpdir, "restore1")
opts.ValueDir = opts.Dir
db2, err := Open(opts)
db2, err := Open(DefaultOptions(filepath.Join(tmpdir, "restore1")))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -407,17 +390,14 @@ func TestBackupLoadIncremental(t *testing.T) {
}
defer os.RemoveAll(tmpdir)

opts := DefaultOptions
N := 100
entries := createEntries(N)
updates := make(map[int]byte)
var bb bytes.Buffer

// backup
{
opts.Dir = filepath.Join(tmpdir, "backup2")
opts.ValueDir = opts.Dir
db1, err := Open(opts)
db1, err := Open(DefaultOptions(filepath.Join(tmpdir, "backup2")))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -477,9 +457,7 @@ func TestBackupLoadIncremental(t *testing.T) {
require.True(t, bb.Len() > 0)

// restore
opts.Dir = filepath.Join(tmpdir, "restore2")
opts.ValueDir = opts.Dir
db2, err := Open(opts)
db2, err := Open(DefaultOptions(filepath.Join(tmpdir, "restore2")))
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 3 additions & 5 deletions badger/cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ func init() {

func doBackup(cmd *cobra.Command, args []string) error {
// Open DB
opts := badger.DefaultOptions
opts.Dir = sstDir
opts.ValueDir = vlogDir
opts.Truncate = truncate
db, err := badger.Open(opts)
db, err := badger.Open(badger.DefaultOptions(sstDir).
WithValueDir(vlogDir).
WithTruncate(truncate))
if err != nil {
return err
}
Expand Down
44 changes: 14 additions & 30 deletions badger/cmd/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,11 @@ func compareTwo(db *badger.DB, before, after uint64) {
}

func runDisect(cmd *cobra.Command, args []string) error {
opts := badger.DefaultOptions
opts.Dir = sstDir
opts.ValueDir = vlogDir
opts.ReadOnly = true

// The total did not match up. So, let's disect the DB to find the
// transction which caused the total mismatch.
db, err := badger.OpenManaged(opts)
db, err := badger.OpenManaged(badger.DefaultOptions(sstDir).
WithValueDir(vlogDir).
WithReadOnly(true))
if err != nil {
return err
}
Expand Down Expand Up @@ -324,17 +321,16 @@ func runTest(cmd *cobra.Command, args []string) error {
rand.Seed(time.Now().UnixNano())

// Open DB
opts := badger.DefaultOptions
opts.Dir = sstDir
opts.ValueDir = vlogDir
opts.MaxTableSize = 4 << 20 // Force more compactions.
opts.NumLevelZeroTables = 2
opts.NumMemtables = 2
// Do not GC any versions, because we need them for the disect.
opts.NumVersionsToKeep = int(math.MaxInt32)
opts.ValueThreshold = 1 // Make all values go to value log.
opts := badger.DefaultOptions(sstDir).
WithValueDir(vlogDir).
WithMaxTableSize(4 << 20). // Force more compactions.
WithNumLevelZeroTables(2).
WithNumMemtables(2).
// Do not GC any versions, because we need them for the disect..
WithNumVersionsToKeep(int(math.MaxInt32)).
WithValueThreshold(1) // Make all values go to value log
if mmap {
opts.TableLoadingMode = options.MemoryMap
opts = opts.WithTableLoadingMode(options.MemoryMap)
}
log.Printf("Opening DB with options: %+v\n", opts)

Expand All @@ -350,13 +346,7 @@ func runTest(cmd *cobra.Command, args []string) error {
dir, err := ioutil.TempDir("", "bank_subscribe")
y.Check(err)

subscribeOpts := badger.DefaultOptions
subscribeOpts.Dir = dir
subscribeOpts.ValueDir = dir
subscribeOpts.SyncWrites = false
log.Printf("Opening subscribe DB with options: %+v\n", subscribeOpts)

subscribeDB, err = badger.Open(subscribeOpts)
subscribeDB, err = badger.Open(badger.DefaultOptions(dir).WithSyncWrites(false))
if err != nil {
return err
}
Expand All @@ -367,13 +357,7 @@ func runTest(cmd *cobra.Command, args []string) error {
dir, err := ioutil.TempDir("", "bank_stream")
y.Check(err)

streamOpts := badger.DefaultOptions
streamOpts.Dir = dir
streamOpts.ValueDir = dir
streamOpts.SyncWrites = false
log.Printf("Opening stream DB with options: %+v\n", streamOpts)

tmpDb, err = badger.Open(streamOpts)
tmpDb, err = badger.Open(badger.DefaultOptions(dir).WithSyncWrites(false))
if err != nil {
return err
}
Expand Down
11 changes: 4 additions & 7 deletions badger/cmd/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,10 @@ func init() {
}

func flatten(cmd *cobra.Command, args []string) error {
opts := badger.DefaultOptions
opts.Dir = sstDir
opts.ValueDir = vlogDir
opts.Truncate = truncate
opts.NumCompactors = 0

db, err := badger.Open(opts)
db, err := badger.Open(badger.DefaultOptions(sstDir).
WithValueDir(vlogDir).
WithTruncate(truncate).
WithNumCompactors(0))
if err != nil {
return err
}
Expand Down
11 changes: 4 additions & 7 deletions badger/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,10 @@ func handleInfo(cmd *cobra.Command, args []string) error {
}

// Open DB
opts := badger.DefaultOptions
opts.TableLoadingMode = options.MemoryMap
opts.Dir = sstDir
opts.ValueDir = vlogDir
opts.ReadOnly = true

db, err := badger.Open(opts)
db, err := badger.Open(badger.DefaultOptions(sstDir).
WithValueDir(vlogDir).
WithReadOnly(true).
WithTableLoadingMode(options.MemoryMap))
if err != nil {
return errors.Wrap(err, "failed to open database")
}
Expand Down
12 changes: 5 additions & 7 deletions badger/cmd/read_bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,11 @@ func readBench(cmd *cobra.Command, args []string) error {
y.AssertTrue(numGoroutines > 0)
mode := getLoadingMode(loadingMode)

opts := badger.DefaultOptions
opts.ReadOnly = readOnly
opts.Dir = sstDir
opts.ValueDir = vlogDir
opts.TableLoadingMode = mode
opts.ValueLogLoadingMode = mode
db, err := badger.Open(opts)
db, err := badger.Open(badger.DefaultOptions(sstDir).
WithValueDir(vlogDir).
WithReadOnly(readOnly).
WithTableLoadingMode(mode).
WithValueLogLoadingMode(mode))
if err != nil {
return y.Wrapf(err, "unable to open DB")
}
Expand Down
5 changes: 1 addition & 4 deletions badger/cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ func doRestore(cmd *cobra.Command, args []string) error {
}

// Open DB
opts := badger.DefaultOptions
opts.Dir = sstDir
opts.ValueDir = vlogDir
db, err := badger.Open(opts)
db, err := badger.Open(badger.DefaultOptions(sstDir).WithValueDir(vlogDir))
if err != nil {
return err
}
Expand Down
15 changes: 6 additions & 9 deletions badger/cmd/write_bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,12 @@ func writeSorted(db *badger.DB, num uint64) error {
}

func writeBench(cmd *cobra.Command, args []string) error {
opts := badger.DefaultOptions
opts.Dir = sstDir
opts.ValueDir = vlogDir
opts.Truncate = truncate
opts.SyncWrites = false
opts.CompactL0OnClose = force
opts.Logger = nil

db, err := badger.Open(opts)
db, err := badger.Open(badger.DefaultOptions(sstDir).
WithValueDir(vlogDir).
WithTruncate(truncate).
WithSyncWrites(false).
WithCompactL0OnClose(force).
WithLogger(nil))
if err != nil {
return err
}
Expand Down
23 changes: 12 additions & 11 deletions db2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,11 @@ func TestBigKeyValuePairs(t *testing.T) {
t.Skip("Skipping test meant to be run manually.")
return
}
opts := DefaultOptions
opts.MaxTableSize = 1 << 20
opts.ValueLogMaxEntries = 64

// Passing an empty directory since it will be filled by runBadgerTest.
opts := DefaultOptions("").
WithMaxTableSize(1 << 20).
WithValueLogMaxEntries(64)
runBadgerTest(t, &opts, func(t *testing.T, db *DB) {
bigK := make([]byte, 65001)
bigV := make([]byte, db.opt.ValueLogFileSize+1)
Expand Down Expand Up @@ -285,9 +287,11 @@ func TestPushValueLogLimit(t *testing.T) {
t.Skip("Skipping test meant to be run manually.")
return
}
opt := DefaultOptions
opt.ValueLogMaxEntries = 64
opt.ValueLogFileSize = 2 << 30

// Passing an empty directory since it will be filled by runBadgerTest.
opt := DefaultOptions("").
WithValueLogMaxEntries(64).
WithValueLogFileSize(2 << 30)
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
data := []byte(fmt.Sprintf("%30d", 1))
key := func(i int) string {
Expand Down Expand Up @@ -338,10 +342,7 @@ func TestDiscardMapTooBig(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(dir)

ops := DefaultOptions
ops.Dir = dir
ops.ValueDir = dir
db, err := Open(ops)
db, err := Open(DefaultOptions(dir))
require.NoError(t, err, "error while openning db")

// Add some data so that memtable flush happens on close
Expand All @@ -356,7 +357,7 @@ func TestDiscardMapTooBig(t *testing.T) {

require.NoError(t, db.Close())
// reopen the same DB
db, err = Open(ops)
db, err = Open(DefaultOptions(dir))
require.NoError(t, err, "error while openning db")
require.NoError(t, db.Close())
}
Loading