Skip to content

Commit

Permalink
Merge pull request #1212 from ecordell/spannerclose
Browse files Browse the repository at this point in the history
spanner: close row iterators when done
  • Loading branch information
ecordell authored Mar 20, 2023
2 parents cf1004c + d4e47b9 commit 03896f6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 34 deletions.
14 changes: 5 additions & 9 deletions internal/datastore/spanner/migrations/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,22 @@ func NewSpannerDriver(database, credentialsFilePath, emulatorHost string) (*Span
}

func (smd *SpannerMigrationDriver) Version(ctx context.Context) (string, error) {
rows := smd.client.Single().Read(
var schemaRevision string
if err := smd.client.Single().Read(
ctx,
tableSchemaVersion,
spanner.AllKeys(),
[]string{colVersionNum},
)
row, err := rows.Next()
if err != nil {
).Do(func(r *spanner.Row) error {
return r.Columns(&schemaRevision)
}); err != nil {
if spanner.ErrCode(err) == codes.NotFound {
// There is no schema table, empty database
return "", nil
}
return "", err
}

var schemaRevision string
if err := row.Columns(&schemaRevision); err != nil {
return "", err
}

return schemaRevision, nil
}

Expand Down
12 changes: 3 additions & 9 deletions internal/datastore/spanner/revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,10 @@ func (sd spannerDatastore) now(ctx context.Context) (time.Time, error) {
ctx, span := tracer.Start(ctx, "now")
defer span.End()

iter := sd.client.Single().Query(ctx, spanner.NewStatement("SELECT CURRENT_TIMESTAMP()"))
defer iter.Stop()

row, err := iter.Next()
if err != nil {
return time.Time{}, err
}

var timestamp time.Time
if err := row.Columns(&timestamp); err != nil {
if err := sd.client.Single().Query(ctx, spanner.NewStatement("SELECT CURRENT_TIMESTAMP()")).Do(func(r *spanner.Row) error {
return r.Columns(&timestamp)
}); err != nil {
return time.Time{}, err
}

Expand Down
24 changes: 8 additions & 16 deletions internal/datastore/spanner/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@ import (
var queryRelationshipEstimate = fmt.Sprintf("SELECT SUM(%s) FROM %s", colCount, tableCounters)

func (sd spannerDatastore) Statistics(ctx context.Context) (datastore.Stats, error) {
idRows := sd.client.Single().Read(
var uniqueID string
if err := sd.client.Single().Read(
context.Background(),
tableMetadata,
spanner.AllKeys(),
[]string{colUniqueID},
)
idRow, err := idRows.Next()
if err != nil {
return datastore.Stats{}, fmt.Errorf("unable to read metadata table: %w", err)
}

var uniqueID string
if err := idRow.Columns(&uniqueID); err != nil {
).Do(func(r *spanner.Row) error {
return r.Columns(&uniqueID)
}); err != nil {
return datastore.Stats{}, fmt.Errorf("unable to read unique ID: %w", err)
}

Expand All @@ -44,14 +40,10 @@ func (sd spannerDatastore) Statistics(ctx context.Context) (datastore.Stats, err
}

var estimate spanner.NullInt64
countRows := sd.client.Single().Query(ctx, spanner.Statement{SQL: queryRelationshipEstimate})
countRow, err := countRows.Next()
if err != nil && spanner.ErrCode(err) != codes.NotFound {
if err := sd.client.Single().Query(ctx, spanner.Statement{SQL: queryRelationshipEstimate}).Do(func(r *spanner.Row) error {
return r.Columns(&estimate)
}); err != nil {
return datastore.Stats{}, fmt.Errorf("unable to read row counts: %w", err)
} else if err == nil {
if err := countRow.Columns(&estimate); err != nil {
return datastore.Stats{}, fmt.Errorf("unable to decode row count: %w", err)
}
}

return datastore.Stats{
Expand Down

0 comments on commit 03896f6

Please sign in to comment.