Skip to content

Commit

Permalink
*: add hash set check for not changed value.
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuyesuifeng committed Nov 9, 2015
1 parent 34100b6 commit 7bab524
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion store/localstore/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (txn *dbTxn) Commit() error {
txn.close()
}()

return txn.doCommit()
return errors.Trace(txn.doCommit())
}

func (txn *dbTxn) CommittedVersion() (kv.Version, error) {
Expand Down
19 changes: 15 additions & 4 deletions structure/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ func (t *TxStructure) updateHash(key []byte, field []byte, fn func(oldValue []by
return errors.Trace(err)
}

newMeta := meta
if oldValue == nil {
meta.Length++
newMeta.Length++
}

var newValue []byte
Expand All @@ -115,11 +116,21 @@ func (t *TxStructure) updateHash(key []byte, field []byte, fn func(oldValue []by
return errors.Trace(err)
}

if err = t.txn.Set(dataKey, newValue); err != nil {
return errors.Trace(err)
// Check if hash field has been changed.
if bytes.Compare(oldValue, newValue) != 0 {
if err = t.txn.Set(dataKey, newValue); err != nil {
return errors.Trace(err)
}
}

// Check if hash meta has been changed.
if bytes.Compare(meta.Value(), newMeta.Value()) != 0 {
if err = t.txn.Set(metaKey, newMeta.Value()); err != nil {
return errors.Trace(err)
}
}

return errors.Trace(t.txn.Set(metaKey, meta.Value()))
return nil
}

// HLen gets the number of fields in a hash.
Expand Down
24 changes: 22 additions & 2 deletions structure/structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (s *tesTxStructureSuite) TestHash(c *C) {

value, err = tx.HGet(key, []byte("fake"))
c.Assert(err, IsNil)
c.Assert(err, IsNil)
c.Assert(value, IsNil)

keys, err := tx.HKeys(key)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -224,13 +224,33 @@ func (s *tesTxStructureSuite) TestHash(c *C) {
c.Assert(err, IsNil)
c.Assert(l, Equals, int64(2))

// Test set new value which equals to old value.
err = tx.HSet(key, []byte("1"), []byte("1"))
c.Assert(err, IsNil)

value, err = tx.HGet(key, []byte("1"))
c.Assert(err, IsNil)
c.Assert(value, DeepEquals, []byte("1"))

l, err = tx.HLen(key)
c.Assert(err, IsNil)
c.Assert(l, Equals, int64(2))

n, err = tx.HInc(key, []byte("1"), 1)
c.Assert(err, IsNil)
c.Assert(n, Equals, int64(2))

l, err = tx.HLen(key)
c.Assert(err, IsNil)
c.Assert(l, Equals, int64(2))

n, err = tx.HInc(key, []byte("1"), 1)
c.Assert(err, IsNil)
c.Assert(n, Equals, int64(3))

n, err = tx.HGetInt64(key, []byte("1"))
c.Assert(err, IsNil)
c.Assert(n, Equals, int64(2))
c.Assert(n, Equals, int64(3))

l, err = tx.HLen(key)
c.Assert(err, IsNil)
Expand Down

0 comments on commit 7bab524

Please sign in to comment.