Conversation
ayushjain2809
left a comment
There was a problem hiding this comment.
Thanks for the issue and PR. I have a similar use case.
I have a couple of suggestions, which combined will:
- Pass
existstoUpdateCbto allow distinguishing between zero-values and non-existent ones. This is also consistent withUpsert,RemoveCb - Allow
UpdateCbto cancel the update if needed.Updatewould return the final value and a bool to indicate whether the update went through.
concurrent_map.go
Outdated
| // It is called while lock is held, therefore it MUST NOT | ||
| // try to access other keys in same map, as it can lead to deadlock since | ||
| // Go sync.RWLock is not reentrant | ||
| type UpdateCb[V any] func(valueInMap V) V |
There was a problem hiding this comment.
Suggested change
| type UpdateCb[V any] func(valueInMap V) V | |
| type UpdateCb[V any] func(exists bool, valueInMap V) (newVal V, ok bool) |
|
|
||
| // Update an existing element using UpdateCb, assuming the key exists. | ||
| // If it does not, the zero value for the value type is passed to the callback. | ||
| func (m ConcurrentMap[K, V]) Update(key K, cb UpdateCb[V]) (res V) { |
There was a problem hiding this comment.
Suggested change
| func (m ConcurrentMap[K, V]) Update(key K, cb UpdateCb[V]) (res V) { | |
| func (m ConcurrentMap[K, V]) Update(key K, cb UpdateCb[V]) (res V, ok bool) { |
The update method allows atomically mutating a value in the map. If the key does not exist, the zero value for the value type is mutated and saved.
Author
|
Thanks for the suggestion @ayushjain2809 , updated accordingly. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The update method allows atomically mutating a value in the map. If the key does not exist, the zero value for the value type is mutated and saved.
Fixes #133