-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support conditions of Put/Get/Delete (#10)
* fix: conditions * feat: MGet MPut MDelete * feat: rename to filters * feat: new condition * test: condition * feat: HasBeforePut * feat: BeforePut
- Loading branch information
Showing
8 changed files
with
457 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package boltutil | ||
|
||
import ( | ||
"bytes" | ||
) | ||
|
||
type Filter struct { | ||
min, max []byte | ||
prefix []byte | ||
filters []func(k, v []byte) (skip bool, stop bool) | ||
storableFilters []func(obj Storable) (skip bool, stop bool) | ||
} | ||
|
||
func NewFilter() *Filter { | ||
return &Filter{} | ||
} | ||
|
||
func (c *Filter) SetRange(min, max []byte) *Filter { | ||
c.min = min | ||
c.max = max | ||
return c | ||
} | ||
|
||
func (c *Filter) SetPrefix(prefix []byte) *Filter { | ||
c.min = prefix | ||
c.max = make([]byte, len(prefix)) | ||
copy(c.max, c.max) | ||
for i := len(c.max) - 1; i >= 0; i-- { | ||
if c.max[i] < 0xff { | ||
c.max[i]++ | ||
break | ||
} | ||
} | ||
return c | ||
} | ||
|
||
func (c *Filter) AddCondition(f func(k, v []byte) (skip bool, stop bool)) *Filter { | ||
c.filters = append(c.filters, f) | ||
return c | ||
} | ||
|
||
func (c *Filter) AddStorableCondition(f func(obj Storable) (skip bool, stop bool)) *Filter { | ||
c.storableFilters = append(c.storableFilters, f) | ||
return c | ||
} | ||
|
||
func (c *Filter) seek() []byte { | ||
if c == nil { | ||
return nil | ||
} | ||
if c.prefix != nil && bytes.Compare(c.prefix, c.min) > 0 { | ||
return c.prefix | ||
} | ||
return c.min | ||
} | ||
|
||
func (c *Filter) goon(k []byte) bool { | ||
if k == nil { | ||
return false | ||
} | ||
if c == nil { | ||
return true | ||
} | ||
if len(c.max) > 0 && bytes.Compare(k, c.max) > 0 { | ||
return false | ||
} | ||
if len(c.prefix) > 0 && !bytes.HasPrefix(k, c.prefix) { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func (c *Filter) getConditions() []func(k, v []byte) (skip bool, stop bool) { | ||
if c == nil { | ||
return nil | ||
} | ||
return c.filters | ||
} | ||
|
||
func (c *Filter) getStorableConditions() []func(obj Storable) (skip bool, stop bool) { | ||
if c == nil { | ||
return nil | ||
} | ||
return c.storableFilters | ||
} | ||
|
||
type Condition struct { | ||
ignoreIfExist bool // for Put | ||
failIfExist bool // for Put | ||
|
||
ignoreIfNotExist bool // for Get | ||
failIfNotExist bool // for Put, Delete | ||
} | ||
|
||
func NewCondition() *Condition { | ||
return &Condition{} | ||
} | ||
|
||
func (c *Condition) IgnoreIfExist(v ...bool) *Condition { | ||
if len(v) == 0 { | ||
c.ignoreIfExist = true | ||
} else { | ||
c.ignoreIfExist = v[0] | ||
} | ||
return c | ||
} | ||
|
||
func (c *Condition) FailIfExist(v ...bool) *Condition { | ||
if len(v) == 0 { | ||
c.failIfExist = true | ||
} else { | ||
c.failIfExist = v[0] | ||
} | ||
return c | ||
} | ||
|
||
func (c *Condition) IgnoreIfNotExist(v ...bool) *Condition { | ||
if len(v) == 0 { | ||
c.ignoreIfNotExist = true | ||
} else { | ||
c.ignoreIfNotExist = v[0] | ||
} | ||
return c | ||
} | ||
|
||
func (c *Condition) FailIfNotExist(v ...bool) *Condition { | ||
if len(v) == 0 { | ||
c.failIfNotExist = true | ||
} else { | ||
c.failIfNotExist = v[0] | ||
} | ||
return c | ||
} | ||
|
||
func (c *Condition) getIgnoreIfExist() bool { | ||
if c == nil { | ||
return false | ||
} | ||
return c.ignoreIfExist | ||
} | ||
|
||
func (c *Condition) getFailIfExist() bool { | ||
if c == nil { | ||
return false | ||
} | ||
return c.failIfExist | ||
} | ||
|
||
func (c *Condition) getIgnoreIfNotExist() bool { | ||
if c == nil { | ||
return false | ||
} | ||
return c.ignoreIfNotExist | ||
} | ||
|
||
func (c *Condition) getFailIfNotExist() bool { | ||
if c == nil { | ||
return false | ||
} | ||
return c.failIfNotExist | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package boltutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCondition(t *testing.T) { | ||
t.Run("ignoreIfExist Put", func(t *testing.T) { | ||
db := testDB(t, true) | ||
defer db.Close() | ||
person := &Person{ | ||
Id: "id1", | ||
Name: "name1", | ||
} | ||
assert.NoError(t, db.Put(person)) | ||
|
||
person.Name = "name2" | ||
assert.NoError(t, db.Put(person, NewCondition().IgnoreIfExist(true))) | ||
require.NoError(t, db.Get(person)) | ||
assert.Equal(t, "name1", person.Name) | ||
|
||
person.Name = "name3" | ||
assert.NoError(t, db.Put(person)) | ||
require.NoError(t, db.Get(person)) | ||
assert.Equal(t, "name3", person.Name) | ||
|
||
person.Name = "name4" | ||
assert.NoError(t, db.Put(person, NewCondition().IgnoreIfExist(false))) | ||
require.NoError(t, db.Get(person)) | ||
assert.Equal(t, "name4", person.Name) | ||
}) | ||
|
||
t.Run("failIfExist Put", func(t *testing.T) { | ||
db := testDB(t, true) | ||
defer db.Close() | ||
person := &Person{ | ||
Id: "id1", | ||
Name: "name1", | ||
} | ||
assert.NoError(t, db.Put(person)) | ||
|
||
person.Name = "name2" | ||
assert.Error(t, db.Put(person, NewCondition().FailIfExist(true))) | ||
require.NoError(t, db.Get(person)) | ||
assert.Equal(t, "name1", person.Name) | ||
|
||
person.Name = "name3" | ||
assert.NoError(t, db.Put(person)) | ||
require.NoError(t, db.Get(person)) | ||
assert.Equal(t, "name3", person.Name) | ||
|
||
person.Name = "name4" | ||
assert.NoError(t, db.Put(person, NewCondition().FailIfExist(false))) | ||
require.NoError(t, db.Get(person)) | ||
assert.Equal(t, "name4", person.Name) | ||
}) | ||
|
||
t.Run("ignoreIfNotExist Get", func(t *testing.T) { | ||
db := testDB(t, true) | ||
defer db.Close() | ||
person := &Person{ | ||
Id: "id1", | ||
Name: "name1", | ||
} | ||
|
||
assert.NoError(t, db.Get(person, NewCondition().IgnoreIfNotExist(true))) | ||
|
||
assert.Error(t, db.Get(person)) | ||
|
||
assert.Error(t, db.Get(person, NewCondition().IgnoreIfNotExist(false))) | ||
}) | ||
|
||
t.Run("failIfNotExist Put", func(t *testing.T) { | ||
db := testDB(t, true) | ||
defer db.Close() | ||
person := &Person{ | ||
Id: "id1", | ||
Name: "name1", | ||
} | ||
|
||
assert.Error(t, db.Put(person, NewCondition().FailIfNotExist(true))) | ||
require.Error(t, db.Get(person)) | ||
|
||
assert.NoError(t, db.Put(person)) | ||
require.NoError(t, db.Get(person)) | ||
}) | ||
|
||
t.Run("failIfNotExist Delete", func(t *testing.T) { | ||
db := testDB(t, true) | ||
defer db.Close() | ||
person := &Person{ | ||
Id: "id1", | ||
Name: "name1", | ||
} | ||
|
||
assert.Error(t, db.Delete(person, NewCondition().FailIfNotExist(true))) | ||
|
||
assert.NoError(t, db.Delete(person)) | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.