-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(node): Remove lru_map
dependency
#9300
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
Merged
AbhiPrasad
merged 2 commits into
getsentry:develop
from
timfish:feat/remove-lru_map-dep
Oct 18, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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
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
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
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
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
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
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,60 @@ | ||
/** A simple Least Recently Used map */ | ||
export class LRUMap<K, V> { | ||
private readonly _cache: Map<K, V>; | ||
|
||
public constructor(private readonly _maxSize: number) { | ||
this._cache = new Map<K, V>(); | ||
} | ||
|
||
/** Get the current size of the cache */ | ||
public get size(): number { | ||
return this._cache.size; | ||
} | ||
|
||
/** Get an entry or undefined if it was not in the cache. Re-inserts to update the recently used order */ | ||
public get(key: K): V | undefined { | ||
const value = this._cache.get(key); | ||
if (value === undefined) { | ||
return undefined; | ||
} | ||
// Remove and re-insert to update the order | ||
this._cache.delete(key); | ||
this._cache.set(key, value); | ||
return value; | ||
} | ||
|
||
/** Insert an entry and evict an older entry if we've reached maxSize */ | ||
public set(key: K, value: V): void { | ||
if (this._cache.size >= this._maxSize) { | ||
// keys() returns an iterator in insertion order so keys().next() gives us the oldest key | ||
this._cache.delete(this._cache.keys().next().value); | ||
} | ||
this._cache.set(key, value); | ||
} | ||
|
||
/** Remove an entry and return the entry if it was in the cache */ | ||
public remove(key: K): V | undefined { | ||
const value = this._cache.get(key); | ||
if (value) { | ||
this._cache.delete(key); | ||
} | ||
return value; | ||
} | ||
|
||
/** Clear all entries */ | ||
public clear(): void { | ||
this._cache.clear(); | ||
} | ||
|
||
/** Get all the keys */ | ||
public keys(): Array<K> { | ||
return Array.from(this._cache.keys()); | ||
} | ||
|
||
/** Get all the values */ | ||
public values(): Array<V> { | ||
const values: V[] = []; | ||
this._cache.forEach(value => values.push(value)); | ||
return values; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this over There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That triggers a lint:
This method is currently only used by some tests... |
||
} | ||
} |
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,41 @@ | ||
import { LRUMap } from '../src/lru'; | ||
|
||
describe('LRUMap', () => { | ||
test('evicts older entries when reaching max size', () => { | ||
const map = new LRUMap<string, string>(3); | ||
map.set('a', '1'); | ||
map.set('b', '2'); | ||
map.set('c', '3'); | ||
map.set('d', '4'); | ||
map.set('e', '5'); | ||
|
||
expect(map.keys()).toEqual(['c', 'd', 'e']); | ||
}); | ||
|
||
test('updates last used when calling get', () => { | ||
const map = new LRUMap<string, string>(3); | ||
map.set('a', '1'); | ||
map.set('b', '2'); | ||
map.set('c', '3'); | ||
|
||
map.get('a'); | ||
|
||
map.set('d', '4'); | ||
map.set('e', '5'); | ||
|
||
expect(map.keys()).toEqual(['a', 'd', 'e']); | ||
}); | ||
|
||
test('removes and returns entry', () => { | ||
const map = new LRUMap<string, string>(3); | ||
map.set('a', '1'); | ||
map.set('b', '2'); | ||
map.set('c', '3'); | ||
map.set('d', '4'); | ||
map.set('e', '5'); | ||
|
||
expect(map.remove('c')).toEqual('3'); | ||
|
||
expect(map.keys()).toEqual(['d', 'e']); | ||
}); | ||
}); |
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
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.
was this this just there for
lru_map
?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.
Yep!