Quellcode durchsuchen

move leveldb to sub-package

Aneurin Barker Snook vor 11 Monaten
Ursprung
Commit
3eea26f1e7
7 geänderte Dateien mit 78 neuen und 21 gelöschten Zeilen
  1. 5 2
      README.md
  2. 10 0
      ezleveldb/go.mod
  3. 33 0
      ezleveldb/go.sum
  4. 6 5
      ezleveldb/leveldb.go
  5. 12 9
      ezleveldb/leveldb_iter.go
  6. 1 1
      ezleveldb/leveldb_options.go
  7. 11 4
      ezleveldb/leveldb_test.go

+ 5 - 2
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)

+ 10 - 0
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

+ 33 - 0
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=

+ 6 - 5
leveldb.go → 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,
 

+ 12 - 9
leveldb_iter.go → 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)
 }
 

+ 1 - 1
leveldb_options.go → ezleveldb/leveldb_options.go

@@ -1,4 +1,4 @@
-package ezdb
+package ezleveldb
 
 import "github.com/syndtr/goleveldb/leveldb/opt"
 

+ 11 - 4
leveldb_test.go → 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()