Documentation
¶
Overview ¶
Package sqlstore is a durable client.Store on SQLite (brief AL §8; ADR 0020, stage 4): the images, settings and commissioning records of a controller's client nodes, in the tables matter_client_values and matter_client_versions. It is the place of matter.js's storage contexts under nodes/<id>/endpoints/<ep>/<cluster>/<attr> with the version under "__version__" (packages/node/src/storage/client/ at 0ceb202d).
The schema ships the way store/ ships the device's: Schema is the DDL as one idempotent script, Apply runs it, Migrations are the same schema as versioned steps for a host's own migration tool, and SchemaFingerprint tells a host that its tables match this release's. The list is this package's own, so a device-side host's pin on len(store.Migrations()) does not move for a controller table.
The package takes an open *sql.DB and never opens one; it is written against modernc.org/sqlite, which the module already depends on.
Index ¶
- func Apply(ctx context.Context, db *sql.DB) error
- func Schema() string
- func SchemaFingerprint(ctx context.Context, db *sql.DB) (string, error)
- func Upgrade(context.Context, *sql.DB) error
- type Migration
- type Store
- func (s *Store) EraseCluster(nodeID string, ep uint16, cluster uint32) error
- func (s *Store) EraseEndpoint(nodeID string, ep uint16) error
- func (s *Store) EraseNode(nodeID string) error
- func (s *Store) Load(nodeID string) ([]client.StoredEndpoint, error)
- func (s *Store) NodeIDs() ([]string, error)
- func (s *Store) Save(nodeID string, ep uint16, cluster uint32, values map[uint32][]byte, ...) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Apply ¶
Apply executes Schema against db, then Upgrade. Every statement is idempotent, so it is meant to run on every boot. It neither opens the database nor sets pragmas.
func Schema ¶
func Schema() string
Schema returns the DDL of the matter_client_* tables as one idempotent SQL script — store.Schema's counterpart for the controller's client nodes. A host with its own migration tool feeds it through that tool; a host without one calls Apply.
func SchemaFingerprint ¶
SchemaFingerprint returns the SHA-256, hex-encoded, of db's matter_client_* schema: each table Schema creates and the automatic indexes of its keys, as sqlite_schema records them (type, name, table and the CREATE statement with identifier quotes dropped and whitespace collapsed) — the method of store.SchemaFingerprint. Rows and other tables do not count.
Types ¶
type Migration ¶
type Migration struct {
// Version names the step: "v1", "v2", … in order; never reused, and a
// released step's text never changes.
Version string
// Up moves a database from the previous step to this one.
Up string
// Down reverts Up.
Down string
}
Migration is one versioned step of the matter_client_* schema, for a host that runs its own migration tool — the shape of store.Migration, declared here because that type's documentation and pin are the device tables'.
func Migrations ¶
func Migrations() []Migration
Migrations returns the schema's steps in order. Applied one after the other to an empty database they produce the schema Apply produces — the same SchemaFingerprint — which TestMigrationsMatchApply holds.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is a client.Store over a database the schema was applied to. It is safe for concurrent use: database/sql is, and SQLite serializes the writers.
func New ¶
New returns the store over db, which Apply (or the host's migration of Migrations) prepared.
func (*Store) EraseCluster ¶
EraseCluster implements client.Store.
func (*Store) EraseEndpoint ¶
EraseEndpoint implements client.Store.
func (*Store) Load ¶
func (s *Store) Load(nodeID string) ([]client.StoredEndpoint, error)
Load implements client.Store: the endpoints in number order, their clusters in id order. A cluster with a version row but no values loads empty, as client.MemoryStore keeps a cluster a Save wrote.
func (*Store) NodeIDs ¶
NodeIDs implements client.Store: the distinct node ids of both tables, sorted.
func (*Store) Save ¶
func (s *Store) Save(nodeID string, ep uint16, cluster uint32, values map[uint32][]byte, version *uint32) error
Save implements client.Store in one transaction: each given attribute is written, a nil value deleted (EndpointStore.ts:104-105 deletes an undefined key); the cluster's version is set to version, NULL for nil, as client.MemoryStore replaces it.