func (t *TxStructure) HGetAll(key []byte) ([]HashPair, error) {
	var res []HashPair
	err := t.iterateHash(key, func(field []byte, value []byte) error {
		pair := HashPair{
			Field: append([]byte{}, field...),
			Value: append([]byte{}, value...),
		}
		res = append(res, pair)
		return nil
	})
	return res, errors.Trace(err)
}
HGetAll gets all the fields and values in a hash.
package structure
import (
	"bytes"
	"context"
	"encoding/binary"
	"strconv"
	"github.com/pingcap/errors"
	"github.com/pingcap/tidb/kv"
)
type HashPair struct {
	Field []byte
	Value []byte
}
type hashMeta struct {
	FieldCount int64
}
func (meta hashMeta) Value() []byte {
	buf := make([]byte, 8)
	binary.BigEndian.PutUint64(buf[0:8], uint64(meta.FieldCount))
	return buf
}
func (meta hashMeta) IsEmpty() bool {
	return meta.FieldCount <= 0
}
func (t *TxStructure) HSet(key []byte, field []byte, value []byte) error {
	if t.readWriter == nil {
		return errWriteOnSnapshot
	}
	return t.updateHash(key, field, func([]byte) ([]byte, error) {
		return value, nil
	})
}
func (t *TxStructure) HGet(key []byte, field []byte) ([]byte, error) {
	dataKey := t.encodeHashDataKey(key, field)
	value, err := t.reader.Get(context.TODO(), dataKey)
	if kv.ErrNotExist.Equal(err) {
		err = nil
	}
	return value, errors.Trace(err)
}
func (t *TxStructure) hashFieldIntegerVal(val int64) []byte {
	return []byte(strconv.FormatInt(val, 10))
}
func (t *TxStructure) EncodeHashAutoIDKeyValue(key []byte, field []byte, val int64) (k, v []byte) {
	return t.encodeHashDataKey(key, field), t.hashFieldIntegerVal(val)
}
func (t *TxStructure) HInc(key []byte, field []byte, step int64) (int64, error) {
	if t.readWriter == nil {
		return 0, errWriteOnSnapshot
	}
	base := int64(0)
	err := t.updateHash(key, field, func(oldValue []byte) ([]byte, error) {
		if oldValue != nil {
			var err error
			base, err = strconv.ParseInt(string(oldValue), 10, 64)
			if err != nil {
				return nil, errors.Trace(err)
			}
		}
		base += step
		return t.hashFieldIntegerVal(base), nil
	})
	return base, errors.Trace(err)
}
func (t *TxStructure) HGetInt64(key []byte, field []byte) (int64, error) {
	value, err := t.HGet(key, field)
	if err != nil || value == nil {
		return 0, errors.Trace(err)
	}
	var n int64
	n, err = strconv.ParseInt(string(value), 10, 64)
	return n, errors.Trace(err)
}
func (t *TxStructure) updateHash(key []byte, field []byte, fn func(oldValue []byte) ([]byte, error)) error {
	dataKey := t.encodeHashDataKey(key, field)
	oldValue, err := t.loadHashValue(dataKey)
	if err != nil {
		return errors.Trace(err)
	}
	newValue, err := fn(oldValue)
	if err != nil {
		return errors.Trace(err)
	}
	if bytes.Equal(oldValue, newValue) {
		return nil
	}
	if err = t.readWriter.Set(dataKey, newValue); err != nil {
		return errors.Trace(err)
	}
	metaKey := t.encodeHashMetaKey(key)
	meta, err := t.loadHashMeta(metaKey)
	if err != nil {
		return errors.Trace(err)
	}
	if oldValue == nil {
		meta.FieldCount++
		if err = t.readWriter.Set(metaKey, meta.Value()); err != nil {
			return errors.Trace(err)
		}
	}
	return nil
}
func (t *TxStructure) HLen(key []byte) (int64, error) {
	metaKey := t.encodeHashMetaKey(key)
	meta, err := t.loadHashMeta(metaKey)
	if err != nil {
		return 0, errors.Trace(err)
	}
	return meta.FieldCount, nil
}
func (t *TxStructure) HDel(key []byte, fields ...[]byte) error {
	if t.readWriter == nil {
		return errWriteOnSnapshot
	}
	metaKey := t.encodeHashMetaKey(key)
	meta, err := t.loadHashMeta(metaKey)
	if err != nil || meta.IsEmpty() {
		return errors.Trace(err)
	}
	var value []byte
	for _, field := range fields {
		dataKey := t.encodeHashDataKey(key, field)
		value, err = t.loadHashValue(dataKey)
		if err != nil {
			return errors.Trace(err)
		}
		if value != nil {
			if err = t.readWriter.Delete(dataKey); err != nil {
				return errors.Trace(err)
			}
			meta.FieldCount--
		}
	}
	if meta.IsEmpty() {
		err = t.readWriter.Delete(metaKey)
	} else {
		err = t.readWriter.Set(metaKey, meta.Value())
	}
	return errors.Trace(err)
}
func (t *TxStructure) HKeys(key []byte) ([][]byte, error) {
	var keys [][]byte
	err := t.iterateHash(key, func(field []byte, value []byte) error {
		keys = append(keys, append([]byte{}, field...))
		return nil
	})
	return keys, errors.Trace(err)
}