Artifact b212dbdafdb8919c68509540de1b9d5888f3260364365474f46bc92fffe0cb7b:
- File
src/sqlar.c
— part of check-in
[dd27c70e9d]
at
2026-11-19 16:10:29
on branch userdb
— User state in ~/.pic/pic.sqlite instead of loose files (ticket 2ad667b78b)
pic's user-level state now lives in one SQLite database, shared by every pic process (WAL, a busy timeout), in tables prefixed picu_: settings, the model catalog cache from pi.dev (stored with the SQLite Archive compression rule; sqlar_compress and sqlar_uncompress are registered), project trust, model scopes and the workspaces pic has used. ~/.pic/settings.json and ~/.pic/models-store.json are imported on first open in one transaction and then deleted. pic settings shows and changes settings. Tests and benchmarks now run with a private HOME and an offline catalog, so they no longer read or write the real ~/.pic. (user: dmestas, size: 2567) [annotate] [blame] [check-ins using] [more...]
/* The SQLite Archive's compression functions (https://sqlite.org/sqlar.html), * as SQLite's ext/misc/sqlar.c defines them, over the vendored zlib: * sqlar_compress(X) X deflated (zlib format) when that is smaller, * else X unchanged * sqlar_uncompress(Y, SZ) Y inflated to SZ bytes, or Y itself when SZ is * its length (stored raw) * A row keeps sz = the original length beside the data, so sz = length(data) * marks raw content. */ #include <limits.h> #include <stdlib.h> #include "sqlar.h" #include "zlib.h" static void sqlar_compress(sqlite3_context *ctx, int argc, sqlite3_value **argv) { const Bytef *in; int n; uLongf out_n; Bytef *out; (void)argc; if (sqlite3_value_type(argv[0]) != SQLITE_BLOB && sqlite3_value_type(argv[0]) != SQLITE_TEXT) { sqlite3_result_value(ctx, argv[0]); return; } in = sqlite3_value_blob(argv[0]); n = sqlite3_value_bytes(argv[0]); out_n = compressBound((uLong)n); if (!(out = sqlite3_malloc64(out_n))) { sqlite3_result_error_nomem(ctx); return; } if (compress(out, &out_n, in, (uLong)n) == Z_OK && out_n < (uLongf)n) { sqlite3_result_blob(ctx, out, (int)out_n, sqlite3_free); } else { sqlite3_free(out); sqlite3_result_value(ctx, argv[0]); } } static void sqlar_uncompress(sqlite3_context *ctx, int argc, sqlite3_value **argv) { const Bytef *in = sqlite3_value_blob(argv[0]); int n = sqlite3_value_bytes(argv[0]); sqlite3_int64 sz = sqlite3_value_int64(argv[1]); uLongf out_n; Bytef *out; (void)argc; if (sz == n || sz < 0 || sz > INT_MAX) { sqlite3_result_value(ctx, argv[0]); return; } out_n = (uLongf)sz; if (!(out = sqlite3_malloc64((sqlite3_uint64)sz + 1))) { sqlite3_result_error_nomem(ctx); return; } if (uncompress(out, &out_n, in, (uLong)n) != Z_OK || out_n != (uLongf)sz) { sqlite3_free(out); sqlite3_result_error(ctx, "sqlar_uncompress: corrupt data", -1); return; } sqlite3_result_blob(ctx, out, (int)out_n, sqlite3_free); } int pic_sqlar_register(sqlite3 *db) { int flags = SQLITE_UTF8 | SQLITE_DETERMINISTIC | SQLITE_INNOCUOUS; return sqlite3_create_function(db, "sqlar_compress", 1, flags, NULL, sqlar_compress, NULL, NULL) == SQLITE_OK && sqlite3_create_function(db, "sqlar_uncompress", 2, flags, NULL, sqlar_uncompress, NULL, NULL) == SQLITE_OK ? 0 : -1; }