Documentation
¶
Overview ¶
Package d1 is the Cloudflare D1 backend of meta.Store. D1 has no wire protocol, so the backend speaks the HTTP and JSON protocol of docs/architecture.md "D1 proxy protocol" to a Worker that holds the D1 binding. Each write is one batch, which D1 runs as one transaction, and each read is one query.
Index ¶
- Constants
- func ParamCount(sql string) int
- func SplitScript(script string) []string
- type Client
- type Option
- type Result
- type Row
- type StatementError
- type Stmt
- type Store
- func (s *Store) AckLand(ctx context.Context, repo, id string) error
- func (s *Store) AcquireLandLease(ctx context.Context, repo, ref, owner string, now, until time.Time) (time.Time, error)
- func (s *Store) AcquireLease(ctx context.Context, id string, partition int, owner string, ...) (meta.Lease, error)
- func (s *Store) AdvanceCursor(ctx context.Context, id string, partition int, owner string, cursor int64) error
- func (s *Store) BeginDelete(ctx context.Context, repo string, keys []string, ...) ([]string, error)
- func (s *Store) Claim(ctx context.Context, id string, epoch uint64, owner, repo string) (meta.Workspace, error)
- func (s *Store) ClaimLands(ctx context.Context, repo, ref, owner string, now time.Time, limit int) ([]meta.LandItem, error)
- func (s *Store) Close() error
- func (s *Store) CreateRepo(ctx context.Context, r meta.Repo, repoText, commit string) error
- func (s *Store) CreateSubscription(ctx context.Context, sub meta.Subscription) (meta.Subscription, error)
- func (s *Store) CreateUserToken(ctx context.Context, hash string, t meta.UserToken) error
- func (s *Store) Delete(ctx context.Context, id string) error
- func (s *Store) DeleteRef(ctx context.Context, ws string, epoch uint64, repo, name string, expect int64) error
- func (s *Store) DeleteSubscription(ctx context.Context, id string) error
- func (s *Store) EnqueueLand(ctx context.Context, r meta.LandRequest, now time.Time) (meta.LandItem, error)
- func (s *Store) EventHeads(ctx context.Context) ([]int64, error)
- func (s *Store) Events(ctx context.Context, partition int, after int64, limit int) ([]meta.Event, error)
- func (s *Store) FinishLands(ctx context.Context, repo, ref, owner string, now time.Time, ...) error
- func (s *Store) Flush(ctx context.Context, ws string, epoch uint64, generation int64, ...) error
- func (s *Store) ForgetRetired(ctx context.Context, repo string, keys []string) error
- func (s *Store) LandItem(ctx context.Context, repo, id string) (meta.LandItem, error)
- func (s *Store) LandLogSince(ctx context.Context, repo, ref, base string, limit int) ([]meta.LandLogRow, error)
- func (s *Store) LandPending(ctx context.Context) ([]meta.LandKey, error)
- func (s *Store) LandRef(ctx context.Context, m meta.LandMove, owner string, now time.Time) (meta.Ref, error)
- func (s *Store) Live(ctx context.Context, id string, epoch uint64, repo string) error
- func (s *Store) Manifest(ctx context.Context, repo string) (meta.Manifest, error)
- func (s *Store) ManifestRepos(ctx context.Context) ([]string, error)
- func (s *Store) MergeDeleteRef(ctx context.Context, repo, name string, expect int64) error
- func (s *Store) MergeRef(ctx context.Context, repo, name string, expect int64, sha string) (meta.Ref, error)
- func (s *Store) OpHeads(ctx context.Context, ws string) (meta.OpHeads, error)
- func (s *Store) Ping(ctx context.Context) error
- func (s *Store) ProtectedRefs(ctx context.Context, repo string) ([]string, error)
- func (s *Store) PruneEvents(ctx context.Context, before time.Time) error
- func (s *Store) PruneLands(ctx context.Context, before time.Time) error
- func (s *Store) Ref(ctx context.Context, repo, name string) (meta.Ref, error)
- func (s *Store) Refs(ctx context.Context, repo string) ([]meta.Ref, error)
- func (s *Store) ReleaseLandLease(ctx context.Context, repo, ref, owner string) error
- func (s *Store) Repo(ctx context.Context, path string) (meta.Repo, error)
- func (s *Store) Report(ctx context.Context, ws string, epoch uint64, repo string, generation int64, ...) error
- func (s *Store) Repos(ctx context.Context) ([]meta.Repo, error)
- func (s *Store) RetireFiles(ctx context.Context, repo string, keys []string, at time.Time) error
- func (s *Store) RetiredFiles(ctx context.Context, repo string, retiredBefore, staleBefore time.Time, ...) ([]meta.Retired, error)
- func (s *Store) RevokeUserToken(ctx context.Context, id string) error
- func (s *Store) SetProtectedRefs(ctx context.Context, repo string, patterns []string) error
- func (s *Store) Subscriptions(ctx context.Context) ([]meta.Subscription, error)
- func (s *Store) SwapManifest(ctx context.Context, repo string, expect int64, body []byte) (int64, error)
- func (s *Store) SwapOpHeads(ctx context.Context, ws string, epoch uint64, repo string, expect int64, ...) (meta.OpHeads, error)
- func (s *Store) SwapRef(ctx context.Context, ws string, epoch uint64, repo, name string, expect int64, ...) (meta.Ref, error)
- func (s *Store) UpdateRef(ctx context.Context, repo, name, old, new, kind string) (meta.Ref, error)
- func (s *Store) UpdateSubscription(ctx context.Context, sub meta.Subscription) (meta.Subscription, error)
- func (s *Store) UserTokenByHash(ctx context.Context, hash string) (meta.UserToken, error)
- func (s *Store) UserTokens(ctx context.Context) ([]meta.UserToken, error)
Constants ¶
const DefaultMaxResponse = 16 << 20
DefaultMaxResponse is the largest response that a client reads. It is sized for one chunk of a manifest, base64 and JSON included, with room for the results of a batch.
const MaxInt = 1<<53 - 1
MaxInt is the largest integer that D1 holds exactly through its Worker binding, which reads and writes a JSON number as a double. The backend refuses an integer outside ±MaxInt.
const MaxManifest = 64 << 20
MaxManifest bounds a manifest. A swap stages it in requests of stageBytes each, so memory does not grow with it; the bound is the size that TestRealProxy runs through the Worker.
Variables ¶
This section is empty.
Functions ¶
func ParamCount ¶
ParamCount is the number of parameters that D1 needs to bind for sql: the highest number of a `?N` placeholder, or the count of bare `?` placeholders. D1 refuses a statement with another count.
func SplitScript ¶
SplitScript splits a script of plain statements at each semicolon outside a string, a quoted name and a comment. It does not read a trigger body, which holds semicolons; no migration has one.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client calls the proxy.
type Option ¶
type Option func(*Store)
Option changes how Open builds the store.
func MaxResponse ¶
MaxResponse sets the largest response that the store reads, in bytes.
type Result ¶
Result is the answer to one statement: the rows that it returned, as arrays in column order, and the rows that it changed.
type Row ¶
type Row []any
Row is one row of a result, in column order. A value is nil, a json.Number, a string or a {"$blob": base64} object.
type StatementError ¶
type StatementError struct{ Message string }
StatementError is a request that D1 refuses. D1 does not name the failing statement of a batch. No statement of the batch applied.
func (*StatementError) Error ¶
func (e *StatementError) Error() string
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store keeps the metadata in a D1 database that the proxy at the client's address serves.
func Open ¶
Open connects to the proxy at url with the bearer secret token and applies the migrations that the database lacks.
func (*Store) AcquireLandLease ¶
func (*Store) AcquireLease ¶
func (*Store) AdvanceCursor ¶
func (*Store) BeginDelete ¶
func (*Store) ClaimLands ¶
func (*Store) CreateRepo ¶
func (*Store) CreateSubscription ¶
func (s *Store) CreateSubscription(ctx context.Context, sub meta.Subscription) (meta.Subscription, error)
CreateSubscription is one batch. The cursor rows insert only while the subscription has none, which holds exactly when the first statement created it; the last statement reads the cursors back.
func (*Store) CreateUserToken ¶
func (*Store) DeleteSubscription ¶
func (*Store) EnqueueLand ¶
func (*Store) Events ¶
func (s *Store) Events(ctx context.Context, partition int, after int64, limit int) ([]meta.Event, error)
Events reads the rows first and the floor second, so a prune between the reads gives ErrEventsPruned and never a silent gap.
func (*Store) FinishLands ¶
func (s *Store) FinishLands(ctx context.Context, repo, ref, owner string, now time.Time, results []meta.LandDone) error
FinishLands is one batch: each item ends only while the lease holds, and the last statement reads the lease.
func (*Store) ForgetRetired ¶
func (*Store) LandLogSince ¶
func (s *Store) LandLogSince(ctx context.Context, repo, ref, base string, limit int) ([]meta.LandLogRow, error)
LandLogSince plans the read with the row numbers and sizes only, and answers none when the rows after the base are more than limit. It then reads the rows in pages of at most landLogPageBytes of paths. A prune between the pages leaves a gap, which the read reports as none, as for a base that no row holds.
func (*Store) LandPending ¶
func (*Store) LandRef ¶
func (s *Store) LandRef(ctx context.Context, m meta.LandMove, owner string, now time.Time) (meta.Ref, error)
LandRef is one batch of conditional statements. Each carries the one condition applied: the event insert took its number in this batch. The last statement reads whether it applied, and the generation.
func (*Store) Manifest ¶
Manifest reads the published generation, the stage that holds it and its chunk count, then each chunk in its own query, so a response holds one chunk and a manifest of any size reads back. A published chunk never changes, and a swap only deletes it. A chunk that is gone, or a count that differs, means a swap ran in between, and the read starts again.
func (*Store) ManifestRepos ¶
ManifestRepos lists in pages by repo.
func (*Store) MergeDeleteRef ¶
func (*Store) ProtectedRefs ¶
func (*Store) PruneEvents ¶
func (*Store) ReleaseLandLease ¶
func (*Store) Repos ¶
Repos lists in pages by the key of its order, so no response holds all the repos. A repo that a writer adds or removes between two pages may be missed or not, as in any listing that is not one snapshot.
func (*Store) RetireFiles ¶
func (*Store) RetiredFiles ¶
func (*Store) RevokeUserToken ¶
func (*Store) SetProtectedRefs ¶
func (*Store) Subscriptions ¶
Subscriptions lists in pages of subscriptions by creation order, and reads the cursors of each page in a second query, so a response holds neither every subscription nor the url of a subscription once for each partition.
func (*Store) SwapManifest ¶
func (s *Store) SwapManifest(ctx context.Context, repo string, expect int64, body []byte) (int64, error)
SwapManifest stages the chunks under a random stage that no reader names, in requests of at most stageBytes of blobs, and then publishes with one small batch that binds no blob. The publish moves the head of the repo to the stage and deletes the chunks of the old stage, only while the generation is the expected one and every staged chunk is there. A batch rolls back on an error and never on a statement that changed no row, so each statement carries that condition, and a lost publish changes nothing that a reader sees. The writer then deletes its own stage; a stage that a crash or a failed delete leaves is removed by the next publish of the repo after maxStageAge.
func (*Store) SwapOpHeads ¶
func (*Store) UpdateRef ¶
UpdateRef compares on the commit of the ref instead of its generation. Its event insert has the condition of the write and runs first, as in swapRef.
func (*Store) UpdateSubscription ¶
func (s *Store) UpdateSubscription(ctx context.Context, sub meta.Subscription) (meta.Subscription, error)