-
Notifications
You must be signed in to change notification settings - Fork 871
Helper files for flatKV cache #3056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
111459f
Helper files for flatKV cache
d40395f
add missing struct
c8e85d2
Merge branch 'main' into cjl/cache-auxilery
ed7e4b6
made suggested changes
5c46647
fix tests
e404540
Merge branch 'main' into cjl/cache-auxilery
cody-littley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,45 @@ | ||
| package pebblecache | ||
|
|
||
| import "github.com/sei-protocol/sei-chain/sei-db/db_engine/types" | ||
|
|
||
| // Cache describes a cache capable of being used by a FlatKV store. | ||
| type Cache interface { | ||
|
|
||
| // Get returns the value for the given key, or (nil, false) if not found. | ||
| Get( | ||
| // The entry to fetch. | ||
| key []byte, | ||
| // If true, the LRU queue will be updated. If false, the LRU queue will not be updated. | ||
| // Useful for when an operation is performed multiple times in close succession on the same key, | ||
| // since it requires non-zero overhead to do so with little benefit. | ||
| updateLru bool, | ||
| ) ([]byte, bool, error) | ||
|
|
||
| // Perform a batch read operation. Given a map of keys to read, performs the reads and updates the | ||
| // map with the results. | ||
| // | ||
| // It is not thread safe to read or mutate the map while this method is running. | ||
| BatchGet(keys map[string]types.BatchGetResult) error | ||
|
|
||
| // Set sets the value for the given key. | ||
| Set(key []byte, value []byte) | ||
|
|
||
| // Delete deletes the value for the given key. | ||
| Delete(key []byte) | ||
|
|
||
| // BatchSet applies the given updates to the cache. | ||
| BatchSet(updates []CacheUpdate) error | ||
| } | ||
|
|
||
| // CacheUpdate describes a single key-value mutation to apply to the cache. | ||
| type CacheUpdate struct { | ||
| // The key to update. | ||
| Key []byte | ||
| // The value to set. If nil, the key will be deleted. | ||
| Value []byte | ||
| } | ||
|
|
||
| // IsDelete returns true if the update is a delete operation. | ||
| func (u *CacheUpdate) IsDelete() bool { | ||
| return u.Value == nil | ||
| } |
This file contains hidden or 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,83 @@ | ||
| package pebblecache | ||
|
|
||
| import "container/list" | ||
|
|
||
| // Implements a queue-like abstraction with LRU semantics. Not thread safe. | ||
| type lruQueue struct { | ||
| order *list.List | ||
| entries map[string]*list.Element | ||
| totalSize uint64 | ||
| } | ||
|
|
||
| type lruQueueEntry struct { | ||
| key string | ||
| size uint64 | ||
| } | ||
|
|
||
| // Create a new LRU queue. | ||
| func newLRUQueue() *lruQueue { | ||
| return &lruQueue{ | ||
| order: list.New(), | ||
| entries: make(map[string]*list.Element), | ||
| } | ||
| } | ||
|
|
||
| // Add a new entry to the LRU queue. Can also be used to update an existing value with a new weight. | ||
| func (lru *lruQueue) Push( | ||
| // the key in the cache that was recently interacted with | ||
| key []byte, | ||
| // the size of the key + value | ||
| size uint64, | ||
| ) { | ||
| if elem, ok := lru.entries[string(key)]; ok { | ||
| entry := elem.Value.(*lruQueueEntry) | ||
| lru.totalSize += size - entry.size | ||
| entry.size = size | ||
| lru.order.MoveToBack(elem) | ||
| return | ||
| } | ||
|
|
||
| keyStr := string(key) | ||
| elem := lru.order.PushBack(&lruQueueEntry{ | ||
| key: keyStr, | ||
| size: size, | ||
| }) | ||
| lru.entries[keyStr] = elem | ||
| lru.totalSize += size | ||
| } | ||
|
|
||
| // Signal that an entry has been interated with, moving it to the back of the queue | ||
| // (i.e. making it so it doesn't get popped soon). | ||
| func (lru *lruQueue) Touch(key []byte) { | ||
| elem, ok := lru.entries[string(key)] | ||
| if !ok { | ||
| return | ||
| } | ||
| lru.order.MoveToBack(elem) | ||
| } | ||
|
|
||
| // Returns the total size of all entries in the LRU queue. | ||
| func (lru *lruQueue) GetTotalSize() uint64 { | ||
| return lru.totalSize | ||
| } | ||
|
|
||
| // Returns a count of the number of entries in the LRU queue, where each entry counts for 1 regardless of size. | ||
| func (lru *lruQueue) GetCount() uint64 { | ||
| return uint64(len(lru.entries)) | ||
| } | ||
|
|
||
| // Pops a single element out of the queue. The element removed is the entry least recently passed to Update(). | ||
| // Returns the key in string form to avoid copying the key an additional time. | ||
| // Panics if the queue is empty. | ||
| func (lru *lruQueue) PopLeastRecentlyUsed() string { | ||
| elem := lru.order.Front() | ||
| if elem == nil { | ||
| panic("cannot pop from empty LRU queue") | ||
| } | ||
|
|
||
| lru.order.Remove(elem) | ||
| entry := elem.Value.(*lruQueueEntry) | ||
| delete(lru.entries, entry.key) | ||
| lru.totalSize -= entry.size | ||
| return entry.key | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Push() accepts negative values
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed type to uint64