Files
gf/.example/encoding/gbinary/bits2.go

34 lines
1.0 KiB
Go
Raw Normal View History

2018-01-18 12:02:56 +08:00
package main
import (
2019-04-03 00:03:46 +08:00
"fmt"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/encoding/gbinary"
2018-01-18 12:02:56 +08:00
)
func main() {
2019-04-03 00:03:46 +08:00
// Meta元数据文件数据结构[键名哈希64(64bit,8byte) 键名长度(8bit,1byte) 键值长度(24bit,3byte) 数据文件偏移量(40bit,5byte)](变长)
hash := 521369841259754125
klen := 12
vlen := 35535
offset := 80000000
2018-01-18 12:02:56 +08:00
2019-04-03 00:03:46 +08:00
// 编码
bits := make([]gbinary.Bit, 0)
bits = gbinary.EncodeBits(bits, hash, 64)
bits = gbinary.EncodeBits(bits, klen, 8)
bits = gbinary.EncodeBits(bits, vlen, 24)
bits = gbinary.EncodeBits(bits, offset, 40)
buffer := gbinary.EncodeBitsToBytes(bits)
fmt.Println("meta length:", len(buffer))
2018-01-18 12:02:56 +08:00
2019-04-03 00:03:46 +08:00
/* 文件存储及数据查询过程忽略,这里只展示元数据编码/解码示例 */
2018-01-18 22:14:12 +08:00
2019-04-03 00:03:46 +08:00
// 解码
metabits := gbinary.DecodeBytesToBits(buffer)
fmt.Println("hash :", gbinary.DecodeBits(metabits[0:64]))
fmt.Println("klen :", gbinary.DecodeBits(metabits[64:72]))
fmt.Println("vlen :", gbinary.DecodeBits(metabits[72:96]))
fmt.Println("offset:", gbinary.DecodeBits(metabits[96:136]))
2018-01-18 12:02:56 +08:00
}