store

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: May 5, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec interface {
	Encode(value []byte) (string, error)
	Decode(value string) ([]byte, error)
}

Codec is an interface for encoding and decoding the data provided by the client. At the moment, only key-value store requires data encoding.

type GoogleSheetCountStmt

type GoogleSheetCountStmt struct {
	// contains filtered or unexported fields
}

GoogleSheetCountStmt encapsulates information required to count the number of rows matching some conditions.

func (*GoogleSheetCountStmt) Exec

Exec counts the number of rows matching the provided condition.

There is only 1 API call behind the scene.

func (*GoogleSheetCountStmt) Where

func (s *GoogleSheetCountStmt) Where(condition string, args ...interface{}) *GoogleSheetCountStmt

Where specifies the condition to choose which rows are affected.

It works just like the GoogleSheetSelectStmt.Where() method. Please read GoogleSheetSelectStmt.Where() for more details.

type GoogleSheetDeleteStmt

type GoogleSheetDeleteStmt struct {
	// contains filtered or unexported fields
}

GoogleSheetDeleteStmt encapsulates information required to delete rows.

func (*GoogleSheetDeleteStmt) Exec

Exec deletes rows matching the condition.

There are 2 API calls behind the scene.

func (*GoogleSheetDeleteStmt) Where

func (s *GoogleSheetDeleteStmt) Where(condition string, args ...interface{}) *GoogleSheetDeleteStmt

Where specifies the condition to choose which rows are affected.

It works just like the GoogleSheetSelectStmt.Where() method. Please read GoogleSheetSelectStmt.Where() for more details.

type GoogleSheetInsertStmt

type GoogleSheetInsertStmt struct {
	// contains filtered or unexported fields
}

GoogleSheetInsertStmt encapsulates information required to insert new rows into the Google Sheet.

func (*GoogleSheetInsertStmt) Exec

Exec inserts the provided new rows data into Google Sheet. This method calls the relevant Google Sheet APIs to actually insert the new rows.

There is only 1 API call behind the scene.

type GoogleSheetKVStore

type GoogleSheetKVStore struct {
	// contains filtered or unexported fields
}

GoogleSheetKVStore encapsulates key-value store functionality on top of a Google Sheet.

There are 2 operation modes for the key-value store: default and append only mode.

For more details on how they differ, please read the explanations for each method or the protocol page: https://github.com/FreeLeh/docs/blob/main/freedb/protocols.md.

func NewGoogleSheetKVStore

func NewGoogleSheetKVStore(
	auth sheets.AuthClient,
	spreadsheetID string,
	sheetName string,
	config GoogleSheetKVStoreConfig,
) *GoogleSheetKVStore

NewGoogleSheetKVStore creates an instance of the key-value store with the given configuration. It will also try to create the sheet, in case it does not exist yet.

func (*GoogleSheetKVStore) Close

func (s *GoogleSheetKVStore) Close(ctx context.Context) error

Close cleans up all held resources like the scratchpad cell booked for this specific GoogleSheetKVStore instance.

func (*GoogleSheetKVStore) Delete

func (s *GoogleSheetKVStore) Delete(ctx context.Context, key string) error

Delete deletes the given key from the key-value store.

In default mode,

  • If the key is not in the store, it will not do anything.
  • If the key is in the store, it will remove that row.
  • There are up to 2 API calls behind the scene: getting the row for the key and remove the row (if the key exists).

In append only mode,

  • It creates a new row at the bottom of the sheet with a tombstone value and timestamp.
  • There is only 1 API call behind the scene.

func (*GoogleSheetKVStore) Get

func (s *GoogleSheetKVStore) Get(ctx context.Context, key string) ([]byte, error)

Get retrieves the value associated with the given key. If the key exists in the store, the raw bytes value and no error will be returned. If the key does not exist in the store, a nil []byte and a wrapped ErrKeyNotFound will be returned.

In default mode,

  • There will be only one row with the given key. It will return the value for that in that row.
  • There is only 1 API call behind the scene.

In append only mode,

  • As there could be multiple rows with the same key, we need to only use the latest row as it contains the last updated value.
  • Note that deletion using append only mode results in a new row with a tombstone value. This method will also recognise and handle such cases.
  • There is only 1 API call behind the scene.

func (*GoogleSheetKVStore) Set

func (s *GoogleSheetKVStore) Set(ctx context.Context, key string, value []byte) error

Set inserts the key-value pair into the key-value store.

In default mode,

  • If the key is not in the store, `Set` will create a new row and store the key value pair there.
  • If the key is in the store, `Set` will update the previous row with the new value and timestamp.
  • There are exactly 2 API calls behind the scene: getting the row for the key and creating/updating with the given key value data.

In append only mode,

  • It always creates a new row at the bottom of the sheet with the latest value and timestamp.
  • There is only 1 API call behind the scene.

type GoogleSheetKVStoreConfig

type GoogleSheetKVStoreConfig struct {
	Mode models.KVMode
	// contains filtered or unexported fields
}

GoogleSheetKVStoreConfig defines a list of configurations that can be used to customise how the GoogleSheetKVStore works.

type GoogleSheetKVStoreV2

type GoogleSheetKVStoreV2 struct {
	// contains filtered or unexported fields
}

GoogleSheetKVStoreV2 implements a key-value store using the row store abstraction.

func NewGoogleSheetKVStoreV2

func NewGoogleSheetKVStoreV2(
	auth sheets.AuthClient,
	spreadsheetID string,
	sheetName string,
	config GoogleSheetKVStoreV2Config,
) *GoogleSheetKVStoreV2

NewGoogleSheetKVStoreV2 creates a new instance of the key-value store using row store. You cannot use this V2 store with the V1 store as the sheet format is different.

func (*GoogleSheetKVStoreV2) Close

func (s *GoogleSheetKVStoreV2) Close(ctx context.Context) error

Close cleans up resources used by the store.

func (*GoogleSheetKVStoreV2) Delete

func (s *GoogleSheetKVStoreV2) Delete(ctx context.Context, key string) error

Delete removes the key from the store.

func (*GoogleSheetKVStoreV2) Get

func (s *GoogleSheetKVStoreV2) Get(ctx context.Context, key string) ([]byte, error)

Get retrieves the value associated with the given key.

func (*GoogleSheetKVStoreV2) Set

func (s *GoogleSheetKVStoreV2) Set(ctx context.Context, key string, value []byte) error

Set inserts or updates the key-value pair in the store.

type GoogleSheetKVStoreV2Config

type GoogleSheetKVStoreV2Config struct {
	Mode models.KVMode
	// contains filtered or unexported fields
}

GoogleSheetKVStoreV2Config defines a list of configurations that can be used to customise how the GoogleSheetKVStoreV2 works.

type GoogleSheetRowStore

type GoogleSheetRowStore struct {
	// contains filtered or unexported fields
}

GoogleSheetRowStore encapsulates row store functionality on top of a Google Sheet.

func NewGoogleSheetRowStore

func NewGoogleSheetRowStore(
	auth sheets.AuthClient,
	spreadsheetID string,
	sheetName string,
	config GoogleSheetRowStoreConfig,
) *GoogleSheetRowStore

NewGoogleSheetRowStore creates an instance of the row based store with the given configuration. It will also try to create the sheet, in case it does not exist yet.

func (*GoogleSheetRowStore) Close

Close cleans up all held resources if any.

func (*GoogleSheetRowStore) Count

Count prepares rows counting operation.

Please note that calling Count() does not execute the query yet. Call GoogleSheetCountStmt.Exec() to actually execute the query.

func (*GoogleSheetRowStore) Delete

Delete prepares rows deletion operation.

Please note that calling Delete() does not execute the deletion yet. Call GoogleSheetDeleteStmt.Exec() to actually execute the deletion.

func (*GoogleSheetRowStore) Insert

func (s *GoogleSheetRowStore) Insert(rows ...interface{}) *GoogleSheetInsertStmt

Insert specifies the rows to be inserted into the Google Sheet.

The underlying data type of each row must be a struct or a pointer to a struct. Providing other data types will result in an error.

By default, the column name will be following the struct field name (case-sensitive). If you want to map the struct field name into another name, you can add a "db" struct tag (see GoogleSheetRowStore.Select docs for more details).

Please note that calling Insert() does not execute the insertion yet. Call GoogleSheetInsertStmt.Exec() to actually execute the insertion.

func (*GoogleSheetRowStore) Select

func (s *GoogleSheetRowStore) Select(output interface{}, columns ...string) *GoogleSheetSelectStmt

Select specifies which columns to return from the Google Sheet when querying and the output variable the data should be stored. You can think of this operation like a SQL SELECT statement (with limitations).

If "columns" is an empty slice of string, then all columns will be returned. If a column is not found in the provided list of columns in `GoogleSheetRowStoreConfig.Columns`, that column will be ignored.

"output" must be a pointer to a slice of a data type. The conversion from the Google Sheet data into the slice will be done using https://github.com/mitchellh/mapstructure.

If you are providing a slice of structs into the "output" parameter, and you want to define the mapping between the column name with the field name, you should add a "db" struct tag.

	// Without the `db` struct tag, the column name used will be "Name" and "Age".
	type Person struct {
    	Name string `db:"name"`
    	Age int `db:"age"`
	}

Please note that calling Select() does not execute the query yet. Call GoogleSheetSelectStmt.Exec to actually execute the query.

func (*GoogleSheetRowStore) Update

func (s *GoogleSheetRowStore) Update(colToValue map[string]interface{}) *GoogleSheetUpdateStmt

Update specifies the new value for each of the targeted columns.

The "colToValue" parameter specifies what value should be updated for which column. Each value in the map[string]interface{} is going to be JSON marshalled. If "colToValue" is empty, an error will be returned when GoogleSheetUpdateStmt.Exec() is called.

type GoogleSheetRowStoreConfig

type GoogleSheetRowStoreConfig struct {
	// Columns defines the list of column names.
	// Note that the column ordering matters.
	// The column ordering will be used for arranging the real columns in Google Sheet.
	// Changing the column ordering in this config but not in Google Sheet will result in unexpected behaviour.
	Columns []string

	// ColumnsWithFormula defines the list of column names containing a Google Sheet formula.
	// Note that only string fields can have a formula.
	ColumnsWithFormula []string
}

GoogleSheetRowStoreConfig defines a list of configurations that can be used to customise how the GoogleSheetRowStore works.

type GoogleSheetSelectStmt

type GoogleSheetSelectStmt struct {
	// contains filtered or unexported fields
}

GoogleSheetSelectStmt encapsulates information required to query the row store.

func (*GoogleSheetSelectStmt) Exec

Exec retrieves rows matching with the given condition.

There is only 1 API call behind the scene.

func (*GoogleSheetSelectStmt) Limit

Limit specifies the number of rows to retrieve.

The default value is 0.

func (*GoogleSheetSelectStmt) Offset

Offset specifies the number of rows to skip before starting to include the rows.

The default value is 0.

func (*GoogleSheetSelectStmt) OrderBy

OrderBy specifies the column ordering.

The default value is no ordering specified.

func (*GoogleSheetSelectStmt) Where

func (s *GoogleSheetSelectStmt) Where(condition string, args ...interface{}) *GoogleSheetSelectStmt

Where specifies the condition to meet for a row to be included.

"condition" specifies the WHERE clause. Values in the WHERE clause should be replaced by a placeholder "?". The actual values used for each placeholder (ordering matters) are provided via the "args" parameter.

"args" specifies the real value to replace each placeholder in the WHERE clause. Note that the first "args" value will replace the first placeholder "?" in the WHERE clause.

If you want to understand the reason behind this design, please read the protocol page: https://github.com/FreeLeh/docs/blob/main/freedb/protocols.md.

All conditions supported by Google Sheet "QUERY" function are supported by this library. You can read the full information in https://developers.google.com/chart/interactive/docs/querylanguage#where.

type GoogleSheetUpdateStmt

type GoogleSheetUpdateStmt struct {
	// contains filtered or unexported fields
}

GoogleSheetUpdateStmt encapsulates information required to update rows.

func (*GoogleSheetUpdateStmt) Exec

Exec updates rows matching the condition with the new values for affected columns.

There are 2 API calls behind the scene.

func (*GoogleSheetUpdateStmt) Where

func (s *GoogleSheetUpdateStmt) Where(condition string, args ...interface{}) *GoogleSheetUpdateStmt

Where specifies the condition to choose which rows are affected.

It works just like the GoogleSheetSelectStmt.Where() method. Please read GoogleSheetSelectStmt.Where() for more details.

Jump to

Keyboard shortcuts

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