@@ -6,22 +6,20 @@ import (
66 "errors"
77
88 "github.com/cockroachdb/pebble/v2"
9- "github.com/cockroachdb/pebble/v2/vfs"
109 "github.com/omalloc/tavern/api/defined/v1/storage"
11- "github.com/omalloc/tavern/contrib/log"
1210)
1311
14- var _ storage.SharedKV = (* memSharedKV )(nil )
12+ var _ storage.SharedKV = (* noneSharedKV )(nil )
1513
16- type memSharedKV struct {
14+ type noneSharedKV struct {
1715 db * pebble.DB
1816}
1917
20- func (r * memSharedKV ) Close () error {
18+ func (r * noneSharedKV ) Close () error {
2119 return r .db .Close ()
2220}
2321
24- func (r * memSharedKV ) Get (_ context.Context , key []byte ) ([]byte , error ) {
22+ func (r * noneSharedKV ) Get (_ context.Context , key []byte ) ([]byte , error ) {
2523 val , c , err := r .db .Get (key )
2624 if err != nil {
2725 if errors .Is (err , pebble .ErrNotFound ) {
@@ -35,11 +33,11 @@ func (r *memSharedKV) Get(_ context.Context, key []byte) ([]byte, error) {
3533 return val , nil
3634}
3735
38- func (r * memSharedKV ) Set (_ context.Context , key []byte , val []byte ) error {
36+ func (r * noneSharedKV ) Set (_ context.Context , key []byte , val []byte ) error {
3937 return r .db .Set (key , val , pebble .NoSync )
4038}
4139
42- func (r * memSharedKV ) Incr (_ context.Context , key []byte , delta uint32 ) (uint32 , error ) {
40+ func (r * noneSharedKV ) Incr (_ context.Context , key []byte , delta uint32 ) (uint32 , error ) {
4341 batch := r .db .NewIndexedBatch ()
4442 defer func () { _ = batch .Close () }()
4543
@@ -70,7 +68,7 @@ func (r *memSharedKV) Incr(_ context.Context, key []byte, delta uint32) (uint32,
7068 return counter , nil
7169}
7270
73- func (r * memSharedKV ) Decr (_ context.Context , key []byte , delta uint32 ) (uint32 , error ) {
71+ func (r * noneSharedKV ) Decr (_ context.Context , key []byte , delta uint32 ) (uint32 , error ) {
7472 batch := r .db .NewIndexedBatch ()
7573 defer func () { _ = batch .Close () }()
7674
@@ -105,7 +103,7 @@ func (r *memSharedKV) Decr(_ context.Context, key []byte, delta uint32) (uint32,
105103 return counter , nil
106104}
107105
108- func (r * memSharedKV ) GetCounter (_ context.Context , key []byte ) (uint32 , error ) {
106+ func (r * noneSharedKV ) GetCounter (_ context.Context , key []byte ) (uint32 , error ) {
109107 val , closer , err := r .db .Get (key )
110108 if err != nil {
111109 return 0 , err
@@ -115,19 +113,19 @@ func (r *memSharedKV) GetCounter(_ context.Context, key []byte) (uint32, error)
115113 return binary .BigEndian .Uint32 (val ), nil
116114}
117115
118- func (r * memSharedKV ) Delete (_ context.Context , key []byte ) error {
116+ func (r * noneSharedKV ) Delete (_ context.Context , key []byte ) error {
119117 return r .db .Delete (key , pebble .NoSync )
120118}
121119
122- func (r * memSharedKV ) DropPrefix (ctx context.Context , prefix []byte ) error {
120+ func (r * noneSharedKV ) DropPrefix (ctx context.Context , prefix []byte ) error {
123121 end := make ([]byte , len (prefix ))
124122 copy (end , prefix )
125123 end [len (end )- 1 ]++
126124
127125 return r .db .DeleteRange (prefix , end , pebble .NoSync )
128126}
129127
130- func (r * memSharedKV ) Iterate (ctx context.Context , f func (key []byte , val []byte ) error ) error {
128+ func (r * noneSharedKV ) Iterate (ctx context.Context , f func (key []byte , val []byte ) error ) error {
131129 iter , err := r .db .NewIterWithContext (ctx , & pebble.IterOptions {})
132130 if err != nil {
133131 return err
@@ -148,7 +146,7 @@ func (r *memSharedKV) Iterate(ctx context.Context, f func(key []byte, val []byte
148146 return nil
149147}
150148
151- func (r * memSharedKV ) IteratePrefix (ctx context.Context , prefix []byte , f func (key []byte , val []byte ) error ) error {
149+ func (r * noneSharedKV ) IteratePrefix (ctx context.Context , prefix []byte , f func (key []byte , val []byte ) error ) error {
152150 iter , err := r .db .NewIterWithContext (ctx , & pebble.IterOptions {
153151 LowerBound : prefix ,
154152 UpperBound : keyUpperBound (prefix ),
@@ -184,17 +182,14 @@ func keyUpperBound(b []byte) []byte {
184182 return nil // no upper-bound
185183}
186184
187- func NewMemSharedKV () storage.SharedKV {
188- db , err := pebble .Open ("" , & pebble.Options {
189- FS : vfs .NewMem (),
190- Logger : log .NewHelper (log .NewFilter (log .GetLogger (), log .FilterLevel (log .LevelWarn ))),
191- })
185+ func newNoneKV (storePath string , opts * pebble.Options ) (storage.SharedKV , error ) {
186+ db , err := pebble .Open (storePath , opts )
192187 if err != nil {
193- panic ( err )
188+ return nil , err
194189 }
195190
196- r := & memSharedKV {
191+ r := & noneSharedKV {
197192 db : db ,
198193 }
199- return r
194+ return r , nil
200195}
0 commit comments