Skip to content

Commit

Permalink
add leveldb implementation in the top-level and dos
Browse files Browse the repository at this point in the history
  • Loading branch information
tok-kkk committed Aug 9, 2019
1 parent 8d51a46 commit 5b71f70
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,20 @@ It requires the key to be a non-empty string and the value to be able to be mars

Creating a Table:
```go
// In-memory implementation
table := kv.NewMemTable(kv.JsonCodec)

// Leveldb implementation
ldb, err = leveldb.OpenFile("./.leveldb", nil)
handle(err)
table := kv.NewLevelTable("name", ldb, kv.JsonCodec)

// BadgerDB implementation
bdb, err:= badger.Open(badger.DefaultOptions("."))
handle(err)
table := kv.NewBadgerTable("name", bdb, kv.JsonCodec)

// In-memory implementation
table := kv.NewMemTable(kv.JsonCodec)


```

Expand Down Expand Up @@ -105,13 +112,20 @@ a specific table of the DB directly. Or your can get the table by it's name and

Creating a DB:
```go
// In-memory implementation
db := kv.NewMemDB()

// LevelDB implementation
ldb, err = leveldb.OpenFile("./.leveldb", nil)
handle(err)
db := kv.NewLevelDB(ldb)

// BadgerDB implementation
bdb, err:= badger.Open(badger.DefaultOptions("."))
handle(err)
db := kv.NewBadgerDB(bdb)

// In-memory implementation
db := kv.NewMemDB()

```

Creating new tables or accessing existing ones
Expand Down
13 changes: 12 additions & 1 deletion kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/renproject/kv/badgerdb"
"github.com/renproject/kv/codec"
"github.com/renproject/kv/db"
"github.com/renproject/kv/leveldb"
"github.com/renproject/kv/memdb"
)

Expand Down Expand Up @@ -58,7 +59,7 @@ var (
NewMemDB = memdb.New
)

// BadgerDB implementation of the DB and table
// BadgerDB implementation of the DB and table.
var (
// TODO: Comment!
NewBadgerTable = badgerdb.NewTable
Expand All @@ -68,6 +69,16 @@ var (
NewBadgerDB = badgerdb.New
)

// LevelDB implementation of the DB and table.
var (
// TODO: Comment!
NewLevelTable = leveldb.NewTable

// NewLevelDB returns a key-value database that is implemented using
// levelDB. For more information, see https://github.com/syndtr/goleveldb.
NewLevelDB = leveldb.New
)

var (
// JsonCodec is a json codec that marshals and unmarshals values using the
// standard Golang JSON marshalers. For more information, see
Expand Down
6 changes: 5 additions & 1 deletion testutil/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

var Ran = rand.New(rand.NewSource(time.Now().Unix()))

// TestStruct is a struct which includes some commonly used types.
type TestStruct struct {
A string
B int
Expand All @@ -17,6 +18,7 @@ type TestStruct struct {
E map[string]float64
}

// RandomTestStruct returns a random `TestStruct`
func RandomTestStruct() TestStruct {
t := reflect.TypeOf(TestStruct{})
value, ok := quick.Value(t, Ran)
Expand All @@ -26,6 +28,7 @@ func RandomTestStruct() TestStruct {
return value.Interface().(TestStruct)
}

// RandomTestStructGroups creates a group of random TestStructs.
func RandomTestStructGroups(group, entriesPerGroup int) [][]TestStruct {
testEntries := make([][]TestStruct, group)
for i := range testEntries {
Expand All @@ -38,12 +41,13 @@ func RandomTestStructGroups(group, entriesPerGroup int) [][]TestStruct {
return testEntries
}

// RandomNonDupStrings returns a list of non-duplicate strings.
func RandomNonDupStrings(i int) []string {
cap := rand.Intn(i)
dup := map[string]struct{}{}
res := make([]string, 0, cap)

for len(dup) < cap {
for len(res) < cap {
t := reflect.TypeOf("")
value, ok := quick.Value(t, Ran)
if !ok {
Expand Down

0 comments on commit 5b71f70

Please sign in to comment.