这是indexloc提供的服务,不要输入任何密码
Skip to content

feat(spanner): wrap proto mutation #12497

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

Merged
merged 1 commit into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions spanner/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5937,7 +5937,7 @@ func TestClient_BatchWrite(t *testing.T) {
defer teardown()
mutationGroups := []*MutationGroup{
{[]*Mutation{
{opInsertOrUpdate, "t_test", nil, []string{"key", "val"}, []interface{}{"foo1", 1}},
{opInsertOrUpdate, "t_test", nil, []string{"key", "val"}, []interface{}{"foo1", 1}, nil},
}},
}
iter := client.BatchWrite(context.Background(), mutationGroups)
Expand Down Expand Up @@ -5972,7 +5972,7 @@ func TestClient_BatchWrite_SessionNotFound(t *testing.T) {
)
mutationGroups := []*MutationGroup{
{[]*Mutation{
{opInsertOrUpdate, "t_test", nil, []string{"key", "val"}, []interface{}{"foo1", 1}},
{opInsertOrUpdate, "t_test", nil, []string{"key", "val"}, []interface{}{"foo1", 1}, nil},
}},
}
iter := client.BatchWrite(context.Background(), mutationGroups)
Expand Down Expand Up @@ -6010,7 +6010,7 @@ func TestClient_BatchWrite_Error(t *testing.T) {
)
mutationGroups := []*MutationGroup{
{[]*Mutation{
{opInsertOrUpdate, "t_test", nil, []string{"key", "val"}, []interface{}{"foo1", 1}},
{opInsertOrUpdate, "t_test", nil, []string{"key", "val"}, []interface{}{"foo1", 1}, nil},
}},
}
iter := client.BatchWrite(context.Background(), mutationGroups)
Expand Down Expand Up @@ -6085,7 +6085,7 @@ func TestClient_BatchWrite_Options(t *testing.T) {

mutationGroups := []*MutationGroup{
{[]*Mutation{
{opInsertOrUpdate, "t_test", nil, []string{"key", "val"}, []interface{}{"foo1", 1}},
{opInsertOrUpdate, "t_test", nil, []string{"key", "val"}, []interface{}{"foo1", 1}, nil},
}},
}
iter := client.BatchWriteWithOptions(context.Background(), mutationGroups, tt.write)
Expand Down Expand Up @@ -6131,7 +6131,7 @@ func checkBatchWriteSpan(t *testing.T, errors []error, code codes.Code) {
)
mutationGroups := []*MutationGroup{
{[]*Mutation{
{opInsertOrUpdate, "t_test", nil, []string{"key", "val"}, []interface{}{"foo1", 1}},
{opInsertOrUpdate, "t_test", nil, []string{"key", "val"}, []interface{}{"foo1", 1}, nil},
}},
}
iter := client.BatchWrite(context.Background(), mutationGroups)
Expand Down Expand Up @@ -6683,7 +6683,7 @@ func TestClient_BatchWriteExcludeTxnFromChangeStreams(t *testing.T) {

mutationGroups := []*MutationGroup{
{[]*Mutation{
{opInsertOrUpdate, "t_test", nil, []string{"key", "val"}, []interface{}{"foo1", 1}},
{opInsertOrUpdate, "t_test", nil, []string{"key", "val"}, []interface{}{"foo1", 1}, nil},
}},
}
iter := client.BatchWriteWithOptions(context.Background(), mutationGroups, BatchWriteOptions{ExcludeTxnFromChangeStreams: true})
Expand Down
42 changes: 41 additions & 1 deletion spanner/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package spanner

import (
"fmt"
"math/rand"
"reflect"
"time"
Expand Down Expand Up @@ -141,6 +142,10 @@ type Mutation struct {
// values specifies the new values for the target columns
// named by Columns.
values []interface{}

// wrapped is the protobuf mutation that is the source for this Mutation.
// This is only set if the [WrapMutation] function was used to create the Mutation.
wrapped *sppb.Mutation
}

// A MutationGroup is a list of Mutation to be committed atomically.
Expand Down Expand Up @@ -356,6 +361,37 @@ func Delete(table string, ks KeySet) *Mutation {
}
}

// WrapMutation creates a mutation that wraps an existing protobuf mutation object.
func WrapMutation(proto *sppb.Mutation) (*Mutation, error) {
if proto == nil {
return nil, fmt.Errorf("protobuf mutation may not be nil")
}
op, table, err := getTableAndSpannerOperation(proto)
if err != nil {
return nil, err
}
return &Mutation{
op: op,
table: table,
wrapped: proto,
}, nil
}

func getTableAndSpannerOperation(proto *sppb.Mutation) (op, string, error) {
if proto.GetInsert() != nil {
return opInsert, proto.GetInsert().Table, nil
} else if proto.GetUpdate() != nil {
return opUpdate, proto.GetUpdate().Table, nil
} else if proto.GetReplace() != nil {
return opReplace, proto.GetReplace().Table, nil
} else if proto.GetDelete() != nil {
return opDelete, proto.GetDelete().Table, nil
} else if proto.GetInsertOrUpdate() != nil {
return opInsertOrUpdate, proto.GetInsertOrUpdate().Table, nil
}
return 0, "", spannerErrorf(codes.InvalidArgument, "unknown op type: %d", proto.Operation)
}

// prepareWrite generates sppb.Mutation_Write from table name, column names
// and new column values.
func prepareWrite(table string, columns []string, vals []interface{}) (*sppb.Mutation_Write, error) {
Expand All @@ -375,9 +411,13 @@ func errInvdMutationOp(m Mutation) error {
return spannerErrorf(codes.InvalidArgument, "Unknown op type: %d", m.op)
}

// proto converts spanner.Mutation to sppb.Mutation, in preparation to send
// proto converts a spanner.Mutation to sppb.Mutation, in preparation to send
// RPCs.
func (m Mutation) proto() (*sppb.Mutation, error) {
if m.wrapped != nil {
return m.wrapped, nil
}

var pb *sppb.Mutation
switch m.op {
case opDelete:
Expand Down
Loading
Loading