bitcask

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2021 License: MIT Imports: 16 Imported by: 0

README

bitcask

erlang bitcask golang implementation, Suitable for storing large amounts of small files

Use

go get github.com/dollarkillerx/bitcask

cask, err := bitcask.New(dir, nil)  // Crc32 check is enabled by default
	if err != nil {
		log.Fatalln(err)
	}
defer cask.Close()

err = cask.set([]byte("file.name"), []byte("file boyd"))

r, err := cask.get([]byte("file.name"))

cask.del([]byte("file.name"))

设计模型以及特点

  • 所有 key 存储与内存中, 所有 key 都存储于磁盘中
  • 有序写: Append 方式写入, 减少磁盘寻道时间,
  • 更新数据: 把新数据追加到文件最后, 然后更新数据文件指针映射
  • 读数据: 通过数据指针 + 偏移量, 时间复杂度 O(1)
  • bitcask有一个合并时间窗口, 当旧数据占到一定比例时, 触发合并操作, 同时为了简单, 会把旧数据重新追加到可写入文件中
存储原理
  • 磁盘源数据域 data

bitcask 的数据文件分为只读文件和唯一一个读写文件

crc tstamp ksz value_sz key value
crc tstamp ksz value_sz key value
  • 内存源数据域 hint

为了加快索引的重建速度,每个数据文件对应一个 hint 文件

Key -> file_id value_sz value_pos tstamp
Key -> file_id value_sz value_pos tstamp
Hint File

用于重建 HashMap 数据结构以及 HashMap 持久化

详细设计见 INFO.md

Documentation

Index

Constants

View Source
const (
	// HeaderSize: 4 + 4 + 4 + 4 磁盘源数据域 data
	/**
	crc32	:	tStamp	:	ksz	:	valueSz	:	key	:	value
		4 		:	4 		: 	4 	: 		4	:	xxxx	: xxxx
	*/
	HeaderSize = 16

	// HintHeaderSize: 4 + 4 + 4 + 8 = 20 byte 内存源数据域 hint
	/**
	tstamp	:	ksz	:	valuesz	:	valuePos	:	key
		4	:	4	:	4		:		8		:	xxxx
	*/
	HintHeaderSize = 20
)

key value endurance

Variables

View Source
var (
	ErrNotFound    = errors.New("Not Found")
	ErrIsNotDir    = errors.New("This File is not dir")
	ErrNotReadRoot = errors.New("Can Not Read The Bitcask Root Director")
)

Error

View Source
var ErrCrc32 = errors.New("checksumIEEE error")

Functions

func CRC32

func CRC32(str string) uint32

func DecodeEntry

func DecodeEntry(buf []byte, crc bool) ([]byte, error)

func DecodeEntryDetail

func DecodeEntryDetail(buf []byte, crc bool) (uint32, uint32, uint32, uint32, []byte, []byte, error)

func DecodeEntryHeader

func DecodeEntryHeader(buf []byte) (uint32, uint32, uint32, uint32)

DecodeEntryHeader 获取Header基础信息

func DecodeHint

func DecodeHint(buf []byte) (uint32, uint32, uint32, uint64)

func MD5

func MD5(str string) string

func SHA1

func SHA1(str string) string

Types

type BFile

type BFile struct {
	// contains filtered or unexported fields
}

BFile hint file

type BFiles

type BFiles struct {
	// contains filtered or unexported fields
}

type BitCask

type BitCask struct {
	Opts *Options // base config
	// contains filtered or unexported fields
}

BitCask ...

func New

func New(dirName string, opts *Options) (*BitCask, error)

New ...

func (*BitCask) Close

func (bc *BitCask) Close()

func (*BitCask) Del

func (bc *BitCask) Del(key []byte) error

Del ...

func (*BitCask) Get

func (bc *BitCask) Get(key []byte) ([]byte, error)

Get ...

func (*BitCask) Set

func (bc *BitCask) Set(key []byte, value []byte) error

Set key/val

type KeyDirs

type KeyDirs struct {
	// contains filtered or unexported fields
}

func NewKeyDir

func NewKeyDir(dirName string) *KeyDirs

type Options

type Options struct {
	ExpirySecs      int    // 过期时间
	MaxFileSize     uint64 // 最大文件大小
	OpenTimeoutSecs int    // 最大打开文件
	ReadWrite       bool   // 读写
	MergeSecs       int    // merger 时间
	CheckSumCrc32   bool   // crc32校验
	ValueMaxSize    uint64 // 最大Value
}

Options .

func NewOptions

func NewOptions(expirySecs int, maxFileSize uint64,
	openTimeoutSecs, mergeSecs int, readWrite bool) Options

NewOptions ...

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL