Skip to content

Commit

Permalink
Introduce in-memory mode in badger (#1113)
Browse files Browse the repository at this point in the history
This PR introduces in-memory mode in badger. The in-memory mode
can be enabled by setting options.InMemory=true. When badger is
running in in-memory mode no files are created and everything 
is stored in memory.

On DB close, all stored data is lost.
NOTE - An existing DB cannot be opened in in-memory mode.

Fixes - #1001
  • Loading branch information
Ibrahim Jarif authored Nov 25, 2019
1 parent 3eb4e72 commit f5b6321
Show file tree
Hide file tree
Showing 19 changed files with 647 additions and 298 deletions.
80 changes: 44 additions & 36 deletions backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,48 +276,56 @@ func populateEntries(db *DB, entries []*pb.KV) error {
}

func TestBackup(t *testing.T) {
var bb bytes.Buffer

tmpdir, err := ioutil.TempDir("", "badger-test")
if err != nil {
t.Fatal(err)
}
defer removeDir(tmpdir)

db1, err := Open(DefaultOptions(filepath.Join(tmpdir, "backup0")))
if err != nil {
t.Fatal(err)
}
test := func(t *testing.T, db *DB) {
var bb bytes.Buffer
N := 1000
entries := createEntries(N)
require.NoError(t, populateEntries(db, entries))

N := 1000
entries := createEntries(N)
require.NoError(t, populateEntries(db1, entries))

_, err = db1.Backup(&bb, 0)
require.NoError(t, err)
_, err := db.Backup(&bb, 0)
require.NoError(t, err)

err = db1.View(func(txn *Txn) error {
opts := DefaultIteratorOptions
it := txn.NewIterator(opts)
defer it.Close()
var count int
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
idx, err := strconv.Atoi(string(item.Key())[3:])
if err != nil {
return err
err = db.View(func(txn *Txn) error {
opts := DefaultIteratorOptions
it := txn.NewIterator(opts)
defer it.Close()
var count int
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
idx, err := strconv.Atoi(string(item.Key())[3:])
if err != nil {
return err
}
if idx > N || !bytes.Equal(entries[idx].Key, item.Key()) {
return fmt.Errorf("%s: %s", string(item.Key()), ErrKeyNotFound)
}
count++
}
if idx > N || !bytes.Equal(entries[idx].Key, item.Key()) {
return fmt.Errorf("%s: %s", string(item.Key()), ErrKeyNotFound)
if N != count {
return fmt.Errorf("wrong number of items: %d expected, %d actual", N, count)
}
count++
}
if N != count {
return fmt.Errorf("wrong number of items: %d expected, %d actual", N, count)
return nil
})
require.NoError(t, err)
}
t.Run("disk mode", func(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "badger-test")
if err != nil {
t.Fatal(err)
}
return nil
defer removeDir(tmpdir)
opt := DefaultOptions(filepath.Join(tmpdir, "backup0"))
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
test(t, db)
})
})
t.Run("InMemory mode", func(t *testing.T) {
opt := DefaultOptions("")
opt.InMemory = true
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
test(t, db)
})
})
require.NoError(t, err)
}

func TestBackupRestore3(t *testing.T) {
Expand Down
15 changes: 14 additions & 1 deletion batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestWriteBatch(t *testing.T) {
return []byte(fmt.Sprintf("%128d", i))
}

runBadgerTest(t, nil, func(t *testing.T, db *DB) {
test := func(t *testing.T, db *DB) {
wb := db.NewWriteBatch()
defer wb.Cancel()

Expand Down Expand Up @@ -65,5 +65,18 @@ func TestWriteBatch(t *testing.T) {
return nil
})
require.NoError(t, err)
}
t.Run("disk mode", func(t *testing.T) {
runBadgerTest(t, nil, func(t *testing.T, db *DB) {
test(t, db)
})
})
t.Run("InMemory mode", func(t *testing.T) {
opt := getTestOptions("")
opt.InMemory = true
db, err := Open(opt)
require.NoError(t, err)
test(t, db)
require.NoError(t, db.Close())
})
}
1 change: 1 addition & 0 deletions contrib/cover.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ for PKG in $(go list ./...|grep -v -E 'vendor'); do
tail -n +2 $TMP >> $OUT
done

echo "Running test with vlog_mmap false"
# Another round of tests after turning off mmap
go test -v -vlog_mmap=false github.com/dgraph-io/badger

Expand Down
Loading

0 comments on commit f5b6321

Please sign in to comment.