googleapis / google-cloud-go

Google Cloud Client Libraries for Go.
https://cloud.google.com/go/docs/reference
Apache License 2.0
3.78k stars 1.3k forks source link

spanner: add (Batch)?Update(WithOptions)? to Client #9583

Open CAFxX opened 8 months ago

CAFxX commented 8 months ago

Is your feature request related to a problem? Please describe. If a ReadWriteTransaction needs to perform only a batch of one or more update operations, it is wasteful and unneccessary to keep the transaction open for an extra roundtrip.

Describe the solution you'd like

Add the following methods to spanner.Client:

Update(ctx context.Context, stmt Statement) (rowCount int64, err error)
UpdateWithOptions(ctx context.Context, stmt Statement, opts QueryOptions) (rowCount int64, err error)
BatchUpdate(ctx context.Context, stmts []Statement) (rowCounts []int64, err error)
BatchUpdateWithOptions(ctx context.Context, stmts []Statement, opts QueryOptions) (rowCounts []int64, err error)

that would behave as a ReadWriteTransaction containing a single call to corresponding method above, so e.g.

rowCount, err := client.Update(ctx, stmt)

would behave as

var rowCount int
err := client.ReadWriteTransaction(ctx, func(ctx context.Context, rwt *spanner.ReadWriteTransaction) (err error) {
    rowCount, err = rwt.Update(ctx, stmt)
    return
})

but allowing Spanner to close the transaction as soon as the update completes, without waiting for the client to explicitly commit.

Describe alternatives you've considered The equivalent code above works today, but is less efficient.

CAFxX commented 7 months ago

@rahul2393 I suspect this was closed by mistake...

rahul2393 commented 7 months ago

@CAFxX reopened

rahul2393 commented 7 months ago

@CAFxX can you please explain how having these methods will be more optimal, auto commit is not yet supported in Cloud Spanner so clients need to explicitly send the commit RPC at the end of request.