Skip to content

Commit

Permalink
Add compaction functions (#39)
Browse files Browse the repository at this point in the history
* add compaction functions

* add wal test, clean up IO

* grab writeLock in Update

* reorganize into separate files
  • Loading branch information
Preetam authored Jul 22, 2017
1 parent caae31f commit 720acf9
Show file tree
Hide file tree
Showing 5 changed files with 750 additions and 458 deletions.
93 changes: 93 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package lm2

import (
"math/rand"
"sync"
)

type recordCache struct {
cache map[int64]*record
maxKeyRecord *record
size int
preventPurge bool
lock sync.RWMutex
}

func newCache(size int) *recordCache {
return &recordCache{
cache: map[int64]*record{},
maxKeyRecord: nil,
size: size,
}
}

func (rc *recordCache) findLastLessThan(key string) int64 {
rc.lock.RLock()
defer rc.lock.RUnlock()

if rc.maxKeyRecord != nil {
if rc.maxKeyRecord.Key < key {
return rc.maxKeyRecord.Offset
}
}
max := ""
maxOffset := int64(0)

for offset, record := range rc.cache {
if record.Key >= key {
continue
}
if record.Key > max {
max = record.Key
maxOffset = offset
}
}
return maxOffset
}

func (rc *recordCache) push(rec *record) {
rc.lock.RLock()

if rc.maxKeyRecord == nil || rc.maxKeyRecord.Key < rec.Key {
rc.lock.RUnlock()

rc.lock.Lock()
if rc.maxKeyRecord == nil || rc.maxKeyRecord.Key < rec.Key {
rc.maxKeyRecord = rec
}
rc.lock.Unlock()

return
}

if len(rc.cache) == rc.size && rand.Float32() >= cacheProb {
rc.lock.RUnlock()
return
}

rc.lock.RUnlock()
rc.lock.Lock()

rc.cache[rec.Offset] = rec
if !rc.preventPurge {
rc.purge()
}

rc.lock.Unlock()
}

func (rc *recordCache) purge() {
purged := 0
for len(rc.cache) > rc.size {
deletedKey := int64(0)
for k := range rc.cache {
if k == rc.maxKeyRecord.Offset {
continue
}
deletedKey = k
break
}
delete(rc.cache, deletedKey)
purged++
}
}
Loading

0 comments on commit 720acf9

Please sign in to comment.