2 Star 0 Fork 0

mirrors_pingcap/badger

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
structs.go 2.35 KB
一键复制 编辑 原始数据 按行查看 历史
coocood 提交于 2020-06-02 10:21 . update path to pingcap (#195)
package badger
import (
"bytes"
"encoding/binary"
"fmt"
"hash/crc32"
"github.com/pingcap/badger/y"
)
// header is used in value log as a header before Entry.
type header struct {
klen uint32
vlen uint32
ver uint64
meta byte
// umlen is the length of UserMeta
umlen byte
}
const (
headerBufSize = 18
metaNotEntryEncoded = 0
)
func (h header) Encode(out []byte) {
y.Assert(len(out) >= headerBufSize)
// Because meta can never be 0xff, so 0x00 in vlog file indicates there is not an entry.
out[0] = ^h.meta
binary.BigEndian.PutUint32(out[1:5], h.klen)
binary.BigEndian.PutUint32(out[5:9], h.vlen)
binary.BigEndian.PutUint64(out[9:17], h.ver)
out[17] = h.umlen
}
// Decodes h from buf.
func (h *header) Decode(buf []byte) {
h.meta = ^buf[0]
h.klen = binary.BigEndian.Uint32(buf[1:5])
h.vlen = binary.BigEndian.Uint32(buf[5:9])
h.ver = binary.BigEndian.Uint64(buf[9:17])
h.umlen = buf[17]
}
func isEncodedHeader(data []byte) bool {
if len(data) < 1 {
return false
}
return data[0] != metaNotEntryEncoded
}
// Entry provides Key, Value, UserMeta. This struct can be used by the user to set data.
type Entry struct {
Key y.Key
Value []byte
UserMeta []byte
meta byte
logOffset logOffset
// Fields maintained internally.
offset uint32
}
func (e *Entry) SetDelete() {
e.meta |= bitDelete
}
func (e *Entry) estimateSize() int {
return e.Key.Len() + len(e.Value) + len(e.UserMeta) + 2 // Meta, UserMeta
}
// Encodes e to buf. Returns number of bytes written.
func encodeEntry(e *Entry, buf *bytes.Buffer) (int, error) {
h := header{
klen: uint32(len(e.Key.UserKey)),
vlen: uint32(len(e.Value)),
ver: e.Key.Version,
meta: e.meta,
umlen: byte(len(e.UserMeta)),
}
var headerEnc [headerBufSize]byte
h.Encode(headerEnc[:])
hash := crc32.New(y.CastagnoliCrcTable)
buf.Write(headerEnc[:])
hash.Write(headerEnc[:])
buf.Write(e.UserMeta)
hash.Write(e.UserMeta)
buf.Write(e.Key.UserKey)
hash.Write(e.Key.UserKey)
buf.Write(e.Value)
hash.Write(e.Value)
var crcBuf [4]byte
binary.BigEndian.PutUint32(crcBuf[:], hash.Sum32())
buf.Write(crcBuf[:])
return len(headerEnc) + len(e.UserMeta) + len(e.Key.UserKey) + len(e.Value) + len(crcBuf), nil
}
func (e Entry) print(prefix string) {
fmt.Printf("%s Key: %s Meta: %d UserMeta: %v Offset: %d len(val)=%d",
prefix, e.Key, e.meta, e.UserMeta, e.offset, len(e.Value))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_pingcap/badger.git
git@gitee.com:mirrors_pingcap/badger.git
mirrors_pingcap
badger
badger
master

搜索帮助