Skip to content

Commit

Permalink
Return error from func used within item.Value(...) so a user can pass…
Browse files Browse the repository at this point in the history
… back any parsing errors.
  • Loading branch information
manishrjain committed Oct 4, 2018
1 parent 1cb0607 commit 439fd46
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 19 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ err := db.View(func(txn *badger.Txn) error {
handle(err)

var valNot, valCopy []byte
err := item.Value(func(val []byte) {
err := item.Value(func(val []byte) error {
// This func with val would only be called if item.Value encounters no error.

// Accessing val here is valid.
Expand All @@ -228,6 +228,7 @@ err := db.View(func(txn *badger.Txn) error {

// Assigning val slice to another variable is NOT OK.
valNot = val // Do not do this.
return nil
})
handle(err)

Expand Down Expand Up @@ -349,8 +350,9 @@ err := db.View(func(txn *badger.Txn) error {
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
k := item.Key()
err := item.Value(func(v []byte) {
err := item.Value(func(v []byte) error {
fmt.Printf("key=%s, value=%s\n", k, v)
return nil
})
if err != nil {
return err
Expand Down Expand Up @@ -380,8 +382,9 @@ db.View(func(txn *badger.Txn) error {
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
item := it.Item()
k := item.Key()
err := item.Value(func(v []byte) {
err := item.Value(func(v []byte) error {
fmt.Printf("key=%s, value=%s\n", k, v)
return nil
})
if err != nil {
return err
Expand Down
6 changes: 4 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1075,8 +1075,9 @@ func (seq *Sequence) updateLease() error {
return err
} else {
var num uint64
if err := item.Value(func(v []byte) {
if err := item.Value(func(v []byte) error {
num = binary.BigEndian.Uint64(v)
return nil
}); err != nil {
return err
}
Expand Down Expand Up @@ -1188,8 +1189,9 @@ func (op *MergeOperator) iterateAndMerge(txn *Txn) (val []byte, err error) {
return nil, err
}
} else {
if err := item.Value(func(newVal []byte) {
if err := item.Value(func(newVal []byte) error {
val = op.f(val, newVal)
return nil
}); err != nil {
return nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions db2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,11 @@ func TestBigKeyValuePairs(t *testing.T) {
if err != nil {
return err
}
return item.Value(func(val []byte) {
return item.Value(func(val []byte) error {
if len(val) == 0 {
log.Fatalf("key not found %q", len(key))
}
return nil
})
})
}
Expand Down Expand Up @@ -311,8 +312,9 @@ func TestPushValueLogLimit(t *testing.T) {
err := db.View(func(txn *Txn) error {
item, err := txn.Get([]byte(key(i)))
require.NoError(t, err, "Getting key: %s", key(i))
err = item.Value(func(v []byte) {
err = item.Value(func(v []byte) error {
_ = v
return nil
})
require.NoError(t, err, "Getting value: %s", key(i))
return nil
Expand Down
18 changes: 12 additions & 6 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ func getTestOptions(dir string) Options {

func getItemValue(t *testing.T, item *Item) (val []byte) {
var v []byte
err := item.Value(func(val []byte) {
err := item.Value(func(val []byte) error {
if val == nil {
v = nil
} else {
v = append([]byte{}, val...)
}
return nil
})
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -134,10 +135,11 @@ func TestUpdateAndView(t *testing.T) {
}

expected := []byte(fmt.Sprintf("val%d", i))
if err := item.Value(func(val []byte) {
if err := item.Value(func(val []byte) error {
require.Equal(t, expected, val,
"Invalid value for key %q. expected: %q, actual: %q",
item.Key(), expected, val)
return nil
}); err != nil {
return err
}
Expand Down Expand Up @@ -676,8 +678,9 @@ func TestIterateParallel(t *testing.T) {
count++
item := itr.Item()
require.Equal(t, "account-", string(item.Key()[0:8]))
err := item.Value(func(val []byte) {
err := item.Value(func(val []byte) error {
require.Equal(t, "1000", string(val))
return nil
})
require.NoError(t, err)
}
Expand Down Expand Up @@ -788,8 +791,9 @@ func TestInvalidKey(t *testing.T) {
if err != nil {
return err
}
require.NoError(t, item.Value(func(val []byte) {
require.NoError(t, item.Value(func(val []byte) error {
require.Equal(t, []byte("BadgerDB"), val)
return nil
}))
return nil
}))
Expand Down Expand Up @@ -1233,8 +1237,9 @@ func TestSequence(t *testing.T) {
return err
}
var num0 uint64
if err := item.Value(func(val []byte) {
if err := item.Value(func(val []byte) error {
num0 = binary.BigEndian.Uint64(val)
return nil
}); err != nil {
return err
}
Expand All @@ -1245,8 +1250,9 @@ func TestSequence(t *testing.T) {
return err
}
var num1 uint64
if err := item.Value(func(val []byte) {
if err := item.Value(func(val []byte) error {
num1 = binary.BigEndian.Uint64(val)
return nil
}); err != nil {
return err
}
Expand Down
11 changes: 7 additions & 4 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (item *Item) Version() uint64 {
// If you need to use a value outside a transaction, please use Item.ValueCopy
// instead, or copy it yourself. Value might change once discard or commit is called.
// Use ValueCopy if you want to do a Set after Get.
func (item *Item) Value(fn func(val []byte)) error {
func (item *Item) Value(fn func(val []byte) error) error {
item.wg.Wait()
if item.status == prefetched {
if item.err == nil && fn != nil {
Expand All @@ -104,10 +104,13 @@ func (item *Item) Value(fn func(val []byte)) error {
}
buf, cb, err := item.yieldItemValue()
defer runCallback(cb)
if err == nil && fn != nil {
fn(buf)
if err != nil {
return err
}
return err
if fn != nil {
return fn(buf)
}
return nil
}

// ValueCopy returns a copy of the value of the item from the value log, writing it to dst slice.
Expand Down
3 changes: 2 additions & 1 deletion transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ func TestTxnSimple(t *testing.T) {
item, err := txn.Get([]byte("key=8"))
require.NoError(t, err)

require.NoError(t, item.Value(func(val []byte) {
require.NoError(t, item.Value(func(val []byte) error {
require.Equal(t, []byte("val=8"), val)
return nil
}))

require.Panics(t, func() { txn.CommitAt(100, nil) })
Expand Down
3 changes: 2 additions & 1 deletion value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,10 +718,11 @@ func TestBug578(t *testing.T) {
if err != nil {
return err
}
if err := item.Value(func(val []byte) {
if err := item.Value(func(val []byte) error {
if !bytes.Equal(val, value) {
t.Fatalf("Invalid value for key: %q", key(i))
}
return nil
}); err != nil {
return err
}
Expand Down

0 comments on commit 439fd46

Please sign in to comment.