Repository navigation
Stores
All stores pass the same conformance suite (storetest), so tests on memory hold on SQLite, Postgres and Cosmos DB. The one difference: Cosmos DB limits how much one transaction writes (see below and Transactions).
db, _ := sql.Open("sqlite", file+"?_pragma=foreign_keys(1)&_pragma=busy_timeout(10000)&_pragma=journal_mode(WAL)&_txlock=immediate")
store, err := sqlstore.New(db, sqlstore.SQLite)With modernc.org/sqlite (no cgo) or github.com/mattn/go-sqlite3 (?_foreign_keys=1). Foreign keys must be on: New checks it. _txlock=immediate avoids lock upgrades failing under concurrent transactions. With db.SetMaxOpenConns(1) (a common choice for SQLite), never use the outer store inside Tx.
Storage: Int, Bool (0/1) and Time (microseconds since 1970, UTC) as INTEGER, Float as REAL, String and JSON as TEXT, Bytes as BLOB.
db, _ := sql.Open("pgx", "postgres://user@host/db")
store, err := sqlstore.New(db, sqlstore.Postgres)Tables go into the search_path schema. Storage: TEXT with collation "C" (byte order, as in the other stores), BIGINT, DOUBLE PRECISION, BOOLEAN, TIMESTAMPTZ, BYTEA, JSONB. String columns of tables created before ablage may have another collation (the database default, usually language rules); ablage compares and sorts them by bytes all the same, but Lt, Gt, Prefix and sorting on such a column cannot use its index.
cosmosstore is its own Go module, so only apps that use it get the Azure SDK:
go get github.com/danielgtmn/ablage/cosmosstorecred, _ := azcosmos.NewKeyCredential(key)
client, _ := azcosmos.NewClientWithKey(endpoint, cred, nil)
db, _ := client.NewDatabase("app")
container, err := cosmosstore.CreateContainer(ctx, db, "rows", 0, tables...) // once, on deploy
store, err := cosmosstore.New(container, tenantID)A store lives in one container and one logical partition: every document carries the partition key value given to New, for example a tenant, and all tables of the store share it. Every operation stays in that partition, which is what lets a transaction commit as one transactional batch. A logical partition holds at most 20 GB and serves at most 10,000 RU/s; split data that grows beyond that by tenant or similar, one store each.
-
Transactions are buffered in the client and committed as one batch, checked with ETags: a row changed by someone else since it was read fails the commit with
ErrStale, and operations outsideTxstart over. Cosmos DB takes at most 100 documents in one batch, so a transaction, or oneUpdateorDeletematching many rows, writes at most 100 documents: each row once, plus one per unique value it sets or releases and one per row a new reference points to. Beyond that it fails withErrTooLargeand writes nothing. Write large amounts in several transactions. -
Unique values are claimed with small guard documents, so soft delete and indexes over several fields work as in SQL, and
UpsertOnfinds the row with two point reads. -
References are checked when written, and the referenced row is touched (a small patch) in the same batch, so a concurrent delete fails and starts over instead of leaving a dangling reference. The touch costs a write of the referenced row;
cosmosstore.WithoutReferenceTouches()turns it off. -
Indexing policy:
CreateContainersets it for the tables you pass;cosmosstore.UpdateIndexingPolicychanges it later. Pass every table stored in the container. Cosmos DB needs a composite index for every sort over more than one property, and ablage always sorts by the id last, so the policy has one for each declared index (both directions): sort by indexed fields. Changing the policy makes Cosmos DB rebuild indexes in the background, which costs RU, soMigratenever does it; do it on deploy. - Migrate keeps the tables of a partition in a schema document. It gives existing rows the fields added since (NULL or the zero value) and creates or drops the guards of unique indexes added or removed.
-
Cost: a lookup by id, also within a scope, is a point read (1 RU for a small document); a single
Insertoutside a transaction is one batch without reads.store.Stats()counts requests.Aggregatereads the needed fields of the matching rows and computes in the client;Offsetis charged by Cosmos DB for the skipped rows.
Storage: String as is (Cosmos DB compares by code point, the byte order of UTF-8), Int and Float as 16 hex digits that sort like the numbers (JSON numbers would lose int64 precision and cannot hold infinity), Time as fixed-width UTC text, Bytes as hex, JSON as text, Bool as JSON.
For tests, the vNext emulator runs everything (make cosmos). It does not check composite indexes, reports no real request charges, and fails every query on a container after its properties were replaced (emulator bug #393, images since vnext-EN20261117pre): do not call UpdateIndexingPolicy against it.
memstore.New(): for tests. Every operation and transaction holds one lock (serializable); data is gone when the process ends.
Values go to the database as parameters, never inside the SQL text. (Cosmos DB: query text is limited to 512 KB including parameters, so an In list beyond that is narrowed to the range of its values on the server and filtered exactly in the client.) An
In list with 100 values or more is passed as one parameter (a JSON array
in SQLite, an array in Postgres), so In has no length limit. Other
conditions take one parameter per value; a statement with more than SQLite
(32,766) or Postgres (65,535) allow fails with an error saying so. Only a
condition with thousands of separate comparisons gets there: use In
instead. Long In lists over a bytes field need SQLite 3.41 or later
(unhex).
Constraint violations are mapped to ablage.ErrDuplicate and ablage.ErrReference, wrapping the driver error. Delete returns the number of rows the condition matched, not counting rows of other tables removed by a cascade.
ablage · Apache-2.0