-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec71ce4
commit 132fce0
Showing
6 changed files
with
364 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,166 @@ | ||
package rocksdb | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFoo(t *testing.T) { | ||
require.Equal(t, 1, 1) | ||
const ( | ||
storeKey1 = "store1" | ||
) | ||
|
||
func TestDatabase_Close(t *testing.T) { | ||
db, err := New(t.TempDir()) | ||
require.NoError(t, err) | ||
require.NoError(t, db.Close()) | ||
} | ||
|
||
func TestDatabase_LatestVersion(t *testing.T) { | ||
db, err := New(t.TempDir()) | ||
require.NoError(t, err) | ||
|
||
lv, err := db.GetLatestVersion() | ||
require.NoError(t, err) | ||
require.Zero(t, lv) | ||
|
||
expected := uint64(1) | ||
|
||
err = db.SetLatestVersion(expected) | ||
require.NoError(t, err) | ||
|
||
lv, err = db.GetLatestVersion() | ||
require.NoError(t, err) | ||
require.Equal(t, expected, lv) | ||
} | ||
|
||
func TestDatabase_CRUD(t *testing.T) { | ||
db, err := New(t.TempDir()) | ||
require.NoError(t, err) | ||
|
||
ok, err := db.Has(storeKey1, 1, []byte("key")) | ||
require.NoError(t, err) | ||
require.False(t, ok) | ||
|
||
err = db.Set(storeKey1, 1, []byte("key"), []byte("value")) | ||
require.NoError(t, err) | ||
|
||
ok, err = db.Has(storeKey1, 1, []byte("key")) | ||
require.NoError(t, err) | ||
require.True(t, ok) | ||
|
||
val, err := db.Get(storeKey1, 1, []byte("key")) | ||
require.NoError(t, err) | ||
require.Equal(t, []byte("value"), val) | ||
|
||
err = db.Delete(storeKey1, 1, []byte("key")) | ||
require.NoError(t, err) | ||
|
||
ok, err = db.Has(storeKey1, 1, []byte("key")) | ||
require.NoError(t, err) | ||
require.False(t, ok) | ||
|
||
val, err = db.Get(storeKey1, 1, []byte("key")) | ||
require.NoError(t, err) | ||
require.Nil(t, val) | ||
} | ||
|
||
func TestDatabase_Batch(t *testing.T) { | ||
db, err := New(t.TempDir()) | ||
require.NoError(t, err) | ||
|
||
batch := db.NewBatch(1) | ||
|
||
for i := 0; i < 100; i++ { | ||
err = batch.Set(storeKey1, []byte(fmt.Sprintf("key%d", i)), []byte("value")) | ||
require.NoError(t, err) | ||
} | ||
|
||
for i := 0; i < 100; i++ { | ||
if i%10 == 0 { | ||
err = batch.Delete(storeKey1, []byte(fmt.Sprintf("key%d", i))) | ||
require.NoError(t, err) | ||
} | ||
} | ||
|
||
require.NotZero(t, batch.Size()) | ||
|
||
err = batch.Write() | ||
require.NoError(t, err) | ||
|
||
lv, err := db.GetLatestVersion() | ||
require.NoError(t, err) | ||
require.Equal(t, uint64(1), lv) | ||
|
||
for i := 0; i < 100; i++ { | ||
ok, err := db.Has(storeKey1, 1, []byte(fmt.Sprintf("key%d", i))) | ||
require.NoError(t, err) | ||
|
||
if i%10 == 0 { | ||
require.False(t, ok) | ||
} else { | ||
require.True(t, ok) | ||
} | ||
} | ||
} | ||
|
||
func TestDatabase_ResetBatch(t *testing.T) { | ||
db, err := New(t.TempDir()) | ||
require.NoError(t, err) | ||
|
||
batch := db.NewBatch(1) | ||
|
||
for i := 0; i < 100; i++ { | ||
err = batch.Set(storeKey1, []byte(fmt.Sprintf("key%d", i)), []byte("value")) | ||
require.NoError(t, err) | ||
} | ||
|
||
for i := 0; i < 100; i++ { | ||
if i%10 == 0 { | ||
err = batch.Delete(storeKey1, []byte(fmt.Sprintf("key%d", i))) | ||
require.NoError(t, err) | ||
} | ||
} | ||
|
||
require.NotZero(t, batch.Size()) | ||
batch.Reset() | ||
require.NotPanics(t, func() { batch.Reset() }) | ||
|
||
// There is an initial cost of 12 bytes for the batch header | ||
require.LessOrEqual(t, batch.Size(), 12) | ||
} | ||
|
||
func TestDatabase_StartIterator(t *testing.T) { | ||
db, err := New(t.TempDir()) | ||
require.NoError(t, err) | ||
|
||
batch := db.NewBatch(1) | ||
|
||
keys := make([]string, 100) | ||
for i := 0; i < 100; i++ { | ||
key := fmt.Sprintf("key%d", i) | ||
err = batch.Set(storeKey1, []byte(key), []byte("value")) | ||
require.NoError(t, err) | ||
|
||
keys[i] = key | ||
} | ||
|
||
sort.Strings(keys) | ||
|
||
err = batch.Write() | ||
require.NoError(t, err) | ||
|
||
iter, err := db.NewIterator(storeKey1, 1, []byte("key0"), nil) | ||
require.NoError(t, err) | ||
|
||
defer iter.Close() | ||
|
||
var i int | ||
for ; iter.Valid(); iter.Next() { | ||
require.Equal(t, []byte(keys[i]), iter.Key()) | ||
require.Equal(t, []byte("value"), iter.Value()) | ||
i++ | ||
} | ||
} |
Oops, something went wrong.