From 3eea26f1e79306ed463375329d4e76ffceb7c575 Mon Sep 17 00:00:00 2001 From: Aneurin Barker Snook Date: Tue, 9 Jul 2024 23:36:31 +0100 Subject: [PATCH] move leveldb to sub-package --- README.md | 7 ++-- ezleveldb/go.mod | 10 ++++++ ezleveldb/go.sum | 33 +++++++++++++++++++ leveldb.go => ezleveldb/leveldb.go | 11 ++++--- leveldb_iter.go => ezleveldb/leveldb_iter.go | 21 +++++++----- .../leveldb_options.go | 2 +- leveldb_test.go => ezleveldb/leveldb_test.go | 15 ++++++--- 7 files changed, 78 insertions(+), 21 deletions(-) create mode 100644 ezleveldb/go.mod create mode 100644 ezleveldb/go.sum rename leveldb.go => ezleveldb/leveldb.go (88%) rename leveldb_iter.go => ezleveldb/leveldb_iter.go (77%) rename leveldb_options.go => ezleveldb/leveldb_options.go (96%) rename leveldb_test.go => ezleveldb/leveldb_test.go (86%) diff --git a/README.md b/README.md index d5f33ad..137d654 100644 --- a/README.md +++ b/README.md @@ -57,13 +57,16 @@ The following marshalers are included in EZ DB: - `JSON[T]` marshals your data `T` to `[]byte` using [encoding/json](https://pkg.go.dev/encoding/json) -## Supported databases +## Database backends The following databases are included in EZ DB: -- `LevelDB[T]` is [fast key-value storage](https://github.com/google/leveldb) on disk - `Memory[T]` is essentially a wrapper for `map[string]T`. It can be provided another Collection to use as a persistence backend +## Additional databases + +- [github.com/annybs/ezdb/ezleveldb](https://github.com/annybs/ezdb/ezleveldb) provides [fast key-value storage](https://github.com/google/leveldb) on disk + ## License See [LICENSE.md](./LICENSE.md) diff --git a/ezleveldb/go.mod b/ezleveldb/go.mod new file mode 100644 index 0000000..e6d875c --- /dev/null +++ b/ezleveldb/go.mod @@ -0,0 +1,10 @@ +module github.com/annybs/ezdb/ezleveldb + +go 1.21.4 + +require ( + github.com/annybs/ezdb v0.0.0-20240709223126-f26b55327f36 + github.com/syndtr/goleveldb v1.0.0 +) + +require github.com/golang/snappy v0.0.4 // indirect diff --git a/ezleveldb/go.sum b/ezleveldb/go.sum new file mode 100644 index 0000000..d243c94 --- /dev/null +++ b/ezleveldb/go.sum @@ -0,0 +1,33 @@ +github.com/annybs/ezdb v0.0.0-20240708223339-f93ef31f5262 h1:BVZ0pokVqE9oLTxpxWmIpEfX7amLR8l6cWwkE9YbVC4= +github.com/annybs/ezdb v0.0.0-20240708223339-f93ef31f5262/go.mod h1:eWik4kAFQYV94eUfDYnDHYV5bz4+cmqmuHfjBs6qM3s= +github.com/annybs/ezdb v0.0.0-20240709223126-f26b55327f36 h1:I9WnpkbvEuub2TCtxarVtBWahylvLyQY1XIqj/92BhI= +github.com/annybs/ezdb v0.0.0-20240709223126-f26b55327f36/go.mod h1:eWik4kAFQYV94eUfDYnDHYV5bz4+cmqmuHfjBs6qM3s= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w= +github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= +github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= +github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU= +github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE= +github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/leveldb.go b/ezleveldb/leveldb.go similarity index 88% rename from leveldb.go rename to ezleveldb/leveldb.go index c309d9a..ea0fc65 100644 --- a/leveldb.go +++ b/ezleveldb/leveldb.go @@ -1,8 +1,9 @@ -package ezdb +package ezleveldb import ( "os" + "github.com/annybs/ezdb" "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/opt" ) @@ -11,7 +12,7 @@ type LevelDBCollection[T any] struct { path string db *leveldb.DB - m DocumentMarshaler[T, []byte] + m ezdb.DocumentMarshaler[T, []byte] optOpen *opt.Options optRead *opt.ReadOptions @@ -93,7 +94,7 @@ func (c *LevelDBCollection[T]) Has(key string) (bool, error) { return c.db.Has([]byte(key), c.optRead) } -func (c *LevelDBCollection[T]) Iter() Iterator[T] { +func (c *LevelDBCollection[T]) Iter() ezdb.Iterator[T] { i := &LevelDBIterator[T]{ i: c.db.NewIterator(nil, c.optRead), m: c.m, @@ -116,7 +117,7 @@ func (c *LevelDBCollection[T]) Open() error { } func (c *LevelDBCollection[T]) Put(key string, src T) error { - if err := ValidateKey(key); err != nil { + if err := ezdb.ValidateKey(key); err != nil { return err } @@ -129,7 +130,7 @@ func (c *LevelDBCollection[T]) Put(key string, src T) error { } // LevelDB creates a new collection using LevelDB storage. -func LevelDB[T any](path string, m DocumentMarshaler[T, []byte], o *LevelDBOptions) *LevelDBCollection[T] { +func LevelDB[T any](path string, m ezdb.DocumentMarshaler[T, []byte], o *LevelDBOptions) *LevelDBCollection[T] { c := &LevelDBCollection[T]{ path: path, diff --git a/leveldb_iter.go b/ezleveldb/leveldb_iter.go similarity index 77% rename from leveldb_iter.go rename to ezleveldb/leveldb_iter.go index ad2c8aa..3a5753f 100644 --- a/leveldb_iter.go +++ b/ezleveldb/leveldb_iter.go @@ -1,10 +1,13 @@ -package ezdb +package ezleveldb -import "github.com/syndtr/goleveldb/leveldb/iterator" +import ( + "github.com/annybs/ezdb" + "github.com/syndtr/goleveldb/leveldb/iterator" +) type LevelDBIterator[T any] struct { i iterator.Iterator - m DocumentMarshaler[T, []byte] + m ezdb.DocumentMarshaler[T, []byte] } func (i *LevelDBIterator[T]) Count() int { @@ -21,7 +24,7 @@ func (i *LevelDBIterator[T]) Count() int { return n } -func (i *LevelDBIterator[T]) Filter(f FilterFunc[T]) Iterator[T] { +func (i *LevelDBIterator[T]) Filter(f ezdb.FilterFunc[T]) ezdb.Iterator[T] { m := map[string]T{} if i.First() { @@ -39,7 +42,7 @@ func (i *LevelDBIterator[T]) Filter(f FilterFunc[T]) Iterator[T] { m[key] = value } - return MemoryIter(m, nil, i) + return ezdb.MemoryIter(m, nil, i) } func (i *LevelDBIterator[T]) First() bool { @@ -107,15 +110,15 @@ func (i *LevelDBIterator[T]) Release() { i.i.Release() } -func (i *LevelDBIterator[T]) Sort(f SortFunc[T]) Iterator[T] { +func (i *LevelDBIterator[T]) Sort(f ezdb.SortFunc[T]) ezdb.Iterator[T] { all, _ := i.GetAll() - m := MemoryIter(all, nil, i) + m := ezdb.MemoryIter(all, nil, i) return m.Sort(f) } -func (i *LevelDBIterator[T]) SortKeys(f SortFunc[string]) Iterator[T] { +func (i *LevelDBIterator[T]) SortKeys(f ezdb.SortFunc[string]) ezdb.Iterator[T] { all, _ := i.GetAll() - m := MemoryIter(all, nil, i) + m := ezdb.MemoryIter(all, nil, i) return m.SortKeys(f) } diff --git a/leveldb_options.go b/ezleveldb/leveldb_options.go similarity index 96% rename from leveldb_options.go rename to ezleveldb/leveldb_options.go index df9e143..9f15c98 100644 --- a/leveldb_options.go +++ b/ezleveldb/leveldb_options.go @@ -1,4 +1,4 @@ -package ezdb +package ezleveldb import "github.com/syndtr/goleveldb/leveldb/opt" diff --git a/leveldb_test.go b/ezleveldb/leveldb_test.go similarity index 86% rename from leveldb_test.go rename to ezleveldb/leveldb_test.go index 91fc36e..2b990fa 100644 --- a/leveldb_test.go +++ b/ezleveldb/leveldb_test.go @@ -1,8 +1,10 @@ -package ezdb +package ezleveldb import ( "fmt" "testing" + + "github.com/annybs/ezdb" ) func TestLevelDB(t *testing.T) { @@ -13,11 +15,11 @@ func TestLevelDB(t *testing.T) { path := ".leveldb/test_json" - marshaler := JSON(func() *Student { + marshaler := ezdb.JSON(func() *Student { return &Student{} }) - collection := LevelDB(path, marshaler, nil) + collection := ezdb.LevelDB(path, marshaler, nil) cmp := func(expected, actual *Student) error { if expected == nil && actual != nil { @@ -33,7 +35,7 @@ func TestLevelDB(t *testing.T) { return nil } - tester := &CollectionTester[*Student]{ + tester := &ezdb.CollectionTester[*Student]{ C: collection, Cmp: cmp, @@ -54,6 +56,10 @@ func TestLevelDB(t *testing.T) { } } + destroy := func() error { + return collection.Destroy() + } + sequence := []func(){ named("init 1", tester.Init), named("has", tester.Has), @@ -69,6 +75,7 @@ func TestLevelDB(t *testing.T) { named("iterSortKeys", tester.IterSortKeys), named("deleteAll", tester.DeleteAll), named("close", tester.Close), + named("destroy", destroy), } for _, do := range sequence { do()