Skip to content
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

refactor(sdk): Use Mapping instead of CachingMap #2903

Merged
merged 32 commits into from
Dec 4, 2024

Conversation

teogeb
Copy link
Contributor

@teogeb teogeb commented Nov 22, 2024

Use Mapping cache instead of CachingMap in StreamRegistry and StreamStorageRegistry. After these modifications there are no usages for CacheMap in sdk, and we can remove it (in a follow-up PR).

Testing

The implementations of Mapping and CachingMap are very different, but the functionality should be almost/exactly same. Double-check that there are no corner case in which behavior is significantly different. Checked that CachingMap tests pass also with the new implementation (see temporary commit e5bfdcf). The same functionality was already covered by Mapping.test.ts, so there was no need keep any CachingMap.test.ts test cases for the Mapping class.

Other changes

  • Changed argument type of Mapping#get() and Mapping#valueFactory(), and return type of Mapping#values()

Future improvements

  • Remove CachingMap

@github-actions github-actions bot added the sdk label Nov 22, 2024
@teogeb teogeb changed the title refactor(sdk): Use Mapping instead of CacheMap refactor(sdk): Use Mapping instead of CachingMap Nov 22, 2024
teogeb added a commit that referenced this pull request Nov 26, 2024
Added `maxSize` and `maxAge` options to the `Mapping` class. These are
needed in follow-up PR
(#2903).

Added two builder functions: `createCacheMap()` and `createLazyMap()`.
This way the user of the class needs to decide if the Mapping if is a
cache or just a lazy loading map. For caches it is required to configure
`maxSize`.

## Type safety

The constructor of `Mapping` is annotated as `@internal` so that in
encourages the usage of the builder functions. If we make it private,
the builder functions needs to have access to it somehow. Could e.g. be
static methods. Or we could export `Mapping` as a type.

## Testing

In unit tests we test the basic functionality for the lazy map, and use
cache map only test the size limitations. This is maybe ok as in the
`Mapping` the differences are abstracted out to the `delegate` (which is
either a LRU cache or plain `Map`).

## Future improvements

- Use `Mapping` instead of `CacheMap` in
`StreamRegistry`/`StreamStorageRegistry` and remove the `CacheMap` class
Base automatically changed from sdk-mapping-cache-constraints to main November 26, 2024 21:05
Copy link
Contributor

@mondoreale mondoreale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I have a couple of loose suggestions:

I'd see the key to be a primitive by default OR something more complex if needed.

For a tuple
const a = new Mapping<[number, string], string>()

And for a string
const a = new Mapping()

And for a number
const a = new Mapping<number>()

So, in short, basic KeyType could be anything that satisfies K extends string | number | symbol. Then the applied key could be of type K | K[].

type Key<K> = K | K[]

class Mapping<K extends Key<string | number | symbol> = string, V = string> {}

To simplify usage of more complex keys we could rewrite formLookupKey to JSON.stringify(key).

↑, maybe worth considering. It'd also allow to bypass 1-element tuple keys.

packages/sdk/src/utils/Mapping.ts Outdated Show resolved Hide resolved
@teogeb
Copy link
Contributor Author

teogeb commented Dec 4, 2024

I'd see the key to be a primitive by default OR something more complex if needed.

Added this functionality 👍 Didn't set any default types as string is just one possibility.

Copy link
Contributor

@harbu harbu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had a quick read thru; lgtm

@teogeb teogeb merged commit 9a31260 into main Dec 4, 2024
24 checks passed
@teogeb teogeb deleted the sdk-use-Mapping-instead-of-CacheMap-in-registries branch December 4, 2024 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants