@xyo-network/diviner-indexing-memory 2.107.1 → 2.107.4

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.
@@ -1,2 +1,253 @@
1
- "use strict";var v=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var P=Object.getPrototypeOf,Q=Object.prototype.hasOwnProperty;var R=Reflect.get;var B=(s,i,e)=>i in s?v(s,i,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[i]=e;var p=(s,i)=>v(s,"name",{value:i,configurable:!0});var H=(s,i)=>{for(var e in i)v(s,e,{get:i[e],enumerable:!0})},M=(s,i,e,a)=>{if(i&&typeof i=="object"||typeof i=="function")for(let t of C(i))!Q.call(s,t)&&t!==e&&v(s,t,{get:()=>i[t],enumerable:!(a=A(i,t))||a.enumerable});return s};var q=s=>M(v({},"__esModule",{value:!0}),s);var m=(s,i,e)=>B(s,typeof i!="symbol"?i+"":i,e);var x=(s,i,e)=>R(P(s),e,i);var j={};H(j,{IndexingDiviner:()=>u});module.exports=q(j);var l=require("@xylabs/assert"),g=require("@xylabs/timer"),$=require("@xyo-network/archivist-wrapper"),b=require("@xyo-network/boundwitness-builder"),F=require("@xyo-network/boundwitness-model"),I=require("@xyo-network/diviner-abstract"),_=require("@xyo-network/diviner-boundwitness-model"),w=require("@xyo-network/diviner-indexing-model"),W=require("@xyo-network/diviner-model"),y=require("@xyo-network/diviner-wrapper"),d=require("@xyo-network/module-model"),S=require("@xyo-network/payload-builder");function O(s,i,e,a){var t=arguments.length,n=t<3?i:a===null?a=Object.getOwnPropertyDescriptor(i,e):a,o;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")n=Reflect.decorate(s,i,e,a);else for(var r=s.length-1;r>=0;r--)(o=s[r])&&(n=(t<3?o(n):t>3?o(i,e,n):o(i,e))||n);return t>3&&n&&Object.defineProperty(i,e,n),n}p(O,"_ts_decorate");var c="IndexingDiviner",f=class f extends I.AbstractDiviner{_lastState;_pollId;get payloadDivinerLimit(){return this.config.payloadDivinerLimit??1e3}get pollFrequency(){return this.config.pollFrequency??1e4}backgroundDivine=p(async()=>{let i=await this.retrieveState(),e=await this.getIndexingDivinerStage("stateToIndexCandidateDiviner"),a=i?await e.divine([i]):await e.divine(),t=a.find(d.isModuleStateWithMeta),n=a.filter(D=>!(0,d.isModuleStateWithMeta)(D)),r=await(await this.getIndexingDivinerStage("indexCandidateToIndexDiviner")).divine(n);await(await this.getArchivistForStore("indexStore")).insert(r),t&&await this.commitState(t)},"backgroundDivine");async commitState(i){var t;if(i.state.offset===((t=this._lastState)==null?void 0:t.state.offset))return;this._lastState=i;let e=await this.getArchivistForStore("stateStore"),[a]=await new b.BoundWitnessBuilder().payload(i).signer(this.account).build();await e.insert([a,i])}async divineHandler(i=[]){let e=await this.getPayloadDivinerForStore("indexStore"),a=await this.getIndexingDivinerStage("divinerQueryToIndexQueryDiviner"),t=await this.getIndexingDivinerStage("indexQueryResponseToDivinerQueryResponseDiviner"),n=(await Promise.all(i.map(async o=>{let r=await a.divine([o]),h=await e.divine(r);return(await Promise.all(h.flat().map(T=>t.divine([o,T])))).flat()}))).flat();return await Promise.all(n.map(o=>S.PayloadBuilder.build(o)))}async getArchivistForStore(i){var t,n;let e=(0,l.assertEx)((n=(t=this.config)==null?void 0:t[i])==null?void 0:n.archivist,()=>`${c}: Config for ${i}.archivist not specified`),a=(0,l.assertEx)(await this.resolve(e),()=>`${c}: Failed to resolve ${i}.archivist [${e}]`);return $.ArchivistWrapper.wrap(a,this.account)}async getBoundWitnessDivinerForStore(i){var t,n;let e=(0,l.assertEx)((n=(t=this.config)==null?void 0:t[i])==null?void 0:n.boundWitnessDiviner,()=>`${c}: Config for ${i}.boundWitnessDiviner not specified`),a=(0,l.assertEx)(await this.resolve(e),()=>`${c}: Failed to resolve ${i}.boundWitnessDiviner [${e}]`);return y.DivinerWrapper.wrap(a,this.account)}async getIndexingDivinerStage(i){var t,n;let e=(0,l.assertEx)((n=(t=this.config)==null?void 0:t.indexingDivinerStages)==null?void 0:n[i],()=>`${c}: Config for indexingDivinerStages.${i} not specified`),a=await this.resolve(e);return(0,l.assertEx)((0,W.asDivinerInstance)(a),()=>`${c}: Failed to resolve indexing diviner stage for ${i}`)}async getPayloadDivinerForStore(i){var t,n;let e=(0,l.assertEx)((n=(t=this.config)==null?void 0:t[i])==null?void 0:n.payloadDiviner,()=>`${c}: Config for ${i}.payloadDiviner not specified`),a=(0,l.assertEx)(await this.resolve(e),()=>`${c}: Failed to resolve ${i}.payloadDiviner [${e}]`);return y.DivinerWrapper.wrap(a,this.account)}async retrieveState(){if(this._lastState)return this._lastState;let i="",e=await this.getBoundWitnessDivinerForStore("stateStore"),a=await new S.PayloadBuilder({schema:_.BoundWitnessDivinerQuerySchema}).fields({address:this.account.address,limit:1,offset:0,order:"desc",payload_schemas:[d.ModuleStateSchema]}).build(),t=await e.divine([a]);if(t.length>0){let n=t[0];(0,F.isBoundWitness)(n)&&(i=n.addresses.map((o,r)=>({address:o,index:r})).filter(({address:o})=>o===this.account.address).reduce((o,r)=>{var h;return((h=n.payload_schemas)==null?void 0:h[r==null?void 0:r.index])===d.ModuleStateSchema?n.payload_hashes[r==null?void 0:r.index]:o},""))}if(i!==""){let o=(await(await this.getArchivistForStore("stateStore")).get([i])).find(d.isModuleState);if(o)return o}}async startHandler(){return await super.startHandler(),this.poll(),!0}async stopHandler(i){return this._pollId&&(clearTimeout(this._pollId),this._pollId=void 0),await super.stopHandler()}poll(){this._pollId=(0,g.setTimeoutEx)(async()=>{try{await Promise.resolve(),await this.backgroundDivine()}catch(i){console.log(i)}finally{this._pollId&&(0,g.clearTimeoutEx)(this._pollId),this._pollId=void 0,this.poll()}},this.pollFrequency)}};p(f,"IndexingDiviner"),m(f,"allowRandomAccount",!1),m(f,"configSchemas",[...x(f,f,"configSchemas"),w.IndexingDivinerConfigSchema]),m(f,"defaultConfigSchema",w.IndexingDivinerConfigSchema);var u=f;u=O([(0,d.creatableModule)()],u);0&&(module.exports={IndexingDiviner});
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __reflectGet = Reflect.get;
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
10
+ var __export = (target, all) => {
11
+ for (var name in all)
12
+ __defProp(target, name, { get: all[name], enumerable: true });
13
+ };
14
+ var __copyProps = (to, from, except, desc) => {
15
+ if (from && typeof from === "object" || typeof from === "function") {
16
+ for (let key of __getOwnPropNames(from))
17
+ if (!__hasOwnProp.call(to, key) && key !== except)
18
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
19
+ }
20
+ return to;
21
+ };
22
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
23
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
24
+ var __superGet = (cls, obj, key) => __reflectGet(__getProtoOf(cls), key, obj);
25
+
26
+ // src/index.ts
27
+ var src_exports = {};
28
+ __export(src_exports, {
29
+ IndexingDiviner: () => IndexingDiviner
30
+ });
31
+ module.exports = __toCommonJS(src_exports);
32
+
33
+ // src/Diviner.ts
34
+ var import_assert = require("@xylabs/assert");
35
+ var import_timer = require("@xylabs/timer");
36
+ var import_archivist_wrapper = require("@xyo-network/archivist-wrapper");
37
+ var import_boundwitness_builder = require("@xyo-network/boundwitness-builder");
38
+ var import_boundwitness_model = require("@xyo-network/boundwitness-model");
39
+ var import_diviner_abstract = require("@xyo-network/diviner-abstract");
40
+ var import_diviner_boundwitness_model = require("@xyo-network/diviner-boundwitness-model");
41
+ var import_diviner_indexing_model = require("@xyo-network/diviner-indexing-model");
42
+ var import_diviner_model = require("@xyo-network/diviner-model");
43
+ var import_diviner_wrapper = require("@xyo-network/diviner-wrapper");
44
+ var import_module_model = require("@xyo-network/module-model");
45
+ var import_payload_builder = require("@xyo-network/payload-builder");
46
+ function _ts_decorate(decorators, target, key, desc) {
47
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
48
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
49
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
50
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
51
+ }
52
+ __name(_ts_decorate, "_ts_decorate");
53
+ var moduleName = "IndexingDiviner";
54
+ var _IndexingDiviner = class _IndexingDiviner extends import_diviner_abstract.AbstractDiviner {
55
+ _lastState;
56
+ _pollId;
57
+ get payloadDivinerLimit() {
58
+ return this.config.payloadDivinerLimit ?? 1e3;
59
+ }
60
+ get pollFrequency() {
61
+ return this.config.pollFrequency ?? 1e4;
62
+ }
63
+ /**
64
+ * Works via batched iteration of the source archivist to populate the index.
65
+ * @returns A promise that resolves when the background process is complete
66
+ */
67
+ backgroundDivine = /* @__PURE__ */ __name(async () => {
68
+ const lastState = await this.retrieveState();
69
+ const indexCandidateDiviner = await this.getIndexingDivinerStage("stateToIndexCandidateDiviner");
70
+ const results = lastState ? await indexCandidateDiviner.divine([
71
+ lastState
72
+ ]) : await indexCandidateDiviner.divine();
73
+ const nextState = results.find(import_module_model.isModuleStateWithMeta);
74
+ const indexCandidates = results.filter((x) => !(0, import_module_model.isModuleStateWithMeta)(x));
75
+ const toIndexTransformDiviner = await this.getIndexingDivinerStage("indexCandidateToIndexDiviner");
76
+ const indexes = await toIndexTransformDiviner.divine(indexCandidates);
77
+ const indexArchivist = await this.getArchivistForStore("indexStore");
78
+ await indexArchivist.insert(indexes);
79
+ if (nextState) {
80
+ await this.commitState(nextState);
81
+ }
82
+ }, "backgroundDivine");
83
+ /**
84
+ * Commit the internal state of the Diviner process. This is similar
85
+ * to a transaction completion in a database and should only be called
86
+ * when results have been successfully persisted to the appropriate
87
+ * external stores.
88
+ * @param nextState The state to commit
89
+ */
90
+ async commitState(nextState) {
91
+ var _a;
92
+ if (nextState.state.offset === ((_a = this._lastState) == null ? void 0 : _a.state.offset)) return;
93
+ this._lastState = nextState;
94
+ const archivist = await this.getArchivistForStore("stateStore");
95
+ const [bw] = await new import_boundwitness_builder.BoundWitnessBuilder().payload(nextState).signer(this.account).build();
96
+ await archivist.insert([
97
+ bw,
98
+ nextState
99
+ ]);
100
+ }
101
+ async divineHandler(payloads = []) {
102
+ const indexPayloadDiviner = await this.getPayloadDivinerForStore("indexStore");
103
+ const divinerQueryToIndexQueryDiviner = await this.getIndexingDivinerStage("divinerQueryToIndexQueryDiviner");
104
+ const indexQueryResponseToDivinerQueryResponseDiviner = await this.getIndexingDivinerStage("indexQueryResponseToDivinerQueryResponseDiviner");
105
+ const results = (await Promise.all(payloads.map(async (payload) => {
106
+ const indexQuery = await divinerQueryToIndexQueryDiviner.divine([
107
+ payload
108
+ ]);
109
+ const indexedResults = await indexPayloadDiviner.divine(indexQuery);
110
+ const response = await Promise.all(indexedResults.flat().map((indexedResult) => indexQueryResponseToDivinerQueryResponseDiviner.divine([
111
+ payload,
112
+ indexedResult
113
+ ])));
114
+ return response.flat();
115
+ }))).flat();
116
+ return await Promise.all(results.map((result) => import_payload_builder.PayloadBuilder.build(result)));
117
+ }
118
+ /**
119
+ * Retrieves the archivist for the specified store
120
+ * @param store The store to retrieve the archivist for
121
+ * @returns The archivist for the specified store
122
+ */
123
+ async getArchivistForStore(store) {
124
+ var _a, _b;
125
+ const name = (0, import_assert.assertEx)((_b = (_a = this.config) == null ? void 0 : _a[store]) == null ? void 0 : _b.archivist, () => `${moduleName}: Config for ${store}.archivist not specified`);
126
+ const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.archivist [${name}]`);
127
+ return import_archivist_wrapper.ArchivistWrapper.wrap(mod, this.account);
128
+ }
129
+ /**
130
+ * Retrieves the BoundWitness Diviner for the specified store
131
+ * @param store The store to retrieve the BoundWitness Diviner for
132
+ * @returns The BoundWitness Diviner for the specified store
133
+ */
134
+ async getBoundWitnessDivinerForStore(store) {
135
+ var _a, _b;
136
+ const name = (0, import_assert.assertEx)((_b = (_a = this.config) == null ? void 0 : _a[store]) == null ? void 0 : _b.boundWitnessDiviner, () => `${moduleName}: Config for ${store}.boundWitnessDiviner not specified`);
137
+ const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.boundWitnessDiviner [${name}]`);
138
+ return import_diviner_wrapper.DivinerWrapper.wrap(mod, this.account);
139
+ }
140
+ /**
141
+ * Gets the Diviner for the supplied Indexing Diviner stage
142
+ * @param transform The Indexing Diviner stage
143
+ * @returns The diviner corresponding to the Indexing Diviner stage
144
+ */
145
+ async getIndexingDivinerStage(transform) {
146
+ var _a, _b;
147
+ const nameOrAddress = (0, import_assert.assertEx)((_b = (_a = this.config) == null ? void 0 : _a.indexingDivinerStages) == null ? void 0 : _b[transform], () => `${moduleName}: Config for indexingDivinerStages.${transform} not specified`);
148
+ const mod = await this.resolve(nameOrAddress);
149
+ return (0, import_assert.assertEx)((0, import_diviner_model.asDivinerInstance)(mod), () => `${moduleName}: Failed to resolve indexing diviner stage for ${transform}`);
150
+ }
151
+ /**
152
+ * Retrieves the Payload Diviner for the specified store
153
+ * @param store The store to retrieve the Payload Diviner for
154
+ * @returns The Payload Diviner for the specified store
155
+ */
156
+ async getPayloadDivinerForStore(store) {
157
+ var _a, _b;
158
+ const name = (0, import_assert.assertEx)((_b = (_a = this.config) == null ? void 0 : _a[store]) == null ? void 0 : _b.payloadDiviner, () => `${moduleName}: Config for ${store}.payloadDiviner not specified`);
159
+ const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.payloadDiviner [${name}]`);
160
+ return import_diviner_wrapper.DivinerWrapper.wrap(mod, this.account);
161
+ }
162
+ /**
163
+ * Retrieves the last state of the Diviner process. Used to recover state after
164
+ * preemptions, reboots, etc.
165
+ */
166
+ async retrieveState() {
167
+ if (this._lastState) return this._lastState;
168
+ let hash = "";
169
+ const diviner = await this.getBoundWitnessDivinerForStore("stateStore");
170
+ const query = await new import_payload_builder.PayloadBuilder({
171
+ schema: import_diviner_boundwitness_model.BoundWitnessDivinerQuerySchema
172
+ }).fields({
173
+ address: this.account.address,
174
+ limit: 1,
175
+ offset: 0,
176
+ order: "desc",
177
+ payload_schemas: [
178
+ import_module_model.ModuleStateSchema
179
+ ]
180
+ }).build();
181
+ const boundWitnesses = await diviner.divine([
182
+ query
183
+ ]);
184
+ if (boundWitnesses.length > 0) {
185
+ const boundWitness = boundWitnesses[0];
186
+ if ((0, import_boundwitness_model.isBoundWitness)(boundWitness)) {
187
+ hash = boundWitness.addresses.map((address, index) => ({
188
+ address,
189
+ index
190
+ })).filter(({ address }) => address === this.account.address).reduce((prev, curr) => {
191
+ var _a;
192
+ return ((_a = boundWitness.payload_schemas) == null ? void 0 : _a[curr == null ? void 0 : curr.index]) === import_module_model.ModuleStateSchema ? boundWitness.payload_hashes[curr == null ? void 0 : curr.index] : prev;
193
+ }, "");
194
+ }
195
+ }
196
+ if (hash !== "") {
197
+ const archivist = await this.getArchivistForStore("stateStore");
198
+ const payload = (await archivist.get([
199
+ hash
200
+ ])).find(import_module_model.isModuleState);
201
+ if (payload) {
202
+ return payload;
203
+ }
204
+ }
205
+ return void 0;
206
+ }
207
+ async startHandler() {
208
+ await super.startHandler();
209
+ this.poll();
210
+ return true;
211
+ }
212
+ async stopHandler(_timeout) {
213
+ if (this._pollId) {
214
+ clearTimeout(this._pollId);
215
+ this._pollId = void 0;
216
+ }
217
+ return await super.stopHandler();
218
+ }
219
+ /**
220
+ * Runs the background divine process on a loop with a delay
221
+ * specified by the `config.pollFrequency`
222
+ */
223
+ poll() {
224
+ this._pollId = (0, import_timer.setTimeoutEx)(async () => {
225
+ try {
226
+ await Promise.resolve();
227
+ await this.backgroundDivine();
228
+ } catch (e) {
229
+ console.log(e);
230
+ } finally {
231
+ if (this._pollId) (0, import_timer.clearTimeoutEx)(this._pollId);
232
+ this._pollId = void 0;
233
+ this.poll();
234
+ }
235
+ }, this.pollFrequency);
236
+ }
237
+ };
238
+ __name(_IndexingDiviner, "IndexingDiviner");
239
+ __publicField(_IndexingDiviner, "allowRandomAccount", false);
240
+ __publicField(_IndexingDiviner, "configSchemas", [
241
+ ...__superGet(_IndexingDiviner, _IndexingDiviner, "configSchemas"),
242
+ import_diviner_indexing_model.IndexingDivinerConfigSchema
243
+ ]);
244
+ __publicField(_IndexingDiviner, "defaultConfigSchema", import_diviner_indexing_model.IndexingDivinerConfigSchema);
245
+ var IndexingDiviner = _IndexingDiviner;
246
+ IndexingDiviner = _ts_decorate([
247
+ (0, import_module_model.creatableModule)()
248
+ ], IndexingDiviner);
249
+ // Annotate the CommonJS export names for ESM import in node:
250
+ 0 && (module.exports = {
251
+ IndexingDiviner
252
+ });
2
253
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts","../../src/Diviner.ts"],"sourcesContent":["export * from './Diviner'\n","import { assertEx } from '@xylabs/assert'\nimport { Hash } from '@xylabs/hex'\nimport { clearTimeoutEx, setTimeoutEx } from '@xylabs/timer'\nimport { ArchivistWrapper } from '@xyo-network/archivist-wrapper'\nimport { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { AbstractDiviner } from '@xyo-network/diviner-abstract'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport {\n IndexingDivinerConfig,\n IndexingDivinerConfigSchema,\n IndexingDivinerParams,\n IndexingDivinerStage,\n IndexingDivinerState,\n} from '@xyo-network/diviner-indexing-model'\nimport { asDivinerInstance, DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'\nimport { DivinerWrapper } from '@xyo-network/diviner-wrapper'\nimport { creatableModule, isModuleState, isModuleStateWithMeta, ModuleState, ModuleStateSchema } from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Payload, Schema, WithMeta } from '@xyo-network/payload-model'\n\nexport type ConfigStoreKey = 'indexStore' | 'stateStore'\n\nexport type ConfigStore = Extract<keyof IndexingDivinerConfig, ConfigStoreKey>\n\nconst moduleName = 'IndexingDiviner'\n\n@creatableModule<IndexingDiviner>()\nexport class IndexingDiviner<\n TParams extends IndexingDivinerParams = IndexingDivinerParams,\n TIn extends Payload = Payload,\n TOut extends Payload = Payload,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly allowRandomAccount = false\n static override readonly configSchemas: Schema[] = [...super.configSchemas, IndexingDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = IndexingDivinerConfigSchema\n\n private _lastState?: ModuleState<IndexingDivinerState>\n private _pollId?: string\n\n get payloadDivinerLimit() {\n return this.config.payloadDivinerLimit ?? 1000\n }\n\n get pollFrequency() {\n return this.config.pollFrequency ?? 10_000\n }\n\n /**\n * Works via batched iteration of the source archivist to populate the index.\n * @returns A promise that resolves when the background process is complete\n */\n protected backgroundDivine = async (): Promise<void> => {\n // Load last state\n const lastState = await this.retrieveState()\n // Get next batch of results\n const indexCandidateDiviner = await this.getIndexingDivinerStage('stateToIndexCandidateDiviner')\n const results = lastState ? await indexCandidateDiviner.divine([lastState]) : await indexCandidateDiviner.divine()\n // Filter next state out from results\n const nextState = results.find(isModuleStateWithMeta<IndexingDivinerState>)\n const indexCandidates = results.filter((x) => !isModuleStateWithMeta(x))\n // Transform candidates to indexes\n const toIndexTransformDiviner = await this.getIndexingDivinerStage('indexCandidateToIndexDiviner')\n const indexes = await toIndexTransformDiviner.divine(indexCandidates)\n // Insert index results\n const indexArchivist = await this.getArchivistForStore('indexStore')\n await indexArchivist.insert(indexes)\n // Update state\n if (nextState) {\n await this.commitState(nextState)\n }\n }\n\n /**\n * Commit the internal state of the Diviner process. This is similar\n * to a transaction completion in a database and should only be called\n * when results have been successfully persisted to the appropriate\n * external stores.\n * @param nextState The state to commit\n */\n protected async commitState(nextState: ModuleState<IndexingDivinerState>) {\n // Don't commit state if no state has changed\n if (nextState.state.offset === this._lastState?.state.offset) return\n this._lastState = nextState\n const archivist = await this.getArchivistForStore('stateStore')\n const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build()\n await archivist.insert([bw, nextState])\n }\n\n protected override async divineHandler(payloads: TIn[] = []): Promise<WithMeta<TOut>[]> {\n const indexPayloadDiviner = await this.getPayloadDivinerForStore('indexStore')\n const divinerQueryToIndexQueryDiviner = await this.getIndexingDivinerStage('divinerQueryToIndexQueryDiviner')\n const indexQueryResponseToDivinerQueryResponseDiviner = await this.getIndexingDivinerStage('indexQueryResponseToDivinerQueryResponseDiviner')\n const results = (\n await Promise.all(\n payloads.map(async (payload) => {\n const indexQuery = await divinerQueryToIndexQueryDiviner.divine([payload])\n // Divine the results\n const indexedResults = await indexPayloadDiviner.divine(indexQuery)\n // Transform the results to the response shape\n const response = await Promise.all(\n indexedResults.flat().map((indexedResult) => indexQueryResponseToDivinerQueryResponseDiviner.divine([payload, indexedResult])),\n )\n return response.flat()\n }),\n )\n ).flat()\n // TODO: Infer this type over casting to this type\n return (await Promise.all(results.map((result) => PayloadBuilder.build(result)))) as WithMeta<TOut>[]\n }\n\n /**\n * Retrieves the archivist for the specified store\n * @param store The store to retrieve the archivist for\n * @returns The archivist for the specified store\n */\n protected async getArchivistForStore(store: ConfigStore) {\n const name = assertEx(this.config?.[store]?.archivist, () => `${moduleName}: Config for ${store}.archivist not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.archivist [${name}]`)\n return ArchivistWrapper.wrap(mod, this.account)\n }\n\n /**\n * Retrieves the BoundWitness Diviner for the specified store\n * @param store The store to retrieve the BoundWitness Diviner for\n * @returns The BoundWitness Diviner for the specified store\n */\n protected async getBoundWitnessDivinerForStore(store: ConfigStore) {\n const name = assertEx(this.config?.[store]?.boundWitnessDiviner, () => `${moduleName}: Config for ${store}.boundWitnessDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.boundWitnessDiviner [${name}]`)\n return DivinerWrapper.wrap(mod, this.account)\n }\n\n /**\n * Gets the Diviner for the supplied Indexing Diviner stage\n * @param transform The Indexing Diviner stage\n * @returns The diviner corresponding to the Indexing Diviner stage\n */\n protected async getIndexingDivinerStage(transform: IndexingDivinerStage) {\n const nameOrAddress = assertEx(\n this.config?.indexingDivinerStages?.[transform],\n () => `${moduleName}: Config for indexingDivinerStages.${transform} not specified`,\n )\n const mod = await this.resolve(nameOrAddress)\n return assertEx(asDivinerInstance(mod), () => `${moduleName}: Failed to resolve indexing diviner stage for ${transform}`) as DivinerInstance\n }\n\n /**\n * Retrieves the Payload Diviner for the specified store\n * @param store The store to retrieve the Payload Diviner for\n * @returns The Payload Diviner for the specified store\n */\n protected async getPayloadDivinerForStore(store: ConfigStore) {\n const name = assertEx(this.config?.[store]?.payloadDiviner, () => `${moduleName}: Config for ${store}.payloadDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.payloadDiviner [${name}]`)\n return DivinerWrapper.wrap(mod, this.account)\n }\n\n /**\n * Retrieves the last state of the Diviner process. Used to recover state after\n * preemptions, reboots, etc.\n */\n protected async retrieveState(): Promise<ModuleState<IndexingDivinerState> | undefined> {\n if (this._lastState) return this._lastState\n let hash: Hash = ''\n const diviner = await this.getBoundWitnessDivinerForStore('stateStore')\n const query = await new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })\n .fields({\n address: this.account.address,\n limit: 1,\n offset: 0,\n order: 'desc',\n payload_schemas: [ModuleStateSchema],\n })\n .build()\n const boundWitnesses = await diviner.divine([query])\n if (boundWitnesses.length > 0) {\n const boundWitness = boundWitnesses[0]\n if (isBoundWitness(boundWitness)) {\n // Find the index for this address in the BoundWitness that is a ModuleState\n hash = boundWitness.addresses\n .map((address, index) => ({ address, index }))\n .filter(({ address }) => address === this.account.address)\n // eslint-disable-next-line unicorn/no-array-reduce\n .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '' as Hash,\n )\n }\n }\n\n // If we able to located the last state\n if (hash !== '') {\n // Get last state\n const archivist = await this.getArchivistForStore('stateStore')\n const payload = (await archivist.get([hash])).find(isModuleState<IndexingDivinerState>)\n if (payload) {\n return payload as WithMeta<ModuleState<IndexingDivinerState>>\n }\n }\n return undefined\n }\n\n protected override async startHandler(): Promise<boolean> {\n await super.startHandler()\n this.poll()\n return true\n }\n\n protected override async stopHandler(_timeout?: number | undefined): Promise<boolean> {\n if (this._pollId) {\n clearTimeout(this._pollId)\n this._pollId = undefined\n }\n return await super.stopHandler()\n }\n\n /**\n * Runs the background divine process on a loop with a delay\n * specified by the `config.pollFrequency`\n */\n private poll() {\n this._pollId = setTimeoutEx(async () => {\n try {\n await Promise.resolve()\n await this.backgroundDivine()\n } catch (e) {\n console.log(e)\n } finally {\n if (this._pollId) clearTimeoutEx(this._pollId)\n this._pollId = undefined\n this.poll()\n }\n }, this.pollFrequency)\n }\n}\n"],"mappings":"yqBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,qBAAAE,IAAA,eAAAC,EAAAH,GCAA,IAAAI,EAAyB,0BAEzBC,EAA6C,yBAC7CC,EAAiC,0CACjCC,EAAo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EV,QAAAA,EAASU,MAAAA,CAAM,EAAA,EAC1C/D,OAAO,CAAC,CAAEqD,QAAAA,CAAO,IAAOA,IAAY,KAAKvC,QAAQuC,OAAO,EAExDW,OACC,CAACC,EAAMC,IAAAA,OAAUN,QAAAA,EAAAA,EAAaJ,kBAAbI,YAAAA,EAA+BM,GAAAA,YAAAA,EAAMH,UAAWN,oBAAoBG,EAAaO,eAAeD,GAAAA,YAAAA,EAAMH,KAAAA,EAASE,GAChI,EAAA,EAGR,CAGA,GAAIlB,IAAS,GAAI,CAGf,IAAMnC,GAAW,MADC,MAAM,KAAKT,qBAAqB,YAAA,GACjBiE,IAAI,CAACrB,EAAK,GAAGlD,KAAKwE,eAAAA,EACnD,GAAIzD,EACF,OAAOA,CAEX,CAEF,CAEA,MAAyB0D,cAAiC,CACxD,aAAM,MAAMA,aAAAA,EACZ,KAAKC,KAAI,EACF,EACT,CAEA,MAAyBC,YAAYC,EAAiD,CACpF,OAAI,KAAKzF,UACP0F,aAAa,KAAK1F,OAAO,EACzB,KAAKA,QAAU2F,QAEV,MAAM,MAAMH,YAAAA,CACrB,CAMQD,MAAO,CACb,KAAKvF,WAAU4F,gBAAa,SAAA,CAC1B,GAAI,CACF,MAAMtD,QAAQc,QAAO,EACrB,MAAM,KAAKhD,iBAAgB,CAC7B,OAASyF,EAAG,CACVC,QAAQC,IAAIF,CAAAA,CACd,QAAA,CACM,KAAK7F,YAASgG,kBAAe,KAAKhG,OAAO,EAC7C,KAAKA,QAAU2F,OACf,KAAKJ,KAAI,CACX,CACF,EAAG,KAAKpF,aAAa,CACvB,CACF,EA3MUL,EAAAA,EAAAA,mBACRmG,EAVWpG,EAUcqG,qBAAqB,IAC9CD,EAXWpG,EAWcsG,gBAA0B,IAAIC,EAAAC,IAAMF,iBAAeG,gCAC5EL,EAZWpG,EAYc0G,sBAA8BD,+BAZlD,IAAMzG,EAANwG","names":["src_exports","__export","IndexingDiviner","__toCommonJS","import_assert","import_timer","import_archivist_wrapper","import_boundwitness_builder","import_boundwitness_model","import_diviner_abstract","import_diviner_boundwitness_model","import_diviner_indexing_model","import_diviner_model","import_diviner_wrapper","import_module_model","import_payload_builder","moduleName","IndexingDiviner","AbstractDiviner","_lastState","_pollId","payloadDivinerLimit","config","pollFrequency","backgroundDivine","__name","lastState","retrieveState","indexCandidateDiviner","getIndexingDivinerStage","results","divine","nextState","find","isModuleStateWithMeta","indexCandidates","filter","x","indexes","getArchivistForStore","insert","commitState","state","offset","_a","archivist","bw","BoundWitnessBuilder","payload","signer","account","build","divineHandler","payloads","indexPayloadDiviner","getPayloadDivinerForStore","divinerQueryToIndexQueryDiviner","indexQueryResponseToDivinerQueryResponseDiviner","Promise","all","map","indexQuery","indexedResults","flat","indexedResult","result","PayloadBuilder","store","name","assertEx","_b","mod","resolve","ArchivistWrapper","wrap","getBoundWitnessDivinerForStore","boundWitnessDiviner","DivinerWrapper","transform","nameOrAddress","indexingDivinerStages","asDivinerInstance","payloadDiviner","hash","diviner","query","schema","BoundWitnessDivinerQuerySchema","fields","address","limit","order","payload_schemas","ModuleStateSchema","boundWitnesses","length","boundWitness","isBoundWitness","addresses","index","reduce","prev","curr","payload_hashes","get","isModuleState","startHandler","poll","stopHandler","_timeout","clearTimeout","undefined","setTimeoutEx","e","console","log","clearTimeoutEx","__publicField","allowRandomAccount","configSchemas","__superGet","_IndexingDiviner","IndexingDivinerConfigSchema","defaultConfigSchema"]}
1
+ {"version":3,"sources":["../../src/index.ts","../../src/Diviner.ts"],"sourcesContent":["export * from './Diviner'\n","import { assertEx } from '@xylabs/assert'\nimport { Hash } from '@xylabs/hex'\nimport { clearTimeoutEx, setTimeoutEx } from '@xylabs/timer'\nimport { ArchivistWrapper } from '@xyo-network/archivist-wrapper'\nimport { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { AbstractDiviner } from '@xyo-network/diviner-abstract'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport {\n IndexingDivinerConfig,\n IndexingDivinerConfigSchema,\n IndexingDivinerParams,\n IndexingDivinerStage,\n IndexingDivinerState,\n} from '@xyo-network/diviner-indexing-model'\nimport { asDivinerInstance, DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'\nimport { DivinerWrapper } from '@xyo-network/diviner-wrapper'\nimport { creatableModule, isModuleState, isModuleStateWithMeta, ModuleState, ModuleStateSchema } from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Payload, Schema, WithMeta } from '@xyo-network/payload-model'\n\nexport type ConfigStoreKey = 'indexStore' | 'stateStore'\n\nexport type ConfigStore = Extract<keyof IndexingDivinerConfig, ConfigStoreKey>\n\nconst moduleName = 'IndexingDiviner'\n\n@creatableModule<IndexingDiviner>()\nexport class IndexingDiviner<\n TParams extends IndexingDivinerParams = IndexingDivinerParams,\n TIn extends Payload = Payload,\n TOut extends Payload = Payload,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly allowRandomAccount = false\n static override readonly configSchemas: Schema[] = [...super.configSchemas, IndexingDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = IndexingDivinerConfigSchema\n\n private _lastState?: ModuleState<IndexingDivinerState>\n private _pollId?: string\n\n get payloadDivinerLimit() {\n return this.config.payloadDivinerLimit ?? 1000\n }\n\n get pollFrequency() {\n return this.config.pollFrequency ?? 10_000\n }\n\n /**\n * Works via batched iteration of the source archivist to populate the index.\n * @returns A promise that resolves when the background process is complete\n */\n protected backgroundDivine = async (): Promise<void> => {\n // Load last state\n const lastState = await this.retrieveState()\n // Get next batch of results\n const indexCandidateDiviner = await this.getIndexingDivinerStage('stateToIndexCandidateDiviner')\n const results = lastState ? await indexCandidateDiviner.divine([lastState]) : await indexCandidateDiviner.divine()\n // Filter next state out from results\n const nextState = results.find(isModuleStateWithMeta<IndexingDivinerState>)\n const indexCandidates = results.filter((x) => !isModuleStateWithMeta(x))\n // Transform candidates to indexes\n const toIndexTransformDiviner = await this.getIndexingDivinerStage('indexCandidateToIndexDiviner')\n const indexes = await toIndexTransformDiviner.divine(indexCandidates)\n // Insert index results\n const indexArchivist = await this.getArchivistForStore('indexStore')\n await indexArchivist.insert(indexes)\n // Update state\n if (nextState) {\n await this.commitState(nextState)\n }\n }\n\n /**\n * Commit the internal state of the Diviner process. This is similar\n * to a transaction completion in a database and should only be called\n * when results have been successfully persisted to the appropriate\n * external stores.\n * @param nextState The state to commit\n */\n protected async commitState(nextState: ModuleState<IndexingDivinerState>) {\n // Don't commit state if no state has changed\n if (nextState.state.offset === this._lastState?.state.offset) return\n this._lastState = nextState\n const archivist = await this.getArchivistForStore('stateStore')\n const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build()\n await archivist.insert([bw, nextState])\n }\n\n protected override async divineHandler(payloads: TIn[] = []): Promise<WithMeta<TOut>[]> {\n const indexPayloadDiviner = await this.getPayloadDivinerForStore('indexStore')\n const divinerQueryToIndexQueryDiviner = await this.getIndexingDivinerStage('divinerQueryToIndexQueryDiviner')\n const indexQueryResponseToDivinerQueryResponseDiviner = await this.getIndexingDivinerStage('indexQueryResponseToDivinerQueryResponseDiviner')\n const results = (\n await Promise.all(\n payloads.map(async (payload) => {\n const indexQuery = await divinerQueryToIndexQueryDiviner.divine([payload])\n // Divine the results\n const indexedResults = await indexPayloadDiviner.divine(indexQuery)\n // Transform the results to the response shape\n const response = await Promise.all(\n indexedResults.flat().map((indexedResult) => indexQueryResponseToDivinerQueryResponseDiviner.divine([payload, indexedResult])),\n )\n return response.flat()\n }),\n )\n ).flat()\n // TODO: Infer this type over casting to this type\n return (await Promise.all(results.map((result) => PayloadBuilder.build(result)))) as WithMeta<TOut>[]\n }\n\n /**\n * Retrieves the archivist for the specified store\n * @param store The store to retrieve the archivist for\n * @returns The archivist for the specified store\n */\n protected async getArchivistForStore(store: ConfigStore) {\n const name = assertEx(this.config?.[store]?.archivist, () => `${moduleName}: Config for ${store}.archivist not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.archivist [${name}]`)\n return ArchivistWrapper.wrap(mod, this.account)\n }\n\n /**\n * Retrieves the BoundWitness Diviner for the specified store\n * @param store The store to retrieve the BoundWitness Diviner for\n * @returns The BoundWitness Diviner for the specified store\n */\n protected async getBoundWitnessDivinerForStore(store: ConfigStore) {\n const name = assertEx(this.config?.[store]?.boundWitnessDiviner, () => `${moduleName}: Config for ${store}.boundWitnessDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.boundWitnessDiviner [${name}]`)\n return DivinerWrapper.wrap(mod, this.account)\n }\n\n /**\n * Gets the Diviner for the supplied Indexing Diviner stage\n * @param transform The Indexing Diviner stage\n * @returns The diviner corresponding to the Indexing Diviner stage\n */\n protected async getIndexingDivinerStage(transform: IndexingDivinerStage) {\n const nameOrAddress = assertEx(\n this.config?.indexingDivinerStages?.[transform],\n () => `${moduleName}: Config for indexingDivinerStages.${transform} not specified`,\n )\n const mod = await this.resolve(nameOrAddress)\n return assertEx(asDivinerInstance(mod), () => `${moduleName}: Failed to resolve indexing diviner stage for ${transform}`) as DivinerInstance\n }\n\n /**\n * Retrieves the Payload Diviner for the specified store\n * @param store The store to retrieve the Payload Diviner for\n * @returns The Payload Diviner for the specified store\n */\n protected async getPayloadDivinerForStore(store: ConfigStore) {\n const name = assertEx(this.config?.[store]?.payloadDiviner, () => `${moduleName}: Config for ${store}.payloadDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.payloadDiviner [${name}]`)\n return DivinerWrapper.wrap(mod, this.account)\n }\n\n /**\n * Retrieves the last state of the Diviner process. Used to recover state after\n * preemptions, reboots, etc.\n */\n protected async retrieveState(): Promise<ModuleState<IndexingDivinerState> | undefined> {\n if (this._lastState) return this._lastState\n let hash: Hash = ''\n const diviner = await this.getBoundWitnessDivinerForStore('stateStore')\n const query = await new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })\n .fields({\n address: this.account.address,\n limit: 1,\n offset: 0,\n order: 'desc',\n payload_schemas: [ModuleStateSchema],\n })\n .build()\n const boundWitnesses = await diviner.divine([query])\n if (boundWitnesses.length > 0) {\n const boundWitness = boundWitnesses[0]\n if (isBoundWitness(boundWitness)) {\n // Find the index for this address in the BoundWitness that is a ModuleState\n hash = boundWitness.addresses\n .map((address, index) => ({ address, index }))\n .filter(({ address }) => address === this.account.address)\n // eslint-disable-next-line unicorn/no-array-reduce\n .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '' as Hash,\n )\n }\n }\n\n // If we able to located the last state\n if (hash !== '') {\n // Get last state\n const archivist = await this.getArchivistForStore('stateStore')\n const payload = (await archivist.get([hash])).find(isModuleState<IndexingDivinerState>)\n if (payload) {\n return payload as WithMeta<ModuleState<IndexingDivinerState>>\n }\n }\n return undefined\n }\n\n protected override async startHandler(): Promise<boolean> {\n await super.startHandler()\n this.poll()\n return true\n }\n\n protected override async stopHandler(_timeout?: number | undefined): Promise<boolean> {\n if (this._pollId) {\n clearTimeout(this._pollId)\n this._pollId = undefined\n }\n return await super.stopHandler()\n }\n\n /**\n * Runs the background divine process on a loop with a delay\n * specified by the `config.pollFrequency`\n */\n private poll() {\n this._pollId = setTimeoutEx(async () => {\n try {\n await Promise.resolve()\n await this.backgroundDivine()\n } catch (e) {\n console.log(e)\n } finally {\n if (this._pollId) clearTimeoutEx(this._pollId)\n this._pollId = undefined\n this.poll()\n }\n }, this.pollFrequency)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;ACAA,oBAAyB;AAEzB,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VWD,kBAUcoG,sBAAqB;AAC9C,cAXWpG,kBAWcqG,iBAA0B;KAAI,+CAAMA;EAAeC;;AAC5E,cAZWtG,kBAYcuG,uBAA8BD;AAZlD,IAAMtG,kBAAN;;;;","names":["moduleName","IndexingDiviner","AbstractDiviner","_lastState","_pollId","payloadDivinerLimit","config","pollFrequency","backgroundDivine","lastState","retrieveState","indexCandidateDiviner","getIndexingDivinerStage","results","divine","nextState","find","isModuleStateWithMeta","indexCandidates","filter","x","toIndexTransformDiviner","indexes","indexArchivist","getArchivistForStore","insert","commitState","state","offset","archivist","bw","BoundWitnessBuilder","payload","signer","account","build","divineHandler","payloads","indexPayloadDiviner","getPayloadDivinerForStore","divinerQueryToIndexQueryDiviner","indexQueryResponseToDivinerQueryResponseDiviner","Promise","all","map","indexQuery","indexedResults","response","flat","indexedResult","result","PayloadBuilder","store","name","assertEx","mod","resolve","ArchivistWrapper","wrap","getBoundWitnessDivinerForStore","boundWitnessDiviner","DivinerWrapper","transform","nameOrAddress","indexingDivinerStages","asDivinerInstance","payloadDiviner","hash","diviner","query","schema","BoundWitnessDivinerQuerySchema","fields","address","limit","order","payload_schemas","ModuleStateSchema","boundWitnesses","length","boundWitness","isBoundWitness","addresses","index","reduce","prev","curr","payload_hashes","get","isModuleState","undefined","startHandler","poll","stopHandler","_timeout","clearTimeout","setTimeoutEx","e","console","log","clearTimeoutEx","allowRandomAccount","configSchemas","IndexingDivinerConfigSchema","defaultConfigSchema"]}
@@ -1,2 +1,228 @@
1
- var m=Object.defineProperty;var b=Object.getPrototypeOf;var F=Reflect.get;var I=(r,i,e)=>i in r?m(r,i,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[i]=e;var h=(r,i)=>m(r,"name",{value:i,configurable:!0});var v=(r,i,e)=>I(r,typeof i!="symbol"?i+"":i,e);var g=(r,i,e)=>F(b(r),e,i);import{assertEx as d}from"@xylabs/assert";import{clearTimeoutEx as W,setTimeoutEx as T}from"@xylabs/timer";import{ArchivistWrapper as A}from"@xyo-network/archivist-wrapper";import{BoundWitnessBuilder as C}from"@xyo-network/boundwitness-builder";import{isBoundWitness as P}from"@xyo-network/boundwitness-model";import{AbstractDiviner as Q}from"@xyo-network/diviner-abstract";import{BoundWitnessDivinerQuerySchema as R}from"@xyo-network/diviner-boundwitness-model";import{IndexingDivinerConfigSchema as w}from"@xyo-network/diviner-indexing-model";import{asDivinerInstance as B}from"@xyo-network/diviner-model";import{DivinerWrapper as y}from"@xyo-network/diviner-wrapper";import{creatableModule as H,isModuleState as M,isModuleStateWithMeta as S,ModuleStateSchema as D}from"@xyo-network/module-model";import{PayloadBuilder as x}from"@xyo-network/payload-builder";function _(r,i,e,a){var n=arguments.length,t=n<3?i:a===null?a=Object.getOwnPropertyDescriptor(i,e):a,s;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")t=Reflect.decorate(r,i,e,a);else for(var o=r.length-1;o>=0;o--)(s=r[o])&&(t=(n<3?s(t):n>3?s(i,e,t):s(i,e))||t);return n>3&&t&&Object.defineProperty(i,e,t),t}h(_,"_ts_decorate");var l="IndexingDiviner",c=class c extends Q{_lastState;_pollId;get payloadDivinerLimit(){return this.config.payloadDivinerLimit??1e3}get pollFrequency(){return this.config.pollFrequency??1e4}backgroundDivine=h(async()=>{let i=await this.retrieveState(),e=await this.getIndexingDivinerStage("stateToIndexCandidateDiviner"),a=i?await e.divine([i]):await e.divine(),n=a.find(S),t=a.filter(p=>!S(p)),o=await(await this.getIndexingDivinerStage("indexCandidateToIndexDiviner")).divine(t);await(await this.getArchivistForStore("indexStore")).insert(o),n&&await this.commitState(n)},"backgroundDivine");async commitState(i){var n;if(i.state.offset===((n=this._lastState)==null?void 0:n.state.offset))return;this._lastState=i;let e=await this.getArchivistForStore("stateStore"),[a]=await new C().payload(i).signer(this.account).build();await e.insert([a,i])}async divineHandler(i=[]){let e=await this.getPayloadDivinerForStore("indexStore"),a=await this.getIndexingDivinerStage("divinerQueryToIndexQueryDiviner"),n=await this.getIndexingDivinerStage("indexQueryResponseToDivinerQueryResponseDiviner"),t=(await Promise.all(i.map(async s=>{let o=await a.divine([s]),f=await e.divine(o);return(await Promise.all(f.flat().map($=>n.divine([s,$])))).flat()}))).flat();return await Promise.all(t.map(s=>x.build(s)))}async getArchivistForStore(i){var n,t;let e=d((t=(n=this.config)==null?void 0:n[i])==null?void 0:t.archivist,()=>`${l}: Config for ${i}.archivist not specified`),a=d(await this.resolve(e),()=>`${l}: Failed to resolve ${i}.archivist [${e}]`);return A.wrap(a,this.account)}async getBoundWitnessDivinerForStore(i){var n,t;let e=d((t=(n=this.config)==null?void 0:n[i])==null?void 0:t.boundWitnessDiviner,()=>`${l}: Config for ${i}.boundWitnessDiviner not specified`),a=d(await this.resolve(e),()=>`${l}: Failed to resolve ${i}.boundWitnessDiviner [${e}]`);return y.wrap(a,this.account)}async getIndexingDivinerStage(i){var n,t;let e=d((t=(n=this.config)==null?void 0:n.indexingDivinerStages)==null?void 0:t[i],()=>`${l}: Config for indexingDivinerStages.${i} not specified`),a=await this.resolve(e);return d(B(a),()=>`${l}: Failed to resolve indexing diviner stage for ${i}`)}async getPayloadDivinerForStore(i){var n,t;let e=d((t=(n=this.config)==null?void 0:n[i])==null?void 0:t.payloadDiviner,()=>`${l}: Config for ${i}.payloadDiviner not specified`),a=d(await this.resolve(e),()=>`${l}: Failed to resolve ${i}.payloadDiviner [${e}]`);return y.wrap(a,this.account)}async retrieveState(){if(this._lastState)return this._lastState;let i="",e=await this.getBoundWitnessDivinerForStore("stateStore"),a=await new x({schema:R}).fields({address:this.account.address,limit:1,offset:0,order:"desc",payload_schemas:[D]}).build(),n=await e.divine([a]);if(n.length>0){let t=n[0];P(t)&&(i=t.addresses.map((s,o)=>({address:s,index:o})).filter(({address:s})=>s===this.account.address).reduce((s,o)=>{var f;return((f=t.payload_schemas)==null?void 0:f[o==null?void 0:o.index])===D?t.payload_hashes[o==null?void 0:o.index]:s},""))}if(i!==""){let s=(await(await this.getArchivistForStore("stateStore")).get([i])).find(M);if(s)return s}}async startHandler(){return await super.startHandler(),this.poll(),!0}async stopHandler(i){return this._pollId&&(clearTimeout(this._pollId),this._pollId=void 0),await super.stopHandler()}poll(){this._pollId=T(async()=>{try{await Promise.resolve(),await this.backgroundDivine()}catch(i){console.log(i)}finally{this._pollId&&W(this._pollId),this._pollId=void 0,this.poll()}},this.pollFrequency)}};h(c,"IndexingDiviner"),v(c,"allowRandomAccount",!1),v(c,"configSchemas",[...g(c,c,"configSchemas"),w]),v(c,"defaultConfigSchema",w);var u=c;u=_([H()],u);export{u as IndexingDiviner};
1
+ var __defProp = Object.defineProperty;
2
+ var __getProtoOf = Object.getPrototypeOf;
3
+ var __reflectGet = Reflect.get;
4
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+ var __superGet = (cls, obj, key) => __reflectGet(__getProtoOf(cls), key, obj);
8
+
9
+ // src/Diviner.ts
10
+ import { assertEx } from "@xylabs/assert";
11
+ import { clearTimeoutEx, setTimeoutEx } from "@xylabs/timer";
12
+ import { ArchivistWrapper } from "@xyo-network/archivist-wrapper";
13
+ import { BoundWitnessBuilder } from "@xyo-network/boundwitness-builder";
14
+ import { isBoundWitness } from "@xyo-network/boundwitness-model";
15
+ import { AbstractDiviner } from "@xyo-network/diviner-abstract";
16
+ import { BoundWitnessDivinerQuerySchema } from "@xyo-network/diviner-boundwitness-model";
17
+ import { IndexingDivinerConfigSchema } from "@xyo-network/diviner-indexing-model";
18
+ import { asDivinerInstance } from "@xyo-network/diviner-model";
19
+ import { DivinerWrapper } from "@xyo-network/diviner-wrapper";
20
+ import { creatableModule, isModuleState, isModuleStateWithMeta, ModuleStateSchema } from "@xyo-network/module-model";
21
+ import { PayloadBuilder } from "@xyo-network/payload-builder";
22
+ function _ts_decorate(decorators, target, key, desc) {
23
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
24
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
25
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
26
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
27
+ }
28
+ __name(_ts_decorate, "_ts_decorate");
29
+ var moduleName = "IndexingDiviner";
30
+ var _IndexingDiviner = class _IndexingDiviner extends AbstractDiviner {
31
+ _lastState;
32
+ _pollId;
33
+ get payloadDivinerLimit() {
34
+ return this.config.payloadDivinerLimit ?? 1e3;
35
+ }
36
+ get pollFrequency() {
37
+ return this.config.pollFrequency ?? 1e4;
38
+ }
39
+ /**
40
+ * Works via batched iteration of the source archivist to populate the index.
41
+ * @returns A promise that resolves when the background process is complete
42
+ */
43
+ backgroundDivine = /* @__PURE__ */ __name(async () => {
44
+ const lastState = await this.retrieveState();
45
+ const indexCandidateDiviner = await this.getIndexingDivinerStage("stateToIndexCandidateDiviner");
46
+ const results = lastState ? await indexCandidateDiviner.divine([
47
+ lastState
48
+ ]) : await indexCandidateDiviner.divine();
49
+ const nextState = results.find(isModuleStateWithMeta);
50
+ const indexCandidates = results.filter((x) => !isModuleStateWithMeta(x));
51
+ const toIndexTransformDiviner = await this.getIndexingDivinerStage("indexCandidateToIndexDiviner");
52
+ const indexes = await toIndexTransformDiviner.divine(indexCandidates);
53
+ const indexArchivist = await this.getArchivistForStore("indexStore");
54
+ await indexArchivist.insert(indexes);
55
+ if (nextState) {
56
+ await this.commitState(nextState);
57
+ }
58
+ }, "backgroundDivine");
59
+ /**
60
+ * Commit the internal state of the Diviner process. This is similar
61
+ * to a transaction completion in a database and should only be called
62
+ * when results have been successfully persisted to the appropriate
63
+ * external stores.
64
+ * @param nextState The state to commit
65
+ */
66
+ async commitState(nextState) {
67
+ var _a;
68
+ if (nextState.state.offset === ((_a = this._lastState) == null ? void 0 : _a.state.offset)) return;
69
+ this._lastState = nextState;
70
+ const archivist = await this.getArchivistForStore("stateStore");
71
+ const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build();
72
+ await archivist.insert([
73
+ bw,
74
+ nextState
75
+ ]);
76
+ }
77
+ async divineHandler(payloads = []) {
78
+ const indexPayloadDiviner = await this.getPayloadDivinerForStore("indexStore");
79
+ const divinerQueryToIndexQueryDiviner = await this.getIndexingDivinerStage("divinerQueryToIndexQueryDiviner");
80
+ const indexQueryResponseToDivinerQueryResponseDiviner = await this.getIndexingDivinerStage("indexQueryResponseToDivinerQueryResponseDiviner");
81
+ const results = (await Promise.all(payloads.map(async (payload) => {
82
+ const indexQuery = await divinerQueryToIndexQueryDiviner.divine([
83
+ payload
84
+ ]);
85
+ const indexedResults = await indexPayloadDiviner.divine(indexQuery);
86
+ const response = await Promise.all(indexedResults.flat().map((indexedResult) => indexQueryResponseToDivinerQueryResponseDiviner.divine([
87
+ payload,
88
+ indexedResult
89
+ ])));
90
+ return response.flat();
91
+ }))).flat();
92
+ return await Promise.all(results.map((result) => PayloadBuilder.build(result)));
93
+ }
94
+ /**
95
+ * Retrieves the archivist for the specified store
96
+ * @param store The store to retrieve the archivist for
97
+ * @returns The archivist for the specified store
98
+ */
99
+ async getArchivistForStore(store) {
100
+ var _a, _b;
101
+ const name = assertEx((_b = (_a = this.config) == null ? void 0 : _a[store]) == null ? void 0 : _b.archivist, () => `${moduleName}: Config for ${store}.archivist not specified`);
102
+ const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.archivist [${name}]`);
103
+ return ArchivistWrapper.wrap(mod, this.account);
104
+ }
105
+ /**
106
+ * Retrieves the BoundWitness Diviner for the specified store
107
+ * @param store The store to retrieve the BoundWitness Diviner for
108
+ * @returns The BoundWitness Diviner for the specified store
109
+ */
110
+ async getBoundWitnessDivinerForStore(store) {
111
+ var _a, _b;
112
+ const name = assertEx((_b = (_a = this.config) == null ? void 0 : _a[store]) == null ? void 0 : _b.boundWitnessDiviner, () => `${moduleName}: Config for ${store}.boundWitnessDiviner not specified`);
113
+ const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.boundWitnessDiviner [${name}]`);
114
+ return DivinerWrapper.wrap(mod, this.account);
115
+ }
116
+ /**
117
+ * Gets the Diviner for the supplied Indexing Diviner stage
118
+ * @param transform The Indexing Diviner stage
119
+ * @returns The diviner corresponding to the Indexing Diviner stage
120
+ */
121
+ async getIndexingDivinerStage(transform) {
122
+ var _a, _b;
123
+ const nameOrAddress = assertEx((_b = (_a = this.config) == null ? void 0 : _a.indexingDivinerStages) == null ? void 0 : _b[transform], () => `${moduleName}: Config for indexingDivinerStages.${transform} not specified`);
124
+ const mod = await this.resolve(nameOrAddress);
125
+ return assertEx(asDivinerInstance(mod), () => `${moduleName}: Failed to resolve indexing diviner stage for ${transform}`);
126
+ }
127
+ /**
128
+ * Retrieves the Payload Diviner for the specified store
129
+ * @param store The store to retrieve the Payload Diviner for
130
+ * @returns The Payload Diviner for the specified store
131
+ */
132
+ async getPayloadDivinerForStore(store) {
133
+ var _a, _b;
134
+ const name = assertEx((_b = (_a = this.config) == null ? void 0 : _a[store]) == null ? void 0 : _b.payloadDiviner, () => `${moduleName}: Config for ${store}.payloadDiviner not specified`);
135
+ const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.payloadDiviner [${name}]`);
136
+ return DivinerWrapper.wrap(mod, this.account);
137
+ }
138
+ /**
139
+ * Retrieves the last state of the Diviner process. Used to recover state after
140
+ * preemptions, reboots, etc.
141
+ */
142
+ async retrieveState() {
143
+ if (this._lastState) return this._lastState;
144
+ let hash = "";
145
+ const diviner = await this.getBoundWitnessDivinerForStore("stateStore");
146
+ const query = await new PayloadBuilder({
147
+ schema: BoundWitnessDivinerQuerySchema
148
+ }).fields({
149
+ address: this.account.address,
150
+ limit: 1,
151
+ offset: 0,
152
+ order: "desc",
153
+ payload_schemas: [
154
+ ModuleStateSchema
155
+ ]
156
+ }).build();
157
+ const boundWitnesses = await diviner.divine([
158
+ query
159
+ ]);
160
+ if (boundWitnesses.length > 0) {
161
+ const boundWitness = boundWitnesses[0];
162
+ if (isBoundWitness(boundWitness)) {
163
+ hash = boundWitness.addresses.map((address, index) => ({
164
+ address,
165
+ index
166
+ })).filter(({ address }) => address === this.account.address).reduce((prev, curr) => {
167
+ var _a;
168
+ return ((_a = boundWitness.payload_schemas) == null ? void 0 : _a[curr == null ? void 0 : curr.index]) === ModuleStateSchema ? boundWitness.payload_hashes[curr == null ? void 0 : curr.index] : prev;
169
+ }, "");
170
+ }
171
+ }
172
+ if (hash !== "") {
173
+ const archivist = await this.getArchivistForStore("stateStore");
174
+ const payload = (await archivist.get([
175
+ hash
176
+ ])).find(isModuleState);
177
+ if (payload) {
178
+ return payload;
179
+ }
180
+ }
181
+ return void 0;
182
+ }
183
+ async startHandler() {
184
+ await super.startHandler();
185
+ this.poll();
186
+ return true;
187
+ }
188
+ async stopHandler(_timeout) {
189
+ if (this._pollId) {
190
+ clearTimeout(this._pollId);
191
+ this._pollId = void 0;
192
+ }
193
+ return await super.stopHandler();
194
+ }
195
+ /**
196
+ * Runs the background divine process on a loop with a delay
197
+ * specified by the `config.pollFrequency`
198
+ */
199
+ poll() {
200
+ this._pollId = setTimeoutEx(async () => {
201
+ try {
202
+ await Promise.resolve();
203
+ await this.backgroundDivine();
204
+ } catch (e) {
205
+ console.log(e);
206
+ } finally {
207
+ if (this._pollId) clearTimeoutEx(this._pollId);
208
+ this._pollId = void 0;
209
+ this.poll();
210
+ }
211
+ }, this.pollFrequency);
212
+ }
213
+ };
214
+ __name(_IndexingDiviner, "IndexingDiviner");
215
+ __publicField(_IndexingDiviner, "allowRandomAccount", false);
216
+ __publicField(_IndexingDiviner, "configSchemas", [
217
+ ...__superGet(_IndexingDiviner, _IndexingDiviner, "configSchemas"),
218
+ IndexingDivinerConfigSchema
219
+ ]);
220
+ __publicField(_IndexingDiviner, "defaultConfigSchema", IndexingDivinerConfigSchema);
221
+ var IndexingDiviner = _IndexingDiviner;
222
+ IndexingDiviner = _ts_decorate([
223
+ creatableModule()
224
+ ], IndexingDiviner);
225
+ export {
226
+ IndexingDiviner
227
+ };
2
228
  //# sourceMappingURL=index.js.map