spnr

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2025 License: MIT Imports: 10 Imported by: 0

README

spnr-go

A google spanner library for golang

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Db

type Db struct {
	Name   string
	Client *spanner.Client
}

A database.

func NewDbClient added in v1.0.1

func NewDbClient(ctx context.Context, database, role string) (*Db, error)

Returns a database client that only retries transactions if "Unavailable" is returned.

  • ctx: The context
  • database: The full name of the database in the format 'projects/*/instances/*/databases/*'
  • role: The database role to use for fine grained database access.

func (*Db) Mutate added in v1.0.1

func (d *Db) Mutate(ctx context.Context, txn WriteTxn, mutations ...*spanner.Mutation) error

Apply one/more row mutations on a Db. Creates a new ReadWrite transaction if none given.

func (*Db) ReadOnlyTxn added in v1.0.3

func (d *Db) ReadOnlyTxn() *spanner.ReadOnlyTransaction

Returns a read-only spanner transaction to read multiple rows at the exact same point in time.

func (*Db) ReadWriteTxn added in v1.0.3

func (d *Db) ReadWriteTxn(ctx context.Context, f func(context.Context, *spanner.ReadWriteTransaction) error) error

Executes a read-write transaction, with retries as necessary.

The function f will be called one or more times. It must not maintain any state between calls.

If the transaction cannot be committed or if f returns an ABORTED error, ReadWriteTransaction will call f again. It will continue to call f until the transaction can be committed or the Context times out or is cancelled. If f returns an error other than ABORTED, ReadWriteTransaction will abort the transaction and return the error.

To limit the number of retries, set a deadline on the Context rather than using a fixed limit on the number of attempts. ReadWriteTransaction will retry as needed until that deadline is met.

See https://godoc.org/cloud.google.com/go/spanner#ReadWriteTransaction for more details.

type QueryOpts added in v1.0.1

type QueryOpts struct {
	// The columns to select
	Cols []string
	// The sorting to apply
	SortCols []*SortedColumn
	// The max number of rows to return
	Limit int32
	// Start after these nr of rows; useful for pagination
	Offset int64
	// A filter to apply; preceeded by "WHERE" keyword
	Where string
}

type ReadTxn

type ReadTxn interface {
	ReadRow(ctx context.Context, table string, key spanner.Key, columns []string) (*spanner.Row, error)
	Read(ctx context.Context, table string, keys spanner.KeySet, columns []string) *spanner.RowIterator
	Query(ctx context.Context, statement spanner.Statement) *spanner.RowIterator
}

Interface satisfied by a ReadOnly and ReadWrite transactions.

type SortedColumn added in v1.0.1

type SortedColumn struct {
	Col        string
	Descending bool
}

A column to sort on

type Table added in v1.0.1

type Table[RowT any, KeyT any] struct {
	Db *Db
	// contains filtered or unexported fields
}

A table.

func NewTableClient added in v1.0.1

func NewTableClient[RowT any, KeyT any](db *Db, table string, columns []string, keyConverter func(key KeyT) spanner.Key, readConverter func(r *spanner.Row) (RowT, error), writeConverter func(row RowT) (map[string]any, error)) (*Table[RowT, KeyT], error)

Returns a table client.

  • db: A database client created with spnr.NewDbClient
  • table: The table name
  • columns: All the columns in the table including the key(s)
  • rowConverter: A function that takes in a spanner row and returns the generic type of this client

func (*Table[RowT, KeyT]) BatchDelete added in v1.0.1

func (t *Table[RowT, KeyT]) BatchDelete(ctx context.Context, txn WriteTxn, keys ...KeyT) error

Delete the rows at the given keys. Creates a new ReadWrite transaction if none given.

func (*Table[RowT, KeyT]) BatchRead added in v1.0.1

func (t *Table[RowT, KeyT]) BatchRead(ctx context.Context, txn ReadTxn, keys []KeyT, cols ...string) ([]RowT, error)

Reads the specified rows. Does not fail if a row is not found. If no txn given, a ReadOnly txn is created. Its closed after returning the result. If cols not specified, all columns are read.

func (*Table[RowT, KeyT]) Create added in v1.0.1

func (t *Table[RowT, KeyT]) Create(ctx context.Context, txn WriteTxn, row RowT) error

Creates a new row.

  • ctx: context
  • txn: optional spanner ReadWrite txn; if none given, one is created and also closed after the create
  • row: the new row's data

func (*Table[RowT, KeyT]) Delete added in v1.0.1

func (t *Table[RowT, KeyT]) Delete(ctx context.Context, txn WriteTxn, key KeyT) error

Delete the row at the given key. Creates a new ReadWrite transaction if none given.

func (*Table[RowT, KeyT]) Query added in v1.0.1

func (t *Table[RowT, KeyT]) Query(ctx context.Context, txn ReadTxn, opts *QueryOpts) ([]RowT, error)

Queries the rows in the table using the parameters specifed in opts. Creates a new ReadOnly txn if none given.

func (*Table[RowT, KeyT]) Read added in v1.0.1

func (t *Table[RowT, KeyT]) Read(ctx context.Context, txn ReadTxn, key KeyT, cols ...string) (RowT, error)

Reads the specified row. If no txn given, a ReadOnly txn is created. Its closed after returning the result. If cols not specified, all columns are read.

func (*Table[RowT, KeyT]) Update added in v1.0.1

func (t *Table[RowT, KeyT]) Update(ctx context.Context, txn WriteTxn, row RowT, cols ...string) error

Update an existing row.

  • ctx: context
  • txn: optional spanner ReadWrite txn; if none given, one is created and also closed after the update
  • row: the existing row's data
  • cols: optional list of columns to limit the update to.

type WriteTxn

type WriteTxn interface {
	BufferWrite(ms []*spanner.Mutation) error
}

Interface satisfied by a ReadWrite transaction.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL