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

GetOrSet and map key types #16

Closed
f8lft opened this issue Oct 22, 2022 · 5 comments
Closed

GetOrSet and map key types #16

f8lft opened this issue Oct 22, 2022 · 5 comments

Comments

@f8lft
Copy link

f8lft commented Oct 22, 2022

Hello! Thank you for your awesome package and for the recent (important) GetOrSet functionality.

I want to ask if you can allow (and implement) the usage of function (as the value constructor) which will be called just once (to avoid unnecessary value construction logic everytime using GetOrSet).

The second thing is to ask to allow the usage of types which underlying type is among of those which your hashable supports. For example, type SeqId uint64.

@alphadose
Copy link
Owner

@f8lft

I want to ask if you can allow (and implement) the usage of function (as the value constructor) which will be called just once (to avoid unnecessary value construction logic everytime using GetOrSet).

Could you elaborate on this with an example code snippet so that I can get a better idea ?

The second thing is to ask to allow the usage of types which underlying type is among of those which your hashable supports. For example, type SeqId uint64.

Sure this is useful and also possible, I will add it in the next release.

@f8lft
Copy link
Author

f8lft commented Oct 22, 2022

Could you elaborate on this with an example code snippet so that I can get a better idea ?

Sure. Please, have a look.

ex2

A thread-safe value constructor doesn't necessarily have to return an error (this is just an example and it's up to you and to your design to make this error aware or not. 😃 ).

@f8lft
Copy link
Author

f8lft commented Oct 22, 2022

Another interesting and useful thing is the ability to atomically take and remove an element from the map (to prevent mutation due to other threads which may access the same key at the same time). You can easily imagine such behavior if your Del would return the deleted values ^_^.

T1: loading the value by key 5; (haxmap.Get)
T2: loading the value by key 5; (haxmap.Get)
T1: deleting the value (haxmap.Del) and perfoming the neccessary mutation ("singleflight", for more sense :) ) on the value;
T2: may still load the same value, may perform the same logic 😢 , becase of the gap between load and delete;

@alphadose
Copy link
Owner

got it, will implement both features :)

@alphadose
Copy link
Owner

@f8lft deletion is now thread-safe and can be used concurrently with other operations

GetOrCompute was added with ba78b2b which allows setting the value in GetOrSet with a value constructor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants