ds

package
v0.0.0-...-abdc0c1 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCacheMiss   = errors.New("dsorm: cache miss")
	ErrCASConflict = errors.New("dsorm: cas conflict")
	ErrNotStored   = errors.New("dsorm: not stored")
)
View Source
var (

	// Tag Keys
	KeyKind, _ = tag.NewKey("kind")

	// Views
	AllViews = []*view.View{
		{
			Name:        "dsorm/cache_hit",
			Description: "Cache hits",
			Measure:     mCacheHit,
			Aggregation: view.Sum(),
			TagKeys:     []tag.Key{KeyKind},
		},
		{
			Name:        "dsorm/cache_miss",
			Description: "Cache misses",
			Measure:     mCacheMiss,
			Aggregation: view.Sum(),
			TagKeys:     []tag.Key{KeyKind},
		},
	}
)

Functions

func MarshalPropertyList

func MarshalPropertyList(pl datastore.PropertyList) ([]byte, error)

func NewCloudStore

func NewCloudStore(client *datastore.Client) *cloudStore

NewCloudStore wraps a datastore.Client

func UnmarshalPropertyList

func UnmarshalPropertyList(data []byte, pl *datastore.PropertyList) error

Types

type Cache

type Cache interface {
	// AddMulti adds items if keys are not in use. Returns ErrNotStored on conflict.
	AddMulti(ctx context.Context, items []*Item) error

	// CompareAndSwapMulti updates items if unchanged since Get. Returns ErrCASConflict or ErrNotStored.
	CompareAndSwapMulti(ctx context.Context, items []*Item) error

	// DeleteMulti removes keys.
	DeleteMulti(ctx context.Context, keys []string) error

	// GetMulti fetches keys. Returns map of found items. No error on miss.
	GetMulti(ctx context.Context, keys []string) (map[string]*Item, error)

	// SetMulti sets items blindly. Returns error per item on failure.
	SetMulti(ctx context.Context, items []*Item) error

	// Increment atomically increments a key's integer value by delta.
	// Returns the new value after incrementing. Creates the key with
	// value=delta and the given expiration if the key does not exist.
	Increment(ctx context.Context, key string, delta int64, expiration time.Duration) (int64, error)
}

Cache interface for backend storage.

type Client

type Client struct {
	Store
	// contains filtered or unexported fields
}

func NewClient

func NewClient(ctx context.Context, cacher Cache, opts ...ClientOption) (*Client, error)

NewClient returns a dsorm.Client using the provided cache.

func (*Client) Cacher

func (c *Client) Cacher() Cache

Cacher returns the underlying Cache implementation used by the client.

func (*Client) Close

func (c *Client) Close() error

Close closes the underlying store.

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, key *datastore.Key) error

Delete deletes a single entity.

func (*Client) DeleteMulti

func (c *Client) DeleteMulti(ctx context.Context, keys []*datastore.Key) error

DeleteMulti deletes multiple entities. - Batches requests (limit 500). - Manages cache locks directly.

func (*Client) Get

func (c *Client) Get(ctx context.Context, key *datastore.Key, val interface{}) error

Get retrieves entity by key into val (struct pointer).

func (*Client) GetMulti

func (c *Client) GetMulti(ctx context.Context,
	keys []*datastore.Key, vals interface{}) error

GetMulti retrieves entities by keys. - Batches requests (limit 1000). - Uses cache transparently. - Falls back to datastore on cache miss/fail.

func (*Client) Mutate

func (c *Client) Mutate(ctx context.Context, muts ...*Mutation) ([]*datastore.Key, error)

Mutate applies mutations. - Locks affecting keys in cache. - Invalidates cache on success.

func (*Client) NewTransaction

func (c *Client) NewTransaction(ctx context.Context, opts ...datastore.TransactionOption) (*Transaction, error)

NewTransaction starts a transaction with cache-aware context.

func (*Client) Put

func (c *Client) Put(ctx context.Context,
	key *datastore.Key, val interface{}) (*datastore.Key, error)

Put saves an entity.

func (*Client) PutMulti

func (c *Client) PutMulti(ctx context.Context,
	keys []*datastore.Key, vals interface{}) ([]*datastore.Key, error)

PutMulti saves entities. - Batches requests (limit 500). - Invalidates/Updates cache locks.

func (*Client) RunInTransaction

func (c *Client) RunInTransaction(ctx context.Context, f func(tx *Transaction) error, opts ...datastore.TransactionOption) (cmt *datastore.Commit, err error)

RunInTransaction wrapper ensuring cache interaction.

type ClientOption

type ClientOption func(*Client)

ClientOption option for dsorm Client.

func WithCachePrefix

func WithCachePrefix(prefix string) ClientOption

func WithDatastoreClient

func WithDatastoreClient(ds *datastore.Client) ClientOption

func WithOnErrorFunc

func WithOnErrorFunc(f OnErrorFunc) ClientOption

WithOnErrorFunc sets an error handler for internal non-fatal errors.

func WithStore

func WithStore(s Store) ClientOption

type CloudAccess

type CloudAccess interface {
	DatastoreClient() *datastore.Client
}

CloudAccess is an optional interface that cloud-backed Store implementations can implement to expose the underlying *datastore.Client:

if ca, ok := store.(CloudAccess); ok {
    dsClient := ca.DatastoreClient()
}

type Filter

type Filter struct {
	Field string
	Op    string
	Value interface{}
}

Filter represents a query filter natively in the ds package.

type Item

type Item struct {
	Key        string
	Value      []byte
	Flags      uint32
	Expiration time.Duration
	// contains filtered or unexported fields
}

Item cache entry.

func (*Item) GetCASInfo

func (i *Item) GetCASInfo() interface{}

GetCASInfo gets opaque CAS value.

func (*Item) SetCASInfo

func (i *Item) SetCASInfo(value interface{})

SetCASInfo sets opaque CAS value once.

type Iterator

type Iterator interface {
	Next(dst interface{}) (*datastore.Key, error)
	Cursor() (string, error)
}

Iterator is an interface that abstracts datastore.Iterator

type LocalAccess

type LocalAccess interface {
	DB(namespace string) (*sql.DB, error)
}

LocalAccess is an optional interface that local Store implementations can implement to expose the underlying *sql.DB for a given namespace:

if la, ok := store.(LocalAccess); ok {
    db, err := la.DB("")
}

type MultiError

type MultiError []error

MultiError maps errors to input elements.

func (MultiError) Error

func (m MultiError) Error() string

type Mutation

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

Mutation represents a datastore mutation.

func NewDelete

func NewDelete(k *datastore.Key) *Mutation

func NewInsert

func NewInsert(k *datastore.Key, src interface{}) *Mutation

func NewUpdate

func NewUpdate(k *datastore.Key, src interface{}) *Mutation

func NewUpsert

func NewUpsert(k *datastore.Key, src interface{}) *Mutation

type Mutator

type Mutator interface {
	Mutate(ctx context.Context, muts ...*datastore.Mutation) ([]*datastore.Key, error)
}

Mutator is an optional interface for stores that support datastore mutations.

type OnErrorFunc

type OnErrorFunc func(ctx context.Context, err error)

type Order

type Order struct {
	Field     string
	Direction string // "asc" or "desc"
}

Order represents a query order natively in the ds package.

type Query

type Query interface {
	Kind() string
	Filters() []Filter
	Orders() []Order
	GetLimit() int
	GetOffset() int
	IsKeysOnly() bool
	GetAncestor() *datastore.Key
	GetCursor() string
	GetNamespace() string
}

Query defines the methods required for a backend to translate a generic dsorm query.

type Store

type Store interface {
	Get(ctx context.Context, key *datastore.Key, dst interface{}) error
	GetMulti(ctx context.Context, keys []*datastore.Key, dst interface{}) error
	Put(ctx context.Context, key *datastore.Key, src interface{}) (*datastore.Key, error)
	PutMulti(ctx context.Context, keys []*datastore.Key, src interface{}) ([]*datastore.Key, error)
	Delete(ctx context.Context, key *datastore.Key) error
	DeleteMulti(ctx context.Context, keys []*datastore.Key) error
	Run(ctx context.Context, q Query) Iterator
	NewTransaction(ctx context.Context, opts ...datastore.TransactionOption) (TransactionStore, error)
	RunInTransaction(ctx context.Context, f func(tx TransactionStore) error, opts ...datastore.TransactionOption) (*datastore.Commit, error)
	Close() error
}

Store is the interface that abstracts the underlying datastore. It allows replacing Google Cloud Datastore with implementations like LocalDB.

type Transaction

type Transaction struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Transaction wraps ds.TransactionStore with caching.

func (*Transaction) Commit

func (t *Transaction) Commit() (*datastore.Commit, error)

Commit commits cache changes then TX.

func (*Transaction) Delete

func (t *Transaction) Delete(key *datastore.Key) error

func (*Transaction) DeleteMulti

func (t *Transaction) DeleteMulti(keys []*datastore.Key) (err error)

DeleteMulti batch delete. Locks keys.

func (*Transaction) Get

func (t *Transaction) Get(key *datastore.Key, dst interface{}) error

func (*Transaction) GetMulti

func (t *Transaction) GetMulti(keys []*datastore.Key, dst interface{}) error

GetMulti batch get. Bypasses cache in TX.

func (*Transaction) Mutate

func (t *Transaction) Mutate(muts ...*Mutation) ([]*datastore.PendingKey, error)

Mutate locks keys from mutations.

func (*Transaction) Put

func (t *Transaction) Put(key *datastore.Key, src interface{}) (*datastore.PendingKey, error)

func (*Transaction) PutMulti

func (t *Transaction) PutMulti(keys []*datastore.Key, src interface{}) (ret []*datastore.PendingKey, err error)

PutMulti batch put. Locks keys.

func (*Transaction) Rollback

func (t *Transaction) Rollback() (err error)

Rollback passes through to TX.

type TransactionMutator

type TransactionMutator interface {
	Mutate(muts ...*datastore.Mutation) ([]*datastore.PendingKey, error)
}

TransactionMutator is an optional interface for transaction stores that support mutations.

type TransactionStore

type TransactionStore interface {
	Get(key *datastore.Key, dst interface{}) error
	GetMulti(keys []*datastore.Key, dst interface{}) error
	Put(key *datastore.Key, src interface{}) (*datastore.PendingKey, error)
	PutMulti(keys []*datastore.Key, src interface{}) ([]*datastore.PendingKey, error)
	Delete(key *datastore.Key) error
	DeleteMulti(keys []*datastore.Key) error
	Commit() (*datastore.Commit, error)
	Rollback() error
}

TransactionStore abstracts datastore.Transaction

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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