diff --git a/bitcask_test.go b/bitcask_test.go index 167d0b5..1fec386 100644 --- a/bitcask_test.go +++ b/bitcask_test.go @@ -1196,10 +1196,10 @@ func BenchmarkPut(b *testing.B) { } variants := map[string][]Option{ - "NoSync": []Option{ + "NoSync": { WithSync(false), }, - "Sync": []Option{ + "Sync": { WithSync(true), }, } diff --git a/internal/data/datafile.go b/internal/data/datafile.go index 50a7acb..c9c20e9 100644 --- a/internal/data/datafile.go +++ b/internal/data/datafile.go @@ -13,16 +13,17 @@ import ( ) const ( - DefaultDatafileFilename = "%09d.data" + defaultDatafileFilename = "%09d.data" ) var ( - ErrReadonly = errors.New("error: read only datafile") - ErrReadError = errors.New("error: read error") + errReadonly = errors.New("error: read only datafile") + errReadError = errors.New("error: read error") mxMemPool sync.RWMutex ) +// Datafile is an interface that represents a readable and writeable datafile type Datafile interface { FileID() int Name() string @@ -57,7 +58,7 @@ func NewDatafile(path string, id int, readonly bool, maxKeySize uint32, maxValue err error ) - fn := filepath.Join(path, fmt.Sprintf(DefaultDatafileFilename, id)) + fn := filepath.Join(path, fmt.Sprintf(defaultDatafileFilename, id)) if !readonly { w, err = os.OpenFile(fn, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0640) @@ -165,7 +166,7 @@ func (df *datafile) ReadAt(index, size int64) (e internal.Entry, err error) { return } if int64(n) != size { - err = ErrReadError + err = errReadError return } @@ -176,7 +177,7 @@ func (df *datafile) ReadAt(index, size int64) (e internal.Entry, err error) { func (df *datafile) Write(e internal.Entry) (int64, int64, error) { if df.w == nil { - return -1, 0, ErrReadonly + return -1, 0, errReadonly } df.Lock() diff --git a/internal/entry.go b/internal/entry.go index 4f776dd..a1272f5 100644 --- a/internal/entry.go +++ b/internal/entry.go @@ -12,6 +12,7 @@ type Entry struct { Value []byte } +// NewEntry creates a new `Entry` with the given `key` and `value` func NewEntry(key, value []byte) Entry { checksum := crc32.ChecksumIEEE(value) diff --git a/internal/index/index.go b/internal/index/index.go index a48143c..d8d2d28 100644 --- a/internal/index/index.go +++ b/internal/index/index.go @@ -7,11 +7,14 @@ import ( "github.com/prologic/bitcask/internal" ) +// Indexer is an interface for loading and saving the index (an Adaptive Radix Tree) type Indexer interface { Load(path string, maxkeySize uint32) (art.Tree, bool, error) Save(t art.Tree, path string) error } +// NewIndexer returns an instance of the default `Indexer` implemtnation +// which perists the index (an Adaptive Radix Tree) as a binary blob on file func NewIndexer() Indexer { return &indexer{} } diff --git a/internal/item.go b/internal/item.go index 61bbabf..6d245de 100644 --- a/internal/item.go +++ b/internal/item.go @@ -1,5 +1,8 @@ package internal +// Item represents the location of the value on disk. This is used by the +// internal Adaptive Radix Tree to hold an in-memory structure mapping keys to +// locations on disk of where the value(s) can be read from. type Item struct { FileID int `json:"fileid"` Offset int64 `json:"offset"` diff --git a/internal/utils.go b/internal/utils.go index 158c0d1..a10f402 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -9,11 +9,14 @@ import ( "strings" ) +// Exists returns `true` if the given `path` on the current file system exists func Exists(path string) bool { _, err := os.Stat(path) return err == nil } +// DirSize returns the space occupied by the given `path` on disk on the current +// file system. func DirSize(path string) (int64, error) { var size int64 err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error { @@ -28,6 +31,9 @@ func DirSize(path string) (int64, error) { return size, err } +// GetDatafiles returns a list of all data files stored in the database path +// given by `path`. All datafiles are identified by the the glob `*.data` and +// the basename is represented by an monotomic increasing integer. func GetDatafiles(path string) ([]string, error) { fns, err := filepath.Glob(fmt.Sprintf("%s/*.data", path)) if err != nil { @@ -37,6 +43,8 @@ func GetDatafiles(path string) ([]string, error) { return fns, nil } +// ParseIds will parse a list of datafiles as returned by `GetDatafiles` and +// extract the id part and return a slice of ints. func ParseIds(fns []string) ([]int, error) { var ids []int for _, fn := range fns {