Skip to content

Commit

Permalink
build data
Browse files Browse the repository at this point in the history
  • Loading branch information
qichengzx committed Mar 4, 2023
1 parent d337442 commit ee1d9f2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
44 changes: 24 additions & 20 deletions bitcask.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bitcask

import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -65,8 +64,6 @@ func (b *Bitcask) loadIndex() {
continue
}
fid, _ := getFid(file.Name())
fmt.Println(file.Name(), fid)
var offset int64 = 0

hintFp, err := openHintFile(b.option.Dir, fid)
if err == nil && hintFp != nil {
Expand All @@ -81,27 +78,34 @@ func (b *Bitcask) loadIndex() {

hintfp, _ := newHintFile(b.option.Dir, fid)
b.oldFiles.add(fid, bitFile)
for {
entry, entrySize := bitFile.newEntryFromBuf(offset)
if entry == nil {
break
}
offset += int64(entrySize)

if entry.valueSize == 0 {
b.index.del(string(entry.key))
//key was deleted
continue
}

//load to map
b.index.put(string(entry.key), entry)
putHint(hintfp, entry.key, uint32(len(entry.key)), uint32(entry.valueSize), uint32(offset))
}

b.buildFromData(bitFile, hintfp)
}
}
log.Println("load old file use:", time.Since(t1).String())
}

func (b *Bitcask) buildFromData(bfile *BitFile, fp *os.File) {
var offset int64 = 0
for {
entry, entrySize, key := bfile.newEntryFromBuf(offset)
if entry == nil {
break
}
offset += int64(entrySize)

if entry.valueSize == 0 {
b.index.del(string(key))
//key was deleted
continue
}

//load to index
b.index.put(string(key), entry)
putHint(fp, key, uint32(len(key)), uint32(entry.valueSize), uint32(offset))
}
}

func (b *Bitcask) Put(key, value []byte) error {
b.mu.Lock()
defer b.mu.Unlock()
Expand Down
10 changes: 5 additions & 5 deletions db_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (bf *BitFile) newFid() string {
return fmt.Sprintf("%06d", bf.fid)
}

func (bf *BitFile) newEntryFromBuf(offset int64) (*entry, uint32) {
func (bf *BitFile) newEntryFromBuf(offset int64) (*entry, uint32, []byte) {
return newEntryFromBuf(bf.fp, bf.fid, offset)
}

Expand Down Expand Up @@ -219,11 +219,11 @@ const (
mergeFileExt = ".merge"
)

func newEntryFromBuf(fp *os.File, fid uint32, offset int64) (*entry, uint32) {
func newEntryFromBuf(fp *os.File, fid uint32, offset int64) (*entry, uint32, []byte) {
buf, err := read(fp, offset, HeaderSize)
if err != nil {
if err == io.EOF {
return nil, 0
return nil, 0, nil
}
}
ts := binary.BigEndian.Uint32(buf[4:8])
Expand All @@ -233,13 +233,13 @@ func newEntryFromBuf(fp *os.File, fid uint32, offset int64) (*entry, uint32) {
entrySize := getSize(keySize, valueSize)
keyByte := make([]byte, keySize)
if _, err := fp.ReadAt(keyByte, offset+HeaderSize); err != nil {
return nil, 0
return nil, 0, nil
}

entry := newEntry(fid, keySize, valueSize, uint64(offset)+uint64(HeaderSize+keySize), uint64(ts))
entry.key = keyByte

return entry, entrySize
return entry, entrySize, keyByte
}

func newMergeFileName(dir string, fid uint32) string {
Expand Down

0 comments on commit ee1d9f2

Please sign in to comment.