1 Star 0 Fork 0

wkk/self

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
bucket_meta.go 2.96 KB
一键复制 编辑 原始数据 按行查看 历史
1231 提交于 2021-06-18 09:48 . 初始版本0.6.0
// Copyright 2019 The nutsdb Author. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nutsdb
import (
"encoding/binary"
"hash/crc32"
"os"
)
const (
// BucketMetaHeaderSize returns the header size of the BucketMeta.
BucketMetaHeaderSize = 12
// BucketMetaSuffix returns b+ tree index suffix.
BucketMetaSuffix = ".meta"
)
// BucketMeta represents the bucket's meta-information.
type BucketMeta struct {
startSize uint32
endSize uint32
start []byte
end []byte
crc uint32
}
// Encode returns the slice after the BucketMeta be encoded.
func (bm *BucketMeta) Encode() []byte {
buf := make([]byte, bm.Size())
binary.LittleEndian.PutUint32(buf[4:8], bm.startSize)
binary.LittleEndian.PutUint32(buf[8:12], bm.endSize)
startBuf := buf[BucketMetaHeaderSize:(BucketMetaHeaderSize + bm.startSize)]
copy(startBuf, bm.start)
endBuf := buf[BucketMetaHeaderSize+bm.startSize : (BucketMetaHeaderSize + bm.startSize + bm.endSize)]
copy(endBuf, bm.end)
c32 := crc32.ChecksumIEEE(buf[4:])
binary.LittleEndian.PutUint32(buf[0:4], c32)
return buf
}
// GetCrc returns the crc at given buf slice.
func (bm *BucketMeta) GetCrc(buf []byte) uint32 {
crc := crc32.ChecksumIEEE(buf[4:])
crc = crc32.Update(crc, crc32.IEEETable, bm.start)
crc = crc32.Update(crc, crc32.IEEETable, bm.end)
return crc
}
// Size returns the size of the BucketMeta.
func (bm *BucketMeta) Size() int64 {
return int64(BucketMetaHeaderSize + bm.startSize + bm.endSize)
}
// ReadBucketMeta returns bucketMeta at given file path name.
func ReadBucketMeta(name string) (bucketMeta *BucketMeta, err error) {
var off int64
fd, err := os.OpenFile(name, os.O_CREATE|os.O_RDWR, 0644)
defer fd.Close()
if err != nil {
return
}
buf := make([]byte, BucketMetaHeaderSize)
_, err = fd.ReadAt(buf, off)
if err != nil {
return
}
startSize := binary.LittleEndian.Uint32(buf[4:8])
endSize := binary.LittleEndian.Uint32(buf[8:12])
bucketMeta = &BucketMeta{
startSize: startSize,
endSize: endSize,
crc: binary.LittleEndian.Uint32(buf[0:4]),
}
// read start
off += BucketMetaHeaderSize
startBuf := make([]byte, startSize)
if _, err = fd.ReadAt(startBuf, off); err != nil {
return nil, err
}
bucketMeta.start = startBuf
// read end
off += int64(startSize)
endBuf := make([]byte, endSize)
if _, err = fd.ReadAt(endBuf, off); err != nil {
return nil, err
}
bucketMeta.end = endBuf
if bucketMeta.GetCrc(buf) != bucketMeta.crc {
return nil, ErrCrc
}
return
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/wkkcool/self.git
git@gitee.com:wkkcool/self.git
wkkcool
self
self
v0.1.3

搜索帮助