Skip to content

Commit

Permalink
Breaking Change: Introduce new APIs for Entry creation/modification
Browse files Browse the repository at this point in the history
* Add functions to create, modify Entry

Currently no functions are exposed to create and modify Entry, to
be set in a transaction. Functions Set, SetWithMeta, SetWithDiscard,
SetWithTTL, all are part of Txn struct. These functions internally
create an entry and then call txn.SetEntry. All of Set functions,
set a specific property of Entry apart from key and value. This
restricts us from setting multiple property for same entry such as
meta and TTL.
This PR adds functions to set different properties on an existing
Entry. After setting desired properties, user can call txn.SetEntry
to add entry in the transaction. It also removes entry related functions
from Txn Struct except SetEntry and Set.

* Fix all compilation failure due to above change using
new Entry methods.

* Fix comments from GolangCI.
  • Loading branch information
ashish-goswami authored May 28, 2019
1 parent 0ce1d2e commit e9447c9
Show file tree
Hide file tree
Showing 16 changed files with 198 additions and 206 deletions.
25 changes: 13 additions & 12 deletions backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestBackupRestore1(t *testing.T) {

err = db.Update(func(txn *Txn) error {
e := entries[0]
err := txn.SetWithMeta(e.key, e.val, e.userMeta)
err := txn.SetEntry(NewEntry(e.key, e.val).WithMeta(e.userMeta))
if err != nil {
return err
}
Expand All @@ -62,7 +62,7 @@ func TestBackupRestore1(t *testing.T) {

err = db.Update(func(txn *Txn) error {
e := entries[1]
err := txn.SetWithMeta(e.key, e.val, e.userMeta)
err := txn.SetEntry(NewEntry(e.key, e.val).WithMeta(e.userMeta))
if err != nil {
return err
}
Expand Down Expand Up @@ -140,20 +140,20 @@ func TestBackupRestore2(t *testing.T) {
rawValue := []byte("NotLongValue")
N := byte(251)
err = db1.Update(func(tx *Txn) error {
if err := tx.Set(key1, rawValue); err != nil {
if err := tx.SetEntry(NewEntry(key1, rawValue)); err != nil {
return err
}
return tx.Set(key2, rawValue)
return tx.SetEntry(NewEntry(key2, rawValue))
})
if err != nil {
t.Fatal(err)
}
for i := byte(1); i < N; i++ {
err = db1.Update(func(tx *Txn) error {
if err := tx.Set(append(key1, i), rawValue); err != nil {
if err := tx.SetEntry(NewEntry(append(key1, i), rawValue)); err != nil {
return err
}
return tx.Set(append(key2, i), rawValue)
return tx.SetEntry(NewEntry(append(key2, i), rawValue))
})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -204,10 +204,10 @@ func TestBackupRestore2(t *testing.T) {

for i := byte(1); i < N; i++ {
err = db2.Update(func(tx *Txn) error {
if err := tx.Set(append(key1, i), rawValue); err != nil {
if err := tx.SetEntry(NewEntry(append(key1, i), rawValue)); err != nil {
return err
}
return tx.Set(append(key2, i), rawValue)
return tx.SetEntry(NewEntry(append(key2, i), rawValue))
})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -278,7 +278,7 @@ func populateEntries(db *DB, entries []*pb.KV) error {
return db.Update(func(txn *Txn) error {
var err error
for i, e := range entries {
if err = txn.Set(e.Key, e.Value); err != nil {
if err = txn.SetEntry(NewEntry(e.Key, e.Value)); err != nil {
return err
}
entries[i].Version = 1
Expand Down Expand Up @@ -445,8 +445,8 @@ func TestBackupLoadIncremental(t *testing.T) {
// pick 5 items to mark as expired.
err = db1.Update(func(txn *Txn) error {
for _, i := range (ints)[10:15] {
if err := txn.SetWithTTL(
entries[i].Key, entries[i].Value, -time.Hour); err != nil {
entry := NewEntry(entries[i].Key, entries[i].Value).WithTTL(-time.Hour)
if err := txn.SetEntry(entry); err != nil {
return err
}
updates[i] = bitDelete // expired
Expand All @@ -460,7 +460,8 @@ func TestBackupLoadIncremental(t *testing.T) {
// pick 5 items to mark as discard.
err = db1.Update(func(txn *Txn) error {
for _, i := range ints[15:20] {
if err := txn.SetWithDiscard(entries[i].Key, entries[i].Value, 0); err != nil {
entry := NewEntry(entries[i].Key, entries[i].Value).WithDiscard()
if err := txn.SetEntry(entry); err != nil {
return err
}
updates[i] = bitDiscardEarlierVersions
Expand Down
2 changes: 1 addition & 1 deletion badger/cmd/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func getBalance(txn *badger.Txn, account int) (uint64, error) {
}

func putBalance(txn *badger.Txn, account int, bal uint64) error {
return txn.Set(key(account), toSlice(bal))
return txn.SetEntry(badger.NewEntry(key(account), toSlice(bal)))
}

func min(a, b uint64) uint64 {
Expand Down
4 changes: 2 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ func (seq *Sequence) Release() error {
err := seq.db.Update(func(txn *Txn) error {
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], seq.next)
return txn.Set(seq.key, buf[:])
return txn.SetEntry(NewEntry(seq.key, buf[:]))
})
if err != nil {
return err
Expand Down Expand Up @@ -1142,7 +1142,7 @@ func (seq *Sequence) updateLease() error {
lease := seq.next + seq.bandwidth
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], lease)
if err = txn.Set(seq.key, buf[:]); err != nil {
if err = txn.SetEntry(NewEntry(seq.key, buf[:])); err != nil {
return err
}
seq.leased = lease
Expand Down
23 changes: 12 additions & 11 deletions db2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestTruncateVlogWithClose(t *testing.T) {
require.NoError(t, err)

err = db.Update(func(txn *Txn) error {
return txn.Set(key(0), data(4055))
return txn.SetEntry(NewEntry(key(0), data(4055)))
})
require.NoError(t, err)

Expand All @@ -67,7 +67,7 @@ func TestTruncateVlogWithClose(t *testing.T) {
require.NoError(t, err)
for i := 0; i < 32; i++ {
err := db.Update(func(txn *Txn) error {
return txn.Set(key(i), data(10))
return txn.SetEntry(NewEntry(key(i), data(10)))
})
require.NoError(t, err)
}
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestTruncateVlogNoClose(t *testing.T) {
}
data := fmt.Sprintf("%4055d", 1)
err = kv.Update(func(txn *Txn) error {
return txn.Set([]byte(key(0)), []byte(data))
return txn.SetEntry(NewEntry([]byte(key(0)), []byte(data)))
})
require.NoError(t, err)
}
Expand All @@ -147,7 +147,7 @@ func TestTruncateVlogNoClose2(t *testing.T) {
data := fmt.Sprintf("%10d", 1)
for i := 32; i < 64; i++ {
err := kv.Update(func(txn *Txn) error {
return txn.Set([]byte(key(i)), []byte(data))
return txn.SetEntry(NewEntry([]byte(key(i)), []byte(data)))
})
require.NoError(t, err)
}
Expand Down Expand Up @@ -205,11 +205,12 @@ func TestBigKeyValuePairs(t *testing.T) {
small := make([]byte, 65000)

txn := db.NewTransaction(true)
require.Regexp(t, regexp.MustCompile("Key.*exceeded"), txn.Set(bigK, small))
require.Regexp(t, regexp.MustCompile("Value.*exceeded"), txn.Set(small, bigV))
require.Regexp(t, regexp.MustCompile("Key.*exceeded"), txn.SetEntry(NewEntry(bigK, small)))
require.Regexp(t, regexp.MustCompile("Value.*exceeded"),
txn.SetEntry(NewEntry(small, bigV)))

require.NoError(t, txn.Set(small, small))
require.Regexp(t, regexp.MustCompile("Key.*exceeded"), txn.Set(bigK, bigV))
require.NoError(t, txn.SetEntry(NewEntry(small, small)))
require.Regexp(t, regexp.MustCompile("Key.*exceeded"), txn.SetEntry(NewEntry(bigK, bigV)))

require.NoError(t, db.View(func(txn *Txn) error {
_, err := txn.Get(small)
Expand All @@ -225,7 +226,7 @@ func TestBigKeyValuePairs(t *testing.T) {

saveByKey := func(key string, value []byte) error {
return db.Update(func(txn *Txn) error {
return txn.Set([]byte(key), value)
return txn.SetEntry(NewEntry([]byte(key), value))
})
}

Expand Down Expand Up @@ -297,12 +298,12 @@ func TestPushValueLogLimit(t *testing.T) {
if i == 4 {
v := make([]byte, 2<<30)
err := db.Update(func(txn *Txn) error {
return txn.Set([]byte(key(i)), v)
return txn.SetEntry(NewEntry([]byte(key(i)), v))
})
require.NoError(t, err)
} else {
err := db.Update(func(txn *Txn) error {
return txn.Set([]byte(key(i)), data)
return txn.SetEntry(NewEntry([]byte(key(i)), data))
})
require.NoError(t, err)
}
Expand Down
Loading

0 comments on commit e9447c9

Please sign in to comment.