Reference / KV API for Go
KV API for Go
Every public type, function and constant of the KV engine in tinyshed.org/tinystore/kv, generated from its source. The KV guide explains how to use them, and the Bun and Node, Python and Rust pages list the same API.
After
func After(key string) afterAfter starts a page after a key: the page before's Next.
Burst
func Burst(n int) burstBurst is how many requests a rate limit lets through at once.
EncryptedNew in 0.13.1
func Encrypted() encryptedEncrypted keeps every value of the bucket, or every answer of a once, sealed with the store's encryption key; a name keeps whether it is.
ExpiresAt
func ExpiresAt(t time.Time) expiresAtExpiresAt is when a key a write gives expires, as a time.
FlushEvery
func FlushEvery(d time.Duration) flushEveryFlushEvery keeps counts in memory and writes them that often, so that an add costs no commit and a crash loses at most that span.
FreshNew in 0.13.1
func Fresh(d time.Duration) freshFresh is how long an answer is answered without making a new one, shorter than Keep: past it, the kept answer is answered at once and one new one is made in the background.
Idle
func Idle(d time.Duration) idleIdle is how long a key lives from its last read or write: a session gone unused. A bucket has a TTL or an Idle, not both.
IfVersion
func IfVersion(v Version) ifVersionIfVersion writes only while the key is at this version, an entry's Version: tinystore.ErrConflict otherwise.
KeepNew in 0.13.1
func Keep(d time.Duration) keepKeep is how long an answer is kept: a day unless given.
Limit
func Limit(n int) limitLimit is how many keys a page holds: 1000, and 10,000 at most.
Rate
func Rate(n int, per time.Duration) rateRate is a rate limit's smooth rate: n requests a per, kv.Rate(100, time.Second).
TTL
func TTL(d time.Duration) ttlTTL is how long a key lives from its last write: a bucket's or counters' for every key, or one write's own.
Window
func Window(name string, n int, per time.Duration) windowOptionWindow is a quota's window: n uses a per, counted from a key's first use, kv.Window("daily", 100, 24*time.Hour).
Allowance
type Allowance struct {
OK bool
// Left is how many more would pass now.
Left uint64
// RetryAt is when a refused request may try again; the zero time when it passed.
RetryAt time.Time
// Windows are a quota's windows by name; none for a rate limit.
Windows map[string]WindowUse
}Allowance is what a rate limit or a quota answers a request.
Bucket
type Bucket[T any] struct {
// contains filtered or unexported fields
}Bucket is a bucket's values under one branch of owners, the bucket's own when there are none. Inside a transaction, from In, its calls go through it.
OpenBucket
func OpenBucket[T any](home tinystore.Home, name string, opts ...BucketOption) (*Bucket[T], error)OpenBucket opens a bucket of values of type T by key, on a store or on a database whose file then keeps it. It opens at its first call; its error is a name or an option it refuses.
sessions, err := kv.OpenBucket[Session](store, "sessions", kv.Idle(30*24*time.Hour))
codes, err := kv.OpenBucket[int64](store, "login-codes", kv.TTL(15*time.Minute))
seen, err := kv.OpenBucket[struct{}](store, "stripe-events", kv.TTL(7*24*time.Hour))A string, []byte, integer, float or bool is kept as itself, struct{} as keys alone, and any other type as JSON.
Bucket.Add
func (b *Bucket[T]) Add(ctx context.Context, key string, opts ...CreateOption) (bool, error)Add puts the key in a bucket used as a set, a bucket of struct{}; false when it was there.
Bucket.All
func (b *Bucket[T]) All(ctx context.Context, opts ...ListOption) iter.Seq2[Entry[T], error]All walks this branch's own keys a page at a time, holding nothing between pages. A key written during the walk may or may not be met; it renews no idle key.
Bucket.Clear
func (b *Bucket[T]) Clear(ctx context.Context) errorClear removes this branch's keys and every branch under it, at once however many.
Bucket.Count
func (b *Bucket[T]) Count(ctx context.Context) (int64, error)Count is how many keys All would walk now: this branch's own, not those of the branches under it. An expired key not yet swept and a cleared one are not counted, and an idle key is counted without being renewed. A large branch is counted in several reads, so a key written meanwhile may or may not be. Inside a transaction it is refused as Invalid.
Bucket.Create
func (b *Bucket[T]) Create(ctx context.Context, key string, value T, opts ...CreateOption) (bool, error)Create writes the value only where no live key is, and says whether it did.
Bucket.Delete
func (b *Bucket[T]) Delete(ctx context.Context, key string, opts ...CheckOption) (bool, error)Delete removes the key, and says whether it was there.
Bucket.Entry
func (b *Bucket[T]) Entry(ctx context.Context, key string) (Entry[T], bool, error)Entry is the key's value with its version and expiry, and whether it holds one.
Bucket.Expire
func (b *Bucket[T]) Expire(ctx context.Context, key string, d time.Duration, opts ...CheckOption) errorExpire gives a live key a new expiry, d from now, keeping its value.
Bucket.ExpireAt
func (b *Bucket[T]) ExpireAt(ctx context.Context, key string, at time.Time, opts ...CheckOption) errorExpireAt gives a live key a new expiry at a time, keeping its value.
Bucket.Get
func (b *Bucket[T]) Get(ctx context.Context, key string) (T, bool, error)Get is the key's value, and whether it holds one.
Bucket.Has
func (b *Bucket[T]) Has(ctx context.Context, key string) (bool, error)Has says whether the key holds a value.
Bucket.In
func (b *Bucket[T]) In(tx tinystore.InTx) *Bucket[T]In is the bucket inside a transaction: the store's, whose writes commit together when its function returns, or a database's, whose writes commit with its rows.
Bucket.List
func (b *Bucket[T]) List(ctx context.Context, opts ...ListOption) (Page[T], error)List is one page of this branch's own keys, in the byte order of their text: "10" before "9".
Bucket.Name
func (b *Bucket[T]) Name() stringName is the bucket's name.
Bucket.Reseal
func (b *Bucket[T]) Reseal(ctx context.Context) (uint64, error)Reseal seals again with the store's sealing key every value of this branch, and of the branches under it, that an older key sealed, and says how many. Once every value of the bucket is, the older key can leave the store's options. A value sealed with a key the store does not hold stops it with an Invalid error that names the key, and a reseal after the key is given goes on.
Bucket.Set
func (b *Bucket[T]) Set(ctx context.Context, key string, value T, opts ...SetOption) errorSet writes the value; a key that exists keeps its expiry unless the call gives one.
Bucket.Take
func (b *Bucket[T]) Take(ctx context.Context, key string, opts ...CheckOption) (T, bool, error)Take reads the value and removes the key in one commit: of two callers taking a code at once, one gets it.
Bucket.Under
func (b *Bucket[T]) Under(owners ...any) *Bucket[T]Under is the branch the owners name below this one: whose keys these are.
Bucket.Update
func (b *Bucket[T]) Update(ctx context.Context, key string, change func(current T, found bool) (T, bool)) (T, bool, error)Update writes what change makes of the key's value without losing a write made meanwhile: it reads the key, calls change with its value and whether it holds one, and writes what change returns at the version it read, or creates the key. When another write came first it reads again and calls change again, eight times at most, then fails with a Conflict: change may run more than once, so it must do nothing else. A change that returns false writes nothing. Update returns the value written, or the one there when nothing was, and whether there is one. A key that is there keeps its expiry, and one it creates takes the bucket's, as Set and Create do.
pinned, _, err := courses.Update(ctx, studentID, func(pinned []int, found bool) ([]int, bool) {
return append(pinned, 2518), true
})A store's transaction refuses it, since the transaction runs its function again when a key it read changed: read with Get and write with Set there.
BucketOption
type BucketOption interface {
// contains filtered or unexported methods
}BucketOption is how a bucket opens.
CheckOption
type CheckOption interface {
// contains filtered or unexported methods
}CheckOption is the version a Take, a Delete or an Expire needs the key at.
Counters
type Counters struct {
// contains filtered or unexported fields
}Counters are numbers by key that only add up: 0 for one never added to, or expired.
OpenCounters
func OpenCounters(home tinystore.Home, name string, opts ...CountersOption) (*Counters, error)OpenCounters opens counters by key on the store.
attempts, err := kv.OpenCounters(store, "login-attempts", kv.TTL(15*time.Minute))Counters.Add
func (c *Counters) Add(ctx context.Context, key string, n int64) (int64, error)Add adds n and gives the new count; inside a transaction the count is known once it committed, and Add gives 0.
Counters.Clear
func (c *Counters) Clear(ctx context.Context) errorClear removes this branch's counters and every branch's under it.
Counters.Delete
func (c *Counters) Delete(ctx context.Context, key string) (bool, error)Delete removes the counter, and says whether it was there.
Counters.Get
func (c *Counters) Get(ctx context.Context, key string) (int64, error)Get is the key's count.
Counters.In
func (c *Counters) In(tx tinystore.InTx) *CountersIn is the counters inside the store's transaction: an add commits with its writes.
Counters.Under
func (c *Counters) Under(owners ...any) *CountersUnder is the counters of a branch, each key apart from the same key elsewhere.
CountersOption
type CountersOption interface {
// contains filtered or unexported methods
}CountersOption is how counters open.
CreateOption
type CreateOption interface {
// contains filtered or unexported methods
}CreateOption is what a Create or an Add gives besides its value: an expiry.
Entry
type Entry[T any] struct {
Key string
Value T
Version Version
// ExpiresAt is the zero time for a key that never expires.
ExpiresAt time.Time
}Entry is a key's value with its version and expiry.
Fetched
type Fetched[T any] struct {
Value T
Age time.Duration
Stale bool
}Fetched is an answer with how old it is: Stale once it is older than the Fresh its answers opened with.
ListOption
type ListOption interface {
// contains filtered or unexported methods
}ListOption is how a page of keys reads.
Once
type Once[T any] struct {
// contains filtered or unexported fields
}Once runs a function once a key and keeps its answer: a request sent twice is answered as the first was rather than done twice. The core lets one run of a key go at a time, every client of the store included: it answers the kept answer, or hands the run to this client and keeps what it sends back. Opened with Fresh, a kept answer older than it is answered at once, and the core may hand this client the build of the next one, which runs in the background.
OpenOnceNew in 0.13.1
func OpenOnce[T any](home tinystore.Home, name string, opts ...OnceOption) (*Once[T], error)OpenOnce opens answers of type T kept once a key, a day unless Keep says.
charges, err := kv.OpenOnce[Receipt](store, "charges", kv.Keep(24*time.Hour))
books, err := kv.OpenOnce[GradeBook](store, "cabinet-books", kv.Fresh(10*time.Minute), kv.Keep(12*time.Hour))
grades, err := kv.OpenOnce[GradeBook](store, "grade-books", kv.Encrypted())Once.Clear
func (o *Once[T]) Clear(ctx context.Context) errorClear forgets every answer of this branch and of the branches under it, in one commit. A run under way meanwhile keeps its answer once it ends.
Once.Delete
func (o *Once[T]) Delete(ctx context.Context, key string) (bool, error)Delete forgets the answer kept for key, so that its next run runs fn again.
Once.Fetch
func (o *Once[T]) Fetch(ctx context.Context, key string, fn func(ctx context.Context) (T, error)) (Fetched[T], error)Fetch is Run's answer with how old it is. Opened with Fresh, a kept answer older than it comes back at once, Stale, and fn may make the next one in the background, on a context that the caller's cancel does not reach: its answer replaces the kept one, and its error, which no caller sees, keeps it until Keep ends.
book, err := books.Fetch(ctx, userID, func(ctx context.Context) (GradeBook, error) { return cabinet.Book(ctx, userID) })Once.Get
func (o *Once[T]) Get(ctx context.Context, key string) (T, bool, error)Get is the answer kept for key, and whether one is.
Once.Reseal
func (o *Once[T]) Reseal(ctx context.Context) (uint64, error)Reseal seals again with the store's sealing key every answer of this branch, and of the branches under it, that an older key sealed, and says how many, as an encrypted bucket's Reseal does; answers opened without Encrypted are an Invalid error.
Once.Run
func (o *Once[T]) Run(ctx context.Context, key string, fn func(ctx context.Context) (T, error)) (T, error)Run is the answer kept for key, or fn's, which is kept. A run of a key that another runs, here or in another process, waits for it and gets its answer; an error of fn keeps nothing and is Run's, so the next run runs fn again. A connection lost after fn returned and before its answer was kept is OutcomeUnknown.
Once.Under
func (o *Once[T]) Under(owners ...any) *Once[T]Under is the answers of a branch, each key apart from the same key elsewhere.
OnceOption
type OnceOption interface {
// contains filtered or unexported methods
}OnceOption is how answers kept once a key open.
Page
type Page[T any] struct {
Entries []Entry[T]
Next string
}Page is a page of a branch's keys, and where the next starts: "" on the last.
Quota
type Quota struct {
// contains filtered or unexported fields
}Quota counts uses by key in named windows that reset, all of them or none in one durable write.
OpenQuota
func OpenQuota(home tinystore.Home, name string, opts ...QuotaOption) (*Quota, error)OpenQuota opens a quota of windows, each counted from a key's first use: kv.OpenQuota(store, "ai", kv.Window("daily", 100, 24*time.Hour)).
Quota.Allow
func (q *Quota) Allow(ctx context.Context, key string, n ...int64) (Allowance, error)Allow uses one of key's every window, or n, or none when one has no room; more than a window's limit is Invalid.
Quota.Peek
func (q *Quota) Peek(ctx context.Context, key string) (Allowance, error)Peek is how key stands in every window, using nothing.
Quota.Refund
func (q *Quota) Refund(ctx context.Context, key string, n ...int64) errorRefund gives one use back to every window of key, or n.
Quota.Reset
func (q *Quota) Reset(ctx context.Context, key string) errorReset forgets key's uses in every window.
Quota.Under
func (q *Quota) Under(owners ...any) *QuotaUnder is the quota of a branch, each key apart from the same key elsewhere.
QuotaOption
type QuotaOption interface {
// contains filtered or unexported methods
}QuotaOption is how a quota opens.
RateLimit
type RateLimit struct {
// contains filtered or unexported fields
}RateLimit is a smooth rate of requests a key, the generic cell rate algorithm with its times in the core's memory.
OpenRateLimit
func OpenRateLimit(home tinystore.Home, name string, opts ...RateLimitOption) (*RateLimit, error)OpenRateLimit opens a rate limit: kv.OpenRateLimit(store, "api", kv.Rate(100, time.Second), kv.Burst(20)).
RateLimit.Allow
func (r *RateLimit) Allow(ctx context.Context, key string, n ...int64) (Allowance, error)Allow asks for one request of key, or n: all pass or none; more than the burst is Invalid.
RateLimit.Peek
func (r *RateLimit) Peek(ctx context.Context, key string) (Allowance, error)Peek is the answer one request would get, using nothing.
RateLimit.Reset
func (r *RateLimit) Reset(ctx context.Context, key string) errorReset forgets key, so that its next request starts afresh.
RateLimit.Under
func (r *RateLimit) Under(owners ...any) *RateLimitUnder is the rate limit of a branch, each key apart from the same key elsewhere.
RateLimitOption
type RateLimitOption interface {
// contains filtered or unexported methods
}RateLimitOption is how a rate limit opens.
SetOption
type SetOption interface {
// contains filtered or unexported methods
}SetOption is what a Set gives besides its value: an expiry, or the version the key must still be at.
Version
type Version []byteVersion is what a key was when it was read: compared only for equality, given back to IfVersion.
WindowUse
type WindowUse struct {
Used, Limit, Left uint64
// ResetsAt is when the window resets; the zero time for one not started.
ResetsAt time.Time
}WindowUse is one window of a quota as a key stands in it.