Documentation ¶
Index ¶
- Constants
- Variables
- type Chunk
- func (c *Chunk) GetBinaryLength() Offset
- func (c *Chunk) GetKeyData() ([]byte, []byte)
- func (c *Chunk) MarshalBinary() ([]byte, error)
- func (c *Chunk) ReadAt(r io.ReaderAt, off, size int64) error
- func (c *Chunk) Set(key, data []byte) error
- func (c *Chunk) UnmarshalBinary(data []byte) error
- func (c *Chunk) Verify() error
- func (c *Chunk) WriteAt(w io.WriterAt, off int64) error
- type ChunkHeader
- type Dir
- type DirManager
- func (dm *DirManager) DiagDumpAllDirs()
- func (dm *DirManager) DiagDumpAllDirsToString() string
- func (dm *DirManager) DiagHangFreeDirs() (int, error)
- func (dm *DirManager) DiagHangUsedDirs() (int, error)
- func (dm *DirManager) DiagPanicHangUpDirs() error
- func (dm *DirManager) Get(key []byte) (hit bool, dirOffset Offset, d Dir)
- func (dm *DirManager) Init(dirNum Offset) Offset
- func (dm *DirManager) InitEmptyDirs()
- func (dm *DirManager) MarshalBinary() (data []byte, err error)
- func (dm *DirManager) Set(key []byte, off Offset, size int) (dirOffset Offset, err error)
- func (dm *DirManager) UnmarshalBinary(data []byte) (err error)
- type Engine
- type EngineConfig
- type Offset
- type OffsetReaderWriterCloser
- type Vol
- type VolHeaderFooter
- type VolOptions
Constants ¶
const ( MajorVersion = 0 MinorVersion = 1 )
const ( DirDataSizeLv0 = SectorSize << (0 * 3) // 512B DirDataSizeLv1 = SectorSize << (1 * 3) // 4KB DirDataSizeLv2 = SectorSize << (2 * 3) // 32KB DirDataSizeLv3 = SectorSize << (3 * 3) // 256KB DirMaxDataSize = (SectorSize << (3 * 3)) * (1 << 6) //16MB )
Dir constants
const ( ChunkHeaderSizeFixed = 8 * 1 << 10 // 8KB ChunkKeyMaxSize = 4 * 1 << 10 // 4KB ChunkDataSize = 1 * 1 << 20 // 1MB )
const ( MagicBocchi = 0x000b0cc1 MagicChunk = 0x00114514 DirDepth = 4 MaxBucketsPerSegment = 1 << 16 / DirDepth )
Vol constants
const BlockSize = 1 << 12
const MaxKeyLength = 4096
const (
SectorSize = 512
)
Variables ¶
var ( HeaderSize = binary.Size(&VolHeaderFooter{}) DirSize = binary.Size(&Dir{}) )
var ErrCacheMiss = errors.New("cache miss")
var ErrChunkDataTooLarge = errors.New("chunk data too large")
var ErrChunkKeyTooLarge = errors.New("chunk key too large")
var ErrChunkVerifyFailed = errors.New("chunk verify failed")
var ErrKeyTooLong = errors.New("key too long")
var ErrVolFileCorrupted = errors.New("vol file corrupted")
Functions ¶
This section is empty.
Types ¶
type Chunk ¶
type Chunk struct { Header ChunkHeader DataRaw []byte }
Chunk is the unit of data storage. Contains a header(meta) and data.
func (*Chunk) GetBinaryLength ¶
GetBinaryLength returns the binary length of the chunk.
func (*Chunk) GetKeyData ¶
GetKeyData returns the key and data of the chunk. Note: The key is trimmed by the null character.
func (*Chunk) MarshalBinary ¶
MarshalBinary returns the binary of the chunk.
func (*Chunk) UnmarshalBinary ¶
UnmarshalBinary unmarshal the binary of the chunk, and verify it. Note: the data must be the whole chunk.
type ChunkHeader ¶
type ChunkHeader struct { Magic uint32 Checksum uint32 Key [ChunkKeyMaxSize]byte DataLength uint32 HeaderSize uint32 HeaderChecksum uint32 }
ChunkHeader is the meta of a chunk.
func (*ChunkHeader) GenerateHeaderChecksum ¶
func (c *ChunkHeader) GenerateHeaderChecksum() uint32
func (*ChunkHeader) MarshalBinary ¶
func (c *ChunkHeader) MarshalBinary() ([]byte, error)
MarshalBinary returns the binary representation of the chunk header. TODO: could use a buffer pool to avoid allocating a new buffer every time.
func (*ChunkHeader) UnmarshalBinary ¶
func (c *ChunkHeader) UnmarshalBinary(data []byte) error
UnmarshalBinary unmarshal the binary representation of the chunk header.
type Dir ¶
type Dir struct {
// contains filtered or unexported fields
}
func (*Dir) MarshalBinary ¶
func (*Dir) UnmarshalBinary ¶
type DirManager ¶
type DirManager struct { ChunksNum Offset SegmentsNum Offset BucketsNum Offset BucketsNumPerSegment Offset // map segment id to dirs Dirs map[segId][]*Dir DirFreeStart map[segId]uint16 // rw mutex for each segment SegMutexes map[segId]*sync.RWMutex }
DirManager manages the dirs attached to a vol.
func (*DirManager) DiagDumpAllDirs ¶
func (dm *DirManager) DiagDumpAllDirs()
func (*DirManager) DiagDumpAllDirsToString ¶
func (dm *DirManager) DiagDumpAllDirsToString() string
func (*DirManager) DiagHangFreeDirs ¶
func (dm *DirManager) DiagHangFreeDirs() (int, error)
func (*DirManager) DiagHangUsedDirs ¶
func (dm *DirManager) DiagHangUsedDirs() (int, error)
func (*DirManager) DiagPanicHangUpDirs ¶
func (dm *DirManager) DiagPanicHangUpDirs() error
func (*DirManager) Get ¶
func (dm *DirManager) Get(key []byte) (hit bool, dirOffset Offset, d Dir)
Get returns HIT: the offset of the dir entry with the given key, MISS: the offset of last dir entry in the bucket
func (*DirManager) Init ¶
func (dm *DirManager) Init(dirNum Offset) Offset
Init initializes the dir manager. Dirs will Initialized as empty by default.
func (*DirManager) InitEmptyDirs ¶
func (dm *DirManager) InitEmptyDirs()
InitEmptyDirs initializes all dirs as empty, make chain.
func (*DirManager) MarshalBinary ¶
func (dm *DirManager) MarshalBinary() (data []byte, err error)
MarshalBinary converts the Dirs to binary format
func (*DirManager) UnmarshalBinary ¶
func (dm *DirManager) UnmarshalBinary(data []byte) (err error)
type Engine ¶
type Engine struct { SizeMb uint32 SliceSizeKb uint32 Volume *Vol // contains filtered or unexported fields }
func NewEngine ¶
func NewEngine(cfg *EngineConfig) *Engine
type EngineConfig ¶
type Vol ¶
type Vol struct { Path string Fp OffsetReaderWriterCloser Dm *DirManager WritePos Offset Header *VolHeaderFooter SectorSize uint32 Length Offset ChunkAvgSize Offset // average chunk size, adjusted by user. ChunksMaxNum Offset // max chunks num in this vol. calculated from ChunkAvgSize and Length HeaderAOffset Offset HeaderBOffset Offset DataOffset Offset DirAOffset Offset // contains filtered or unexported fields }
Vol is a volume represents a file on disk. structure: Meta_A(header, dirs, footer) + Meta_B(header, dirs, footer) + Data(Chunks) dirs are organized segment->bucket->dir logically.
func (*Vol) SyncFlushLoop ¶
SyncFlushLoop flushes metadata to disk periodically.
type VolHeaderFooter ¶
type VolHeaderFooter struct {}
func (*VolHeaderFooter) GenerateChecksum ¶
func (v *VolHeaderFooter) GenerateChecksum() uint32
func (*VolHeaderFooter) MarshalBinary ¶
func (v *VolHeaderFooter) MarshalBinary() (data []byte, err error)
func (*VolHeaderFooter) UnmarshalBinary ¶
func (v *VolHeaderFooter) UnmarshalBinary(data []byte) error
type VolOptions ¶
type VolOptions struct { Fp OffsetReaderWriterCloser FileSize Offset ChunkSize Offset FlushMetaInterval time.Duration }
VolOptions to init a Vol. Note: do file open/truncate outside.
func NewDefaultVolOptions ¶
func NewDefaultVolOptions(path string, fileSize, avgChunkSize uint64) (*VolOptions, error)
NewDefaultVolOptions creates a VolOptions with a file path. Note: It will create a file if not exists, and truncate it to the given sizeInternal.
func (*VolOptions) Check ¶
func (cfg *VolOptions) Check() error
Check checks if the VolOptions is valid.