Skip to content

Commit

Permalink
fix merge if amount of files is not power of 2
Browse files Browse the repository at this point in the history
  • Loading branch information
strogiyotec committed Jun 22, 2021
1 parent 2396092 commit fd372dc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ func main() {
}
vlog := NewVlog(parse.Vlog, parse.Checkpoint)
memtable := NewMemTable(parse.MemtableSize)
tree := NewLsmTree(vlog, parse.SStablePath, memtable, 30)
tree := NewLsmTree(vlog, parse.SStablePath, memtable, 120)
http.Start(tree)
}
9 changes: 8 additions & 1 deletion pkg/lsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func NewLsmTree(log *vlog, sstableDir string, memtable *Memtable, gc uint) *LsmT
return lsm
}

//Merge sstables, the final result is the sstable files with amount decreased by x2
func (lsm *LsmTree) Merge() error {
lsm.rwm.Lock()
defer lsm.rwm.Unlock()
Expand All @@ -67,8 +68,10 @@ func (lsm *LsmTree) Merge() error {
for index < len(lsm.sstables) {
firstReader, _ := os.Open(lsm.sstables[index])
secondReader, _ := os.Open(lsm.sstables[index+1])
//read two sstables
firstSStable := ReadTable(firstReader, lsm.log)
secondSStable := ReadTable(secondReader, lsm.log)
//merge them together into the single file
filePath, err := lsm.mergeFiles(firstSStable, secondSStable)
if err != nil {
return err
Expand All @@ -86,8 +89,8 @@ func (lsm *LsmTree) Merge() error {
}
index += 2
}
lsm.sstables = newSstableFiles
}
lsm.sstables = newSstableFiles
return nil
}
func (lsm *LsmTree) Get(key []byte) ([]byte, bool) {
Expand All @@ -104,6 +107,7 @@ func (lsm *LsmTree) Get(key []byte) ([]byte, bool) {
if err != nil {
panic(err)
}
//check if it's a tombstone
if len(entry.value) == len(tombstone) && bytes.Compare(entry.value, []byte(tombstone)) == 0 {
return nil, false
} else {
Expand Down Expand Up @@ -174,14 +178,17 @@ func (lsm *LsmTree) Flush() error {
}

func (lsm *LsmTree) save(entry *TableEntry) error {
//append to log
meta, err := lsm.log.Append(entry)
if err != nil {
return err
}
//save to memtable
err = lsm.memtable.Put(entry.key, meta)
if err != nil {
return err
}
//if full flush memtable to sstable
if lsm.memtable.isFull() {
err := lsm.Flush()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/memtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const (
tombstone = "THOMB" //in lsm tree , when key is deleted the value is marked as tombstone
)

// apple -> THOMB

//in memory redblack tree
type Memtable struct {
tree *rbt.Tree //red black tree where key is a string and value is ValueMeta that shows where value is stored in vlog
Expand Down

0 comments on commit fd372dc

Please sign in to comment.