versionary

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2023 License: MIT Imports: 18 Imported by: 0

README

Versionary

Versionary provides an opinionated way of managing versioned entities in a NoSQL database, such as AWS DynamoDB. It's a simple way of managing "wide rows", which provide really fast access to denormalized data, answering specific questions that one might have for the data. And, it insulates the developer from some details of the underlying NoSQL database. However, if you're designing your table schema, you'll need to understand the basic concepts of partition and sort keys. Partition keys are used for grouping data, and sort keys are used for sorting data in the group. They must be unique. Versionary benefits from using Time-based Unique Identifiers (TUID) for entities, because they contain an embedded timestamp at the beginning of the ID, such that an alphabetical sort is also chronological.

One of the wide rows (the "EntityRow") is the complete revision history of an entity. This is a list of all the versions of the entity, sorted chronologically. The partition key is the entity ID, and the sort key is the version ID. If the entity is never revised (such as an event in an event log), then there will only be one version, and the partition key and sort key will be the same.

There is also a collection of "IndexRows", which are typically lists of entities grouped by a particular attribute value. These rows contain only the most recent version of each entity. An example of an index row might be articles grouped by their author. The partition key would be the author ID, and the sort key would be the article ID.

The entity row and index rows are all stored in a single table, reducing the number of separate tables in the database. Versionary takes care to ensure that the index rows reflect current versions of the entities. It also maintains lists of all the partition keys used for each row, so that you can efficiently "walk the data" for all the entities, and so that you know what the complete vocabulary is for all the values used for grouping things.

To save space in the denormalized database, the entity values are stored as compressed JSON. This helps, but for large entities (such as articles), it can take up a lot of space. To avoid this, you can create wide rows that store only the entity ID as a sort key, or possibly a combination of the entity ID and an optional associated text or numeric value (e.g. the article ID and it's title). Then, you could use a two-stage approach, where first you get the list of article IDs and titles for a given author, and if you need the full body of the articles, you can fetch a collection of them by ID, in parallel, in a second stage.

Installation

Versionary requires Go 1.18 or later, because it takes advantage of Type Parameters ("Generics").

go get github.com/voxtechnica/versionary

To use Versionary and run its tests, you'll need an AWS account, and you'll need to configure your workstation to use the AWS CLI. The integration test creates, exercises, and deletes a DynamoDB table. For testing in your applications, you can use the provided MemTable implementation, which is backed by a simple in-memory table, and supports the same TableReader, TableWriter, and TableReadWriter interfaces.

Documentation

Overview

Package versionary provides an opinionated way of managing versioned entities in a NoSQL database.

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyFilter = errors.New("empty filter")

ErrEmptyFilter is returned when the filter string is empty.

View Source
var ErrNotFound = errors.New("versionary: not found")

ErrNotFound is returned when a specified thing is not found

Functions

func Batch added in v1.2.0

func Batch[T any](items []T, batchSize int) [][]T

Batch divides the provided slice of things into batches of the specified maximum size.

func Contains

func Contains[T comparable](s []T, value T) bool

Contains returns true if the slice contains the provided value.

func ContainsFilter added in v1.4.0

func ContainsFilter(contains string, anyMatch bool) (func(tv TextValue) bool, error)

ContainsFilter returns a function that can be used to filter TextValues. The case-insensitive contains query is split into words, and the words are compared with the value in the TextValue. If anyMatch is true, then a TextValue is included in the results if any of the words are found (OR filter). If anyMatch is false, then the TextValue must contain all the words in the query string (AND filter).

func Filter

func Filter[T any](s []T, f func(T) bool) []T

Filter filters values from a slice using a filter function. It returns a new slice with only the elements of s that satisfy the predicate.

func FromCompressedJSON added in v1.1.0

func FromCompressedJSON[T any](j []byte) (T, error)

FromCompressedJSON deserializes the provided gzip-compressed JSON byte slice into an entity.

func FromJSON added in v1.1.0

func FromJSON[T any](j []byte) (T, error)

FromJSON deserializes the provided JSON byte slice into an entity.

func IsValidDate added in v1.4.0

func IsValidDate(date string) bool

IsValidDate returns true if the supplied string is a valid date in the format YYYY-MM-DD. Note that this function does not validate the actual number of days in a given month. For example, it does not indicate that February 31 is invalid. It only validates the format, including the maximum value for each field.

func Map

func Map[T1, T2 any](s []T1, f func(T1) T2) []T2

Map turns a slice of T1 into a slice of T2 using a mapping function.

func NumValuesMap added in v1.4.0

func NumValuesMap(values []NumValue) map[string]float64

NumValuesMap converts a slice of NumValues into a key/value map.

func Reduce

func Reduce[T1, T2 any](s []T1, initializer T2, f func(T2, T1) T2) T2

Reduce reduces a slice of T1 to a single value using a reduction function.

func TextValuesMap added in v1.4.0

func TextValuesMap(values []TextValue) map[string]string

TextValuesMap converts a slice of TextValues into a key/value map.

func ToCompressedJSON added in v1.1.0

func ToCompressedJSON[T any](entity T) ([]byte, error)

ToCompressedJSON serializes the provided entity as a gzip-compressed JSON byte slice.

func ToJSON added in v1.1.0

func ToJSON[T any](t T) ([]byte, error)

ToJSON serializes the provided entity as a JSON byte slice.

func UncompressJSON added in v1.1.0

func UncompressJSON(j []byte) ([]byte, error)

UncompressJSON converts the provided gzip-compressed JSON byte slice into uncompressed bytes.

Types

type MemTable

type MemTable[T any] struct {
	Records    *RecordSet
	EntityType string
	TableName  string
	TTL        bool
	EntityRow  TableRow[T]
	IndexRows  map[string]TableRow[T]
}

MemTable represents a single in-memory table that stores all the "wide rows" for a given entity. Denormalized entity data are stored in a single table, reusing the attribute names indicated in the TableRow definition. The EntityRow is special; it contains the revision history for the entity, whereas the IndexRows contain only values from the latest version of the entity. MemTable implements the TableWriter and TableReader interfaces and is intended to be used for testing purposes only.

func NewMemTable

func NewMemTable[T any](table Table[T]) MemTable[T]

NewMemTable creates a new MemTable from a DynamoDB table definition.

func (MemTable[T]) CountPartKeyValues added in v1.3.0

func (table MemTable[T]) CountPartKeyValues(ctx context.Context, row TableRow[T]) (int64, error)

CountPartKeyValues counts the total number of partition key values for the specified row.

func (MemTable[T]) CountSortKeyValues added in v1.3.0

func (table MemTable[T]) CountSortKeyValues(ctx context.Context, row TableRow[T], partKeyValue string) (int64, error)

CountSortKeyValues counts the total number of sort key values for the specified row and partition key.

func (MemTable[T]) DeleteEntity

func (table MemTable[T]) DeleteEntity(ctx context.Context, entity T) error

DeleteEntity deletes the provided entity from the table (including all versions).

func (MemTable[T]) DeleteEntityVersionWithID added in v1.3.0

func (table MemTable[T]) DeleteEntityVersionWithID(ctx context.Context, entityID, versionID string) (T, error)

DeleteEntityVersionWithID deletes the specified version of the entity from the table.

func (MemTable[T]) DeleteEntityWithID

func (table MemTable[T]) DeleteEntityWithID(ctx context.Context, entityID string) (T, error)

DeleteEntityWithID deletes the specified entity from the table (including all versions).

func (MemTable[T]) DeleteItem

func (table MemTable[T]) DeleteItem(ctx context.Context, rowPartKeyValue string, sortKeyValue string) error

DeleteItem deletes the item with the specified row partition key and sort key. Note that this method will seldom be used. It is primarily for cleaning up obsolete items.

func (MemTable[T]) DeleteRow

func (table MemTable[T]) DeleteRow(ctx context.Context, row TableRow[T], partKeyValue string) error

DeleteRow deletes the row for the specified partition key value.

func (MemTable[T]) EntityExists

func (table MemTable[T]) EntityExists(ctx context.Context, entityID string) bool

EntityExists checks if an entity exists in the table.

func (MemTable[T]) EntityID added in v1.2.0

func (table MemTable[T]) EntityID(entity T) string

EntityID returns the unique entity ID for the provided entity.

func (MemTable[T]) EntityReferenceID added in v1.2.0

func (table MemTable[T]) EntityReferenceID(entity T) string

EntityReferenceID returns a unique Reference ID (EntityType-ID-VersionID) for the provided entity.

func (MemTable[T]) EntityVersionExists

func (table MemTable[T]) EntityVersionExists(ctx context.Context, entityID string, versionID string) bool

EntityVersionExists checks if an entity version exists in the table.

func (MemTable[T]) EntityVersionID added in v1.2.0

func (table MemTable[T]) EntityVersionID(entity T) string

EntityVersionID returns the unique entity version ID for the provided entity.

func (MemTable[T]) FilterEntityLabels added in v1.3.0

func (table MemTable[T]) FilterEntityLabels(ctx context.Context, f func(TextValue) bool) ([]TextValue, error)

FilterEntityLabels returns all entity labels that match the specified filter. The resulting text values are sorted by value.

func (MemTable[T]) FilterPartKeyLabels added in v1.3.0

func (table MemTable[T]) FilterPartKeyLabels(ctx context.Context, row TableRow[T], f func(TextValue) bool) ([]TextValue, error)

FilterPartKeyLabels returns all partition key labels from the specified row that match the specified filter. The resulting text values are sorted by value.

func (MemTable[T]) FilterTextValues added in v1.3.0

func (table MemTable[T]) FilterTextValues(ctx context.Context, row TableRow[T], partKeyValue string, f func(TextValue) bool) ([]TextValue, error)

FilterTextValues returns all text values from the specified row that match the specified filter. The resulting text values are sorted by value.

func (MemTable[T]) GetEntityRow

func (table MemTable[T]) GetEntityRow() TableRow[T]

GetEntityRow returns the entity row definition. This row contains the revision history for the entity.

func (MemTable[T]) GetEntityType added in v1.3.0

func (table MemTable[T]) GetEntityType() string

GetEntityType returns the name of the entity type for the MemTable.

func (MemTable[T]) GetRow

func (table MemTable[T]) GetRow(rowName string) (TableRow[T], bool)

GetRow returns the specified row definition.

func (MemTable[T]) GetTableName added in v1.3.0

func (table MemTable[T]) GetTableName() string

GetTableName returns the name of the table.

func (MemTable[T]) IsValid

func (table MemTable[T]) IsValid() bool

IsValid returns true if the MemTable fields and rows are valid.

func (MemTable[T]) ReadAllEntitiesFromRow

func (table MemTable[T]) ReadAllEntitiesFromRow(ctx context.Context, row TableRow[T], partKeyValue string) ([]T, error)

ReadAllEntitiesFromRow reads all entities from the specified row. Note: this could be a lot of data! It should only be used for small collections.

func (MemTable[T]) ReadAllEntitiesFromRowAsJSON added in v1.1.0

func (table MemTable[T]) ReadAllEntitiesFromRowAsJSON(ctx context.Context, row TableRow[T], partKeyValue string) ([]byte, error)

ReadAllEntitiesFromRowAsJSON reads all entities from the specified row as a JSON byte slice. Note: this could be a lot of data! It should only be used for small collections.

func (MemTable[T]) ReadAllEntityIDs

func (table MemTable[T]) ReadAllEntityIDs(ctx context.Context) ([]string, error)

ReadAllEntityIDs reads all entity IDs from the EntityRow. Note that this can return a very large number of IDs!

func (MemTable[T]) ReadAllEntityLabels added in v1.3.0

func (table MemTable[T]) ReadAllEntityLabels(ctx context.Context, sortByValue bool) ([]TextValue, error)

ReadAllEntityLabels reads all entity labels. Note: this can return a very large number of values! Use with caution.

func (MemTable[T]) ReadAllEntityVersionIDs

func (table MemTable[T]) ReadAllEntityVersionIDs(ctx context.Context, entityID string) ([]string, error)

ReadAllEntityVersionIDs reads all entity version IDs for the specified entity. Note that this can return a very large number of IDs!

func (MemTable[T]) ReadAllEntityVersions

func (table MemTable[T]) ReadAllEntityVersions(ctx context.Context, entityID string) ([]T, error)

ReadAllEntityVersions reads all versions of the specified entity. Note: this can return a very large number of versions! Use with caution.

func (MemTable[T]) ReadAllEntityVersionsAsJSON added in v1.1.0

func (table MemTable[T]) ReadAllEntityVersionsAsJSON(ctx context.Context, entityID string) ([]byte, error)

ReadAllEntityVersionsAsJSON reads all versions of the specified entity as a JSON byte slice. Note: this can return a very large number of versions! Use with caution.

func (MemTable[T]) ReadAllNumericValues

func (table MemTable[T]) ReadAllNumericValues(ctx context.Context, row TableRow[T], partKeyValue string, sortByValue bool) ([]NumValue, error)

ReadAllNumericValues reads all numeric values from the specified row. Note: this can return a very large number of values! Use with caution.

func (MemTable[T]) ReadAllPartKeyLabels added in v1.3.0

func (table MemTable[T]) ReadAllPartKeyLabels(ctx context.Context, row TableRow[T], sortByValue bool) ([]TextValue, error)

ReadAllPartKeyLabels reads all partition key labels from the specified row. Note: this can return a very large number of values! Use with caution.

func (MemTable[T]) ReadAllPartKeyValues

func (table MemTable[T]) ReadAllPartKeyValues(ctx context.Context, row TableRow[T]) ([]string, error)

ReadAllPartKeyValues reads all the values of the partition keys used for the specified row. Note that for some rows, this may be a very large number of values.

func (MemTable[T]) ReadAllRecords added in v1.2.0

func (table MemTable[T]) ReadAllRecords(ctx context.Context, row TableRow[T], partKeyValue string) ([]Record, error)

ReadAllRecords reads all Records from the specified row. Note: this can return a very large number of values! Use with caution.

func (MemTable[T]) ReadAllSortKeyValues

func (table MemTable[T]) ReadAllSortKeyValues(ctx context.Context, row TableRow[T], partKeyValue string) ([]string, error)

ReadAllSortKeyValues reads all the sort key values for the specified row and partition key value. Note that for some rows, this may be a very large number of values.

func (MemTable[T]) ReadAllTextValues

func (table MemTable[T]) ReadAllTextValues(ctx context.Context, row TableRow[T], partKeyValue string, sortByValue bool) ([]TextValue, error)

ReadAllTextValues reads all text values from the specified row. Note: this can return a very large number of values! Use with caution.

func (MemTable[T]) ReadCurrentEntityVersionID

func (table MemTable[T]) ReadCurrentEntityVersionID(ctx context.Context, entityID string) (string, error)

ReadCurrentEntityVersionID reads the current entity version ID for the specified entity.

func (MemTable[T]) ReadEntities

func (table MemTable[T]) ReadEntities(ctx context.Context, entityIDs []string) []T

ReadEntities reads the current versions of the specified entities from the EntityRow. If an entity does not exist, it will be omitted from the results.

func (MemTable[T]) ReadEntitiesAsJSON added in v1.1.0

func (table MemTable[T]) ReadEntitiesAsJSON(ctx context.Context, entityIDs []string) []byte

ReadEntitiesAsJSON reads the current versions of the specified entities from the EntityRow as a JSON byte slice. If an entity does not exist, it will be omitted from the results.

func (MemTable[T]) ReadEntitiesFromRow

func (table MemTable[T]) ReadEntitiesFromRow(ctx context.Context, row TableRow[T], partKeyValue string, reverse bool, limit int, offset string) ([]T, error)

ReadEntitiesFromRow reads paginated entities from the specified row.

func (MemTable[T]) ReadEntitiesFromRowAsJSON added in v1.1.0

func (table MemTable[T]) ReadEntitiesFromRowAsJSON(ctx context.Context, row TableRow[T], partKeyValue string, reverse bool, limit int, offset string) ([]byte, error)

ReadEntitiesFromRowAsJSON reads paginated entities from the specified row, returning a JSON byte slice.

func (MemTable[T]) ReadEntity

func (table MemTable[T]) ReadEntity(ctx context.Context, entityID string) (T, error)

ReadEntity reads the current version of the specified entity.

func (MemTable[T]) ReadEntityAsCompressedJSON added in v1.1.0

func (table MemTable[T]) ReadEntityAsCompressedJSON(ctx context.Context, entityID string) ([]byte, error)

ReadEntityAsCompressedJSON reads the current version of the specified entity, returning a compressed JSON byte slice.

func (MemTable[T]) ReadEntityAsJSON added in v1.1.0

func (table MemTable[T]) ReadEntityAsJSON(ctx context.Context, entityID string) ([]byte, error)

ReadEntityAsJSON reads the current version of the specified entity as a JSON byte slice.

func (MemTable[T]) ReadEntityFromRow

func (table MemTable[T]) ReadEntityFromRow(ctx context.Context, row TableRow[T], partKeyValue string, sortKeyValue string) (T, error)

ReadEntityFromRow reads the specified entity from the specified row.

func (MemTable[T]) ReadEntityFromRowAsCompressedJSON added in v1.1.0

func (table MemTable[T]) ReadEntityFromRowAsCompressedJSON(ctx context.Context, row TableRow[T], partKeyValue string, sortKeyValue string) ([]byte, error)

ReadEntityFromRowAsCompressedJSON reads the specified entity from the specified row as compressed JSON bytes.

func (MemTable[T]) ReadEntityFromRowAsJSON added in v1.1.0

func (table MemTable[T]) ReadEntityFromRowAsJSON(ctx context.Context, row TableRow[T], partKeyValue string, sortKeyValue string) ([]byte, error)

ReadEntityFromRowAsJSON reads the specified entity from the specified row as JSON bytes.

func (MemTable[T]) ReadEntityIDRange added in v1.4.0

func (table MemTable[T]) ReadEntityIDRange(ctx context.Context, from string, to string, reverse bool) ([]string, error)

ReadEntityIDRange reads a range of entity IDs from the EntityRow, where the entity IDs range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (MemTable[T]) ReadEntityIDs

func (table MemTable[T]) ReadEntityIDs(ctx context.Context, reverse bool, limit int, offset string) ([]string, error)

ReadEntityIDs reads paginated entity IDs from the EntityRow.

func (MemTable[T]) ReadEntityLabel added in v1.3.0

func (table MemTable[T]) ReadEntityLabel(ctx context.Context, entityID string) (TextValue, error)

ReadEntityLabel reads the label for the specified entity.

func (MemTable[T]) ReadEntityLabelRange added in v1.4.0

func (table MemTable[T]) ReadEntityLabelRange(ctx context.Context, from, to string, reverse bool) ([]TextValue, error)

ReadEntityLabelRange reads a range of entity labels, where the sort key values range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (MemTable[T]) ReadEntityLabels added in v1.3.0

func (table MemTable[T]) ReadEntityLabels(ctx context.Context, reverse bool, limit int, offset string) ([]TextValue, error)

ReadEntityLabels reads paginated entity labels.

func (MemTable[T]) ReadEntityRangeFromRow added in v1.4.0

func (table MemTable[T]) ReadEntityRangeFromRow(ctx context.Context, row TableRow[T], partKeyValue string, from, to string, reverse bool) ([]T, error)

ReadEntityRangeFromRow reads a range of entities from the specified row, where the sort key values range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (MemTable[T]) ReadEntityRangeFromRowAsJSON added in v1.4.0

func (table MemTable[T]) ReadEntityRangeFromRowAsJSON(ctx context.Context, row TableRow[T], partKeyValue string, from, to string, reverse bool) ([]byte, error)

ReadEntityRangeFromRowAsJSON reads a range of entities from the specified row as JSON, where the sort key values range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (MemTable[T]) ReadEntityVersion

func (table MemTable[T]) ReadEntityVersion(ctx context.Context, entityID string, versionID string) (T, error)

ReadEntityVersion reads the specified version of the specified entity.

func (MemTable[T]) ReadEntityVersionAsJSON added in v1.1.0

func (table MemTable[T]) ReadEntityVersionAsJSON(ctx context.Context, entityID string, versionID string) ([]byte, error)

ReadEntityVersionAsJSON reads the specified version of the specified entity as a JSON byte slice.

func (MemTable[T]) ReadEntityVersionIDs

func (table MemTable[T]) ReadEntityVersionIDs(ctx context.Context, entityID string, reverse bool, limit int, offset string) ([]string, error)

ReadEntityVersionIDs reads paginated entity version IDs for the specified entity.

func (MemTable[T]) ReadEntityVersions

func (table MemTable[T]) ReadEntityVersions(ctx context.Context, entityID string, reverse bool, limit int, offset string) ([]T, error)

ReadEntityVersions reads paginated versions of the specified entity.

func (MemTable[T]) ReadEntityVersionsAsJSON added in v1.1.0

func (table MemTable[T]) ReadEntityVersionsAsJSON(ctx context.Context, entityID string, reverse bool, limit int, offset string) ([]byte, error)

ReadEntityVersionsAsJSON reads paginated versions of the specified entity as a JSON byte slice.

func (MemTable[T]) ReadFirstSortKeyValue added in v1.2.0

func (table MemTable[T]) ReadFirstSortKeyValue(ctx context.Context, row TableRow[T], partKeyValue string) (string, error)

ReadFirstSortKeyValue reads the first sort key value for the specified row and partition key value. This method is typically used for looking up an ID corresponding to a foreign key.

func (MemTable[T]) ReadLastSortKeyValue added in v1.2.0

func (table MemTable[T]) ReadLastSortKeyValue(ctx context.Context, row TableRow[T], partKeyValue string) (string, error)

ReadLastSortKeyValue reads the last sort key value for the specified row and partition key value. This method is typically used for looking up the current version ID of a versioned entity.

func (MemTable[T]) ReadNumericValue

func (table MemTable[T]) ReadNumericValue(ctx context.Context, row TableRow[T], partKeyValue string, sortKeyValue string) (NumValue, error)

ReadNumericValue reads the specified numeric value from the specified row.

func (MemTable[T]) ReadNumericValueRange added in v1.4.0

func (table MemTable[T]) ReadNumericValueRange(ctx context.Context, row TableRow[T], partKeyValue string, from, to string, reverse bool) ([]NumValue, error)

ReadNumericValueRange reads a range of numeric values from the specified row, where the sort key values range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (MemTable[T]) ReadNumericValues

func (table MemTable[T]) ReadNumericValues(ctx context.Context, row TableRow[T], partKeyValue string, reverse bool, limit int, offset string) ([]NumValue, error)

ReadNumericValues reads paginated numeric values from the specified row.

func (MemTable[T]) ReadPartKeyLabel added in v1.3.0

func (table MemTable[T]) ReadPartKeyLabel(ctx context.Context, row TableRow[T], partKeyValue string) (TextValue, error)

ReadPartKeyLabel reads the text label for the specified partition key value from the specified row.

func (MemTable[T]) ReadPartKeyLabelRange added in v1.4.0

func (table MemTable[T]) ReadPartKeyLabelRange(ctx context.Context, row TableRow[T], from, to string, reverse bool) ([]TextValue, error)

ReadPartKeyLabelRange reads a range of partition key labels from the specified row, where the sort key values range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (MemTable[T]) ReadPartKeyLabels added in v1.3.0

func (table MemTable[T]) ReadPartKeyLabels(ctx context.Context, row TableRow[T], reverse bool, limit int, offset string) ([]TextValue, error)

ReadPartKeyLabels reads paginated partition key labels from the specified row.

func (MemTable[T]) ReadPartKeyValueRange added in v1.4.0

func (table MemTable[T]) ReadPartKeyValueRange(ctx context.Context, row TableRow[T], from string, to string, reverse bool) ([]string, error)

ReadPartKeyValueRange reads a range of partition key values for the specified row, where the partition key values range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (MemTable[T]) ReadPartKeyValues

func (table MemTable[T]) ReadPartKeyValues(ctx context.Context, row TableRow[T], reverse bool, limit int, offset string) ([]string, error)

ReadPartKeyValues reads paginated partition key values for the specified row.

func (MemTable[T]) ReadRecord added in v1.2.0

func (table MemTable[T]) ReadRecord(ctx context.Context, row TableRow[T], partKeyValue string, sortKeyValue string) (Record, error)

ReadRecord reads the specified Record from the specified row.

func (MemTable[T]) ReadRecordRange added in v1.4.0

func (table MemTable[T]) ReadRecordRange(ctx context.Context, row TableRow[T], partKeyValue string, from, to string, reverse bool) ([]Record, error)

ReadRecordRange reads a range of Records from the specified row, where the sort key values range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (MemTable[T]) ReadRecords added in v1.2.0

func (table MemTable[T]) ReadRecords(ctx context.Context, row TableRow[T], partKeyValue string, reverse bool, limit int, offset string) ([]Record, error)

ReadRecords reads paginated Records from the specified row.

func (MemTable[T]) ReadSortKeyValueRange added in v1.4.0

func (table MemTable[T]) ReadSortKeyValueRange(ctx context.Context, row TableRow[T], partKeyValue string, from string, to string, reverse bool) ([]string, error)

ReadSortKeyValueRange reads a range of sort key values for the specified row and partition key value, where the sort key values range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (MemTable[T]) ReadSortKeyValues

func (table MemTable[T]) ReadSortKeyValues(ctx context.Context, row TableRow[T], partKeyValue string, reverse bool, limit int, offset string) ([]string, error)

ReadSortKeyValues reads paginated sort key values for the specified row and partition key value, where the sort key values are returned in ascending or descending order. The offset is the last sort key value returned in the previous page of results. If the offset is empty, it will be replaced with the appropriate sentinel value.

func (MemTable[T]) ReadTextValue

func (table MemTable[T]) ReadTextValue(ctx context.Context, row TableRow[T], partKeyValue string, sortKeyValue string) (TextValue, error)

ReadTextValue reads the specified text value from the specified row.

func (MemTable[T]) ReadTextValueRange added in v1.4.0

func (table MemTable[T]) ReadTextValueRange(ctx context.Context, row TableRow[T], partKeyValue string, from, to string, reverse bool) ([]TextValue, error)

ReadTextValueRange reads a range of text values from the specified row, where the sort key values range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (MemTable[T]) ReadTextValues

func (table MemTable[T]) ReadTextValues(ctx context.Context, row TableRow[T], partKeyValue string, reverse bool, limit int, offset string) ([]TextValue, error)

ReadTextValues reads paginated text values from the specified row.

func (MemTable[T]) TableExists

func (table MemTable[T]) TableExists() bool

TableExists returns true if the in-memory table exists.

func (MemTable[T]) UpdateEntity

func (table MemTable[T]) UpdateEntity(ctx context.Context, entity T) error

UpdateEntity batch-writes the provided entity to each of the wide rows in the table, including deleting any obsolete values in index rows.

func (MemTable[T]) UpdateEntityVersion

func (table MemTable[T]) UpdateEntityVersion(ctx context.Context, oldVersion T, newVersion T) error

UpdateEntityVersion batch-writes the provided entity to each of the wide rows in the table, including deleting any obsolete values in index rows.

func (MemTable[T]) WriteEntity

func (table MemTable[T]) WriteEntity(ctx context.Context, entity T) error

WriteEntity batch-writes the provided entity to each of the wide rows in the table.

type NumValue

type NumValue struct {
	Key   string  `json:"key"`
	Value float64 `json:"value"`
}

NumValue represents a key-value pair where the value is a number.

func (NumValue) String added in v1.3.0

func (nv NumValue) String() string

String returns a string representation of the NumValue.

type Record

type Record struct {
	PartKeyValue string
	SortKeyValue string
	JsonValue    []byte
	TextValue    string
	NumericValue float64
	TimeToLive   int64
}

Record is a struct that represents a single item in a database table. PartKeyValue and SortKeyValue are used to represent the primary key and are required fields. All other fields are optional. Note that the PartKeyValue will be a full pipe-delimited partition key: rowName|partKeyName|partKeyValue.

func (*Record) IsValid

func (r *Record) IsValid() bool

IsValid returns true if the Record is valid (all required fields are supplied).

type RecordSet

type RecordSet map[string]map[string]Record

RecordSet provides an in-memory data structure for storing a set of Records, used for lightweight testing purposes.

func (*RecordSet) CountSortKeys added in v1.3.0

func (rs *RecordSet) CountSortKeys(partKey string) int64

CountSortKeys returns the total number of sort keys for a specified partition key.

func (*RecordSet) DeleteRecord

func (rs *RecordSet) DeleteRecord(r Record)

DeleteRecord removes the provided Record from the RecordSet.

func (*RecordSet) DeleteRecordForKeys

func (rs *RecordSet) DeleteRecordForKeys(partKey string, sortKey string)

DeleteRecordForKeys removes a specified Record from the RecordSet.

func (*RecordSet) DeleteRecords

func (rs *RecordSet) DeleteRecords(records []Record)

DeleteRecords removes the provided list of Records from the RecordSet.

func (*RecordSet) DeleteRecordsForKey

func (rs *RecordSet) DeleteRecordsForKey(partKey string)

DeleteRecordsForKey removes all Records for a specified partition key from the RecordSet.

func (*RecordSet) GetRecord

func (rs *RecordSet) GetRecord(partKey string, sortKey string) (Record, bool)

GetRecord returns a specified Record from the RecordSet.

func (*RecordSet) GetRecords

func (rs *RecordSet) GetRecords(partKey string, sortKeys []string) []Record

GetRecords returns a list of Records from the RecordSet.

func (*RecordSet) GetSortKeys

func (rs *RecordSet) GetSortKeys(partKey string) []string

GetSortKeys returns a complete list of sort keys for a specified partition key.

func (*RecordSet) RecordExists

func (rs *RecordSet) RecordExists(partKey string, sortKey string) bool

RecordExists returns true if the RecordSet contains a record for the provided partition and sort key.

func (*RecordSet) RecordsExist

func (rs *RecordSet) RecordsExist(partKey string) bool

RecordsExist returns true if the RecordSet contains any records for the provided partition key.

func (*RecordSet) SetRecord

func (rs *RecordSet) SetRecord(r Record)

SetRecord adds a Record to the RecordSet.

func (*RecordSet) SetRecords

func (rs *RecordSet) SetRecords(records []Record)

SetRecords adds a list of Records to the RecordSet.

type Table

type Table[T any] struct {
	Client           *dynamodb.Client       // AWS DynamoDB client
	EntityType       string                 // the type of entity stored in this table
	TableName        string                 // name of the table, typically including the entity type and environment name
	PartKeyAttr      string                 // partition key attribute name (e.g. "v_part")
	SortKeyAttr      string                 // sort key attribute name (e.g. "v_sort")
	JsonValueAttr    string                 // JSON value attribute name (e.g. "v_json")
	TextValueAttr    string                 // text value attribute name (e.g. "v_text")
	NumericValueAttr string                 // numeric value attribute name (e.g. "v_num")
	TimeToLiveAttr   string                 // time to live attribute name (e.g. "v_expires")
	TTL              bool                   // true if the table has a time to live attribute
	EntityRow        TableRow[T]            // the row that stores the entity versions
	IndexRows        map[string]TableRow[T] // index rows, based on various entity properties
}

Table represents a single DynamoDB table that stores all the "wide rows" for a given entity. Denormalized entity data are stored in a single table, reusing the attribute names indicated in the Table definition. The EntityRow is special; it contains the revision history for the entity, whereas the IndexRows contain only values from the latest revision of the entity.

func (Table[T]) CountPartKeyValues added in v1.3.0

func (table Table[T]) CountPartKeyValues(ctx context.Context, row TableRow[T]) (int64, error)

CountPartKeyValues returns the total number of partition key values in the specified row. Note that this may be a slow operation; it pages through all the values to count them.

func (Table[T]) CountSortKeyValues added in v1.3.0

func (table Table[T]) CountSortKeyValues(ctx context.Context, row TableRow[T], partKeyValue string) (int64, error)

CountSortKeyValues returns the total number of sort key values for the specified row and partition key value. Note that this may be a slow operation; it pages through all the values to count them.

func (Table[T]) CreateTable

func (table Table[T]) CreateTable(ctx context.Context) error

CreateTable creates the DynamoDB table for the entity. This operation may take a few seconds to complete, as it waits for the table to become active.

func (Table[T]) DeleteEntity

func (table Table[T]) DeleteEntity(ctx context.Context, entity T) error

DeleteEntity deletes the provided entity from the table (including all versions).

func (Table[T]) DeleteEntityVersionWithID added in v1.3.0

func (table Table[T]) DeleteEntityVersionWithID(ctx context.Context, entityID, versionID string) (T, error)

DeleteEntityVersionWithID deletes the specified version of the entity from the table.

func (Table[T]) DeleteEntityWithID

func (table Table[T]) DeleteEntityWithID(ctx context.Context, entityID string) (T, error)

DeleteEntityWithID deletes the specified entity from the table (including all versions).

func (Table[T]) DeleteItem

func (table Table[T]) DeleteItem(ctx context.Context, rowPartKeyValue string, sortKeyValue string) error

DeleteItem deletes the item with the specified row partition key and sort key. Note that this method will seldom be used, perhaps for cleaning up obsolete items.

func (Table[T]) DeleteRow

func (table Table[T]) DeleteRow(ctx context.Context, row TableRow[T], partKeyValue string) error

DeleteRow deletes the entire row for the specified partition key.

func (Table[T]) DeleteTable

func (table Table[T]) DeleteTable(ctx context.Context) error

DeleteTable deletes the DynamoDB table. Note that the table's status may be DELETING for a few seconds.

func (Table[T]) EntityExists

func (table Table[T]) EntityExists(ctx context.Context, entityID string) bool

EntityExists checks if an entity exists in the table.

func (Table[T]) EntityID added in v1.2.0

func (table Table[T]) EntityID(entity T) string

EntityID returns the unique entity ID for the provided entity.

func (Table[T]) EntityReferenceID added in v1.2.0

func (table Table[T]) EntityReferenceID(entity T) string

EntityReferenceID returns a unique Reference ID (EntityType-ID-VersionID) for the provided entity.

func (Table[T]) EntityVersionExists

func (table Table[T]) EntityVersionExists(ctx context.Context, entityID string, versionID string) bool

EntityVersionExists checks if an entity version exists in the table.

func (Table[T]) EntityVersionID added in v1.2.0

func (table Table[T]) EntityVersionID(entity T) string

EntityVersionID returns the unique entity version ID for the provided entity.

func (Table[T]) FilterEntityLabels added in v1.3.0

func (table Table[T]) FilterEntityLabels(ctx context.Context, f func(TextValue) bool) ([]TextValue, error)

FilterEntityLabels returns all entity labels that match the specified filter. The resulting text values are sorted by value.

func (Table[T]) FilterPartKeyLabels added in v1.3.0

func (table Table[T]) FilterPartKeyLabels(ctx context.Context, row TableRow[T], f func(TextValue) bool) ([]TextValue, error)

FilterPartKeyLabels returns all partition key labels from the specified row that match the specified filter. The resulting text values are sorted by value.

func (Table[T]) FilterTextValues added in v1.3.0

func (table Table[T]) FilterTextValues(ctx context.Context, row TableRow[T], partKeyValue string, f func(TextValue) bool) ([]TextValue, error)

FilterTextValues returns all text values from the specified row that match the specified filter. The resulting text values are sorted by value.

func (Table[T]) GetEntityRow

func (table Table[T]) GetEntityRow() TableRow[T]

GetEntityRow returns the entity row definition. This row contains the revision history for the entity.

func (Table[T]) GetEntityType added in v1.3.0

func (table Table[T]) GetEntityType() string

GetEntityType returns the name of the entity type stored in this table.

func (Table[T]) GetRow

func (table Table[T]) GetRow(rowName string) (TableRow[T], bool)

GetRow returns the specified row definition.

func (Table[T]) GetTableDescription

func (table Table[T]) GetTableDescription(ctx context.Context) (*types.TableDescription, error)

GetTableDescription fetches metadata about the DynamoDB table.

func (Table[T]) GetTableName added in v1.3.0

func (table Table[T]) GetTableName() string

GetTableName returns the name of the table.

func (Table[T]) IsValid added in v1.2.0

func (table Table[T]) IsValid() bool

IsValid returns true if the table fields and rows are valid.

func (Table[T]) ReadAllEntitiesFromRow

func (table Table[T]) ReadAllEntitiesFromRow(ctx context.Context, row TableRow[T], partKeyValue string) ([]T, error)

ReadAllEntitiesFromRow reads all entities from the specified row. Note: this could be a lot of data! It should only be used for small collections.

func (Table[T]) ReadAllEntitiesFromRowAsJSON added in v1.1.0

func (table Table[T]) ReadAllEntitiesFromRowAsJSON(ctx context.Context, row TableRow[T], partKeyValue string) ([]byte, error)

ReadAllEntitiesFromRowAsJSON reads all entities from the specified row as a JSON byte slice. Note: this could be a lot of data! It should only be used for small collections.

func (Table[T]) ReadAllEntityIDs

func (table Table[T]) ReadAllEntityIDs(ctx context.Context) ([]string, error)

ReadAllEntityIDs reads all entity IDs from the EntityRow. Note that this can return a very large number of IDs!

func (Table[T]) ReadAllEntityLabels added in v1.3.0

func (table Table[T]) ReadAllEntityLabels(ctx context.Context, sortByValue bool) ([]TextValue, error)

ReadAllEntityLabels reads all entity labels. Note: this can return a very large number of values! Use with caution.

func (Table[T]) ReadAllEntityVersionIDs

func (table Table[T]) ReadAllEntityVersionIDs(ctx context.Context, entityID string) ([]string, error)

ReadAllEntityVersionIDs reads all entity version IDs for the specified entity. Note that this can return a very large number of IDs!

func (Table[T]) ReadAllEntityVersions

func (table Table[T]) ReadAllEntityVersions(ctx context.Context, entityID string) ([]T, error)

ReadAllEntityVersions reads all versions of the specified entity. Note: this can return a very large number of versions! Use with caution.

func (Table[T]) ReadAllEntityVersionsAsJSON added in v1.1.0

func (table Table[T]) ReadAllEntityVersionsAsJSON(ctx context.Context, entityID string) ([]byte, error)

ReadAllEntityVersionsAsJSON reads all versions of the specified entity as a JSON byte slice. Note: this can return a very large number of versions! Use with caution.

func (Table[T]) ReadAllNumericValues

func (table Table[T]) ReadAllNumericValues(ctx context.Context, row TableRow[T], partKeyValue string, sortByValue bool) ([]NumValue, error)

ReadAllNumericValues reads all numeric values from the specified row. Note: this can return a very large number of values! Use with caution.

func (Table[T]) ReadAllPartKeyLabels added in v1.3.0

func (table Table[T]) ReadAllPartKeyLabels(ctx context.Context, row TableRow[T], sortByValue bool) ([]TextValue, error)

ReadAllPartKeyLabels reads all partition key labels from the specified row. Note: this can return a very large number of values! Use with caution.

func (Table[T]) ReadAllPartKeyValues

func (table Table[T]) ReadAllPartKeyValues(ctx context.Context, row TableRow[T]) ([]string, error)

ReadAllPartKeyValues reads all the values of the partition keys used for the specified row. Note that for some rows, this may be a very large number of values.

func (Table[T]) ReadAllRecords added in v1.2.0

func (table Table[T]) ReadAllRecords(ctx context.Context, row TableRow[T], partKeyValue string) ([]Record, error)

ReadAllRecords reads all Records from the specified row. Note: this can return a very large number of values! Use with caution.

func (Table[T]) ReadAllSortKeyValues

func (table Table[T]) ReadAllSortKeyValues(ctx context.Context, row TableRow[T], partKeyValue string) ([]string, error)

ReadAllSortKeyValues reads all the sort key values for the specified row and partition key value. Note that for some rows, this may be a very large number of values.

func (Table[T]) ReadAllTextValues

func (table Table[T]) ReadAllTextValues(ctx context.Context, row TableRow[T], partKeyValue string, sortByValue bool) ([]TextValue, error)

ReadAllTextValues reads all text values from the specified row. Note: this can return a very large number of values! Use with caution.

func (Table[T]) ReadCurrentEntityVersionID

func (table Table[T]) ReadCurrentEntityVersionID(ctx context.Context, entityID string) (string, error)

ReadCurrentEntityVersionID reads the current entity version ID for the specified entity.

func (Table[T]) ReadEntities

func (table Table[T]) ReadEntities(ctx context.Context, entityIDs []string) []T

ReadEntities reads the current versions of the specified entities from the EntityRow. If an entity does not exist, it will be omitted from the results.

func (Table[T]) ReadEntitiesAsJSON added in v1.1.0

func (table Table[T]) ReadEntitiesAsJSON(ctx context.Context, entityIDs []string) []byte

ReadEntitiesAsJSON reads the current versions of the specified entities from the EntityRow as a JSON byte slice. If an entity does not exist, it will be omitted from the results.

func (Table[T]) ReadEntitiesFromRow

func (table Table[T]) ReadEntitiesFromRow(ctx context.Context, row TableRow[T], partKeyValue string, reverse bool, limit int, offset string) ([]T, error)

ReadEntitiesFromRow reads paginated entities from the specified row.

func (Table[T]) ReadEntitiesFromRowAsJSON added in v1.1.0

func (table Table[T]) ReadEntitiesFromRowAsJSON(ctx context.Context, row TableRow[T], partKeyValue string, reverse bool, limit int, offset string) ([]byte, error)

ReadEntitiesFromRowAsJSON reads paginated entities from the specified row, returning a JSON byte slice.

func (Table[T]) ReadEntity

func (table Table[T]) ReadEntity(ctx context.Context, entityID string) (T, error)

ReadEntity reads the current version of the specified entity.

func (Table[T]) ReadEntityAsCompressedJSON added in v1.1.0

func (table Table[T]) ReadEntityAsCompressedJSON(ctx context.Context, entityID string) ([]byte, error)

ReadEntityAsCompressedJSON reads the current version of the specified entity, returning a compressed JSON byte slice.

func (Table[T]) ReadEntityAsJSON added in v1.1.0

func (table Table[T]) ReadEntityAsJSON(ctx context.Context, entityID string) ([]byte, error)

ReadEntityAsJSON reads the current version of the specified entity as a JSON byte slice.

func (Table[T]) ReadEntityFromRow

func (table Table[T]) ReadEntityFromRow(ctx context.Context, row TableRow[T], partKeyValue string, sortKeyValue string) (T, error)

ReadEntityFromRow reads the specified entity from the specified row.

func (Table[T]) ReadEntityFromRowAsCompressedJSON added in v1.1.0

func (table Table[T]) ReadEntityFromRowAsCompressedJSON(ctx context.Context, row TableRow[T], partKeyValue string, sortKeyValue string) ([]byte, error)

ReadEntityFromRowAsCompressedJSON reads the specified entity from the specified row as compressed JSON bytes.

func (Table[T]) ReadEntityFromRowAsJSON added in v1.1.0

func (table Table[T]) ReadEntityFromRowAsJSON(ctx context.Context, row TableRow[T], partKeyValue string, sortKeyValue string) ([]byte, error)

ReadEntityFromRowAsJSON reads the specified entity from the specified row as JSON bytes.

func (Table[T]) ReadEntityIDRange added in v1.4.0

func (table Table[T]) ReadEntityIDRange(ctx context.Context, from string, to string, reverse bool) ([]string, error)

ReadEntityIDRange reads a range of entity IDs from the EntityRow, where the entity IDs range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (Table[T]) ReadEntityIDs

func (table Table[T]) ReadEntityIDs(ctx context.Context, reverse bool, limit int, offset string) ([]string, error)

ReadEntityIDs reads paginated entity IDs from the EntityRow.

func (Table[T]) ReadEntityLabel added in v1.3.0

func (table Table[T]) ReadEntityLabel(ctx context.Context, entityID string) (TextValue, error)

ReadEntityLabel reads the label for the specified entity.

func (Table[T]) ReadEntityLabelRange added in v1.4.0

func (table Table[T]) ReadEntityLabelRange(ctx context.Context, from, to string, reverse bool) ([]TextValue, error)

ReadEntityLabelRange reads a range of entity labels, where the entity IDs range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (Table[T]) ReadEntityLabels added in v1.3.0

func (table Table[T]) ReadEntityLabels(ctx context.Context, reverse bool, limit int, offset string) ([]TextValue, error)

ReadEntityLabels reads paginated entity labels.

func (Table[T]) ReadEntityRangeFromRow added in v1.4.0

func (table Table[T]) ReadEntityRangeFromRow(ctx context.Context, row TableRow[T], partKeyValue string, from, to string, reverse bool) ([]T, error)

ReadEntityRangeFromRow reads a range of entities from the specified row, where the sort key values range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (Table[T]) ReadEntityRangeFromRowAsJSON added in v1.4.0

func (table Table[T]) ReadEntityRangeFromRowAsJSON(ctx context.Context, row TableRow[T], partKeyValue string, from, to string, reverse bool) ([]byte, error)

ReadEntityRangeFromRowAsJSON reads a range of entities from the specified row as JSON, where the sort key values range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (Table[T]) ReadEntityVersion

func (table Table[T]) ReadEntityVersion(ctx context.Context, entityID string, versionID string) (T, error)

ReadEntityVersion reads the specified version of the specified entity.

func (Table[T]) ReadEntityVersionAsJSON added in v1.1.0

func (table Table[T]) ReadEntityVersionAsJSON(ctx context.Context, entityID string, versionID string) ([]byte, error)

ReadEntityVersionAsJSON reads the specified version of the specified entity as a JSON byte slice.

func (Table[T]) ReadEntityVersionIDs

func (table Table[T]) ReadEntityVersionIDs(ctx context.Context, entityID string, reverse bool, limit int, offset string) ([]string, error)

ReadEntityVersionIDs reads paginated entity version IDs for the specified entity.

func (Table[T]) ReadEntityVersions

func (table Table[T]) ReadEntityVersions(ctx context.Context, entityID string, reverse bool, limit int, offset string) ([]T, error)

ReadEntityVersions reads paginated versions of the specified entity.

func (Table[T]) ReadEntityVersionsAsJSON added in v1.1.0

func (table Table[T]) ReadEntityVersionsAsJSON(ctx context.Context, entityID string, reverse bool, limit int, offset string) ([]byte, error)

ReadEntityVersionsAsJSON reads paginated versions of the specified entity as a JSON byte slice.

func (Table[T]) ReadFirstSortKeyValue added in v1.2.0

func (table Table[T]) ReadFirstSortKeyValue(ctx context.Context, row TableRow[T], partKeyValue string) (string, error)

ReadFirstSortKeyValue reads the first sort key value for the specified row and partition key value. This method is typically used for looking up an ID corresponding to a foreign key.

func (Table[T]) ReadLastSortKeyValue added in v1.2.0

func (table Table[T]) ReadLastSortKeyValue(ctx context.Context, row TableRow[T], partKeyValue string) (string, error)

ReadLastSortKeyValue reads the last sort key value for the specified row and partition key value. This method is typically used for looking up the current version ID of a versioned entity.

func (Table[T]) ReadNumericValue

func (table Table[T]) ReadNumericValue(ctx context.Context, row TableRow[T], partKeyValue string, sortKeyValue string) (NumValue, error)

ReadNumericValue reads the specified numeric value from the specified row.

func (Table[T]) ReadNumericValueRange added in v1.4.0

func (table Table[T]) ReadNumericValueRange(ctx context.Context, row TableRow[T], partKeyValue string, from, to string, reverse bool) ([]NumValue, error)

ReadNumericValueRange reads a range of numeric values from the specified row, where the sort key values range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (Table[T]) ReadNumericValues

func (table Table[T]) ReadNumericValues(ctx context.Context, row TableRow[T], partKeyValue string, reverse bool, limit int, offset string) ([]NumValue, error)

ReadNumericValues reads paginated numeric values from the specified row.

func (Table[T]) ReadPartKeyLabel added in v1.3.0

func (table Table[T]) ReadPartKeyLabel(ctx context.Context, row TableRow[T], partKeyValue string) (TextValue, error)

ReadPartKeyLabel reads the text label for the specified partition key value from the specified row.

func (Table[T]) ReadPartKeyLabelRange added in v1.4.0

func (table Table[T]) ReadPartKeyLabelRange(ctx context.Context, row TableRow[T], from, to string, reverse bool) ([]TextValue, error)

ReadPartKeyLabelRange reads a range of partition key labels from the specified row, where the sort key values range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (Table[T]) ReadPartKeyLabels added in v1.3.0

func (table Table[T]) ReadPartKeyLabels(ctx context.Context, row TableRow[T], reverse bool, limit int, offset string) ([]TextValue, error)

ReadPartKeyLabels reads paginated partition key labels from the specified row.

func (Table[T]) ReadPartKeyValueRange added in v1.4.0

func (table Table[T]) ReadPartKeyValueRange(ctx context.Context, row TableRow[T], from string, to string, reverse bool) ([]string, error)

ReadPartKeyValueRange reads a range of partition key values for the specified row.

func (Table[T]) ReadPartKeyValues

func (table Table[T]) ReadPartKeyValues(ctx context.Context, row TableRow[T], reverse bool, limit int, offset string) ([]string, error)

ReadPartKeyValues reads paginated partition key values for the specified row.

func (Table[T]) ReadRecord added in v1.2.0

func (table Table[T]) ReadRecord(ctx context.Context, row TableRow[T], partKeyValue string, sortKeyValue string) (Record, error)

ReadRecord reads the specified record from the provided table row.

func (Table[T]) ReadRecordRange added in v1.4.0

func (table Table[T]) ReadRecordRange(ctx context.Context, row TableRow[T], partKeyValue string, from, to string, reverse bool) ([]Record, error)

ReadRecordRange reads a range of Records from the specified row, where the sort key values range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (Table[T]) ReadRecords added in v1.2.0

func (table Table[T]) ReadRecords(ctx context.Context, row TableRow[T], partKeyValue string, reverse bool, limit int, offset string) ([]Record, error)

ReadRecords reads paginated Records from the specified row.

func (Table[T]) ReadSortKeyValueRange added in v1.4.0

func (table Table[T]) ReadSortKeyValueRange(ctx context.Context, row TableRow[T], partKeyValue string, from string, to string, reverse bool) ([]string, error)

ReadSortKeyValueRange reads a range of sort key values for the specified row and partition key value, where the sort key values range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (Table[T]) ReadSortKeyValues

func (table Table[T]) ReadSortKeyValues(ctx context.Context, row TableRow[T], partKeyValue string, reverse bool, limit int, offset string) ([]string, error)

ReadSortKeyValues reads paginated sort key values for the specified row and partition key value.

func (Table[T]) ReadTextValue

func (table Table[T]) ReadTextValue(ctx context.Context, row TableRow[T], partKeyValue string, sortKeyValue string) (TextValue, error)

ReadTextValue reads the specified text value from the specified row.

func (Table[T]) ReadTextValueRange added in v1.4.0

func (table Table[T]) ReadTextValueRange(ctx context.Context, row TableRow[T], partKeyValue string, from, to string, reverse bool) ([]TextValue, error)

ReadTextValueRange reads a range of text values from the specified row, where the sort key values range between the specified inclusive 'from' and 'to' values. If either 'from' or 'to' are empty, they will be replaced with the appropriate sentinel value.

func (Table[T]) ReadTextValues

func (table Table[T]) ReadTextValues(ctx context.Context, row TableRow[T], partKeyValue string, reverse bool, limit int, offset string) ([]TextValue, error)

ReadTextValues reads paginated text values from the specified row.

func (Table[T]) TableExists

func (table Table[T]) TableExists(ctx context.Context) bool

TableExists returns true if the DynamoDB table exists.

func (Table[T]) UpdateEntity

func (table Table[T]) UpdateEntity(ctx context.Context, entity T) error

UpdateEntity batch-writes the provided entity to each of the wide rows in the table, including deleting any obsolete values in index rows. If a previous version does not exist, it is treated as a new entity.

func (Table[T]) UpdateEntityVersion

func (table Table[T]) UpdateEntityVersion(ctx context.Context, oldVersion T, newVersion T) error

UpdateEntityVersion batch-writes the provided entity to each of the wide rows in the table, including deleting any obsolete values in index rows.

func (Table[T]) UpdateTTL

func (table Table[T]) UpdateTTL(ctx context.Context) error

UpdateTTL updates the DynamoDB table's time-to-live settings.

func (Table[T]) WriteEntity

func (table Table[T]) WriteEntity(ctx context.Context, entity T) error

WriteEntity batch-writes the provided entity to each of the wide rows in the table.

type TableReadWriter

type TableReadWriter[T any] interface {
	TableReader[T]
	TableWriter[T]
}

TableReadWriter is both a TableReader and TableWriter

type TableReader

type TableReader[T any] interface {
	IsValid() bool
	GetEntityType() string
	GetTableName() string
	GetRow(rowName string) (TableRow[T], bool)
	GetEntityRow() TableRow[T]
	EntityID(entity T) string
	EntityVersionID(entity T) string
	EntityReferenceID(entity T) string
	EntityExists(ctx context.Context, entityID string) bool
	EntityVersionExists(ctx context.Context, entityID string, versionID string) bool
	CountPartKeyValues(ctx context.Context, row TableRow[T]) (int64, error)
	CountSortKeyValues(ctx context.Context, row TableRow[T], partKeyValue string) (int64, error)
	ReadAllPartKeyValues(ctx context.Context, row TableRow[T]) ([]string, error)
	ReadAllSortKeyValues(ctx context.Context, row TableRow[T], partKeyValue string) ([]string, error)
	ReadPartKeyValues(ctx context.Context, row TableRow[T], reverse bool, limit int, offset string) ([]string, error)
	ReadPartKeyValueRange(ctx context.Context, row TableRow[T], from string, to string, reverse bool) ([]string, error)
	ReadSortKeyValues(ctx context.Context, row TableRow[T], partKeyValue string, reverse bool, limit int, offset string) ([]string, error)
	ReadSortKeyValueRange(ctx context.Context, row TableRow[T], partKeyValue string, from string, to string, reverse bool) ([]string, error)
	ReadFirstSortKeyValue(ctx context.Context, row TableRow[T], partKeyValue string) (string, error)
	ReadLastSortKeyValue(ctx context.Context, row TableRow[T], partKeyValue string) (string, error)
	ReadAllEntityIDs(ctx context.Context) ([]string, error)
	ReadEntityIDs(ctx context.Context, reverse bool, limit int, offset string) ([]string, error)
	ReadEntityIDRange(ctx context.Context, from string, to string, reverse bool) ([]string, error)
	ReadAllEntityVersionIDs(ctx context.Context, entityID string) ([]string, error)
	ReadEntityVersionIDs(ctx context.Context, entityID string, reverse bool, limit int, offset string) ([]string, error)
	ReadCurrentEntityVersionID(ctx context.Context, entityID string) (string, error)
	ReadEntities(ctx context.Context, entityIDs []string) []T
	ReadEntitiesAsJSON(ctx context.Context, entityIDs []string) []byte
	ReadEntity(ctx context.Context, entityID string) (T, error)
	ReadEntityAsJSON(ctx context.Context, entityID string) ([]byte, error)
	ReadEntityAsCompressedJSON(ctx context.Context, entityID string) ([]byte, error)
	ReadEntityVersion(ctx context.Context, entityID string, versionID string) (T, error)
	ReadEntityVersionAsJSON(ctx context.Context, entityID string, versionID string) ([]byte, error)
	ReadEntityVersions(ctx context.Context, entityID string, reverse bool, limit int, offset string) ([]T, error)
	ReadEntityVersionsAsJSON(ctx context.Context, entityID string, reverse bool, limit int, offset string) ([]byte, error)
	ReadAllEntityVersions(ctx context.Context, entityID string) ([]T, error)
	ReadAllEntityVersionsAsJSON(ctx context.Context, entityID string) ([]byte, error)
	ReadEntityFromRow(ctx context.Context, row TableRow[T], partKeyValue string, sortKeyValue string) (T, error)
	ReadEntityFromRowAsJSON(ctx context.Context, row TableRow[T], partKeyValue string, sortKeyValue string) ([]byte, error)
	ReadEntityFromRowAsCompressedJSON(ctx context.Context, row TableRow[T], partKeyValue string, sortKeyValue string) ([]byte, error)
	ReadEntitiesFromRow(ctx context.Context, row TableRow[T], partKeyValue string, reverse bool, limit int, offset string) ([]T, error)
	ReadEntitiesFromRowAsJSON(ctx context.Context, row TableRow[T], partKeyValue string, reverse bool, limit int, offset string) ([]byte, error)
	ReadEntityRangeFromRow(ctx context.Context, row TableRow[T], partKeyValue string, from, to string, reverse bool) ([]T, error)
	ReadEntityRangeFromRowAsJSON(ctx context.Context, row TableRow[T], partKeyValue string, from, to string, reverse bool) ([]byte, error)
	ReadAllEntitiesFromRow(ctx context.Context, row TableRow[T], partKeyValue string) ([]T, error)
	ReadAllEntitiesFromRowAsJSON(ctx context.Context, row TableRow[T], partKeyValue string) ([]byte, error)
	ReadRecord(ctx context.Context, row TableRow[T], partKeyValue string, sortKeyValue string) (Record, error)
	ReadRecords(ctx context.Context, row TableRow[T], partKeyValue string, reverse bool, limit int, offset string) ([]Record, error)
	ReadRecordRange(ctx context.Context, row TableRow[T], partKeyValue string, from, to string, reverse bool) ([]Record, error)
	ReadAllRecords(ctx context.Context, row TableRow[T], partKeyValue string) ([]Record, error)
	ReadEntityLabel(ctx context.Context, entityID string) (TextValue, error)
	ReadEntityLabels(ctx context.Context, reverse bool, limit int, offset string) ([]TextValue, error)
	ReadEntityLabelRange(ctx context.Context, from, to string, reverse bool) ([]TextValue, error)
	ReadAllEntityLabels(ctx context.Context, sortByValue bool) ([]TextValue, error)
	FilterEntityLabels(ctx context.Context, f func(TextValue) bool) ([]TextValue, error)
	ReadPartKeyLabel(ctx context.Context, row TableRow[T], partKeyValue string) (TextValue, error)
	ReadPartKeyLabels(ctx context.Context, row TableRow[T], reverse bool, limit int, offset string) ([]TextValue, error)
	ReadPartKeyLabelRange(ctx context.Context, row TableRow[T], from, to string, reverse bool) ([]TextValue, error)
	ReadAllPartKeyLabels(ctx context.Context, row TableRow[T], sortByValue bool) ([]TextValue, error)
	FilterPartKeyLabels(ctx context.Context, row TableRow[T], f func(TextValue) bool) ([]TextValue, error)
	ReadTextValue(ctx context.Context, row TableRow[T], partKeyValue string, sortKeyValue string) (TextValue, error)
	ReadTextValues(ctx context.Context, row TableRow[T], partKeyValue string, reverse bool, limit int, offset string) ([]TextValue, error)
	ReadTextValueRange(ctx context.Context, row TableRow[T], partKeyValue string, from, to string, reverse bool) ([]TextValue, error)
	ReadAllTextValues(ctx context.Context, row TableRow[T], partKeyValue string, sortByValue bool) ([]TextValue, error)
	FilterTextValues(ctx context.Context, row TableRow[T], partKeyValue string, f func(TextValue) bool) ([]TextValue, error)
	ReadNumericValue(ctx context.Context, row TableRow[T], partKeyValue string, sortKeyValue string) (NumValue, error)
	ReadNumericValues(ctx context.Context, row TableRow[T], partKeyValue string, reverse bool, limit int, offset string) ([]NumValue, error)
	ReadNumericValueRange(ctx context.Context, row TableRow[T], partKeyValue string, from, to string, reverse bool) ([]NumValue, error)
	ReadAllNumericValues(ctx context.Context, row TableRow[T], partKeyValue string, sortByValue bool) ([]NumValue, error)
}

TableReader is the interface that defines methods for reading entity-related information from a DynamoDB table based on an opinionated implementation of DynamoDB by Table.

type TableRow

type TableRow[T any] struct {
	RowName       string
	PartKeyName   string
	PartKeyValue  func(T) string
	PartKeyValues func(T) []string
	PartKeyLabel  func(T) string
	SortKeyName   string
	SortKeyValue  func(T) string
	JsonValue     func(T) []byte
	TextValue     func(T) string
	NumericValue  func(T) float64
	TimeToLive    func(T) int64
}

TableRow represents a single "wide row" in an Entity Table. A "wide row" is used to quickly provide a response to a specific query, or question for the data. Example: "What error events occurred on this day?"

The following attributes are stored in the table. These attribute names are configurable.

  • v_part: partition key (required)
  • v_sort: sorting key (required; for an entity row without versions, reuse the partition key)
  • v_json: gzip compressed JSON data (optional)
  • v_text: string value (optional)
  • v_num: double-wide floating point numeric value (optional)
  • v_expires: expiration timestamp in epoch seconds (optional)

Pipe-separated key values are used to avoid naming collisions, supporting multiple row types in a single table: Entity Values: rowName|partKeyName|partKeyValue -- (sortKeyValue, value), (sortKeyValue, value), ... Partition Keys: rowName|partKeyName -- (partKeyValue, label), (partKeyValue, label), ... Note that in addition to storing the entity data in a given row, we're also storing the partition key values used for each row, to support queries that "walk" the entire data set for a given row type.

func (TableRow[T]) IsValid

func (row TableRow[T]) IsValid() bool

IsValid returns true if the TableRow contains all required fields.

type TableWriter

type TableWriter[T any] interface {
	WriteEntity(ctx context.Context, entity T) error
	UpdateEntity(ctx context.Context, entity T) error
	UpdateEntityVersion(ctx context.Context, oldVersion T, newVersion T) error
	DeleteEntity(ctx context.Context, entity T) error
	DeleteEntityWithID(ctx context.Context, entityID string) (T, error)
	DeleteEntityVersionWithID(ctx context.Context, entityID, versionID string) (T, error)
}

TableWriter is the interface that defines methods for writing, updating, or deleting an entity from a DynamoDB table based on an opinionated implementation of DynamoDB by Table.

type TextValue

type TextValue struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

TextValue represents a key-value pair where the value is a string.

func (TextValue) ContainsAll added in v1.3.0

func (tv TextValue) ContainsAll(terms []string) bool

ContainsAll returns true if the Value contains all the terms (an AND filter). The terms should be lowercase for a case-insensitive search.

func (TextValue) ContainsAny added in v1.3.0

func (tv TextValue) ContainsAny(terms []string) bool

ContainsAny returns true if the Value contains any of the terms (an OR filter). The terms should be lowercase for a case-insensitive search.

func (TextValue) String added in v1.3.0

func (tv TextValue) String() string

String returns a string representation of the TextValue.

Directories

Path Synopsis
Package thing provides an example demonstrating a way of using the versionary package.
Package thing provides an example demonstrating a way of using the versionary package.

Jump to

Keyboard shortcuts

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