@yaks/sqlite 0.0.0-stage → 0.2.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (289) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +1596 -2
  3. package/_dist/archetype.d.ts +75 -0
  4. package/_dist/archetype.d.ts.map +1 -0
  5. package/_dist/branches.d.ts +23 -0
  6. package/_dist/branches.d.ts.map +1 -0
  7. package/_dist/budget.d.ts +52 -0
  8. package/_dist/budget.d.ts.map +1 -0
  9. package/_dist/bundle.d.ts +2 -0
  10. package/_dist/bundle.d.ts.map +1 -0
  11. package/_dist/cache.d.ts +24 -0
  12. package/_dist/cache.d.ts.map +1 -0
  13. package/_dist/catalog.d.ts +9 -0
  14. package/_dist/catalog.d.ts.map +1 -0
  15. package/_dist/check.d.ts +4 -0
  16. package/_dist/check.d.ts.map +1 -0
  17. package/_dist/db.d.ts +36 -0
  18. package/_dist/db.d.ts.map +1 -0
  19. package/_dist/ddl.d.ts +143 -0
  20. package/_dist/ddl.d.ts.map +1 -0
  21. package/_dist/doom.d.ts +22 -0
  22. package/_dist/doom.d.ts.map +1 -0
  23. package/_dist/follow.d.ts +74 -0
  24. package/_dist/follow.d.ts.map +1 -0
  25. package/_dist/history-shapes.d.ts +3 -0
  26. package/_dist/history-shapes.d.ts.map +1 -0
  27. package/_dist/history.d.ts +18 -0
  28. package/_dist/history.d.ts.map +1 -0
  29. package/_dist/inspect.d.ts +43 -0
  30. package/_dist/inspect.d.ts.map +1 -0
  31. package/_dist/jsonb.d.ts +17 -0
  32. package/_dist/jsonb.d.ts.map +1 -0
  33. package/_dist/layout.d.ts +7 -0
  34. package/_dist/layout.d.ts.map +1 -0
  35. package/_dist/ledger.d.ts +41 -0
  36. package/_dist/ledger.d.ts.map +1 -0
  37. package/_dist/meta.d.ts +89 -0
  38. package/_dist/meta.d.ts.map +1 -0
  39. package/_dist/migration.d.ts +41 -0
  40. package/_dist/migration.d.ts.map +1 -0
  41. package/_dist/mod.d.ts +201 -0
  42. package/_dist/mod.d.ts.map +1 -0
  43. package/_dist/mode.d.ts +14 -0
  44. package/_dist/mode.d.ts.map +1 -0
  45. package/_dist/overlay.d.ts +30 -0
  46. package/_dist/overlay.d.ts.map +1 -0
  47. package/_dist/owe.d.ts +180 -0
  48. package/_dist/owe.d.ts.map +1 -0
  49. package/_dist/physical.d.ts +89 -0
  50. package/_dist/physical.d.ts.map +1 -0
  51. package/_dist/point.d.ts +10 -0
  52. package/_dist/point.d.ts.map +1 -0
  53. package/_dist/raise.d.ts +22 -0
  54. package/_dist/raise.d.ts.map +1 -0
  55. package/_dist/read-db.d.ts +9 -0
  56. package/_dist/read-db.d.ts.map +1 -0
  57. package/_dist/read-store.d.ts +10 -0
  58. package/_dist/read-store.d.ts.map +1 -0
  59. package/_dist/read.d.ts +59 -0
  60. package/_dist/read.d.ts.map +1 -0
  61. package/_dist/reader.d.ts +11 -0
  62. package/_dist/reader.d.ts.map +1 -0
  63. package/_dist/rules.d.ts +19 -0
  64. package/_dist/rules.d.ts.map +1 -0
  65. package/_dist/service.d.ts +12 -0
  66. package/_dist/service.d.ts.map +1 -0
  67. package/_dist/shape.d.ts +12 -0
  68. package/_dist/shape.d.ts.map +1 -0
  69. package/_dist/statements.d.ts +4 -0
  70. package/_dist/statements.d.ts.map +1 -0
  71. package/_dist/survey.d.ts +31 -0
  72. package/_dist/survey.d.ts.map +1 -0
  73. package/_dist/tools.d.ts +15 -0
  74. package/_dist/tools.d.ts.map +1 -0
  75. package/_dist/trash.d.ts +18 -0
  76. package/_dist/trash.d.ts.map +1 -0
  77. package/_dist/unit.d.ts +11 -0
  78. package/_dist/unit.d.ts.map +1 -0
  79. package/_dist/vocab.d.ts +6 -0
  80. package/_dist/vocab.d.ts.map +1 -0
  81. package/_dist/write.d.ts +169 -0
  82. package/_dist/write.d.ts.map +1 -0
  83. package/answer.js +110 -0
  84. package/answer.js.map +1 -0
  85. package/answer.ts +122 -0
  86. package/archetype.js +730 -0
  87. package/archetype.js.map +1 -0
  88. package/archetype.ts +790 -0
  89. package/binary_cases.ts +240 -0
  90. package/branches.js +195 -0
  91. package/branches.js.map +1 -0
  92. package/branches.ts +257 -0
  93. package/budget.js +89 -0
  94. package/budget.js.map +1 -0
  95. package/budget.ts +117 -0
  96. package/builtin.js +34 -0
  97. package/builtin.js.map +1 -0
  98. package/builtin.ts +31 -0
  99. package/bundle.js +21 -0
  100. package/bundle.js.map +1 -0
  101. package/bundle.ts +22 -0
  102. package/buried.js +95 -0
  103. package/buried.js.map +1 -0
  104. package/buried.ts +124 -0
  105. package/cache.js +153 -0
  106. package/cache.js.map +1 -0
  107. package/cache.ts +154 -0
  108. package/cache_worker.ts +16 -0
  109. package/catalog.js +173 -0
  110. package/catalog.js.map +1 -0
  111. package/catalog.ts +224 -0
  112. package/check.js +80 -0
  113. package/check.js.map +1 -0
  114. package/check.ts +92 -0
  115. package/checkpoint.js +98 -0
  116. package/checkpoint.js.map +1 -0
  117. package/checkpoint.ts +129 -0
  118. package/checkpoint_worker.ts +37 -0
  119. package/db.js +170 -0
  120. package/db.js.map +1 -0
  121. package/db.ts +173 -0
  122. package/ddl.js +1441 -0
  123. package/ddl.js.map +1 -0
  124. package/ddl.ts +1508 -0
  125. package/deno.json +1 -0
  126. package/doom.js +59 -0
  127. package/doom.js.map +1 -0
  128. package/doom.ts +75 -0
  129. package/driver.js +69 -0
  130. package/driver.js.map +1 -0
  131. package/driver.ts +101 -0
  132. package/error.js +30 -0
  133. package/error.js.map +1 -0
  134. package/error.ts +46 -0
  135. package/ffi/LICENSE +202 -0
  136. package/ffi/blob.js +99 -0
  137. package/ffi/blob.js.map +1 -0
  138. package/ffi/blob.ts +156 -0
  139. package/ffi/constants.js +65 -0
  140. package/ffi/constants.js.map +1 -0
  141. package/ffi/constants.ts +67 -0
  142. package/ffi/database.js +613 -0
  143. package/ffi/database.js.map +1 -0
  144. package/ffi/database.ts +931 -0
  145. package/ffi/ffi.js +535 -0
  146. package/ffi/ffi.js.map +1 -0
  147. package/ffi/ffi.ts +604 -0
  148. package/ffi/mod.js +15 -0
  149. package/ffi/mod.js.map +1 -0
  150. package/ffi/mod.ts +28 -0
  151. package/ffi/statement.js +555 -0
  152. package/ffi/statement.js.map +1 -0
  153. package/ffi/statement.ts +788 -0
  154. package/ffi/util.js +32 -0
  155. package/ffi/util.js.map +1 -0
  156. package/ffi/util.ts +48 -0
  157. package/follow.js +342 -0
  158. package/follow.js.map +1 -0
  159. package/follow.ts +401 -0
  160. package/history-branch.js +166 -0
  161. package/history-branch.js.map +1 -0
  162. package/history-branch.ts +140 -0
  163. package/history-shapes.js +254 -0
  164. package/history-shapes.js.map +1 -0
  165. package/history-shapes.ts +268 -0
  166. package/history.js +620 -0
  167. package/history.js.map +1 -0
  168. package/history.ts +674 -0
  169. package/inspect.js +120 -0
  170. package/inspect.js.map +1 -0
  171. package/inspect.ts +164 -0
  172. package/jsonb.js +73 -0
  173. package/jsonb.js.map +1 -0
  174. package/jsonb.ts +108 -0
  175. package/layout.js +22 -0
  176. package/layout.js.map +1 -0
  177. package/layout.ts +26 -0
  178. package/ledger.js +210 -0
  179. package/ledger.js.map +1 -0
  180. package/ledger.ts +210 -0
  181. package/legacy.js +4 -0
  182. package/legacy.js.map +1 -0
  183. package/legacy.ts +3 -0
  184. package/maintenance.js +148 -0
  185. package/maintenance.js.map +1 -0
  186. package/maintenance.ts +154 -0
  187. package/measure.js +133 -0
  188. package/measure.js.map +1 -0
  189. package/measure.ts +167 -0
  190. package/measure_worker.ts +18 -0
  191. package/memo.js +1250 -0
  192. package/memo.js.map +1 -0
  193. package/memo.ts +1480 -0
  194. package/meta.js +265 -0
  195. package/meta.js.map +1 -0
  196. package/meta.ts +321 -0
  197. package/migration.js +272 -0
  198. package/migration.js.map +1 -0
  199. package/migration.ts +286 -0
  200. package/mod.js +1103 -0
  201. package/mod.js.map +1 -0
  202. package/mod.ts +1762 -0
  203. package/mode.js +192 -0
  204. package/mode.js.map +1 -0
  205. package/mode.ts +207 -0
  206. package/native.js +544 -0
  207. package/native.js.map +1 -0
  208. package/native.ts +544 -0
  209. package/overlay.js +408 -0
  210. package/overlay.js.map +1 -0
  211. package/overlay.ts +485 -0
  212. package/owe.js +719 -0
  213. package/owe.js.map +1 -0
  214. package/owe.ts +873 -0
  215. package/package.json +99 -4
  216. package/physical.js +350 -0
  217. package/physical.js.map +1 -0
  218. package/physical.ts +434 -0
  219. package/plan.js +78 -0
  220. package/plan.js.map +1 -0
  221. package/plan.ts +104 -0
  222. package/point.js +81 -0
  223. package/point.js.map +1 -0
  224. package/point.ts +52 -0
  225. package/population.js +639 -0
  226. package/population.js.map +1 -0
  227. package/population.ts +630 -0
  228. package/purge.js +117 -0
  229. package/purge.js.map +1 -0
  230. package/purge.ts +127 -0
  231. package/raise.js +270 -0
  232. package/raise.js.map +1 -0
  233. package/raise.ts +316 -0
  234. package/read-db.js +381 -0
  235. package/read-db.js.map +1 -0
  236. package/read-db.ts +282 -0
  237. package/read-store.js +74 -0
  238. package/read-store.js.map +1 -0
  239. package/read-store.ts +86 -0
  240. package/read.js +1100 -0
  241. package/read.js.map +1 -0
  242. package/read.ts +1441 -0
  243. package/reader.js +141 -0
  244. package/reader.js.map +1 -0
  245. package/reader.ts +159 -0
  246. package/rules.js +88 -0
  247. package/rules.js.map +1 -0
  248. package/rules.ts +113 -0
  249. package/service.js +27 -0
  250. package/service.js.map +1 -0
  251. package/service.ts +41 -0
  252. package/shape.js +76 -0
  253. package/shape.js.map +1 -0
  254. package/shape.ts +108 -0
  255. package/sparse_presence_fixture.ts +45 -0
  256. package/sqlitepath.js +24 -0
  257. package/sqlitepath.js.map +1 -0
  258. package/sqlitepath.ts +29 -0
  259. package/statements.js +78 -0
  260. package/statements.js.map +1 -0
  261. package/statements.ts +96 -0
  262. package/survey.js +135 -0
  263. package/survey.js.map +1 -0
  264. package/survey.ts +196 -0
  265. package/tools.js +17 -0
  266. package/tools.js.map +1 -0
  267. package/tools.ts +50 -0
  268. package/trash.js +144 -0
  269. package/trash.js.map +1 -0
  270. package/trash.ts +187 -0
  271. package/unit.js +215 -0
  272. package/unit.js.map +1 -0
  273. package/unit.ts +211 -0
  274. package/versions.js +115 -0
  275. package/versions.js.map +1 -0
  276. package/versions.ts +110 -0
  277. package/vocab.js +19 -0
  278. package/vocab.js.map +1 -0
  279. package/vocab.json +27 -0
  280. package/vocab.ts +19 -0
  281. package/watch.js +33 -0
  282. package/watch.js.map +1 -0
  283. package/watch.ts +42 -0
  284. package/write.js +1164 -0
  285. package/write.js.map +1 -0
  286. package/write.ts +1356 -0
  287. package/written.js +200 -0
  288. package/written.js.map +1 -0
  289. package/written.ts +202 -0
@@ -0,0 +1,140 @@
1
+ // A branch's writes touch its version rows, leaving main's rows alone.
2
+ import {
3
+ among,
4
+ and,
5
+ col,
6
+ eq,
7
+ exists,
8
+ type Expr,
9
+ lit,
10
+ type Query,
11
+ select,
12
+ type Stmt,
13
+ table,
14
+ val,
15
+ } from '@yaks/sql'
16
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
17
+ import { type Kept, stored } from './history.js'
18
+ import { beside, BRANCH, FROM, TO } from './history-shapes.js'
19
+ type W = Extract<Stmt, { t: 'insert' | 'update' | 'delete' }>
20
+ type Insert = Extract<W, { t: 'insert' }>
21
+
22
+ // An insert's query with more columns after its own.
23
+ let appended = (q: Query, es: Expr[]): Query => {
24
+ if (q.t != 'select') throw new Error('a versioned insert from a compound')
25
+ return { ...q, cols: [...q.cols ?? [], ...es] }
26
+ }
27
+
28
+ // The entities an insert names: its rows' owners, or its query's.
29
+ let entities = (w: Insert): Expr[] | Query => {
30
+ let at = w.cols?.indexOf('entity') ?? -1
31
+ if (at < 0) {
32
+ throw new Error(`a versioned insert into ${w.into} names no owner`)
33
+ }
34
+ if (w.rows) return w.rows.map((r) => r[at])
35
+ if (w.q?.t != 'select') throw new Error('a versioned insert from a compound')
36
+ return { ...w.q, with: w.q.with ?? w.with, cols: [w.q.cols![at]] }
37
+ }
38
+
39
+ // ---- writes on a branch ----
40
+
41
+ // The branch's open row of the entity a write's condition names.
42
+ let open = (branch: number) =>
43
+ and(eq(col(BRANCH), val(branch)), eq(col(TO), lit(0)))
44
+
45
+ /**
46
+ * A write on branch `b`, as a write to the branch's rows beside. Every read in
47
+ * it names the live table, which on a branch's connection is the branch's view
48
+ * of it (./mode.ts), so it sees the branch.
49
+ *
50
+ * Before any write, each entity's component as the branch sees it is copied
51
+ * into an open row of the branch's own, where it has none: an insert then
52
+ * lands on that row as it would on main's, an update changes it, and a removal
53
+ * closes it, so it goes on hiding main's. A branch keeps no versions of its
54
+ * own: its rows are what it holds now, and what it removed.
55
+ */
56
+ export let branched = (
57
+ vocab: Vocab,
58
+ comp: string,
59
+ w: W,
60
+ b: number,
61
+ tx: number,
62
+ ): Kept => {
63
+ let cols = stored(vocab, comp)
64
+ let to = beside(comp)
65
+ let seed = (where: Expr): W => ({
66
+ t: 'insert',
67
+ or: 'ignore',
68
+ into: to,
69
+ cols: ['entity', ...cols, BRANCH, FROM, TO],
70
+ q: select({
71
+ cols: [
72
+ col('entity'),
73
+ ...cols.map((c) => col(c)),
74
+ val(b),
75
+ val(tx),
76
+ lit(0),
77
+ ],
78
+ from: table(comp),
79
+ where,
80
+ }),
81
+ })
82
+ if (w.t == 'insert') {
83
+ let e = entities(w)
84
+ return {
85
+ before: [seed(among(col('entity'), e))],
86
+ write: {
87
+ ...w,
88
+ into: to,
89
+ cols: [...w.cols!, BRANCH, FROM, TO],
90
+ ...w.rows
91
+ ? { rows: w.rows.map((r) => [...r, val(b), val(tx), lit(0)]) }
92
+ : {},
93
+ ...w.q ? { q: appended(w.q, [val(b), val(tx), lit(0)]) } : {},
94
+ ...w.upsert
95
+ ? {
96
+ upsert: w.upsert.map((u) => ({
97
+ ...u,
98
+ on: [col('entity'), col(BRANCH), col(TO)],
99
+ })),
100
+ }
101
+ : {},
102
+ },
103
+ }
104
+ }
105
+ let where = w.where ?? lit(true)
106
+ let mine = and(where, open(b))
107
+ if (w.t == 'update') {
108
+ return {
109
+ before: [seed(where)],
110
+ write: {
111
+ t: 'update',
112
+ table: to,
113
+ set: { ...w.set, [FROM]: val(tx) },
114
+ where: mine,
115
+ ...w.returning ? { returning: w.returning } : {},
116
+ },
117
+ }
118
+ }
119
+ // A row this transaction both opened and closed already has its closed
120
+ // twin at `tx`; it goes. Any other open row is closed where it stands.
121
+ let twin = exists(select({
122
+ cols: [lit(1)],
123
+ from: table(to, '__twin'),
124
+ where: and(
125
+ eq(col('entity', '__twin'), col('entity', to)),
126
+ eq(col(BRANCH, '__twin'), val(b)),
127
+ eq(col(TO, '__twin'), val(tx)),
128
+ ),
129
+ }))
130
+ return {
131
+ before: [seed(where), { t: 'delete', from: to, where: and(mine, twin) }],
132
+ write: {
133
+ t: 'update',
134
+ table: to,
135
+ set: { [TO]: val(tx) },
136
+ where: mine,
137
+ ...w.returning ? { returning: w.returning } : {},
138
+ },
139
+ }
140
+ }
@@ -0,0 +1,254 @@
1
+ // History's archetypes: an indexed table of the component memberships each
2
+ // version write already knows. A past or branch read names its owners here,
3
+ // rather than asking every component view whether it holds them.
4
+ import { and, as, col, eq, exists, fn, from, iff, isNull, left, lit, not, oneOf, op, or, over, select, table, val } from "@yaks/sql";
5
+ import { tablesOf } from "@yaks/archetype/sets";
6
+ export let FROM = '$from', TO = '$to', BRANCH = '$branch', BESIDE = '$versions';
7
+ export let beside = (comp)=>`${comp}${BESIDE}`;
8
+ export let SHAPES = '$archetype$versions';
9
+ let columns = [
10
+ 'entity',
11
+ 'component',
12
+ BRANCH,
13
+ FROM,
14
+ TO
15
+ ];
16
+ export let shapeDdl = [
17
+ {
18
+ t: 'create table',
19
+ name: SHAPES,
20
+ ifNot: true,
21
+ cols: [
22
+ {
23
+ name: 'entity',
24
+ type: 'integer',
25
+ notNull: true,
26
+ ref: {
27
+ table: 'entity',
28
+ cols: [
29
+ 'id'
30
+ ]
31
+ }
32
+ },
33
+ {
34
+ name: 'component',
35
+ type: 'text',
36
+ notNull: true
37
+ },
38
+ {
39
+ name: BRANCH,
40
+ type: 'integer',
41
+ notNull: true
42
+ },
43
+ {
44
+ name: FROM,
45
+ type: 'integer'
46
+ },
47
+ {
48
+ name: TO,
49
+ type: 'integer',
50
+ notNull: true
51
+ }
52
+ ]
53
+ },
54
+ {
55
+ t: 'create index',
56
+ name: `${SHAPES}.key`,
57
+ on: SHAPES,
58
+ ifNot: true,
59
+ unique: true,
60
+ cols: [
61
+ 'entity',
62
+ 'component',
63
+ BRANCH,
64
+ TO
65
+ ].map((c)=>col(c))
66
+ }
67
+ ];
68
+ // Repair a missing historical index once. Adjacent value versions are one
69
+ // membership span: editing a property's value does not change an archetype.
70
+ let copied = (comp)=>{
71
+ let key = [
72
+ col('entity'),
73
+ col(BRANCH)
74
+ ];
75
+ let order = [
76
+ fn('coalesce', col(FROM), lit(0))
77
+ ];
78
+ let starts = select({
79
+ cols: [
80
+ ...key,
81
+ col(FROM),
82
+ col(TO),
83
+ as(iff(op('is', over(fn('lag', col(TO)), key, order), col(FROM)), lit(0), lit(1)), 'start')
84
+ ],
85
+ from: table(beside(comp))
86
+ });
87
+ let spans = select({
88
+ cols: [
89
+ ...key,
90
+ col(FROM),
91
+ col(TO),
92
+ as(over(fn('sum', col('start')), key, order), 'span')
93
+ ],
94
+ from: from(starts, 'starts')
95
+ });
96
+ return {
97
+ t: 'insert',
98
+ into: SHAPES,
99
+ cols: columns,
100
+ q: select({
101
+ cols: [
102
+ col('entity'),
103
+ lit(comp),
104
+ col(BRANCH),
105
+ iff(eq(fn('count', col(FROM)), fn('count', lit(1))), fn('min', col(FROM)), lit(null)),
106
+ iff(eq(fn('min', col(TO)), lit(0)), lit(0), fn('max', col(TO)))
107
+ ],
108
+ from: from(spans, 'spans'),
109
+ group: [
110
+ ...key,
111
+ col('span')
112
+ ]
113
+ })
114
+ };
115
+ };
116
+ export let shapeSeeds = (comps, made)=>comps.filter((comp)=>made(SHAPES) || made(beside(comp))).map(copied);
117
+ /** A row's membership changed. Property edits keep its open span; only a
118
+ * birth or removal writes the history of its archetype. */ export let shaped = (comp, owners, branch, tx, present)=>{
119
+ if (!owners.length) return [];
120
+ let own = and(oneOf(col('entity'), owners), eq(col('component'), lit(comp)), eq(col(BRANCH), val(branch)));
121
+ if (present) {
122
+ return [
123
+ {
124
+ t: 'insert',
125
+ into: SHAPES,
126
+ cols: columns,
127
+ q: select({
128
+ cols: [
129
+ col('id', 'e'),
130
+ lit(comp),
131
+ val(branch),
132
+ val(tx),
133
+ lit(0)
134
+ ],
135
+ from: table('entity', 'e'),
136
+ where: and(oneOf(col('id', 'e'), owners), not(exists(select({
137
+ cols: [
138
+ lit(1)
139
+ ],
140
+ from: table(SHAPES, 's'),
141
+ where: and(eq(col('entity', 's'), col('id', 'e')), eq(col('component', 's'), lit(comp)), eq(col(BRANCH, 's'), val(branch)), eq(col(TO, 's'), lit(0)))
142
+ }))))
143
+ })
144
+ }
145
+ ];
146
+ }
147
+ return [
148
+ {
149
+ t: 'delete',
150
+ from: SHAPES,
151
+ where: and(own, eq(col(TO), lit(0)), exists(select({
152
+ cols: [
153
+ lit(1)
154
+ ],
155
+ from: table(SHAPES, 'closed'),
156
+ where: and(eq(col('entity', 'closed'), col('entity', SHAPES)), eq(col('component', 'closed'), lit(comp)), eq(col(BRANCH, 'closed'), val(branch)), eq(col(TO, 'closed'), val(tx)))
157
+ })))
158
+ },
159
+ {
160
+ t: 'update',
161
+ table: SHAPES,
162
+ set: {
163
+ [TO]: val(tx)
164
+ },
165
+ where: and(own, eq(col(TO), lit(0)))
166
+ },
167
+ // A first branch removal must shadow the main row even if the branch never
168
+ // opened its own membership span.
169
+ ...branch ? [
170
+ {
171
+ t: 'insert',
172
+ or: 'ignore',
173
+ into: SHAPES,
174
+ cols: columns,
175
+ rows: owners.map((id)=>[
176
+ val(id),
177
+ lit(comp),
178
+ val(branch),
179
+ val(tx),
180
+ val(tx)
181
+ ])
182
+ }
183
+ ] : []
184
+ ];
185
+ };
186
+ /** The table sets for these owners at this time. Branch rows override only
187
+ * the components that branch wrote; unversioned components still follow main. */ export let pastShapes = (driver, vocab, ids, names, mode)=>{
188
+ let found = new Map(ids.map((id)=>[
189
+ id,
190
+ new Set()
191
+ ]));
192
+ if (!ids.length) return new Map();
193
+ let branch = 'branch' in mode ? mode.branch : 0;
194
+ let at = 'at' in mode ? mode.at : mode.fork;
195
+ let historical = and(eq(col(BRANCH), lit(0)), or(isNull(col(FROM)), op('<=', col(FROM), val(at))), or(eq(col(TO), lit(0)), op('>', col(TO), val(at))));
196
+ let rows = driver.query(select({
197
+ cols: columns.map((c)=>col(c)),
198
+ from: table(SHAPES),
199
+ where: and(oneOf(col('entity'), ids), branch ? or(historical, eq(col(BRANCH), val(branch))) : historical)
200
+ }));
201
+ for (let row of rows){
202
+ if (Number(row[BRANCH]) == 0) {
203
+ found.get(Number(row.entity)).add(String(row.component));
204
+ }
205
+ }
206
+ // The only live tables a past read needs are those declared exempt. Their
207
+ // owner's current archetype names them; the component tables are not asked.
208
+ for (let row of driver.query(select({
209
+ cols: [
210
+ col('id', 'e'),
211
+ col('tables', 'a')
212
+ ],
213
+ from: {
214
+ t: 'table',
215
+ name: 'entity',
216
+ schema: 'main',
217
+ as: 'e'
218
+ },
219
+ joins: [
220
+ left({
221
+ t: 'table',
222
+ name: 'archetype',
223
+ schema: 'main',
224
+ as: 'a'
225
+ }, eq(col('entity', 'a'), col('archetype', 'e')))
226
+ ],
227
+ where: oneOf(col('id', 'e'), ids)
228
+ }))){
229
+ for (let name of tablesOf(row.tables ?? '[]', vocab)){
230
+ if (vocab.comp(name)?.journal === false) {
231
+ found.get(Number(row.id)).add(name);
232
+ }
233
+ }
234
+ }
235
+ // Any branch row, closed or open, shadows main's component. Its open row
236
+ // says it holds it now, even if the same transaction closed another row.
237
+ let own = rows.filter((r)=>Number(r[BRANCH]) == branch && branch != 0);
238
+ for (let r of own)found.get(Number(r.entity)).delete(String(r.component));
239
+ for (let r of own){
240
+ if (Number(r[TO]) == 0) {
241
+ found.get(Number(r.entity)).add(String(r.component));
242
+ }
243
+ }
244
+ let allowed = new Set(names);
245
+ return new Map([
246
+ ...found
247
+ ].map(([id, held])=>[
248
+ id,
249
+ [
250
+ ...held
251
+ ].filter((n)=>allowed.has(n))
252
+ ]));
253
+ };
254
+ //# sourceMappingURL=history-shapes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"history-shapes.js","sources":["./history-shapes.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,4EAA4E;AAC5E,iEAAiE;AACjE,SACE,GAAG,EACH,EAAE,EACF,GAAG,EAGH,EAAE,EACF,MAAM,EACN,EAAE,EACF,IAAI,EACJ,GAAG,EAEH,MAAM,EACN,IAAI,EACJ,GAAG,EAEH,GAAG,EACH,KAAK,EACL,EAAE,EACF,EAAE,EACF,IAAI,EACJ,MAAM,EAEN,KAAK,EAEL,GAAG,yBACwB;AAE7B,SAAS,QAAQ,oCAAyC;AAE1D,OAAO,IAAI,OAAO,SAAS,KAAK,OAAO,SAAS,WAAW,SAAS,YAAW;AAC/E,OAAO,IAAI,SAAS,CAAC,OAAyB,GAAG,OAAO,QAAQ,CAAA;AAChE,OAAO,IAAI,SAAS,sBAAqB;AACzC,IAAI,UAAU;EAAC;EAAU;EAAa;EAAQ;EAAM;CAAG;AAEvD,OAAO,IAAI,WAAmB;EAAC;IAC7B,GAAG;IACH,MAAM;IACN,OAAO;IACP,MAAM;MACJ;QACE,MAAM;QACN,MAAM;QACN,SAAS;QACT,KAAK;UAAE,OAAO;UAAU,MAAM;YAAC;WAAK;QAAC;MACvC;MACA;QAAE,MAAM;QAAa,MAAM;QAAQ,SAAS;MAAK;MACjD;QAAE,MAAM;QAAQ,MAAM;QAAW,SAAS;MAAK;MAC/C;QAAE,MAAM;QAAM,MAAM;MAAU;MAC9B;QAAE,MAAM;QAAI,MAAM;QAAW,SAAS;MAAK;KAC5C;EACH;EAAG;IACD,GAAG;IACH,MAAM,GAAG,OAAO,IAAI,CAAC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,QAAQ;QAAQ,IAAI;MAAI,GACzD,GAAG,IAAI,UAAU,MAAM,IAAI,aAAa;KAE3C;IACD,OAAO,MAAM,IAAI,MAAM,MAAM;EAC/B,IACA;IACA,KAAK,IAAI,QAAQ,SAAS,IAAI,MAAM,IAAI,MAAM,OAAQ;MACpD,IAAI,MAAM,IAAI,CAAC,OAAO,YAAY,OAAO;QACvC,MAAM,GAAG,CAAC,OAAO,IAAI,EAAE,GAAI,GAAG,CAAC;MACjC;IACF;EACF;EACA,yEAAyE;EACzE,yEAAyE;EACzE,IAAI,MAAM,KAAK,MAAM,CAAC,CAAC,IAAM,OAAO,CAAC,CAAC,OAAO,KAAK,UAAU,UAAU;EACtE,KAAK,IAAI,KAAK,IAAK,MAAM,GAAG,CAAC,OAAO,EAAE,MAAM,GAAI,MAAM,CAAC,OAAO,EAAE,SAAS;EACzE,KAAK,IAAI,KAAK,IAAK;IACjB,IAAI,OAAO,CAAC,CAAC,GAAG,KAAK,GAAG;MACtB,MAAM,GAAG,CAAC,OAAO,EAAE,MAAM,GAAI,GAAG,CAAC,OAAO,EAAE,SAAS;IACrD;EACF;EACA,IAAI,UAAU,IAAI,IAAI;EACtB,OAAO,IAAI,IACT;OAAI;GAAM,CAAC,GAAG,CAAC,CACb,CAAC,IAAI,KAAK,GACP;MAAC;MAAI;WAAI;OAAK,CAAC,MAAM,CAAC,CAAC,IAAM,QAAQ,GAAG,CAAC;KAAI;AAEtD,EAAC"}
@@ -0,0 +1,268 @@
1
+ // History's archetypes: an indexed table of the component memberships each
2
+ // version write already knows. A past or branch read names its owners here,
3
+ // rather than asking every component view whether it holds them.
4
+ import {
5
+ and,
6
+ as,
7
+ col,
8
+ type Delete,
9
+ type Driver,
10
+ eq,
11
+ exists,
12
+ fn,
13
+ from,
14
+ iff,
15
+ type Insert,
16
+ isNull,
17
+ left,
18
+ lit,
19
+ type Mode,
20
+ not,
21
+ oneOf,
22
+ op,
23
+ or,
24
+ over,
25
+ select,
26
+ type Stmt,
27
+ table,
28
+ type Update,
29
+ val,
30
+ } from '@yaks/sql'
31
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
32
+ import { tablesOf } from '@yaks/archetype/sets'
33
+
34
+ export let FROM = '$from', TO = '$to', BRANCH = '$branch', BESIDE = '$versions'
35
+ export let beside = (comp: string): string => `${comp}${BESIDE}`
36
+ export let SHAPES = '$archetype$versions'
37
+ let columns = ['entity', 'component', BRANCH, FROM, TO]
38
+
39
+ export let shapeDdl: Stmt[] = [{
40
+ t: 'create table',
41
+ name: SHAPES,
42
+ ifNot: true,
43
+ cols: [
44
+ {
45
+ name: 'entity',
46
+ type: 'integer',
47
+ notNull: true,
48
+ ref: { table: 'entity', cols: ['id'] },
49
+ },
50
+ { name: 'component', type: 'text', notNull: true },
51
+ { name: BRANCH, type: 'integer', notNull: true },
52
+ { name: FROM, type: 'integer' },
53
+ { name: TO, type: 'integer', notNull: true },
54
+ ],
55
+ }, {
56
+ t: 'create index',
57
+ name: `${SHAPES}.key`,
58
+ on: SHAPES,
59
+ ifNot: true,
60
+ unique: true,
61
+ cols: ['entity', 'component', BRANCH, TO].map((c) => col(c)),
62
+ }]
63
+
64
+ // Repair a missing historical index once. Adjacent value versions are one
65
+ // membership span: editing a property's value does not change an archetype.
66
+ let copied = (comp: string): Insert => {
67
+ let key = [col('entity'), col(BRANCH)]
68
+ let order = [fn('coalesce', col(FROM), lit(0))]
69
+ let starts = select({
70
+ cols: [
71
+ ...key,
72
+ col(FROM),
73
+ col(TO),
74
+ as(
75
+ iff(
76
+ op('is', over(fn('lag', col(TO)), key, order), col(FROM)),
77
+ lit(0),
78
+ lit(1),
79
+ ),
80
+ 'start',
81
+ ),
82
+ ],
83
+ from: table(beside(comp)),
84
+ })
85
+ let spans = select({
86
+ cols: [
87
+ ...key,
88
+ col(FROM),
89
+ col(TO),
90
+ as(over(fn('sum', col('start')), key, order), 'span'),
91
+ ],
92
+ from: from(starts, 'starts'),
93
+ })
94
+ return {
95
+ t: 'insert',
96
+ into: SHAPES,
97
+ cols: columns,
98
+ q: select({
99
+ cols: [
100
+ col('entity'),
101
+ lit(comp),
102
+ col(BRANCH),
103
+ iff(
104
+ eq(fn('count', col(FROM)), fn('count', lit(1))),
105
+ fn('min', col(FROM)),
106
+ lit(null),
107
+ ),
108
+ iff(eq(fn('min', col(TO)), lit(0)), lit(0), fn('max', col(TO))),
109
+ ],
110
+ from: from(spans, 'spans'),
111
+ group: [...key, col('span')],
112
+ }),
113
+ }
114
+ }
115
+ export let shapeSeeds = (
116
+ comps: string[],
117
+ made: (name: string) => boolean,
118
+ ): Stmt[] =>
119
+ comps.filter((comp) => made(SHAPES) || made(beside(comp))).map(copied)
120
+
121
+ /** A row's membership changed. Property edits keep its open span; only a
122
+ * birth or removal writes the history of its archetype. */
123
+ export let shaped = (
124
+ comp: string,
125
+ owners: number[],
126
+ branch: number,
127
+ tx: number,
128
+ present: boolean,
129
+ ): (Insert | Update | Delete)[] => {
130
+ if (!owners.length) return []
131
+ let own = and(
132
+ oneOf(col('entity'), owners),
133
+ eq(col('component'), lit(comp)),
134
+ eq(col(BRANCH), val(branch)),
135
+ )
136
+ if (present) {
137
+ return [{
138
+ t: 'insert',
139
+ into: SHAPES,
140
+ cols: columns,
141
+ q: select({
142
+ cols: [col('id', 'e'), lit(comp), val(branch), val(tx), lit(0)],
143
+ from: table('entity', 'e'),
144
+ where: and(
145
+ oneOf(col('id', 'e'), owners),
146
+ not(exists(select({
147
+ cols: [lit(1)],
148
+ from: table(SHAPES, 's'),
149
+ where: and(
150
+ eq(col('entity', 's'), col('id', 'e')),
151
+ eq(col('component', 's'), lit(comp)),
152
+ eq(col(BRANCH, 's'), val(branch)),
153
+ eq(col(TO, 's'), lit(0)),
154
+ ),
155
+ }))),
156
+ ),
157
+ }),
158
+ }]
159
+ }
160
+ return [
161
+ {
162
+ t: 'delete',
163
+ from: SHAPES,
164
+ where: and(
165
+ own,
166
+ eq(col(TO), lit(0)),
167
+ exists(select({
168
+ cols: [lit(1)],
169
+ from: table(SHAPES, 'closed'),
170
+ where: and(
171
+ eq(col('entity', 'closed'), col('entity', SHAPES)),
172
+ eq(col('component', 'closed'), lit(comp)),
173
+ eq(col(BRANCH, 'closed'), val(branch)),
174
+ eq(col(TO, 'closed'), val(tx)),
175
+ ),
176
+ })),
177
+ ),
178
+ },
179
+ {
180
+ t: 'update',
181
+ table: SHAPES,
182
+ set: { [TO]: val(tx) },
183
+ where: and(own, eq(col(TO), lit(0))),
184
+ },
185
+ // A first branch removal must shadow the main row even if the branch never
186
+ // opened its own membership span.
187
+ ...branch
188
+ ? [{
189
+ t: 'insert' as const,
190
+ or: 'ignore' as const,
191
+ into: SHAPES,
192
+ cols: columns,
193
+ rows: owners.map((
194
+ id,
195
+ ) => [val(id), lit(comp), val(branch), val(tx), val(tx)]),
196
+ }]
197
+ : [],
198
+ ]
199
+ }
200
+
201
+ /** The table sets for these owners at this time. Branch rows override only
202
+ * the components that branch wrote; unversioned components still follow main. */
203
+ export let pastShapes = (
204
+ driver: Driver,
205
+ vocab: Vocab,
206
+ ids: number[],
207
+ names: readonly string[],
208
+ mode: Mode,
209
+ ): Map<number, string[]> => {
210
+ let found = new Map(ids.map((id) => [id, new Set<string>()]))
211
+ if (!ids.length) return new Map()
212
+ let branch = 'branch' in mode ? mode.branch : 0
213
+ let at = 'at' in mode ? mode.at : mode.fork
214
+ let historical = and(
215
+ eq(col(BRANCH), lit(0)),
216
+ or(isNull(col(FROM)), op('<=', col(FROM), val(at))),
217
+ or(eq(col(TO), lit(0)), op('>', col(TO), val(at))),
218
+ )
219
+ let rows = driver.query(select({
220
+ cols: columns.map((c) => col(c)),
221
+ from: table(SHAPES),
222
+ where: and(
223
+ oneOf(col('entity'), ids),
224
+ branch ? or(historical, eq(col(BRANCH), val(branch))) : historical,
225
+ ),
226
+ }))
227
+ for (let row of rows) {
228
+ if (Number(row[BRANCH]) == 0) {
229
+ found.get(Number(row.entity))!.add(String(row.component))
230
+ }
231
+ }
232
+ // The only live tables a past read needs are those declared exempt. Their
233
+ // owner's current archetype names them; the component tables are not asked.
234
+ for (
235
+ let row of driver.query(select({
236
+ cols: [col('id', 'e'), col('tables', 'a')],
237
+ from: { t: 'table', name: 'entity', schema: 'main', as: 'e' },
238
+ joins: [
239
+ left(
240
+ { t: 'table', name: 'archetype', schema: 'main', as: 'a' },
241
+ eq(col('entity', 'a'), col('archetype', 'e')),
242
+ ),
243
+ ],
244
+ where: oneOf(col('id', 'e'), ids),
245
+ }))
246
+ ) {
247
+ for (let name of tablesOf(row.tables ?? '[]', vocab)) {
248
+ if (vocab.comp(name)?.journal === false) {
249
+ found.get(Number(row.id))!.add(name)
250
+ }
251
+ }
252
+ }
253
+ // Any branch row, closed or open, shadows main's component. Its open row
254
+ // says it holds it now, even if the same transaction closed another row.
255
+ let own = rows.filter((r) => Number(r[BRANCH]) == branch && branch != 0)
256
+ for (let r of own) found.get(Number(r.entity))!.delete(String(r.component))
257
+ for (let r of own) {
258
+ if (Number(r[TO]) == 0) {
259
+ found.get(Number(r.entity))!.add(String(r.component))
260
+ }
261
+ }
262
+ let allowed = new Set(names)
263
+ return new Map(
264
+ [...found].map((
265
+ [id, held],
266
+ ) => [id, [...held].filter((n) => allowed.has(n))]),
267
+ )
268
+ }