Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Commit

Permalink
chore: package prefixdb (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinsan-line authored Mar 29, 2021
1 parent f246ff6 commit a887b44
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 73 deletions.
10 changes: 2 additions & 8 deletions goleveldb/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,15 @@ func (itr *goLevelDBIterator) Key() []byte {
// Key returns a copy of the current key.
// See https://github.com/syndtr/goleveldb/blob/52c212e6c196a1404ea59592d3f1c227c9f034b2/leveldb/iterator/iter.go#L88
itr.assertIsValid()
return cp(itr.source.Key())
return tmdb.Cp(itr.source.Key())
}

// Value implements Iterator.
func (itr *goLevelDBIterator) Value() []byte {
// Value returns a copy of the current value.
// See https://github.com/syndtr/goleveldb/blob/52c212e6c196a1404ea59592d3f1c227c9f034b2/leveldb/iterator/iter.go#L88
itr.assertIsValid()
return cp(itr.source.Value())
}

func cp(bz []byte) (ret []byte) {
ret = make([]byte, len(bz))
copy(ret, bz)
return ret
return tmdb.Cp(itr.source.Value())
}

// Next implements Iterator.
Expand Down
3 changes: 2 additions & 1 deletion metadb/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/line/tm-db/v2/goleveldb"
"github.com/line/tm-db/v2/internal/dbtest"
"github.com/line/tm-db/v2/memdb"
"github.com/line/tm-db/v2/prefixdb"
"github.com/line/tm-db/v2/rocksdb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -27,7 +28,7 @@ func init() {
mdb.Set([]byte("test"), []byte{0})
mdb.Set([]byte("u"), []byte{21})
mdb.Set([]byte("z"), []byte{26})
return tmdb.NewPrefixDB(mdb, []byte("test/")), nil
return prefixdb.NewDB(mdb, []byte("test/")), nil
}, false)
}

Expand Down
11 changes: 6 additions & 5 deletions metadb/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

tmdb "github.com/line/tm-db/v2"
"github.com/line/tm-db/v2/internal/dbtest"
"github.com/line/tm-db/v2/prefixdb"
)

// Empty iterator for empty db.
Expand All @@ -18,7 +19,7 @@ func TestPrefixIteratorNoMatchNil(t *testing.T) {
t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
db, dir := newTempDB(t, backend)
defer os.RemoveAll(dir)
itr, err := tmdb.IteratePrefix(db, []byte("2"))
itr, err := prefixdb.IteratePrefix(db, []byte("2"))
require.NoError(t, err)

dbtest.Invalid(t, itr)
Expand All @@ -37,7 +38,7 @@ func TestPrefixIteratorNoMatch1(t *testing.T) {
t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
db, dir := newTempDB(t, backend)
defer os.RemoveAll(dir)
itr, err := tmdb.IteratePrefix(db, []byte("2"))
itr, err := prefixdb.IteratePrefix(db, []byte("2"))
require.NoError(t, err)
err = db.SetSync([]byte("1"), []byte("value_1"))
require.NoError(t, err)
Expand All @@ -55,7 +56,7 @@ func TestPrefixIteratorNoMatch2(t *testing.T) {
defer os.RemoveAll(dir)
err := db.SetSync([]byte("3"), []byte("value_3"))
require.NoError(t, err)
itr, err := tmdb.IteratePrefix(db, []byte("4"))
itr, err := prefixdb.IteratePrefix(db, []byte("4"))
require.NoError(t, err)

dbtest.Invalid(t, itr)
Expand All @@ -71,7 +72,7 @@ func TestPrefixIteratorMatch1(t *testing.T) {
defer os.RemoveAll(dir)
err := db.SetSync([]byte("2"), []byte("value_2"))
require.NoError(t, err)
itr, err := tmdb.IteratePrefix(db, []byte("2"))
itr, err := prefixdb.IteratePrefix(db, []byte("2"))
require.NoError(t, err)

dbtest.Valid(t, itr, true)
Expand Down Expand Up @@ -106,7 +107,7 @@ func TestPrefixIteratorMatches1N(t *testing.T) {
require.NoError(t, err)
err = db.SetSync([]byte("abcdefg"), []byte("value_3"))
require.NoError(t, err)
itr, err := tmdb.IteratePrefix(db, []byte("a/"))
itr, err := prefixdb.IteratePrefix(db, []byte("a/"))
require.NoError(t, err)

dbtest.Valid(t, itr, true)
Expand Down
22 changes: 13 additions & 9 deletions prefixdb_batch.go → prefixdb/batch.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package db
package prefixdb

import (
tmdb "github.com/line/tm-db/v2"
)

type prefixDBBatch struct {
prefix []byte
source Batch
source tmdb.Batch
}

var _ Batch = (*prefixDBBatch)(nil)
var _ tmdb.Batch = (*prefixDBBatch)(nil)

func newPrefixBatch(prefix []byte, source Batch) prefixDBBatch {
func newPrefixBatch(prefix []byte, source tmdb.Batch) prefixDBBatch {
return prefixDBBatch{
prefix: prefix,
source: source,
Expand All @@ -17,21 +21,21 @@ func newPrefixBatch(prefix []byte, source Batch) prefixDBBatch {
// Set implements Batch.
func (pb prefixDBBatch) Set(key, value []byte) error {
if len(key) == 0 {
return ErrKeyEmpty
return tmdb.ErrKeyEmpty
}
if value == nil {
return ErrValueNil
return tmdb.ErrValueNil
}
pkey := concat(pb.prefix, key)
pkey := tmdb.Concat(pb.prefix, key)
return pb.source.Set(pkey, value)
}

// Delete implements Batch.
func (pb prefixDBBatch) Delete(key []byte) error {
if len(key) == 0 {
return ErrKeyEmpty
return tmdb.ErrKeyEmpty
}
pkey := concat(pb.prefix, key)
pkey := tmdb.Concat(pb.prefix, key)
return pb.source.Delete(pkey)
}

Expand Down
50 changes: 26 additions & 24 deletions prefixdb.go → prefixdb/db.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package db
package prefixdb

import (
"fmt"
"sync"

tmdb "github.com/line/tm-db/v2"
)

// PrefixDB wraps a namespace of another database as a logical database.
type PrefixDB struct {
mtx sync.Mutex
prefix []byte
db DB
db tmdb.DB
}

var _ DB = (*PrefixDB)(nil)
var _ tmdb.DB = (*PrefixDB)(nil)

// NewPrefixDB lets you namespace multiple DBs within a single DB.
func NewPrefixDB(db DB, prefix []byte) *PrefixDB {
func NewDB(db tmdb.DB, prefix []byte) *PrefixDB {
return &PrefixDB{
prefix: prefix,
db: db,
Expand All @@ -25,7 +27,7 @@ func NewPrefixDB(db DB, prefix []byte) *PrefixDB {
// Get implements DB.
func (pdb *PrefixDB) Get(key []byte) ([]byte, error) {
if len(key) == 0 {
return nil, ErrKeyEmpty
return nil, tmdb.ErrKeyEmpty
}
pdb.mtx.Lock()
defer pdb.mtx.Unlock()
Expand All @@ -41,7 +43,7 @@ func (pdb *PrefixDB) Get(key []byte) ([]byte, error) {
// Has implements DB.
func (pdb *PrefixDB) Has(key []byte) (bool, error) {
if len(key) == 0 {
return false, ErrKeyEmpty
return false, tmdb.ErrKeyEmpty
}
pdb.mtx.Lock()
defer pdb.mtx.Unlock()
Expand All @@ -57,10 +59,10 @@ func (pdb *PrefixDB) Has(key []byte) (bool, error) {
// Set implements DB.
func (pdb *PrefixDB) Set(key []byte, value []byte) error {
if len(key) == 0 {
return ErrKeyEmpty
return tmdb.ErrKeyEmpty
}
if value == nil {
return ErrValueNil
return tmdb.ErrValueNil
}
pdb.mtx.Lock()
defer pdb.mtx.Unlock()
Expand All @@ -75,10 +77,10 @@ func (pdb *PrefixDB) Set(key []byte, value []byte) error {
// SetSync implements DB.
func (pdb *PrefixDB) SetSync(key []byte, value []byte) error {
if len(key) == 0 {
return ErrKeyEmpty
return tmdb.ErrKeyEmpty
}
if value == nil {
return ErrValueNil
return tmdb.ErrValueNil
}
pdb.mtx.Lock()
defer pdb.mtx.Unlock()
Expand All @@ -89,7 +91,7 @@ func (pdb *PrefixDB) SetSync(key []byte, value []byte) error {
// Delete implements DB.
func (pdb *PrefixDB) Delete(key []byte) error {
if len(key) == 0 {
return ErrKeyEmpty
return tmdb.ErrKeyEmpty
}
pdb.mtx.Lock()
defer pdb.mtx.Unlock()
Expand All @@ -100,7 +102,7 @@ func (pdb *PrefixDB) Delete(key []byte) error {
// DeleteSync implements DB.
func (pdb *PrefixDB) DeleteSync(key []byte) error {
if len(key) == 0 {
return ErrKeyEmpty
return tmdb.ErrKeyEmpty
}
pdb.mtx.Lock()
defer pdb.mtx.Unlock()
Expand All @@ -109,19 +111,19 @@ func (pdb *PrefixDB) DeleteSync(key []byte) error {
}

// Iterator implements DB.
func (pdb *PrefixDB) Iterator(start, end []byte) (Iterator, error) {
func (pdb *PrefixDB) Iterator(start, end []byte) (tmdb.Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, ErrKeyEmpty
return nil, tmdb.ErrKeyEmpty
}
pdb.mtx.Lock()
defer pdb.mtx.Unlock()

var pstart, pend []byte
pstart = concat(pdb.prefix, start)
pstart = tmdb.Concat(pdb.prefix, start)
if end == nil {
pend = cpIncr(pdb.prefix)
pend = tmdb.CpIncr(pdb.prefix)
} else {
pend = concat(pdb.prefix, end)
pend = tmdb.Concat(pdb.prefix, end)
}
itr, err := pdb.db.Iterator(pstart, pend)
if err != nil {
Expand All @@ -132,19 +134,19 @@ func (pdb *PrefixDB) Iterator(start, end []byte) (Iterator, error) {
}

// ReverseIterator implements DB.
func (pdb *PrefixDB) ReverseIterator(start, end []byte) (Iterator, error) {
func (pdb *PrefixDB) ReverseIterator(start, end []byte) (tmdb.Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, ErrKeyEmpty
return nil, tmdb.ErrKeyEmpty
}
pdb.mtx.Lock()
defer pdb.mtx.Unlock()

var pstart, pend []byte
pstart = concat(pdb.prefix, start)
pstart = tmdb.Concat(pdb.prefix, start)
if end == nil {
pend = cpIncr(pdb.prefix)
pend = tmdb.CpIncr(pdb.prefix)
} else {
pend = concat(pdb.prefix, end)
pend = tmdb.Concat(pdb.prefix, end)
}
ritr, err := pdb.db.ReverseIterator(pstart, pend)
if err != nil {
Expand All @@ -155,7 +157,7 @@ func (pdb *PrefixDB) ReverseIterator(start, end []byte) (Iterator, error) {
}

// NewBatch implements DB.
func (pdb *PrefixDB) NewBatch() Batch {
func (pdb *PrefixDB) NewBatch() tmdb.Batch {
pdb.mtx.Lock()
defer pdb.mtx.Unlock()

Expand Down Expand Up @@ -200,5 +202,5 @@ func (pdb *PrefixDB) Stats() map[string]string {
}

func (pdb *PrefixDB) prefixed(key []byte) []byte {
return concat(pdb.prefix, key)
return tmdb.Concat(pdb.prefix, key)
}
15 changes: 8 additions & 7 deletions prefixdb_test.go → prefixdb/db_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package db_test
package prefixdb_test

import (
"testing"

tmdb "github.com/line/tm-db/v2"
"github.com/line/tm-db/v2/internal/dbtest"
"github.com/line/tm-db/v2/memdb"
"github.com/line/tm-db/v2/prefixdb"
"github.com/stretchr/testify/require"
)

Expand All @@ -25,7 +26,7 @@ func mockDBWithStuff(t *testing.T) tmdb.DB {

func TestPrefixDBSimple(t *testing.T) {
db := mockDBWithStuff(t)
pdb := tmdb.NewPrefixDB(db, []byte("key"))
pdb := prefixdb.NewDB(db, []byte("key"))

dbtest.Value(t, pdb, []byte("key"), nil)
dbtest.Value(t, pdb, []byte("key1"), nil)
Expand All @@ -42,7 +43,7 @@ func TestPrefixDBSimple(t *testing.T) {

func TestPrefixDBIterator1(t *testing.T) {
db := mockDBWithStuff(t)
pdb := tmdb.NewPrefixDB(db, []byte("key"))
pdb := prefixdb.NewDB(db, []byte("key"))

itr, err := pdb.Iterator(nil, nil)
require.NoError(t, err)
Expand All @@ -59,7 +60,7 @@ func TestPrefixDBIterator1(t *testing.T) {

func TestPrefixDBReverseIterator1(t *testing.T) {
db := mockDBWithStuff(t)
pdb := tmdb.NewPrefixDB(db, []byte("key"))
pdb := prefixdb.NewDB(db, []byte("key"))

itr, err := pdb.ReverseIterator(nil, nil)
require.NoError(t, err)
Expand All @@ -76,7 +77,7 @@ func TestPrefixDBReverseIterator1(t *testing.T) {

func TestPrefixDBReverseIterator5(t *testing.T) {
db := mockDBWithStuff(t)
pdb := tmdb.NewPrefixDB(db, []byte("key"))
pdb := prefixdb.NewDB(db, []byte("key"))

itr, err := pdb.ReverseIterator([]byte("1"), nil)
require.NoError(t, err)
Expand All @@ -93,7 +94,7 @@ func TestPrefixDBReverseIterator5(t *testing.T) {

func TestPrefixDBReverseIterator6(t *testing.T) {
db := mockDBWithStuff(t)
pdb := tmdb.NewPrefixDB(db, []byte("key"))
pdb := prefixdb.NewDB(db, []byte("key"))

itr, err := pdb.ReverseIterator([]byte("2"), nil)
require.NoError(t, err)
Expand All @@ -108,7 +109,7 @@ func TestPrefixDBReverseIterator6(t *testing.T) {

func TestPrefixDBReverseIterator7(t *testing.T) {
db := mockDBWithStuff(t)
pdb := tmdb.NewPrefixDB(db, []byte("key"))
pdb := prefixdb.NewDB(db, []byte("key"))

itr, err := pdb.ReverseIterator(nil, []byte("2"))
require.NoError(t, err)
Expand Down
Loading

0 comments on commit a887b44

Please sign in to comment.