Add SQLite + JSON1 backend for V_gamma document storage#122
Merged
Conversation
Implements step 3 of docs/v2/PLAN.md §9 — the JSON1-fallback SQLite backend that lets +did2 round-trip V_gamma documents end-to-end. - src/did/+did2/+database/sqlitedb.m: V_gamma SQLite backend. Creates the documents / superclasses / depends_on tables from PLAN.md §3.1 (plus a meta key/value table that the constructor uses to reject non-v2 files). Foreign-key cascades clear the sidecar rows on remove. add/get/remove/has/count/allIds and search/searchIds; Validate=false skips schema validation for bulk loads. - src/did/+did2/+database/compileQuery.m: did2.query -> SQL WHERE clause + bound parameters, JSON1 fallback only. json_extract for scalar leaves, EXISTS over chained json_each for [*] paths, the superclasses / depends_on sidecar tables for isa / depends_on, NULL-guarded negation so missing paths flip to true under ~. regexp and multi-element exact_number emit a permissive 1=1; sqlitedb.search runs the in-memory evaluator over the SQL result set as a correctness backstop. - tests/+did2/+unittest/testCompileQuery.m: string-based unit tests over the compiler output (no mksqlite required). - tests/+did2/+unittest/testSqliteDb.m: integration tests that round-trip documents through a real SQLite file. Filters itself out via assumeFail when mksqlite is not on the path. - Updates Contents.m and the progress log in docs/v2/PLAN.md.
Lets compileQuery(did2.query('x','regexp','y')) return '1=1' verbatim
(rather than '(1=1)'), which is what testCompileQuery's regexp test
asserts. Multi-element conjunctions still get the '(a) AND (b)' wrap.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements step 3 of the v2 architecture plan: a SQLite-backed storage engine with JSON1 query compilation. This adds the first concrete database backend for V_gamma documents, complementing the in-memory reference evaluator with a persistent, queryable store.
Key Changes
did2.database.sqlitedb— New storage backend class that:mksqlitewith a v2-specific schemadocuments(full JSON body + metadata),superclasses(class hierarchy forisaqueries), anddepends_on(dependency tracking)add(),remove(),get(),has(),count(),allIds()search(query)andsearchIds(query)with SQL pre-filtering + in-memory post-filteringValidate=falsefor bulk loads (unsafe_insert escape hatch)did2.database.compileQuery— SQL compiler that translatesdid2.queryobjects to SQLiteWHEREclauses:exact_string,contains_string, numeric comparisons) →json_extract()expressions[*]paths) →EXISTSsubqueries with nestedjson_each()joinsisaoperator → indexed lookup onsuperclassessidecar tabledepends_onoperator → indexed lookup ondepends_onsidecar table with wildcard supporthasfield→json_type()checks (treats JSONnullas present)hasmemberand array-of-structures predicates →json_each()expansion1=1) for operators SQLite cannot express natively (regexp, multi-element arrays)Test coverage:
testCompileQuery.m— 20+ unit tests validating SQL output without requiring mksqlitetestSqliteDb.m— 25+ integration tests covering round-trip persistence, schema validation, CRUD, and query semantics across scalar/array/sidecar operatorsImplementation Details
1=1forregexp) and relies on the in-memory evaluator (did2.query.matches) as a correctness backstop, ensuring the SQL layer is only required to be a pre-filter.metatable for version tracking; the constructor validates that opened files are v2 databases.[*]array-iteration segments; nested arrays compose as cross-products ofjson_eachjoins.mksqliteis not available.https://claude.ai/code/session_01JE5mYZsiyVBJYw55836w8h