Skip to content

Commit

Permalink
structure: Address comment.
Browse files Browse the repository at this point in the history
  • Loading branch information
siddontang committed Oct 26, 2015
1 parent fe0ffda commit 4e48eb5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
8 changes: 4 additions & 4 deletions structure/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ type hashMeta struct {
Length int64
}

func (m hashMeta) Value() []byte {
func (meta hashMeta) Value() []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf[0:8], uint64(m.Length))
binary.BigEndian.PutUint64(buf[0:8], uint64(meta.Length))
return buf
}

func (m hashMeta) IsEmpty() bool {
return m.Length <= 0
func (meta hashMeta) IsEmpty() bool {
return meta.Length <= 0
}

// HSet sets the string value of a hash field.
Expand Down
64 changes: 32 additions & 32 deletions structure/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ type listMeta struct {
RIndex int64
}

func (m listMeta) Value() []byte {
func (meta listMeta) Value() []byte {
buf := make([]byte, 16)
binary.BigEndian.PutUint64(buf[0:8], uint64(m.LIndex))
binary.BigEndian.PutUint64(buf[8:16], uint64(m.RIndex))
binary.BigEndian.PutUint64(buf[0:8], uint64(meta.LIndex))
binary.BigEndian.PutUint64(buf[8:16], uint64(meta.RIndex))
return buf
}

func (m listMeta) IsEmpty() bool {
return m.LIndex >= m.RIndex
func (meta listMeta) IsEmpty() bool {
return meta.LIndex >= meta.RIndex
}

// LPush prepends one or multiple values to a list.
Expand All @@ -53,7 +53,7 @@ func (t *TxStructure) listPush(key []byte, left bool, values ...[]byte) error {
}

metaKey := t.encodeListMetaKey(key)
m, err := t.loadListMeta(metaKey)
meta, err := t.loadListMeta(metaKey)
if err != nil {
return errors.Trace(err)
}
Expand All @@ -65,11 +65,11 @@ func (t *TxStructure) listPush(key []byte, left bool, values ...[]byte) error {
index := int64(0)
for _, v := range values {
if left {
m.LIndex--
index = m.LIndex
meta.LIndex--
index = meta.LIndex
} else {
index = m.RIndex
m.RIndex++
index = meta.RIndex
meta.RIndex++
}

dataKey := t.encodeListDataKey(key, index)
Expand All @@ -78,7 +78,7 @@ func (t *TxStructure) listPush(key []byte, left bool, values ...[]byte) error {
}
}

return t.txn.Set(metaKey, m.Value())
return t.txn.Set(metaKey, meta.Value())
}

// LPop removes and gets the first element in a list.
Expand All @@ -93,8 +93,8 @@ func (t *TxStructure) RPop(key []byte) ([]byte, error) {

func (t *TxStructure) listPop(key []byte, left bool) ([]byte, error) {
metaKey := t.encodeListMetaKey(key)
m, err := t.loadListMeta(metaKey)
if err != nil || m.IsEmpty() {
meta, err := t.loadListMeta(metaKey)
if err != nil || meta.IsEmpty() {
return nil, errors.Trace(err)
}

Expand All @@ -104,11 +104,11 @@ func (t *TxStructure) listPop(key []byte, left bool) ([]byte, error) {

index := int64(0)
if left {
index = m.LIndex
m.LIndex++
index = meta.LIndex
meta.LIndex++
} else {
m.RIndex--
index = m.RIndex
meta.RIndex--
index = meta.RIndex
}

dataKey := t.encodeListDataKey(key, index)
Expand All @@ -123,8 +123,8 @@ func (t *TxStructure) listPop(key []byte, left bool) ([]byte, error) {
return nil, errors.Trace(err)
}

if m.LIndex < m.RIndex {
err = t.txn.Set(metaKey, m.Value())
if !meta.IsEmpty() {
err = t.txn.Set(metaKey, meta.Value())
} else {
err = t.txn.Delete(metaKey)
}
Expand All @@ -135,21 +135,21 @@ func (t *TxStructure) listPop(key []byte, left bool) ([]byte, error) {
// LLen gets the length of a list.
func (t *TxStructure) LLen(key []byte) (int64, error) {
metaKey := t.encodeListMetaKey(key)
m, err := t.loadListMeta(metaKey)
return m.RIndex - m.LIndex, errors.Trace(err)
meta, err := t.loadListMeta(metaKey)
return meta.RIndex - meta.LIndex, errors.Trace(err)
}

// LIndex gets an element from a list by its index.
func (t *TxStructure) LIndex(key []byte, index int64) ([]byte, error) {
metaKey := t.encodeListMetaKey(key)
m, err := t.loadListMeta(metaKey)
if err != nil || m.IsEmpty() {
meta, err := t.loadListMeta(metaKey)
if err != nil || meta.IsEmpty() {
return nil, errors.Trace(err)
}

index = adjustIndex(index, m.LIndex, m.RIndex)
index = adjustIndex(index, meta.LIndex, meta.RIndex)

if index >= m.LIndex && index < m.RIndex {
if index >= meta.LIndex && index < meta.RIndex {
return t.txn.Get(t.encodeListDataKey(key, index))
}
return nil, nil
Expand All @@ -158,14 +158,14 @@ func (t *TxStructure) LIndex(key []byte, index int64) ([]byte, error) {
// LSet updates an element in the list by its index.
func (t *TxStructure) LSet(key []byte, index int64, value []byte) error {
metaKey := t.encodeListMetaKey(key)
m, err := t.loadListMeta(metaKey)
if err != nil || m.IsEmpty() {
meta, err := t.loadListMeta(metaKey)
if err != nil || meta.IsEmpty() {
return errors.Trace(err)
}

index = adjustIndex(index, m.LIndex, m.RIndex)
index = adjustIndex(index, meta.LIndex, meta.RIndex)

if index >= m.LIndex && index < m.RIndex {
if index >= meta.LIndex && index < meta.RIndex {
return t.txn.Set(t.encodeListDataKey(key, index), value)
}
return errors.Errorf("invalid index %d", index)
Expand All @@ -174,16 +174,16 @@ func (t *TxStructure) LSet(key []byte, index int64, value []byte) error {
// LClear removes the list of the key.
func (t *TxStructure) LClear(key []byte) error {
metaKey := t.encodeListMetaKey(key)
m, err := t.loadListMeta(metaKey)
if err != nil || m.IsEmpty() {
meta, err := t.loadListMeta(metaKey)
if err != nil || meta.IsEmpty() {
return errors.Trace(err)
}

if err = t.txn.LockKeys(metaKey); err != nil {
return errors.Trace(err)
}

for index := m.LIndex; index < m.RIndex; index++ {
for index := meta.LIndex; index < meta.RIndex; index++ {
dataKey := t.encodeListDataKey(key, index)
if err = t.txn.Delete(dataKey); err != nil {
return errors.Trace(err)
Expand Down
6 changes: 3 additions & 3 deletions structure/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"github.com/pingcap/tidb/kv"
)

// ErrTxDone is the error return after transaction has already been committed or rolled back.
var ErrTxDone = errors.Errorf("Transaction has already been committed or rolled back")
// ErrTxDone is the error returned after transaction has already been committed or rolled back.
var ErrTxDone = errors.New("Transaction has already been committed or rolled back")

// TStore is the storage for data structure.
type TStore struct {
Expand Down Expand Up @@ -59,7 +59,7 @@ func (s *TStore) RunInNewTxn(retryable bool, f func(t *TxStructure) error) error
return errors.Trace(err)
}

// TxStructure supports some simple data structure like string, hash, list, etc... and
// TxStructure supports some simple data structures like string, hash, list, etc... and
// you can use these in a transaction.
type TxStructure struct {
txn kv.Transaction
Expand Down
2 changes: 1 addition & 1 deletion structure/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (t *TxStructure) decodeHashDataKey(ek []byte) ([]byte, []byte, error) {
)

if !bytes.HasPrefix(ek, t.prefix) {
return nil, nil, errors.Errorf("invalid encoded hash data key prefix")
return nil, nil, errors.New("invalid encoded hash data key prefix")
}

ek = ek[len(t.prefix):]
Expand Down

0 comments on commit 4e48eb5

Please sign in to comment.