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

Store based on previous value #123

Open
german94 opened this issue Feb 25, 2024 · 1 comment
Open

Store based on previous value #123

german94 opened this issue Feb 25, 2024 · 1 comment
Labels
question Further information is requested

Comments

@german94
Copy link

Hello,

First of all thanks for this package, I really like it and I'm using it in a few of my side projects.

I just wanted to ask something regarding the MapOf type. Is there a way to store for a key, based on the previous value, atomically?

For example, I'm doing this:

	var new []string
	old, ok := ms.data.Load(id)
	if ok {
		new = old
	}
	new = append(new, values...)
	ms.data.Store(id, new)

however I'd like to do it in an atomic way, everything in a function.

Thanks.

@puzpuzpuz puzpuzpuz added the question Further information is requested label Feb 26, 2024
@puzpuzpuz
Copy link
Owner

puzpuzpuz commented Feb 26, 2024

Thanks for the feedback!

As for updating a value atomically, that's simple. You just need to use Compute method:

func main() {
	ms := xsync.NewMapOf[int, []string]()

	id := 42
	values := make([]string, 3)
	// Update the value atomically.
	v, ok := ms.Compute(id, func(oldValue []string, loaded bool) (newValue []string, delete bool) {
		if loaded { // Existing key case
			// It's important to allocate a new slice each time we mutate a value.
			// That's to avoid races with concurrent readers.
			newValue = append(make([]string, 0, len(oldValue)+len(values)), oldValue...)
			newValue = append(newValue, values...)
		} else { // New key case
			newValue = values
		}
		delete = false
		return
	})
	fmt.Printf("v: %v, ok: %v\n", v, ok)
}

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

No branches or pull requests

2 participants