-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add compaction functions * add wal test, clean up IO * grab writeLock in Update * reorganize into separate files
- Loading branch information
Showing
5 changed files
with
750 additions
and
458 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++ | ||
} | ||
} |
Oops, something went wrong.