From 9fc393beb40d9001f169d11683960bdc1e72a937 Mon Sep 17 00:00:00 2001 From: David Symonds Date: Wed, 21 Aug 2019 11:01:11 +1000 Subject: [PATCH] spanner/spannertest: simplify internal writeValues helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There's no need to pass pkIndexes through to the closures, since the passed row is already in table order, so the primary key is the initial t.pkCols columns of that row. Change-Id: I52c97ec5a4062a6d9d5878c46e28d65121cc1059 Reviewed-on: https://code-review.googlesource.com/c/gocloud/+/44234 Reviewed-by: kokoro Reviewed-by: Knut Olav Løite --- spanner/spannertest/db.go | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/spanner/spannertest/db.go b/spanner/spannertest/db.go index 677b2e30a0df..daa25a3a70e7 100644 --- a/spanner/spannertest/db.go +++ b/spanner/spannertest/db.go @@ -200,7 +200,7 @@ func (d *database) table(tbl string) (*table, error) { } // writeValues executes a write option (Insert, Update, etc.). -func (d *database) writeValues(tbl string, cols []string, values []*structpb.ListValue, f func(t *table, colIndexes, pkIndexes []int, r row) error) error { +func (d *database) writeValues(tbl string, cols []string, values []*structpb.ListValue, f func(t *table, colIndexes []int, r row) error) error { t, err := d.table(tbl) if err != nil { return err @@ -218,13 +218,11 @@ func (d *database) writeValues(tbl string, cols []string, values []*structpb.Lis revIndex[i] = j } - var pkIndexes []int for pki := 0; pki < t.pkCols; pki++ { - i, ok := revIndex[pki] + _, ok := revIndex[pki] if !ok { return status.Errorf(codes.InvalidArgument, "primary key column %s not included in write", t.cols[pki].Name) } - pkIndexes = append(pkIndexes, i) } for _, vs := range values { @@ -245,7 +243,7 @@ func (d *database) writeValues(tbl string, cols []string, values []*structpb.Lis } // TODO: enforce NOT NULL? - if err := f(t, colIndexes, pkIndexes, r); err != nil { + if err := f(t, colIndexes, r); err != nil { return err } } @@ -254,11 +252,8 @@ func (d *database) writeValues(tbl string, cols []string, values []*structpb.Lis } func (d *database) Insert(tbl string, cols []string, values []*structpb.ListValue) error { - return d.writeValues(tbl, cols, values, func(t *table, colIndexes, pkIndexes []int, r row) error { - var pk []interface{} - for _, i := range pkIndexes { - pk = append(pk, r[i]) - } + return d.writeValues(tbl, cols, values, func(t *table, colIndexes []int, r row) error { + pk := r[:t.pkCols] if t.rowForPK(pk) >= 0 { // TODO: how do we return `ALREADY_EXISTS`? return status.Errorf(codes.Unknown, "row already in table") @@ -270,11 +265,8 @@ func (d *database) Insert(tbl string, cols []string, values []*structpb.ListValu } func (d *database) Update(tbl string, cols []string, values []*structpb.ListValue) error { - return d.writeValues(tbl, cols, values, func(t *table, colIndexes, pkIndexes []int, r row) error { - var pk []interface{} - for _, i := range pkIndexes { - pk = append(pk, r[i]) - } + return d.writeValues(tbl, cols, values, func(t *table, colIndexes []int, r row) error { + pk := r[:t.pkCols] rowNum := t.rowForPK(pk) if rowNum < 0 { // TODO: is this the right way to return `NOT_FOUND`? @@ -289,11 +281,8 @@ func (d *database) Update(tbl string, cols []string, values []*structpb.ListValu } func (d *database) InsertOrUpdate(tbl string, cols []string, values []*structpb.ListValue) error { - return d.writeValues(tbl, cols, values, func(t *table, colIndexes, pkIndexes []int, r row) error { - var pk []interface{} - for _, i := range pkIndexes { - pk = append(pk, r[i]) - } + return d.writeValues(tbl, cols, values, func(t *table, colIndexes []int, r row) error { + pk := r[:t.pkCols] rowNum := t.rowForPK(pk) if rowNum < 0 { // New row; do an insert.