gokv icon indicating copy to clipboard operation
gokv copied to clipboard

Batch operations

Open simar7 opened this issue 4 years ago • 3 comments

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.

simar7 avatar Nov 08 '19 01:11 simar7

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?

philippgille avatar Jan 04 '20 16:01 philippgille

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.)

simar7 avatar Jan 06 '20 04:01 simar7

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

glimchb avatar Oct 25 '23 21:10 glimchb