Skip to content

Commit

Permalink
fix load index
Browse files Browse the repository at this point in the history
update options
  • Loading branch information
qichengzx committed Dec 3, 2021
1 parent 8490634 commit 30a2da4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
37 changes: 18 additions & 19 deletions bitcask.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import (
type Bitcask struct {
option Option
index *index
actFile *BitFile
oldFiles *BitFiles
dir string
lock *os.File
oldFiles *BitFiles
actFile *BitFile
mu *sync.RWMutex
}

Expand All @@ -24,19 +23,19 @@ func New(dir string) (*Bitcask, error) {
if err != nil {
return nil, err
}
idx := newIndex()
lockFile, err := lock(dir)
if err != nil {
return nil, err
}
option := NewOption(0)

options := NewOption(dir, 0)

bitcask := &Bitcask{
option: option,
index: idx,
actFile: bf,
oldFiles: newBitFiles(),
dir: dir,
option: options,
index: newIndex(),
lock: lockFile,
oldFiles: newBitFiles(),
actFile: bf,
mu: &sync.RWMutex{},
}
bitcask.loadIndex()
Expand All @@ -50,7 +49,7 @@ func (b *Bitcask) Close() {
}

func (b *Bitcask) loadIndex() {
files, err := scanOldFiles(b.dir)
files, err := scanOldFiles(b.option.Dir)
if err != nil {
panic(err)
}
Expand All @@ -59,16 +58,13 @@ func (b *Bitcask) loadIndex() {
defer b.mu.Unlock()
for _, file := range files {
fid, _ := getFid(file.Name())
var offset int64 = 0
fp, err := os.Open(filepath.Join(b.dir, file.Name()))
if err != nil {
continue
}
fp, err := os.Open(filepath.Join(b.option.Dir, file.Name()))

bitFile, err := toBitFile(fid, fp)
if err != nil {
continue
}
var offset int64 = 0
b.oldFiles.add(fid, bitFile)
for {
buf := make([]byte, HeaderSize)
Expand All @@ -80,20 +76,23 @@ func (b *Bitcask) loadIndex() {
keySize := binary.BigEndian.Uint32(buf[8:12])
valueSize := binary.BigEndian.Uint32(buf[12:HeaderSize])

readOffset := offset + HeaderSize
valueOffset := uint64(readOffset) + uint64(keySize)
offset += int64(getSize(keySize, valueSize))
keyByte := make([]byte, keySize)
if _, err := fp.ReadAt(keyByte, int64(HeaderSize)); err != nil {
if _, err := fp.ReadAt(keyByte, readOffset); err != nil {
continue
}
if valueSize == 0 {
b.index.del(string(keyByte))
//key is deleted
continue
}

timestamp := uint64(binary.BigEndian.Uint32(buf[4:8]))

//load to map
entry := newEntry(fid, valueSize, uint64(HeaderSize+int64(keySize)), timestamp)
entry := newEntry(fid, valueSize, valueOffset, timestamp)
b.index.put(string(keyByte), entry)
}
}
Expand Down Expand Up @@ -167,7 +166,7 @@ func (b *Bitcask) checkFile() error {
b.actFile.fp.Close()
b.oldFiles.add(b.actFile.fid, b.actFile)

bf, err := newBitFile(b.dir)
bf, err := newBitFile(b.option.Dir)
if err != nil {
return err
}
Expand Down
16 changes: 10 additions & 6 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,39 @@ import (
)

type index struct {
entrys map[string]*entry
entries map[string]*entry
*sync.RWMutex
}

var (
ErrKeyNotFound = errors.New("Key not found")
)

func newIndex() *index {
return &index{
entrys: make(map[string]*entry),
entries: make(map[string]*entry),
RWMutex: &sync.RWMutex{},
}
}

func (i *index) put(key string, entry *entry) {
i.Lock()
defer i.Unlock()
i.entrys[key] = entry
i.entries[key] = entry
}

func (i *index) get(key []byte) (*entry, error) {
i.Lock()
defer i.Unlock()
if entry, ok := i.entrys[string(key)]; ok {
if entry, ok := i.entries[string(key)]; ok {
return entry, nil
}

return nil, errors.New("key not found")
return nil, ErrKeyNotFound
}

func (i *index) del(key string) {
i.Lock()
defer i.Unlock()
delete(i.entrys, key)
delete(i.entries, key)
}
8 changes: 6 additions & 2 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ const (
)

type Option struct {
Dir string
MaxFileSize uint64
MergeSecs int
}

func NewOption(MaxFileSize uint64) Option {
func NewOption(dir string, MaxFileSize uint64) Option {
if MaxFileSize <= 0 {
MaxFileSize = defaultMaxFileSize
}
return Option{MaxFileSize: MaxFileSize}
return Option{
Dir: dir,
MaxFileSize: MaxFileSize,
}
}

0 comments on commit 30a2da4

Please sign in to comment.