@agentshouse/mdruntime 0.1.0-c31fdf9e58db7 → 0.1.0-c33f874b1f932

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 (319) hide show
  1. package/README.md +21 -19
  2. package/dist/acceptance.d.ts +3 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +299 -0
  5. package/dist/act-lanes.d.ts +30 -0
  6. package/dist/act-lanes.d.ts.map +1 -0
  7. package/dist/act-lanes.js +225 -0
  8. package/dist/actions.d.ts +26 -0
  9. package/dist/actions.d.ts.map +1 -0
  10. package/dist/actions.js +302 -0
  11. package/dist/adapter.d.ts +89 -79
  12. package/dist/adapter.d.ts.map +1 -1
  13. package/dist/attached-model.d.ts +3 -11
  14. package/dist/attached-model.d.ts.map +1 -1
  15. package/dist/attached-model.js +20 -60
  16. package/dist/bootstrap.d.ts +9 -5
  17. package/dist/bootstrap.d.ts.map +1 -1
  18. package/dist/bootstrap.js +204 -85
  19. package/dist/cell.d.ts +6 -0
  20. package/dist/cell.d.ts.map +1 -0
  21. package/dist/cell.js +9 -0
  22. package/dist/chain.d.ts +2 -1
  23. package/dist/chain.d.ts.map +1 -1
  24. package/dist/change-set.d.ts +4 -0
  25. package/dist/change-set.d.ts.map +1 -1
  26. package/dist/change-set.js +6 -0
  27. package/dist/change.d.ts +1 -0
  28. package/dist/change.d.ts.map +1 -1
  29. package/dist/change.js +4 -2
  30. package/dist/chat-types.d.ts +3 -0
  31. package/dist/chat-types.d.ts.map +1 -0
  32. package/dist/chat-types.js +11 -0
  33. package/dist/client.d.ts +30 -22
  34. package/dist/client.d.ts.map +1 -1
  35. package/dist/client.js +37 -20
  36. package/dist/collection.d.ts +26 -4
  37. package/dist/collection.d.ts.map +1 -1
  38. package/dist/collection.js +19 -2
  39. package/dist/compile.d.ts +8 -6
  40. package/dist/compile.d.ts.map +1 -1
  41. package/dist/compile.js +255 -318
  42. package/dist/connection.d.ts +5 -141
  43. package/dist/connection.d.ts.map +1 -1
  44. package/dist/connection.js +111 -5061
  45. package/dist/context.d.ts +2 -0
  46. package/dist/context.d.ts.map +1 -0
  47. package/dist/context.js +7 -0
  48. package/dist/conversation-live.d.ts +150 -0
  49. package/dist/conversation-live.d.ts.map +1 -0
  50. package/dist/conversation-live.js +259 -0
  51. package/dist/conversation-row.d.ts +35 -0
  52. package/dist/conversation-row.d.ts.map +1 -0
  53. package/dist/conversation-row.js +55 -0
  54. package/dist/conversation.d.ts +61 -45
  55. package/dist/conversation.d.ts.map +1 -1
  56. package/dist/conversation.js +999 -910
  57. package/dist/declaration.d.ts +10 -0
  58. package/dist/declaration.d.ts.map +1 -0
  59. package/dist/declaration.js +24 -0
  60. package/dist/discovery.d.ts +10 -0
  61. package/dist/discovery.d.ts.map +1 -0
  62. package/dist/discovery.js +150 -0
  63. package/dist/durability.d.ts +19 -0
  64. package/dist/durability.d.ts.map +1 -1
  65. package/dist/durability.js +4 -0
  66. package/dist/entry.d.ts +2 -2
  67. package/dist/entry.d.ts.map +1 -1
  68. package/dist/entry.js +18 -8
  69. package/dist/expectation.d.ts +26 -13
  70. package/dist/expectation.d.ts.map +1 -1
  71. package/dist/expectation.js +72 -19
  72. package/dist/failure.d.ts +3 -0
  73. package/dist/failure.d.ts.map +1 -1
  74. package/dist/failure.js +6 -0
  75. package/dist/figures.d.ts +49 -0
  76. package/dist/figures.d.ts.map +1 -0
  77. package/dist/figures.js +199 -0
  78. package/dist/file.d.ts.map +1 -1
  79. package/dist/file.js +2 -1
  80. package/dist/holdings.d.ts +29 -0
  81. package/dist/holdings.d.ts.map +1 -0
  82. package/dist/holdings.js +95 -0
  83. package/dist/host.d.ts +21 -6
  84. package/dist/host.d.ts.map +1 -1
  85. package/dist/host.js +77 -36
  86. package/dist/identity.d.ts +0 -1
  87. package/dist/identity.d.ts.map +1 -1
  88. package/dist/identity.js +0 -3
  89. package/dist/index.d.ts +34 -12
  90. package/dist/index.d.ts.map +1 -1
  91. package/dist/index.js +16 -5
  92. package/dist/install.d.ts +60 -0
  93. package/dist/install.d.ts.map +1 -0
  94. package/dist/install.js +537 -0
  95. package/dist/lifecycle.d.ts +7 -0
  96. package/dist/lifecycle.d.ts.map +1 -0
  97. package/dist/lifecycle.js +278 -0
  98. package/dist/listing.d.ts +52 -0
  99. package/dist/listing.d.ts.map +1 -0
  100. package/dist/listing.js +152 -0
  101. package/dist/live.d.ts +9 -6
  102. package/dist/live.d.ts.map +1 -1
  103. package/dist/live.js +118 -35
  104. package/dist/loaded.d.ts +5 -0
  105. package/dist/loaded.d.ts.map +1 -0
  106. package/dist/loaded.js +40 -0
  107. package/dist/locks.d.ts +7 -0
  108. package/dist/locks.d.ts.map +1 -0
  109. package/dist/locks.js +32 -0
  110. package/dist/lost.d.ts +5 -0
  111. package/dist/lost.d.ts.map +1 -0
  112. package/dist/lost.js +49 -0
  113. package/dist/media.d.ts +6 -1
  114. package/dist/media.d.ts.map +1 -1
  115. package/dist/media.js +68 -17
  116. package/dist/memory.d.ts +11 -3
  117. package/dist/memory.d.ts.map +1 -1
  118. package/dist/memory.js +102 -32
  119. package/dist/messages.d.ts +35 -5
  120. package/dist/messages.d.ts.map +1 -1
  121. package/dist/messages.js +108 -36
  122. package/dist/model.d.ts +2 -10
  123. package/dist/model.d.ts.map +1 -1
  124. package/dist/model.js +0 -14
  125. package/dist/models.d.ts +10 -0
  126. package/dist/models.d.ts.map +1 -0
  127. package/dist/models.js +153 -0
  128. package/dist/observation.d.ts +28 -13
  129. package/dist/observation.d.ts.map +1 -1
  130. package/dist/observation.js +185 -81
  131. package/dist/opened.d.ts +1 -1
  132. package/dist/opened.d.ts.map +1 -1
  133. package/dist/opened.js +5 -5
  134. package/dist/opening.d.ts +4 -0
  135. package/dist/opening.d.ts.map +1 -0
  136. package/dist/opening.js +53 -0
  137. package/dist/outcome.d.ts +11 -1
  138. package/dist/outcome.d.ts.map +1 -1
  139. package/dist/outcome.js +26 -2
  140. package/dist/overlay.d.ts +6 -1
  141. package/dist/overlay.d.ts.map +1 -1
  142. package/dist/overlay.js +47 -21
  143. package/dist/page-metadata.d.ts +8 -0
  144. package/dist/page-metadata.d.ts.map +1 -0
  145. package/dist/page-metadata.js +19 -0
  146. package/dist/parts.d.ts +4 -0
  147. package/dist/parts.d.ts.map +1 -0
  148. package/dist/parts.js +13 -0
  149. package/dist/paths.d.ts +1 -0
  150. package/dist/paths.d.ts.map +1 -1
  151. package/dist/paths.js +22 -0
  152. package/dist/preparation.d.ts +41 -0
  153. package/dist/preparation.d.ts.map +1 -0
  154. package/dist/preparation.js +314 -0
  155. package/dist/presence.d.ts +58 -0
  156. package/dist/presence.d.ts.map +1 -0
  157. package/dist/presence.js +231 -0
  158. package/dist/queries.d.ts +16 -0
  159. package/dist/queries.d.ts.map +1 -0
  160. package/dist/queries.js +308 -0
  161. package/dist/query.d.ts +13 -209
  162. package/dist/query.d.ts.map +1 -1
  163. package/dist/query.js +3 -505
  164. package/dist/questions.d.ts +29 -0
  165. package/dist/questions.d.ts.map +1 -0
  166. package/dist/questions.js +115 -0
  167. package/dist/queue.d.ts +40 -0
  168. package/dist/queue.d.ts.map +1 -0
  169. package/dist/queue.js +450 -0
  170. package/dist/reach.d.ts +15 -14
  171. package/dist/reach.d.ts.map +1 -1
  172. package/dist/reach.js +233 -125
  173. package/dist/react.d.ts +17 -3
  174. package/dist/react.d.ts.map +1 -1
  175. package/dist/react.js +33 -1
  176. package/dist/read.d.ts +7 -0
  177. package/dist/read.d.ts.map +1 -0
  178. package/dist/read.js +17 -0
  179. package/dist/readiness.d.ts +9 -6
  180. package/dist/readiness.d.ts.map +1 -1
  181. package/dist/readiness.js +3 -5
  182. package/dist/reads.d.ts +39 -0
  183. package/dist/reads.d.ts.map +1 -0
  184. package/dist/reads.js +178 -0
  185. package/dist/recovery.d.ts +3 -1
  186. package/dist/recovery.d.ts.map +1 -1
  187. package/dist/recovery.js +38 -8
  188. package/dist/refresh.d.ts +1 -1
  189. package/dist/refresh.d.ts.map +1 -1
  190. package/dist/refresh.js +9 -2
  191. package/dist/refusal.d.ts +5 -0
  192. package/dist/refusal.d.ts.map +1 -0
  193. package/dist/refusal.js +55 -0
  194. package/dist/replica.d.ts +11 -16
  195. package/dist/replica.d.ts.map +1 -1
  196. package/dist/replica.js +64 -30
  197. package/dist/retried.d.ts +30 -0
  198. package/dist/retried.d.ts.map +1 -0
  199. package/dist/retried.js +72 -0
  200. package/dist/room-draft.d.ts +7 -8
  201. package/dist/room-draft.d.ts.map +1 -1
  202. package/dist/room-draft.js +113 -111
  203. package/dist/room-drafts.d.ts +24 -0
  204. package/dist/room-drafts.d.ts.map +1 -0
  205. package/dist/room-drafts.js +217 -0
  206. package/dist/rows.d.ts +16 -22
  207. package/dist/rows.d.ts.map +1 -1
  208. package/dist/rows.js +49 -186
  209. package/dist/runtime.d.ts +34 -0
  210. package/dist/runtime.d.ts.map +1 -0
  211. package/dist/runtime.js +1 -0
  212. package/dist/save.d.ts +42 -86
  213. package/dist/save.d.ts.map +1 -1
  214. package/dist/save.js +344 -174
  215. package/dist/saves.d.ts +32 -0
  216. package/dist/saves.d.ts.map +1 -0
  217. package/dist/saves.js +409 -0
  218. package/dist/search.d.ts +12 -63
  219. package/dist/search.d.ts.map +1 -1
  220. package/dist/search.js +9 -52
  221. package/dist/seed.d.ts +11 -2
  222. package/dist/seed.d.ts.map +1 -1
  223. package/dist/seed.js +71 -1
  224. package/dist/shared.d.ts +1 -6
  225. package/dist/shared.d.ts.map +1 -1
  226. package/dist/shared.js +13 -95
  227. package/dist/shown.d.ts +33 -0
  228. package/dist/shown.d.ts.map +1 -0
  229. package/dist/shown.js +309 -0
  230. package/dist/socket/admission.d.ts +7 -0
  231. package/dist/socket/admission.d.ts.map +1 -0
  232. package/dist/socket/admission.js +39 -0
  233. package/dist/socket/answers.d.ts +6 -0
  234. package/dist/socket/answers.d.ts.map +1 -0
  235. package/dist/socket/answers.js +20 -0
  236. package/dist/socket/lanes.d.ts +28 -0
  237. package/dist/socket/lanes.d.ts.map +1 -0
  238. package/dist/socket/lanes.js +103 -0
  239. package/dist/socket/reads.d.ts +10 -0
  240. package/dist/socket/reads.d.ts.map +1 -0
  241. package/dist/socket/reads.js +31 -0
  242. package/dist/socket/session.d.ts +38 -0
  243. package/dist/socket/session.d.ts.map +1 -0
  244. package/dist/socket/session.js +23 -0
  245. package/dist/socket.d.ts +67 -0
  246. package/dist/socket.d.ts.map +1 -0
  247. package/dist/socket.js +164 -0
  248. package/dist/source.d.ts +54 -0
  249. package/dist/source.d.ts.map +1 -0
  250. package/dist/source.js +82 -0
  251. package/dist/status.d.ts +0 -2
  252. package/dist/status.d.ts.map +1 -1
  253. package/dist/status.js +0 -6
  254. package/dist/store/installs.d.ts +30 -0
  255. package/dist/store/installs.d.ts.map +1 -0
  256. package/dist/store/installs.js +40 -0
  257. package/dist/store/readiness.d.ts +11 -0
  258. package/dist/store/readiness.d.ts.map +1 -0
  259. package/dist/store/readiness.js +68 -0
  260. package/dist/store/records.d.ts +217 -0
  261. package/dist/store/records.d.ts.map +1 -0
  262. package/dist/store/records.js +464 -0
  263. package/dist/store/room-held.d.ts +8 -0
  264. package/dist/store/room-held.d.ts.map +1 -0
  265. package/dist/store/room-held.js +17 -0
  266. package/dist/store/rooms-shown.d.ts +8 -0
  267. package/dist/store/rooms-shown.d.ts.map +1 -0
  268. package/dist/store/rooms-shown.js +13 -0
  269. package/dist/store/space.d.ts +11 -0
  270. package/dist/store/space.d.ts.map +1 -0
  271. package/dist/store/space.js +41 -0
  272. package/dist/store/watchers.d.ts +17 -0
  273. package/dist/store/watchers.d.ts.map +1 -0
  274. package/dist/store/watchers.js +56 -0
  275. package/dist/store/writes.d.ts +24 -0
  276. package/dist/store/writes.d.ts.map +1 -0
  277. package/dist/store/writes.js +125 -0
  278. package/dist/store.d.ts +113 -145
  279. package/dist/store.d.ts.map +1 -1
  280. package/dist/store.js +171 -365
  281. package/dist/stored.d.ts +72 -35
  282. package/dist/stored.d.ts.map +1 -1
  283. package/dist/stored.js +340 -197
  284. package/dist/surface.d.ts +89 -0
  285. package/dist/surface.d.ts.map +1 -0
  286. package/dist/surface.js +222 -0
  287. package/dist/sync.d.ts +19 -0
  288. package/dist/sync.d.ts.map +1 -0
  289. package/dist/sync.js +535 -0
  290. package/dist/tables.d.ts +124 -30
  291. package/dist/tables.d.ts.map +1 -1
  292. package/dist/tables.js +242 -62
  293. package/dist/tallies.d.ts +18 -0
  294. package/dist/tallies.d.ts.map +1 -0
  295. package/dist/tallies.js +128 -0
  296. package/dist/validation.d.ts.map +1 -1
  297. package/dist/validation.js +9 -12
  298. package/dist/windows.d.ts +1 -12
  299. package/dist/windows.d.ts.map +1 -1
  300. package/dist/windows.js +2 -75
  301. package/dist/wire.d.ts +60 -7
  302. package/dist/wire.d.ts.map +1 -1
  303. package/dist/wire.js +21 -4
  304. package/package.json +3 -2
  305. package/dist/attempt.d.ts +0 -16
  306. package/dist/attempt.d.ts.map +0 -1
  307. package/dist/attempt.js +0 -70
  308. package/dist/fence.d.ts +0 -6
  309. package/dist/fence.d.ts.map +0 -1
  310. package/dist/fence.js +0 -10
  311. package/dist/fulltext.d.ts +0 -27
  312. package/dist/fulltext.d.ts.map +0 -1
  313. package/dist/fulltext.js +0 -370
  314. package/dist/log.d.ts +0 -5
  315. package/dist/log.d.ts.map +0 -1
  316. package/dist/log.js +0 -14
  317. package/dist/words.d.ts +0 -19
  318. package/dist/words.d.ts.map +0 -1
  319. package/dist/words.js +0 -156
package/dist/compile.js CHANGED
@@ -1,127 +1,14 @@
1
- import { ARRAY_INDEX, parse, pointerSegments } from '@agentshouse/mdmodel';
1
+ import { ARRAY_INDEX, bodied, byCodePoint, compared, cursorOf, heldKind, parse, pointerSegments, sql, } from '@agentshouse/mdmodel';
2
2
  import { binaryRecord } from './binary.js';
3
- import { documentKey, kindOf, pathOfKey, placedOf, roomOfKey } from './rows.js';
4
- import { byCodePoint, compared, cursorOf, FROM_START, MAXIMUM_WINDOW, } from './windows.js';
5
- const VISIBLE = (alias) => `${alias}.visible = 1 AND ${alias}.present = 1 AND ${alias}.queried = 1`;
6
- function fragment(sql, bind = []) {
7
- return { sql, bind };
8
- }
9
- function joined(parts, separator) {
10
- return {
11
- sql: parts.map((part) => part.sql).join(separator),
12
- bind: parts.flatMap((part) => part.bind),
13
- };
14
- }
15
- function balanced(parts, separator) {
16
- if (parts.length === 1)
17
- return grouped(parts[0]);
18
- const half = Math.ceil(parts.length / 2);
19
- return grouped(joined([balanced(parts.slice(0, half), separator), balanced(parts.slice(half), separator)], separator));
20
- }
21
- function all(parts) {
22
- return parts.length === 0 ? fragment('1') : balanced(parts, ' AND ');
23
- }
24
- function any(parts) {
25
- return parts.length === 0 ? fragment('0') : balanced(parts, ' OR ');
26
- }
27
- function grouped(part) {
28
- return { sql: `(${part.sql})`, bind: part.bind };
29
- }
30
- function list(values) {
31
- return fragment(`(${values.map(() => '?').join(', ')})`, values);
32
- }
33
- function sqlValue(value) {
34
- if (value === null)
35
- return { kind: 'null', value: null };
36
- if (typeof value === 'boolean')
37
- return { kind: 'boolean', value: value ? 1 : 0 };
38
- if (typeof value === 'number')
39
- return { kind: 'number', value };
40
- return { kind: 'string', value };
41
- }
42
- function same(alias, value) {
43
- const held = sqlValue(value);
44
- return held.kind === 'null'
45
- ? fragment(`${alias}.kind = 'null'`)
46
- : fragment(`${alias}.kind = ? AND ${alias}.value = ?`, [held.kind, held.value]);
47
- }
48
- const BOUNDS = { gt: '>', gte: '>=', lt: '<', lte: '<=' };
49
- function pointed(alias, field, reach) {
50
- const { keys, places, glob } = placedOf(field, reach);
51
- return fragment(`${alias}.keys = ? AND ${alias}.places ${glob ? 'GLOB' : '='} ?`, [keys, places]);
52
- }
53
- function heldValues(doc, field, alias, where) {
54
- return joined([
55
- fragment(`SELECT 1 FROM replica_values ${alias} WHERE ${alias}.doc = ${doc}.id AND `),
56
- grouped(pointed(alias, field, 'value')),
57
- fragment(' AND '),
58
- grouped(where),
59
- ], '');
60
- }
61
- function exists(part) {
62
- return { sql: `EXISTS (${part.sql})`, bind: part.bind };
63
- }
64
- function itemOf(held, where) {
65
- return exists(joined([
66
- fragment(`SELECT 1 FROM replica_values i WHERE i.doc = ${held}.doc AND i.keys = ${held}.keys AND i.places = ${held}.places AND i.item >= 0 AND `),
67
- grouped(where),
68
- ], ''));
69
- }
70
- function matching(held, values) {
71
- return any(values.map((value) => any([
72
- same(held, value),
73
- all([fragment(`${held}.kind = 'array'`), itemOf(held, same('i', value))]),
74
- ])));
75
- }
76
- function tested(doc, field, test) {
77
- const reached = exists(heldValues(doc, field, 'r', fragment('r.item = -1')));
78
- if (test.op === 'exists')
79
- return test.exists ? reached : fragment(`NOT ${reached.sql}`, reached.bind);
80
- if (test.op === 'eq' || test.op === 'in') {
81
- const values = test.op === 'eq' ? [test.value] : test.values;
82
- return exists(heldValues(doc, field, 'v', any(values.map((value) => same('v', value)))));
83
- }
84
- if (test.op === 'ne' || test.op === 'nin') {
85
- const values = test.op === 'ne' ? [test.value] : test.values;
86
- const unmatched = matching('h', values);
87
- return any([
88
- fragment(`NOT ${reached.sql}`, reached.bind),
89
- exists(heldValues(doc, field, 'h', all([fragment('h.item = -1'), fragment(`NOT (${unmatched.sql})`, unmatched.bind)]))),
90
- ]);
91
- }
92
- if (test.op === 'range') {
93
- return exists(heldValues(doc, field, 'v', all([
94
- fragment('v.item = -1'),
95
- ...test.bounds.map(([operator, bound]) => {
96
- const held = sqlValue(bound);
97
- return fragment(`v.kind = ? AND v.value ${BOUNDS[operator]} ?`, [
98
- held.kind,
99
- held.value,
100
- ]);
101
- }),
102
- ])));
103
- }
104
- if (test.op === 'all') {
105
- return exists(heldValues(doc, field, 'h', all([
106
- fragment("h.item = -1 AND h.kind = 'array'"),
107
- ...test.values.map((value) => itemOf('h', same('i', value))),
108
- ])));
109
- }
110
- return exists(heldValues(doc, field, 'v', fragment("v.item = -1 AND v.kind = 'array' AND v.size = ?", [test.size])));
111
- }
112
- function conditioned(doc, condition) {
113
- if ('every' in condition)
114
- return all(condition.every.map((one) => conditioned(doc, one)));
115
- if ('some' in condition)
116
- return any(condition.some.map((one) => conditioned(doc, one)));
117
- return tested(doc, condition.field, condition.test);
118
- }
119
- function located(bound, alias) {
120
- if (bound.rooms.length === 0)
121
- return fragment("''");
122
- return fragment(`CASE ${alias}.key ${bound.rooms.map(() => 'WHEN ? THEN ?').join(' ')} END`, bound.rooms.flatMap(({ key, locator }) => [key, locator]));
123
- }
124
- function within(alias, path) {
3
+ import { definitionsAt, UNTYPED } from './collection.js';
4
+ import { documentKey, pathOfKey, roomOfKey } from './rows.js';
5
+ import { FROM_START, MAXIMUM_WINDOW } from './windows.js';
6
+ const { all, any, exists, fragment, grouped, joined, listed } = sql;
7
+ export const VISIBLE = (alias) => `${alias}.visible = 1 AND ${alias}.present = 1`;
8
+ function located(alias) {
9
+ return fragment(`(SELECT k.locator FROM replica_keys k WHERE k.key = ${alias}.key)`);
10
+ }
11
+ export function within(alias, path) {
125
12
  return fragment(`(${alias}.path = ? OR (${alias}.path >= ? AND ${alias}.path < ?))`, [
126
13
  path,
127
14
  `${path}/`,
@@ -164,7 +51,7 @@ function reached(bound, alias) {
164
51
  }));
165
52
  return all([
166
53
  any([
167
- ...(rooms.length === 0 ? [] : [fragment(`${alias}.key IN ${list(rooms).sql}`, rooms)]),
54
+ ...(rooms.length === 0 ? [] : [fragment(`${alias}.key IN ${listed(rooms).sql}`, rooms)]),
168
55
  ...(documents.length === 0
169
56
  ? []
170
57
  : [
@@ -174,40 +61,22 @@ function reached(bound, alias) {
174
61
  ...(reach.areas.length === 0 ? [] : [fragment(`NOT ${grouped(outside).sql}`, outside.bind)]),
175
62
  ]);
176
63
  }
177
- function unindexed(column) {
178
- return `+${column}`;
179
- }
180
- function scoped(alias, scope, byPaths = false) {
181
- const type = byPaths ? unindexed(`${alias}.type`) : `${alias}.type`;
182
- return all([
183
- ...(scope.types === null ? [] : [fragment(`${type} IN ${list(scope.types).sql}`, scope.types)]),
184
- ...(scope.pathPrefix === null || scope.pathPrefix === ''
185
- ? []
186
- : [
187
- fragment(`${alias}.path >= ? AND ${alias}.path < ?`, [
188
- `${scope.pathPrefix}/`,
189
- `${scope.pathPrefix}0`,
190
- ]),
191
- ]),
192
- ...(scope.where === null ? [] : [conditioned(alias, scope.where)]),
193
- ]);
194
- }
195
64
  function related(bound, plan) {
196
65
  const parts = [];
197
66
  if (plan.hop !== null) {
198
67
  parts.push(exists(joined([
199
68
  fragment(`SELECT 1 FROM replica_links l JOIN replica_entries m ON m.key = d.key AND m.path = l.target AND ${VISIBLE('m')} WHERE l.doc = d.id AND l.kind = 'field' AND`),
200
- grouped(pointed('l', plan.hop.field, 'link')),
69
+ grouped(sql.placed('l', plan.hop.field, 'link')),
201
70
  fragment('AND'),
202
- grouped(all([scoped('m', plan.hop), reached(bound, 'm')])),
71
+ grouped(all([sql.scoped('m', plan.hop), reached(bound, 'm')])),
203
72
  ], ' ')));
204
73
  }
205
74
  if (plan.backlinks !== null) {
206
75
  const field = plan.backlinks.field;
207
76
  parts.push(exists(joined([
208
77
  fragment(`SELECT 1 FROM replica_links l JOIN replica_entries s ON s.id = l.doc AND ${VISIBLE('s')} WHERE l.key = d.key AND l.target = d.path AND l.kind = 'field' AND`),
209
- ...(field === null ? [] : [grouped(pointed('l', field, 'link')), fragment('AND')]),
210
- grouped(all([scoped('s', plan.backlinks), reached(bound, 's')])),
78
+ ...(field === null ? [] : [grouped(sql.placed('l', field, 'link')), fragment('AND')]),
79
+ grouped(all([sql.scoped('s', plan.backlinks), reached(bound, 's')])),
211
80
  ], ' ')));
212
81
  }
213
82
  return parts;
@@ -215,11 +84,9 @@ function related(bound, plan) {
215
84
  function filtered(bound, plan) {
216
85
  const keys = bound.rooms.map(({ key }) => key);
217
86
  return all([
218
- fragment(`d.key IN ${list(keys).sql} AND ${VISIBLE('d')}`, keys),
219
- ...(plan.paths === null ? [] : [fragment(`d.path IN ${list(plan.paths).sql}`, plan.paths)]),
220
- ...(plan.folder === null ? [] : [fragment('d.folder = ?', [plan.folder])]),
87
+ fragment(`d.key IN ${listed(keys).sql} AND ${VISIBLE('d')}`, keys),
221
88
  reached(bound, 'd'),
222
- scoped('d', plan.root, plan.paths !== null),
89
+ sql.tests('d', plan),
223
90
  ...related(bound, plan),
224
91
  ]);
225
92
  }
@@ -250,108 +117,65 @@ const GROUP_TIES = {
250
117
  locale: undefined,
251
118
  localeOptions: undefined,
252
119
  };
253
- function classOf(value, ordering) {
254
- if (value === null || value === undefined) {
255
- return { placement: ordering.nulls === 'first' ? 0 : 2, klass: 0, scalar: null };
256
- }
257
- if (typeof value === 'boolean')
258
- return { placement: 1, klass: 1, scalar: value ? 1 : 0 };
259
- if (typeof value === 'number')
260
- return { placement: 1, klass: 1, scalar: value };
261
- if (typeof value === 'string')
262
- return { placement: 1, klass: 2, scalar: value };
263
- return { placement: 1, klass: 3, scalar: JSON.stringify(value) };
264
- }
265
- function placement(term) {
266
- const nulls = term.ordering.nulls === 'first' ? 0 : 2;
267
- return fragment(`CASE WHEN ${term.kind.sql} IS NULL OR ${term.kind.sql} = 'null' THEN ${nulls} ELSE 1 END`, [...term.kind.bind, ...term.kind.bind]);
268
- }
269
- function klass(term) {
270
- return fragment(`CASE ${term.kind.sql} WHEN 'boolean' THEN 1 WHEN 'number' THEN 1 WHEN 'string' THEN 2 WHEN 'array' THEN 3 ELSE 4 END`, term.kind.bind);
271
- }
272
- function classed(term, which) {
273
- const kinds = which === 1 ? "('boolean', 'number')" : which === 2 ? "('string')" : "('array')";
274
- return fragment(`(CASE WHEN ${term.kind.sql} IN ${kinds} THEN ${term.value.sql} END)`, [
275
- ...term.kind.bind,
276
- ...term.value.bind,
277
- ]);
278
- }
279
- function collated(bound, term, which) {
280
- if (which === 1)
281
- return '';
282
- const name = bound.collate(term.ordering, which === 3, term.total === true);
283
- return name === null ? '' : ` COLLATE ${name}`;
284
- }
285
- function sorted(bound, term) {
286
- const direction = term.ordering.direction === 'desc' ? 'DESC' : 'ASC';
287
- return joined([
288
- placement(term),
289
- fragment(`${klass(term).sql} ${direction}`, klass(term).bind),
290
- ...[1, 2, 3].map((which) => {
291
- const value = classed(term, which);
292
- return fragment(`${value.sql}${collated(bound, term, which)} ${direction}`, value.bind);
293
- }),
294
- ], ', ');
295
- }
296
- function beyond(bound, term, value) {
297
- const held = classOf(value, term.ordering);
298
- const place = placement(term);
299
- if (held.placement !== 1) {
300
- return {
301
- beyond: fragment(`${place.sql} > ${held.placement}`, place.bind),
302
- equal: fragment(`${place.sql} = ${held.placement}`, place.bind),
303
- };
304
- }
305
- const after = term.ordering.direction === 'desc' ? '<' : '>';
306
- const kind = klass(term);
307
- const scalar = classed(term, held.klass);
308
- const collation = collated(bound, term, held.klass);
309
- return {
310
- beyond: any([
311
- fragment(`${place.sql} > 1`, place.bind),
312
- all([
313
- fragment(`${place.sql} = 1`, place.bind),
314
- any([
315
- fragment(`${kind.sql} ${after} ${held.klass}`, kind.bind),
316
- all([
317
- fragment(`${kind.sql} = ${held.klass}`, kind.bind),
318
- fragment(`${scalar.sql} ${after} ?${collation}`, [...scalar.bind, held.scalar]),
319
- ]),
320
- ]),
321
- ]),
322
- ]),
323
- equal: all([
324
- fragment(`${place.sql} = 1 AND ${kind.sql} = ${held.klass}`, [...place.bind, ...kind.bind]),
325
- fragment(`${scalar.sql} = ?${collation}`, [...scalar.bind, held.scalar]),
326
- ]),
327
- };
328
- }
329
- function following(parts, tie) {
330
- let condition = tie;
331
- for (const part of [...parts].reverse()) {
332
- condition = any([part.beyond, all([part.equal, condition])]);
333
- }
334
- return condition;
120
+ const GREATEST = { ...GROUP_ORDER, direction: 'desc', nulls: 'last' };
121
+ const TIMES = {
122
+ created: { value: 'd.created', instant: 'd.created_instant' },
123
+ updated: { value: "(d.change ->> '$.time')", instant: 'd.changed_instant' },
124
+ };
125
+ function subfolderOf(column, prefix) {
126
+ const root = prefix ?? '';
127
+ const base = root === '' ? '' : `${root}/`;
128
+ const rest = `substr(${column}, length(?) + 1)`;
129
+ return fragment(`(CASE WHEN ${column} = ? THEN NULL WHEN instr(${rest}, '/') = 0 THEN ${column} ELSE ? || substr(${rest}, 1, instr(${rest}, '/') - 1) END)`, [root, base, base, base, base]);
335
130
  }
336
- function keyTerm(bound, key, ordering, index) {
131
+ function keyTerm(key, ordering, index, prefix) {
337
132
  if (key.of === 'field') {
338
133
  const alias = `o${index}`;
339
134
  return {
340
- term: { ordering, kind: fragment(`${alias}.kind`), value: fragment(`${alias}.value`) },
135
+ term: {
136
+ ordering,
137
+ kind: fragment(`${alias}.kind`),
138
+ value: fragment(`${alias}.value`),
139
+ instant: fragment(`${alias}.instant`),
140
+ },
341
141
  join: joined([
342
142
  fragment(`LEFT JOIN replica_values ${alias} ON ${alias}.doc = d.id AND `),
343
- grouped(pointed(alias, key.field, 'exact')),
143
+ grouped(sql.placed(alias, key.field, 'exact')),
344
144
  fragment(` AND ${alias}.item = -1 AND ${alias}.kind NOT IN ('object', 'undefined')`),
345
145
  ], ''),
346
146
  };
347
147
  }
148
+ if (key.of === 'size') {
149
+ return {
150
+ term: {
151
+ ordering,
152
+ kind: fragment("(CASE WHEN d.bytes IS NULL THEN 'null' ELSE 'number' END)"),
153
+ value: fragment('d.bytes'),
154
+ },
155
+ join: null,
156
+ };
157
+ }
158
+ if (key.of === 'created' || key.of === 'updated') {
159
+ const { value, instant } = TIMES[key.of];
160
+ return {
161
+ term: {
162
+ ordering,
163
+ kind: fragment(`(CASE WHEN ${value} IS NULL THEN 'null' ELSE 'string' END)`),
164
+ value: fragment(value),
165
+ instant: fragment(instant),
166
+ },
167
+ join: null,
168
+ };
169
+ }
348
170
  const expression = key.of === 'room'
349
- ? located(bound, 'd')
171
+ ? located('d')
350
172
  : key.of === 'path'
351
173
  ? fragment('d.path')
352
174
  : key.of === 'type'
353
175
  ? fragment('d.type')
354
- : fragment('d.folder');
176
+ : key.of === 'subfolder'
177
+ ? subfolderOf('d.folder', prefix)
178
+ : fragment('d.folder');
355
179
  return {
356
180
  term: {
357
181
  ordering,
@@ -372,12 +196,20 @@ function decoded(kind, value) {
372
196
  return JSON.parse(value);
373
197
  return null;
374
198
  }
199
+ function sortedOf(kind, value, instant) {
200
+ return sql.sortValue(kind, decoded(kind, value), instant);
201
+ }
202
+ function sortedValue(row, index) {
203
+ return sortedOf(row[`k${index}`], row[`v${index}`], row[`i${index}`]);
204
+ }
375
205
  function decodedKey(key, row, index) {
376
- if (key.of === 'field') {
377
- const kind = row[`k${index}`];
378
- return kind === null || kind === undefined ? undefined : decoded(kind, row[`v${index}`]);
379
- }
380
- return row[`v${index}`] ?? null;
206
+ const kind = row[`k${index}`];
207
+ if (key.of === 'field' && (kind === null || kind === undefined))
208
+ return undefined;
209
+ return sortedValue(row, index);
210
+ }
211
+ function instantOf(term) {
212
+ return term.instant ?? fragment('NULL');
381
213
  }
382
214
  const registered = new WeakMap();
383
215
  function hashed(text) {
@@ -428,9 +260,6 @@ function collating(connection) {
428
260
  return name;
429
261
  };
430
262
  }
431
- function bodied(shape) {
432
- return shape.sections !== null || shape.preamble || shape.markdown;
433
- }
434
263
  function masked(pointers) {
435
264
  const root = { whole: false, members: new Map() };
436
265
  for (const pointer of pointers) {
@@ -482,11 +311,20 @@ function sectionsOf(sections, names) {
482
311
  return sections;
483
312
  return Object.fromEntries(names.flatMap((name) => (Object.hasOwn(sections, name) ? [[name, sections[name]]] : [])));
484
313
  }
485
- function documentOf(shape, room, row, linked) {
314
+ function freeParts(room, path, type, content) {
315
+ const definitions = room.composition === undefined ? undefined : definitionsAt(room.composition, path);
316
+ const definition = definitions?.(type === UNTYPED ? undefined : type);
317
+ if (definition === undefined && type !== UNTYPED)
318
+ return null;
319
+ const read = parse(content, path, definition);
320
+ return read.ok ? read.document : null;
321
+ }
322
+ function documentOf(shape, queried, row, linked) {
323
+ const room = queried.locator;
486
324
  const path = row.path;
487
325
  const type = row.type;
488
- const fileId = row.file_id ?? null;
489
- const revision = row.revision ?? null;
326
+ const fileId = row.file_id;
327
+ const revision = row.revision;
490
328
  if (type === null) {
491
329
  const binary = row.binary === null ? null : JSON.parse(row.binary);
492
330
  const content = row.content ?? null;
@@ -499,12 +337,15 @@ function documentOf(shape, room, row, linked) {
499
337
  binaryFile: binaryRecord(path, binary, revision ?? undefined),
500
338
  }),
501
339
  ...(shape.markdown && content !== null ? { markdown: content } : {}),
340
+ ...(shape.change ? { change: changeOf(row) } : {}),
502
341
  ...(shape.fileId ? { fileId } : {}),
503
342
  ...(shape.revision ? { revision } : {}),
343
+ ...heldFacts(shape, row),
504
344
  };
505
345
  }
506
- const read = bodied(shape) ? parse(row.content, path) : null;
507
- const body = read?.ok === true ? read.document : null;
346
+ const body = shape.sections !== null || shape.preamble
347
+ ? freeParts(queried, path, type, row.content)
348
+ : null;
508
349
  const fields = JSON.parse(row.fields ?? '{}');
509
350
  return {
510
351
  room,
@@ -523,13 +364,19 @@ function documentOf(shape, room, row, linked) {
523
364
  ...(shape.preamble && body !== null ? { preamble: body.preamble } : {}),
524
365
  ...(shape.markdown ? { markdown: row.content } : {}),
525
366
  ...(shape.backlinks ? { backlinks: linked.get(documentKey(room, path)) ?? [] } : {}),
526
- ...(shape.change
527
- ? {
528
- change: row.change === null ? null : JSON.parse(row.change),
529
- }
530
- : {}),
367
+ ...(shape.change ? { change: changeOf(row) } : {}),
531
368
  ...(shape.fileId ? { fileId } : {}),
532
369
  ...(shape.revision ? { revision } : {}),
370
+ ...heldFacts(shape, row),
371
+ };
372
+ }
373
+ function changeOf(row) {
374
+ return row.change === null ? null : JSON.parse(row.change);
375
+ }
376
+ function heldFacts(shape, row) {
377
+ return {
378
+ ...(shape.size ? { size: row.bytes === null ? null : Number(row.bytes) } : {}),
379
+ ...(shape.created ? { created: row.created } : {}),
533
380
  };
534
381
  }
535
382
  function backlinksOf(connection, bound, rows) {
@@ -541,8 +388,8 @@ function backlinksOf(connection, bound, rows) {
541
388
  held.push(row.path);
542
389
  byKey.set(key, held);
543
390
  }
391
+ const reach = reached(bound, 's');
544
392
  for (const [key, targets] of byKey) {
545
- const reach = reached(bound, 's');
546
393
  const room = bound.rooms.find((one) => one.key === key).locator;
547
394
  const found = connection.rows(`SELECT target, path, type, field FROM (SELECT l.target, s.path, s.type, l.field, ROW_NUMBER() OVER (PARTITION BY l.target ORDER BY s.path, l.field) AS rank FROM (SELECT DISTINCT doc, target, field FROM replica_links WHERE key = ? AND kind = 'field' AND target IN (SELECT value FROM json_each(?))) l JOIN replica_entries s ON s.id = l.doc AND ${VISIBLE('s')} WHERE ${reach.sql}) WHERE rank <= ? ORDER BY target, path, field`, [key, JSON.stringify(targets), ...reach.bind, MAXIMUM_WINDOW]);
548
395
  for (const row of found) {
@@ -555,38 +402,36 @@ function backlinksOf(connection, bound, rows) {
555
402
  return linked;
556
403
  }
557
404
  function documents(connection, bound, plan, shape, budget) {
558
- const terms = shape.orderBy.map(({ key, options }, index) => keyTerm(bound, key, orderingOf(options), index));
405
+ const terms = shape.orderBy.map(({ key, options }, index) => keyTerm(key, orderingOf(options), index, plan.root.pathPrefix));
559
406
  const filter = filtered(bound, plan);
560
407
  const cursor = plan.after?.values ?? null;
561
- const room = located(bound, 'd');
562
- const tie = cursor === null
408
+ const room = located('d');
409
+ const after = cursor === null
563
410
  ? null
564
411
  : (() => {
565
412
  const key = cursor[shape.orderBy.length];
566
- return any([
413
+ return sql.following(terms.map(({ term }, index) => sql.beyond(bound.collate, term, cursor[index])), any([
567
414
  fragment(`${room.sql} > ?`, [...room.bind, roomOfKey(key)]),
568
415
  fragment(`${room.sql} = ? AND d.path > ?`, [
569
416
  ...room.bind,
570
417
  roomOfKey(key),
571
418
  pathOfKey(key),
572
419
  ]),
573
- ]);
420
+ ]));
574
421
  })();
575
- const after = cursor === null || tie === null
576
- ? null
577
- : following(terms.map(({ term }, index) => beyond(bound, term, cursor[index])), tie);
578
422
  const selected = joined([
579
- fragment('SELECT d.id, d.key, d.path, d.type, d.binary, d.revision, d.fields, d.change, d.file_id'),
423
+ fragment('SELECT d.id, d.key, d.path, d.type, d.binary, d.revision, d.fields, d.change, d.file_id, d.bytes, d.created'),
580
424
  ...terms.map(({ term }, index) => joined([
581
425
  fragment(`, ${term.kind.sql} AS k${index}`, term.kind.bind),
582
426
  fragment(`, ${term.value.sql} AS v${index}`, term.value.bind),
427
+ fragment(`, ${instantOf(term).sql} AS i${index}`, instantOf(term).bind),
583
428
  ], '')),
584
429
  fragment(' FROM replica_entries d'),
585
430
  ...terms.flatMap(({ join }) => (join === null ? [] : [fragment(' '), join])),
586
431
  fragment(' WHERE '),
587
432
  after === null ? filter : all([filter, after]),
588
433
  fragment(' ORDER BY '),
589
- joined([...terms.map(({ term }) => sorted(bound, term)), room, fragment('d.path')], ', '),
434
+ joined([...terms.map(({ term }) => sql.sorted(bound.collate, term)), room, fragment('d.path')], ', '),
590
435
  ...(plan.limit === null ? [] : [fragment(' LIMIT ?', [plan.limit + 1])]),
591
436
  ], '');
592
437
  const rows = connection.rows(selected.sql, selected.bind);
@@ -594,8 +439,9 @@ function documents(connection, bound, plan, shape, budget) {
594
439
  const kept = bodied(shape) ? withContent(connection, windowed, budget) : windowed;
595
440
  const cut = kept.length;
596
441
  const roomOf = new Map(bound.rooms.map(({ key, locator }) => [key, locator]));
442
+ const queried = new Map(bound.rooms.map((one) => [one.key, one]));
597
443
  const linked = shape.backlinks ? backlinksOf(connection, bound, kept) : new Map();
598
- const found = kept.map((row) => documentOf(shape, roomOf.get(Number(row.key)), row, linked));
444
+ const found = kept.map((row) => documentOf(shape, queried.get(Number(row.key)), row, linked));
599
445
  const last = kept.at(-1);
600
446
  const cursorAt = (row) => cursorOf([
601
447
  ...shape.orderBy.map(({ key }, index) => decodedKey(key, row, index)),
@@ -625,7 +471,7 @@ function withContent(connection, rows, budget) {
625
471
  return rows;
626
472
  const ids = rows.map((row) => row.id);
627
473
  const sizes = new Map(connection
628
- .rows(`SELECT id, octet_length(content) AS size FROM replica_entries WHERE id IN ${list(ids).sql}`, ids)
474
+ .rows(`SELECT id, octet_length(content) AS size FROM replica_entries WHERE id IN ${listed(ids).sql}`, ids)
629
475
  .map((row) => [row.id, Number(row.size ?? 0)]));
630
476
  let cut = rows.length;
631
477
  let spent = 0;
@@ -640,18 +486,22 @@ function withContent(connection, rows, budget) {
640
486
  const kept = rows.slice(0, cut);
641
487
  const read = kept.map((row) => row.id);
642
488
  const held = new Map(connection
643
- .rows(`SELECT id, content FROM replica_entries WHERE id IN ${list(read).sql}`, read)
489
+ .rows(`SELECT id, content FROM replica_entries WHERE id IN ${listed(read).sql}`, read)
644
490
  .map((row) => [row.id, row.content]));
645
491
  return kept.map((row) => ({ ...row, content: held.get(row.id) ?? null }));
646
492
  }
647
- function storedFolders(bound, plan) {
648
- if (plan.paths !== null || plan.hop !== null || plan.backlinks !== null)
493
+ function storedFolders(bound, plan, subfolder) {
494
+ if (plan.paths !== null || plan.fileIds !== null)
495
+ return null;
496
+ if (plan.hop !== null || plan.backlinks !== null)
497
+ return null;
498
+ if (subfolder && plan.folder !== null)
649
499
  return null;
650
500
  const whole = bound.rooms.filter(({ locator }) => bound.reach === null || bound.reach.rooms.includes(locator));
651
501
  if (whole.length === 0)
652
502
  return null;
653
503
  const keys = whole.map(({ key }) => key);
654
- const prefix = plan.root.pathPrefix;
504
+ const prefix = plan.root.pathPrefix ?? '';
655
505
  const outside = (bound.reach?.areas ?? []).flatMap((area) => {
656
506
  const key = bound.keys.get(area.room);
657
507
  if (key === undefined)
@@ -659,100 +509,190 @@ function storedFolders(bound, plan) {
659
509
  const fence = fenced('f', key, area, true);
660
510
  return [fragment(`NOT ${grouped(fence).sql}`, fence.bind)];
661
511
  });
512
+ const under = prefix === ''
513
+ ? subfolder
514
+ ? [fragment("f.path <> ''")]
515
+ : []
516
+ : [
517
+ subfolder
518
+ ? fragment('(f.path >= ? AND f.path < ?)', [`${prefix}/`, `${prefix}0`])
519
+ : within('f', prefix),
520
+ ];
662
521
  return all([
663
- fragment(`f.key IN ${list(keys).sql} AND f.layer = 0 AND f.present = 1`, keys),
522
+ fragment(`f.key IN ${listed(keys).sql} AND f.layer = 0 AND f.present = 1`, keys),
664
523
  ...outside,
665
- ...(prefix === null || prefix === ''
666
- ? []
667
- : [
668
- fragment('(f.path = ? OR (f.path >= ? AND f.path < ?))', [
669
- prefix,
670
- `${prefix}/`,
671
- `${prefix}0`,
672
- ]),
673
- ]),
524
+ ...under,
525
+ ...(plan.outside ?? []).map((folder) => fragment(`NOT ${within('f', folder).sql}`, within('f', folder).bind)),
674
526
  ...(plan.folder === null ? [] : [fragment('f.path = ?', [plan.folder])]),
675
527
  ]);
676
528
  }
529
+ const SIZES = [
530
+ 'CASE WHEN d.binary IS NULL THEN 1 ELSE 0 END AS dn',
531
+ 'CASE WHEN d.binary IS NULL THEN d.bytes ELSE 0 END AS ds',
532
+ 'CASE WHEN d.binary IS NULL THEN 0 ELSE 1 END AS fn',
533
+ 'CASE WHEN d.binary IS NULL THEN 0 ELSE d.bytes END AS fs',
534
+ ];
535
+ const LATEST = "d.changed_instant || ' ' || (d.change ->> '$.time') AS u";
536
+ function greatestTerm(ordering) {
537
+ return { ordering, kind: fragment('gk'), value: fragment('gv'), instant: fragment('gi') };
538
+ }
539
+ function latestOf(held) {
540
+ if (typeof held !== 'string')
541
+ return null;
542
+ return held.slice(held.indexOf(' ') + 1);
543
+ }
677
544
  function groups(connection, bound, plan, shape) {
545
+ const prefix = plan.root.pathPrefix;
678
546
  const keyed = shape.groupBy.map((key, index) => {
679
- const { term, join } = keyTerm(bound, key, GROUP_ORDER, index);
547
+ const { term, join } = keyTerm(key, GROUP_ORDER, index, prefix);
680
548
  const kind = fragment(`COALESCE(${term.kind.sql}, 'null')`, term.kind.bind);
681
549
  return { term: { ...term, kind }, join };
682
550
  });
551
+ const ordering = shape.orderBy.map(({ by }) => by);
552
+ const sized = shape.size || ordering.includes('size');
553
+ const dated = shape.updated || ordering.includes('updated');
554
+ const greatest = shape.greatest === null
555
+ ? null
556
+ : keyTerm({ of: 'field', field: shape.greatest }, GREATEST, keyed.length, prefix);
683
557
  const filter = filtered(bound, plan);
684
558
  const inner = joined([
685
559
  fragment('SELECT '),
686
560
  joined(keyed.map(({ term }, index) => joined([
687
561
  fragment(`${term.kind.sql} AS k${index}`, term.kind.bind),
688
562
  fragment(`, ${term.value.sql} AS v${index}`, term.value.bind),
563
+ fragment(`, ${instantOf(term).sql} AS i${index}`, instantOf(term).bind),
689
564
  ], '')), ', '),
690
- fragment(', 1 AS n FROM replica_entries d'),
691
- ...keyed.flatMap(({ join }) => (join === null ? [] : [fragment(' '), join])),
565
+ fragment(['', '1 AS n', ...(sized ? SIZES : []), ...(dated ? [LATEST] : [])].join(', ')),
566
+ ...(greatest === null
567
+ ? []
568
+ : [
569
+ fragment(`, ${greatest.term.kind.sql} AS gk`, greatest.term.kind.bind),
570
+ fragment(`, ${greatest.term.value.sql} AS gv`, greatest.term.value.bind),
571
+ fragment(`, ${instantOf(greatest.term).sql} AS gi`, instantOf(greatest.term).bind),
572
+ ]),
573
+ fragment(' FROM replica_entries d'),
574
+ ...[...keyed, ...(greatest === null ? [] : [greatest])].flatMap(({ join }) => join === null ? [] : [fragment(' '), join]),
692
575
  fragment(' WHERE '),
693
576
  filter,
694
577
  ], '');
695
- const folders = shape.groupBy.length === 1 && shape.groupBy[0].of === 'folder'
696
- ? storedFolders(bound, plan)
697
- : null;
578
+ const only = shape.groupBy.length === 1 ? shape.groupBy[0].of : null;
579
+ const subfolder = only === 'subfolder';
580
+ const folders = only === 'folder' || subfolder ? storedFolders(bound, plan, subfolder) : null;
581
+ const folderKey = subfolder ? subfolderOf('f.path', prefix) : fragment('f.path');
582
+ const zeros = [
583
+ ...(sized ? ['0', '0', '0', '0'] : []),
584
+ ...(dated ? ['NULL'] : []),
585
+ ...(greatest === null ? [] : ['NULL', 'NULL', 'NULL']),
586
+ ];
698
587
  const source = folders === null
699
588
  ? inner
700
589
  : joined([
701
590
  inner,
702
- fragment(" UNION ALL SELECT 'string' AS k0, f.path AS v0, 0 AS n FROM replica_folders f WHERE "),
591
+ fragment(` UNION ALL SELECT 'string' AS k0, ${folderKey.sql} AS v0, NULL AS i0, 0 AS n${zeros.map((one) => `, ${one}`).join('')} FROM replica_folders f WHERE `, folderKey.bind),
703
592
  folders,
704
593
  ], '');
705
- const columns = keyed.map((_, index) => `k${index}, v${index}`).join(', ');
706
- const counted = {
707
- ordering: { ...GROUP_ORDER },
708
- kind: fragment("'number'"),
709
- value: fragment('n'),
710
- };
711
- const outer = (index, ordering) => ({
712
- ordering,
594
+ const held = keyed
595
+ .map((_, index) => `k${index}, i${index}, CASE WHEN i${index} IS NULL THEN v${index} END`)
596
+ .join(', ');
597
+ const ranked = greatest === null
598
+ ? source
599
+ : joined([
600
+ fragment(`SELECT *, ROW_NUMBER() OVER (PARTITION BY ${held} ORDER BY `),
601
+ sql.sorted(bound.collate, greatestTerm(GREATEST)),
602
+ fragment(', gv) AS gr FROM ('),
603
+ source,
604
+ fragment(')'),
605
+ ], '');
606
+ const columns = [
607
+ ...keyed.map((_, index) => `k${index}, min(v${index}) AS v${index}, i${index}`),
608
+ 'sum(n) AS n',
609
+ ...(sized ? ['sum(dn) AS dn', 'sum(ds) AS ds', 'sum(fn) AS fn', 'sum(fs) AS fs'] : []),
610
+ ...(dated ? ['max(u) AS u'] : []),
611
+ ...(greatest === null
612
+ ? []
613
+ : ['gk', 'gv', 'gi'].map((column) => `min(CASE WHEN gr = 1 THEN ${column} END) AS ${column}`)),
614
+ ].join(', ');
615
+ const outer = (index, order) => ({
616
+ ordering: order,
713
617
  kind: fragment(`k${index}`),
714
618
  value: fragment(`v${index}`),
715
619
  });
716
- const ordered = shape.orderBy.map(({ by, direction }) => by === 'count'
717
- ? { ...counted, ordering: { ...GROUP_ORDER, direction } }
718
- : outer(by, { ...GROUP_ORDER, direction }));
620
+ const termOf = (by, order) => {
621
+ if (by === 'count')
622
+ return { ordering: order, kind: fragment("'number'"), value: fragment('n') };
623
+ if (by === 'size') {
624
+ return { ordering: order, kind: fragment("'number'"), value: fragment('(ds + fs)') };
625
+ }
626
+ if (by === 'updated') {
627
+ return {
628
+ ordering: order,
629
+ kind: fragment("(CASE WHEN u IS NULL THEN 'null' ELSE 'string' END)"),
630
+ value: fragment("substr(u, instr(u, ' ') + 1)"),
631
+ instant: fragment("substr(u, 1, instr(u, ' ') - 1)"),
632
+ };
633
+ }
634
+ if (by === 'greatest')
635
+ return greatestTerm(order);
636
+ return { ...outer(by, order), instant: fragment(`i${by}`) };
637
+ };
638
+ const valueOf = (row, by) => {
639
+ if (by === 'count')
640
+ return Number(row.n);
641
+ if (by === 'size')
642
+ return Number(row.ds) + Number(row.fs);
643
+ if (by === 'updated') {
644
+ const text = latestOf(row.u);
645
+ return text === null ? null : sql.sortValue('string', text, row.u.split(' ')[0]);
646
+ }
647
+ if (by === 'greatest')
648
+ return sortedOf(row.gk, row.gv, row.gi);
649
+ return sortedValue(row, by);
650
+ };
651
+ const ordered = shape.orderBy.map(({ by, direction, nulls }) => termOf(by, { ...GROUP_ORDER, direction, nulls: nulls ?? GROUP_ORDER.nulls }));
719
652
  const ties = keyed.flatMap((_, index) => [
720
653
  { ...outer(index, GROUP_TIES), total: true },
721
654
  { ordering: GROUP_TIES, kind: fragment("'string'"), value: fragment(`k${index}`) },
722
655
  ]);
723
656
  const cursor = plan.after?.values ?? null;
724
- const tieValues = cursor === null
657
+ const after = cursor === null
725
658
  ? null
726
- : cursor[shape.orderBy.length].flatMap((value) => [
727
- value,
728
- kindOf(value ?? null),
729
- ]);
730
- const after = cursor === null || tieValues === null
731
- ? null
732
- : following(ordered.map((term, index) => beyond(bound, term, cursor[index])), following(ties.slice(0, -1).map((term, index) => beyond(bound, term, tieValues[index])), beyond(bound, ties.at(-1), tieValues.at(-1)).beyond));
733
- const grouping = joined([fragment(`SELECT ${columns}, sum(n) AS n FROM (`), source, fragment(`) GROUP BY ${columns}`)], '');
659
+ : (() => {
660
+ const tieValues = cursor[shape.orderBy.length].flatMap((value) => [value, heldKind(value ?? null)]);
661
+ return sql.following(ordered.map((term, index) => sql.beyond(bound.collate, term, cursor[index])), sql.following(ties
662
+ .slice(0, -1)
663
+ .map((term, index) => sql.beyond(bound.collate, term, tieValues[index])), sql.beyond(bound.collate, ties.at(-1), tieValues.at(-1)).beyond));
664
+ })();
665
+ const grouping = joined([fragment(`SELECT ${columns} FROM (`), ranked, fragment(`) GROUP BY ${held}`)], '');
734
666
  const windowed = joined([
735
667
  fragment('SELECT * FROM ('),
736
668
  grouping,
737
669
  fragment(')'),
738
670
  ...(after === null ? [] : [fragment(' WHERE '), after]),
739
671
  fragment(' ORDER BY '),
740
- joined([...ordered, ...ties].map((term) => sorted(bound, term)), ', '),
672
+ joined([...ordered, ...ties].map((term) => sql.sorted(bound.collate, term)), ', '),
741
673
  ...(plan.limit === null ? [] : [fragment(' LIMIT ?', [plan.limit + 1])]),
742
674
  ], '');
743
675
  const rows = connection.rows(windowed.sql, windowed.bind);
744
676
  const kept = plan.limit === null ? rows : rows.slice(0, plan.limit);
745
677
  const keyOf = (row) => keyed.map((_, index) => decoded(row[`k${index}`], row[`v${index}`]));
746
- const found = kept.map((row) => ({ key: keyOf(row), count: Number(row.n) }));
678
+ const found = kept.map((row) => ({
679
+ key: keyOf(row),
680
+ count: Number(row.n),
681
+ ...(shape.size
682
+ ? {
683
+ documents: { count: Number(row.dn), size: Number(row.ds) },
684
+ files: { count: Number(row.fn), size: Number(row.fs) },
685
+ }
686
+ : {}),
687
+ ...(shape.updated ? { updated: latestOf(row.u) } : {}),
688
+ ...(shape.greatest === null ? {} : { greatest: decoded(row.gk, row.gv) }),
689
+ }));
747
690
  const last = kept.at(-1);
748
691
  const next = plan.limit === null || rows.length <= plan.limit
749
692
  ? undefined
750
693
  : last === undefined
751
694
  ? (plan.after?.text ?? FROM_START)
752
- : cursorOf([
753
- ...shape.orderBy.map(({ by }) => by === 'count' ? Number(last.n) : decoded(last[`k${by}`], last[`v${by}`])),
754
- keyOf(last),
755
- ]);
695
+ : cursorOf([...shape.orderBy.map(({ by }) => valueOf(last, by)), keyOf(last)]);
756
696
  const total = plan.total
757
697
  ? Number(connection.rows(joined([fragment('SELECT count(*) AS held FROM ('), grouping, fragment(')')], '').sql, grouping.bind)[0].held)
758
698
  : undefined;
@@ -770,9 +710,6 @@ function boundOf(connection, plan, rooms) {
770
710
  collate: collating(connection),
771
711
  };
772
712
  }
773
- export function selected(connection, plan, rooms) {
774
- return filtered(boundOf(connection, plan, rooms), plan);
775
- }
776
713
  export function answerQuery(connection, plan, rooms, budget = null) {
777
714
  const bound = boundOf(connection, plan, rooms);
778
715
  if (rooms.length === 0) {