@xyo-network/diviner-temporal-indexing-memory 3.6.0-rc.4 → 3.6.0-rc.6

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,14 +1,14 @@
1
+ import { filterAs } from '@xylabs/array'
1
2
  import { assertEx } from '@xylabs/assert'
2
3
  import { exists } from '@xylabs/exists'
3
- import type { Hex } from '@xylabs/hex'
4
- import type { ArchivistInstance } from '@xyo-network/archivist-model'
4
+ import type { ArchivistInstance, ArchivistNextOptions } from '@xyo-network/archivist-model'
5
5
  import { ArchivistWrapper } from '@xyo-network/archivist-wrapper'
6
6
  import type { BoundWitness } from '@xyo-network/boundwitness-model'
7
- import { isBoundWitness } from '@xyo-network/boundwitness-model'
7
+ import { asBoundWitness, isBoundWitness } from '@xyo-network/boundwitness-model'
8
+ import { payloadSchemasContainsAll } from '@xyo-network/boundwitness-validator'
8
9
  import { AbstractDiviner } from '@xyo-network/diviner-abstract'
9
10
  import type { BoundWitnessDiviner } from '@xyo-network/diviner-boundwitness-abstract'
10
11
  import type { BoundWitnessDivinerParams, BoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'
11
- import { BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'
12
12
  import type { IndexingDivinerState } from '@xyo-network/diviner-indexing-model'
13
13
  import type { TemporalIndexingDivinerStateToIndexCandidateDivinerParams } from '@xyo-network/diviner-temporal-indexing-model'
14
14
  import { TemporalIndexingDivinerStateToIndexCandidateDivinerConfigSchema } from '@xyo-network/diviner-temporal-indexing-model'
@@ -17,12 +17,11 @@ import type {
17
17
  Labels, ModuleIdentifier, ModuleState,
18
18
  } from '@xyo-network/module-model'
19
19
  import { isModuleState, ModuleStateSchema } from '@xyo-network/module-model'
20
- import { PayloadBuilder } from '@xyo-network/payload-builder'
21
20
  import type {
22
21
  Payload, Schema,
23
22
  WithStorageMeta,
24
23
  } from '@xyo-network/payload-model'
25
- import { StorageMetaConstants } from '@xyo-network/payload-model'
24
+ import { SequenceConstants } from '@xyo-network/payload-model'
26
25
  import { intraBoundwitnessSchemaCombinations } from '@xyo-network/payload-utils'
27
26
  import type { TimeStamp } from '@xyo-network/witness-timestamp'
28
27
  import { TimestampSchema } from '@xyo-network/witness-timestamp'
@@ -81,31 +80,35 @@ export class TemporalIndexingDivinerStateToIndexCandidateDiviner<
81
80
  return [TimestampSchema, ...(schemas ?? [])]
82
81
  }
83
82
 
84
- protected override async divineHandler(payloads: Payload[] = []): Promise<[ModuleState, ...IndexCandidate[]]> {
83
+ protected override async divineHandler(payloads: Payload[] = []): Promise<TemporalStateToIndexCandidateDivinerResponse> {
85
84
  // Retrieve the last state from what was passed in
86
85
  const lastState = payloads.find(isModuleState<IndexingDivinerState>)
87
86
  // If there is no last state, start from the beginning
88
- if (!lastState) return [{ schema: ModuleStateSchema, state: { cursor: StorageMetaConstants.minLocalSequence } }]
89
- // Otherwise, get the last offset
90
- const { cursor } = lastState.state
91
- // Get next batch of results starting from the offset
92
- const boundWitnessDiviner = await this.getBoundWitnessDivinerForStore()
93
- if (!boundWitnessDiviner) return [lastState]
94
- const query = new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })
95
- .fields({
96
- limit: this.payloadDivinerLimit, cursor: cursor as Hex, order, payload_schemas: this.payload_schemas,
97
- })
98
- .build()
99
- const batch = await boundWitnessDiviner.divine([query])
100
- if (batch.length === 0) return [lastState]
101
- // Get source data
87
+ ?? { schema: ModuleStateSchema, state: { cursor: SequenceConstants.minLocalSequence } }
88
+
89
+ // Get the last cursor
90
+ const cursor = lastState?.state?.cursor
91
+ // Get the archivist for the store
102
92
  const sourceArchivist = await this.getArchivistForStore()
103
93
  if (!sourceArchivist) return [lastState]
94
+
95
+ // Get the next batch of results
96
+ const nextOffset: ArchivistNextOptions = { limit: this.payloadDivinerLimit, order }
97
+ // Only use the cursor if it's a valid offset
98
+ if (cursor !== SequenceConstants.minLocalSequence) nextOffset.cursor = cursor
99
+ // Get next batch of results starting from the offset
100
+ const next = await sourceArchivist.next(nextOffset)
101
+ if (next.length === 0) return [lastState]
102
+ const batch = filterAs(next, asBoundWitness)
103
+ .filter(exists)
104
+ .filter(bw => payloadSchemasContainsAll(bw, this.payload_schemas))
105
+ // Get source data
104
106
  const bws = batch.filter(isBoundWitness)
105
107
  const indexCandidates: IndexCandidate[] = (await Promise.all(bws.map(bw => this.getPayloadsInBoundWitness(bw, sourceArchivist))))
106
108
  .filter(exists)
107
109
  .flat()
108
- const nextState = { schema: ModuleStateSchema, state: { ...lastState.state, cursor: batch.at(-1)?._sequence } }
110
+ const nextCursor = assertEx(next.at(-1)?._sequence, () => `${moduleName}: Expected next to have a sequence`)
111
+ const nextState: ModuleState<IndexingDivinerState> = { schema: ModuleStateSchema, state: { ...lastState.state, cursor: nextCursor } }
109
112
  return [nextState, ...indexCandidates]
110
113
  }
111
114