sqlstore

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(ctx context.Context, db *sql.DB) error

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

func SchemaFingerprint(ctx context.Context, db *sql.DB) (string, error)

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.

func Upgrade

func Upgrade(context.Context, *sql.DB) error

Upgrade rewrites what an idempotent CREATE TABLE cannot reach in a database an earlier release created. The first release has no such step; a host that runs Schema through its own tool still calls it after, so a later step arrives with the dependency bump.

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

func New(db *sql.DB) *Store

New returns the store over db, which Apply (or the host's migration of Migrations) prepared.

func (*Store) EraseCluster

func (s *Store) EraseCluster(nodeID string, ep uint16, cluster uint32) error

EraseCluster implements client.Store.

func (*Store) EraseEndpoint

func (s *Store) EraseEndpoint(nodeID string, ep uint16) error

EraseEndpoint implements client.Store.

func (*Store) EraseNode

func (s *Store) EraseNode(nodeID string) error

EraseNode 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

func (s *Store) NodeIDs() ([]string, error)

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.

Jump to

Keyboard shortcuts

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