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

Batch operations #87

Open
simar7 opened this issue Nov 8, 2019 · 3 comments
Open

Batch operations #87

simar7 opened this issue Nov 8, 2019 · 3 comments

Comments

@simar7
Copy link

simar7 commented Nov 8, 2019

Hello!

Thank you for this wonderful library, I really like the simplicity of it.

Would there be in the future support for batch updates and/or additions? For example BoltDB has support for Batch updates.

@philippgille
Copy link
Owner

Hi! Sorry for the late reply!

Do you mean something exactly like the bbolt Batch method you linked to, where the docs say:

Batch is only useful when there are multiple goroutines calling it.

Or do you mean batch methods in general, where you can set/delete multiple values within a single transaction, so the required gokv.Store method would looke something like SetBatch(key string, values ...interface{}) error (with values being variadic) or similar?

Method signature options could then be (for this latter understanding of what you mean with batch):

package main

import (
	"fmt"
)

func main() {
	fmt.Println("Hello, playground")

	key := "k"

	setBatch1(key, 1, "2")

	vals := make([]interface{}, 2)
	vals[0] = 1
	vals[1] = "2"

	setBatch2(key, vals)

	setBatch3(key, vals)
}

func setBatch1(key string, values ...interface{}) {
	fmt.Printf("values[0]: %v (%T); values[1]: %v (%T)\n", values[0], values[0], values[1], values[1])
}

func setBatch2(key string, values []interface{}) {
	fmt.Printf("values[0]: %v (%T); values[1]: %v (%T)\n", values[0], values[0], values[1], values[1])
}

func setBatch3(key string, values interface{}) {
	vals, ok := values.([]interface{})
	if !ok {
		panic("nok")
	}
	fmt.Printf("values[0]: %v (%T); values[1]: %v (%T)\n", vals[0], vals[0], vals[1], vals[1])
}

(https://play.golang.org/p/HlOR1I6P81f)

But:

Not all of the databases that have a gokv.Store implementation support batch operations natively. For other DBs you could implement a convenience method that then calls the single Set method multiple times for all elements of the slice, but then what about databases that don't support transactions? You could ignore transactions and just call Set multiple times, but the method caller (package user) might expect an atomic transaction for each SetBatch call.

What do you think?

@simar7
Copy link
Author

simar7 commented Jan 6, 2020

Hey! Yes I meant to say that the bbolt batch method could call the underlying bbolt batch method as I had linked earlier.

Not all of the databases that have a gokv.Store implementation support batch operations natively. For other DBs you could implement a convenience method that then calls the single Set method multiple times for all elements of the slice, but then what about databases that don't support transactions? You could ignore transactions and just call Set multiple times, but the method caller (package user) might expect an atomic transaction for each SetBatch call.

Yes precisely this. Where it makes sense, the implementation will call the batch operation for that datastore. Where it doesn't, we could invoke Set multiple times, or better and more safer, not have it implemented and leave the decision on the user to decide if they wanted to implement it themselves (by wrapping regular Set in a loop, interface embedding and extending it, etc.)

@glimchb
Copy link
Contributor

glimchb commented Oct 25, 2023

for atomicity, we can take a mutex... and then call loop of sets... no ?
obviously, only applicable if called from the same app...

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

3 participants