@xyo-network/diviner-temporal-indexing-memory 2.84.7 → 2.84.9
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.
- package/dist/browser/DivinerQueryToIndexQueryDiviner/Diviner.d.cts.map +1 -1
- package/dist/browser/DivinerQueryToIndexQueryDiviner/Diviner.d.mts.map +1 -1
- package/dist/browser/DivinerQueryToIndexQueryDiviner/Diviner.d.ts.map +1 -1
- package/dist/browser/IndexCandidateToIndexDiviner/Diviner.d.cts.map +1 -1
- package/dist/browser/IndexCandidateToIndexDiviner/Diviner.d.mts.map +1 -1
- package/dist/browser/IndexCandidateToIndexDiviner/Diviner.d.ts.map +1 -1
- package/dist/browser/index.cjs +14 -61
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.js +12 -49
- package/dist/browser/index.js.map +1 -1
- package/dist/node/DivinerQueryToIndexQueryDiviner/Diviner.d.cts.map +1 -1
- package/dist/node/DivinerQueryToIndexQueryDiviner/Diviner.d.mts.map +1 -1
- package/dist/node/DivinerQueryToIndexQueryDiviner/Diviner.d.ts.map +1 -1
- package/dist/node/IndexCandidateToIndexDiviner/Diviner.d.cts.map +1 -1
- package/dist/node/IndexCandidateToIndexDiviner/Diviner.d.mts.map +1 -1
- package/dist/node/IndexCandidateToIndexDiviner/Diviner.d.ts.map +1 -1
- package/dist/node/index.cjs +14 -61
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +12 -49
- package/dist/node/index.js.map +1 -1
- package/package.json +27 -28
- package/src/DivinerQueryToIndexQueryDiviner/Diviner.ts +2 -3
- package/src/IndexCandidateToIndexDiviner/Diviner.ts +1 -2
- package/dist/browser/IndexCandidateToIndexDiviner/_Diviner.d.cts +0 -47
- package/dist/browser/IndexCandidateToIndexDiviner/_Diviner.d.cts.map +0 -1
- package/dist/browser/IndexCandidateToIndexDiviner/_Diviner.d.mts +0 -47
- package/dist/browser/IndexCandidateToIndexDiviner/_Diviner.d.mts.map +0 -1
- package/dist/browser/IndexCandidateToIndexDiviner/_Diviner.d.ts +0 -47
- package/dist/browser/IndexCandidateToIndexDiviner/_Diviner.d.ts.map +0 -1
- package/dist/browser/jsonpath/index.d.cts +0 -4
- package/dist/browser/jsonpath/index.d.cts.map +0 -1
- package/dist/browser/jsonpath/index.d.mts +0 -4
- package/dist/browser/jsonpath/index.d.mts.map +0 -1
- package/dist/browser/jsonpath/index.d.ts +0 -4
- package/dist/browser/jsonpath/index.d.ts.map +0 -1
- package/dist/browser/jsonpath/jsonPathToTransformersDictionary.d.cts +0 -10
- package/dist/browser/jsonpath/jsonPathToTransformersDictionary.d.cts.map +0 -1
- package/dist/browser/jsonpath/jsonPathToTransformersDictionary.d.mts +0 -10
- package/dist/browser/jsonpath/jsonPathToTransformersDictionary.d.mts.map +0 -1
- package/dist/browser/jsonpath/jsonPathToTransformersDictionary.d.ts +0 -10
- package/dist/browser/jsonpath/jsonPathToTransformersDictionary.d.ts.map +0 -1
- package/dist/browser/jsonpath/reducePayloads.d.cts +0 -27
- package/dist/browser/jsonpath/reducePayloads.d.cts.map +0 -1
- package/dist/browser/jsonpath/reducePayloads.d.mts +0 -27
- package/dist/browser/jsonpath/reducePayloads.d.mts.map +0 -1
- package/dist/browser/jsonpath/reducePayloads.d.ts +0 -27
- package/dist/browser/jsonpath/reducePayloads.d.ts.map +0 -1
- package/dist/browser/jsonpath/toPayloadTransformer.d.cts +0 -8
- package/dist/browser/jsonpath/toPayloadTransformer.d.cts.map +0 -1
- package/dist/browser/jsonpath/toPayloadTransformer.d.mts +0 -8
- package/dist/browser/jsonpath/toPayloadTransformer.d.mts.map +0 -1
- package/dist/browser/jsonpath/toPayloadTransformer.d.ts +0 -8
- package/dist/browser/jsonpath/toPayloadTransformer.d.ts.map +0 -1
- package/dist/node/IndexCandidateToIndexDiviner/_Diviner.d.cts +0 -47
- package/dist/node/IndexCandidateToIndexDiviner/_Diviner.d.cts.map +0 -1
- package/dist/node/IndexCandidateToIndexDiviner/_Diviner.d.mts +0 -47
- package/dist/node/IndexCandidateToIndexDiviner/_Diviner.d.mts.map +0 -1
- package/dist/node/IndexCandidateToIndexDiviner/_Diviner.d.ts +0 -47
- package/dist/node/IndexCandidateToIndexDiviner/_Diviner.d.ts.map +0 -1
- package/dist/node/jsonpath/index.d.cts +0 -4
- package/dist/node/jsonpath/index.d.cts.map +0 -1
- package/dist/node/jsonpath/index.d.mts +0 -4
- package/dist/node/jsonpath/index.d.mts.map +0 -1
- package/dist/node/jsonpath/index.d.ts +0 -4
- package/dist/node/jsonpath/index.d.ts.map +0 -1
- package/dist/node/jsonpath/jsonPathToTransformersDictionary.d.cts +0 -10
- package/dist/node/jsonpath/jsonPathToTransformersDictionary.d.cts.map +0 -1
- package/dist/node/jsonpath/jsonPathToTransformersDictionary.d.mts +0 -10
- package/dist/node/jsonpath/jsonPathToTransformersDictionary.d.mts.map +0 -1
- package/dist/node/jsonpath/jsonPathToTransformersDictionary.d.ts +0 -10
- package/dist/node/jsonpath/jsonPathToTransformersDictionary.d.ts.map +0 -1
- package/dist/node/jsonpath/reducePayloads.d.cts +0 -27
- package/dist/node/jsonpath/reducePayloads.d.cts.map +0 -1
- package/dist/node/jsonpath/reducePayloads.d.mts +0 -27
- package/dist/node/jsonpath/reducePayloads.d.mts.map +0 -1
- package/dist/node/jsonpath/reducePayloads.d.ts +0 -27
- package/dist/node/jsonpath/reducePayloads.d.ts.map +0 -1
- package/dist/node/jsonpath/toPayloadTransformer.d.cts +0 -8
- package/dist/node/jsonpath/toPayloadTransformer.d.cts.map +0 -1
- package/dist/node/jsonpath/toPayloadTransformer.d.mts +0 -8
- package/dist/node/jsonpath/toPayloadTransformer.d.mts.map +0 -1
- package/dist/node/jsonpath/toPayloadTransformer.d.ts +0 -8
- package/dist/node/jsonpath/toPayloadTransformer.d.ts.map +0 -1
- package/src/IndexCandidateToIndexDiviner/_Diviner.ts +0 -134
- package/src/jsonpath/index.ts +0 -3
- package/src/jsonpath/jsonPathToTransformersDictionary.ts +0 -21
- package/src/jsonpath/reducePayloads.ts +0 -48
- package/src/jsonpath/toPayloadTransformer.ts +0 -23
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import { containsAll } from '@xylabs/array'
|
|
2
|
-
import { assertEx } from '@xylabs/assert'
|
|
3
|
-
import { exists } from '@xylabs/exists'
|
|
4
|
-
import { AbstractDiviner } from '@xyo-network/abstract-diviner'
|
|
5
|
-
import { BoundWitness, isBoundWitness } from '@xyo-network/boundwitness-model'
|
|
6
|
-
import { DivinerConfigSchema } from '@xyo-network/diviner-model'
|
|
7
|
-
import {
|
|
8
|
-
SchemaToJsonPathTransformExpressionsDictionary,
|
|
9
|
-
SchemaToPayloadTransformersDictionary,
|
|
10
|
-
TemporalIndexingDivinerIndexCandidateToIndexDivinerConfigSchema,
|
|
11
|
-
TemporalIndexingDivinerIndexCandidateToIndexDivinerParams,
|
|
12
|
-
TemporalIndexingDivinerResultIndex,
|
|
13
|
-
TemporalIndexingDivinerResultIndexSchema,
|
|
14
|
-
} from '@xyo-network/diviner-temporal-indexing-model'
|
|
15
|
-
import { PayloadHasher } from '@xyo-network/hash'
|
|
16
|
-
import { Labels } from '@xyo-network/module-model'
|
|
17
|
-
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
18
|
-
import { Payload, PayloadFields } from '@xyo-network/payload-model'
|
|
19
|
-
import { isTimestamp, TimeStamp, TimestampSchema } from '@xyo-network/witness-timestamp'
|
|
20
|
-
|
|
21
|
-
import { jsonPathToTransformersDictionary } from '../jsonpath'
|
|
22
|
-
|
|
23
|
-
export type IndexablePayloads = [BoundWitness, TimeStamp, ...Payload[]]
|
|
24
|
-
|
|
25
|
-
const moduleName = 'TemporalIndexingDivinerIndexCandidateToIndexDiviner'
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Diviner which transforms index candidates to indexes using JSON Path to map
|
|
29
|
-
* source properties in the supplied payloads to destination fields in the
|
|
30
|
-
* resultant index
|
|
31
|
-
*/
|
|
32
|
-
export class TemporalIndexingDivinerIndexCandidateToIndexDiviner<
|
|
33
|
-
TParams extends TemporalIndexingDivinerIndexCandidateToIndexDivinerParams = TemporalIndexingDivinerIndexCandidateToIndexDivinerParams,
|
|
34
|
-
> extends AbstractDiviner<TParams> {
|
|
35
|
-
static override configSchema = TemporalIndexingDivinerIndexCandidateToIndexDivinerConfigSchema
|
|
36
|
-
static override configSchemas = [DivinerConfigSchema, TemporalIndexingDivinerIndexCandidateToIndexDivinerConfigSchema]
|
|
37
|
-
static labels: Labels = {
|
|
38
|
-
'network.xyo.diviner.stage': 'indexCandidateToIndexDiviner',
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
private _indexableSchemas: string[] | undefined
|
|
42
|
-
private _payloadTransformers: SchemaToPayloadTransformersDictionary | undefined
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* List of indexable schemas for this diviner
|
|
46
|
-
*/
|
|
47
|
-
protected get indexableSchemas(): string[] {
|
|
48
|
-
if (!this._indexableSchemas) this._indexableSchemas = Object.keys(this.schemaTransforms)
|
|
49
|
-
return this._indexableSchemas
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Dictionary of schemas to payload transformers for creating indexes
|
|
54
|
-
* from the payloads within a Bound Witness
|
|
55
|
-
*/
|
|
56
|
-
protected get payloadTransformers(): SchemaToPayloadTransformersDictionary {
|
|
57
|
-
if (!this._payloadTransformers) this._payloadTransformers = jsonPathToTransformersDictionary(this.schemaTransforms)
|
|
58
|
-
return this._payloadTransformers
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* The dictionary of schemas to JSON Path transform expressions for creating indexes
|
|
63
|
-
* from the payloads within a Bound Witness
|
|
64
|
-
*/
|
|
65
|
-
protected get schemaTransforms(): SchemaToJsonPathTransformExpressionsDictionary {
|
|
66
|
-
return assertEx(this.config?.schemaTransforms, () => `${moduleName}: Missing config.schemaTransforms section`)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
protected override async divineHandler(payloads: Payload[] = []): Promise<Payload[]> {
|
|
70
|
-
const bws: BoundWitness[] = payloads.filter(isBoundWitness)
|
|
71
|
-
const timestampPayloads: TimeStamp[] = payloads.filter(isTimestamp)
|
|
72
|
-
const indexablePayloads: Payload[] = payloads.filter((p) => this.isIndexablePayload(p))
|
|
73
|
-
if (bws.length > 0 && timestampPayloads.length > 0 && indexablePayloads.length > 0) {
|
|
74
|
-
const payloadDictionary = await PayloadHasher.toMap(payloads)
|
|
75
|
-
// eslint-disable-next-line unicorn/no-array-reduce
|
|
76
|
-
const validIndexableTuples: IndexablePayloads[] = bws.reduce<IndexablePayloads[]>((indexableTuples, bw) => {
|
|
77
|
-
// If this Bound Witness doesn't contain all the required schemas don't index it
|
|
78
|
-
if (!containsAll(bw.payload_schemas, this.indexableSchemas)) return indexableTuples
|
|
79
|
-
// Find the timestamp
|
|
80
|
-
const timestampPosition = bw.payload_schemas?.findIndex((schema) => schema === TimestampSchema)
|
|
81
|
-
const timestampHash = bw.payload_hashes?.[timestampPosition]
|
|
82
|
-
const timestamp = [payloadDictionary[timestampHash]].find(isTimestamp)
|
|
83
|
-
// Find the remaining indexable payloads
|
|
84
|
-
const indexablePayloadPositions = this.indexableSchemas.map((schema) => bw.payload_schemas.indexOf(schema))
|
|
85
|
-
const indexablePayloadHashes = indexablePayloadPositions.map((index) => bw.payload_hashes?.[index])
|
|
86
|
-
const indexablePayloads = indexablePayloadHashes.map((hash) => payloadDictionary[hash]).filter(exists)
|
|
87
|
-
// If we found a timestamp and the right amount of indexable payloads (of the
|
|
88
|
-
// correct schema as checked above) in this BW, then index it
|
|
89
|
-
if (timestamp && indexablePayloads.length === this.indexableSchemas.length) indexableTuples.push([bw, timestamp, ...indexablePayloads])
|
|
90
|
-
return indexableTuples
|
|
91
|
-
}, [])
|
|
92
|
-
// Create the indexes from the tuples
|
|
93
|
-
const indexes = await Promise.all(
|
|
94
|
-
validIndexableTuples.map<Promise<TemporalIndexingDivinerResultIndex>>(async ([bw, timestampPayload, ...sourcePayloads]) => {
|
|
95
|
-
// Use the payload transformers to convert the fields from the source payloads to the destination fields
|
|
96
|
-
const indexFields = sourcePayloads.flatMap<PayloadFields[]>((payload) => {
|
|
97
|
-
// Find the transformers for this payload
|
|
98
|
-
const transformers = this.payloadTransformers[payload.schema]
|
|
99
|
-
// If transformers exist, apply them to the payload otherwise return an empty array
|
|
100
|
-
return transformers ? transformers.map((transform) => transform(payload)) : []
|
|
101
|
-
})
|
|
102
|
-
// Extract the timestamp from the timestamp payload
|
|
103
|
-
const { timestamp } = timestampPayload
|
|
104
|
-
// Include all the sources for reference
|
|
105
|
-
const sources = Object.keys(await PayloadHasher.toMap([bw, timestampPayload, ...sourcePayloads]))
|
|
106
|
-
// Build and return the index
|
|
107
|
-
return await new PayloadBuilder<TemporalIndexingDivinerResultIndex>({ schema: TemporalIndexingDivinerResultIndexSchema })
|
|
108
|
-
.fields(Object.assign({ sources, timestamp }, ...indexFields))
|
|
109
|
-
.build()
|
|
110
|
-
}),
|
|
111
|
-
)
|
|
112
|
-
return indexes.flat()
|
|
113
|
-
}
|
|
114
|
-
return []
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Identifies if a payload is one that is indexed by this diviner
|
|
119
|
-
* @param x The candidate payload
|
|
120
|
-
* @returns True if the payload is one indexed by this diviner, false otherwise
|
|
121
|
-
*/
|
|
122
|
-
protected isIndexablePayload = (x: Payload) => {
|
|
123
|
-
return this.indexableSchemas.includes(x?.schema)
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Identifies if a schema is one that is indexed by this diviner
|
|
128
|
-
* @param schema The candidate schema
|
|
129
|
-
* @returns True if this schema is one indexed by this diviner, false otherwise
|
|
130
|
-
*/
|
|
131
|
-
protected isIndexableSchema = (schema?: string | null) => {
|
|
132
|
-
return typeof schema === 'string' ? this.indexableSchemas.includes(schema) : false
|
|
133
|
-
}
|
|
134
|
-
}
|
package/src/jsonpath/index.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { SchemaToJsonPathTransformExpressionsDictionary, SchemaToPayloadTransformersDictionary } from '@xyo-network/diviner-temporal-indexing-model'
|
|
2
|
-
|
|
3
|
-
import { toPayloadTransformer } from './toPayloadTransformer'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Materializes the JSON-path expressions into memoized functions by converting a
|
|
7
|
-
* dictionary of schema to JSON Path transform expressions to a dictionary
|
|
8
|
-
* of schema to payload transformers
|
|
9
|
-
* @param schemaTransforms The schema transforms to convert
|
|
10
|
-
* @returns A dictionary of schema to payload transformers
|
|
11
|
-
*/
|
|
12
|
-
export const jsonPathToTransformersDictionary = (
|
|
13
|
-
schemaTransforms: SchemaToJsonPathTransformExpressionsDictionary,
|
|
14
|
-
): SchemaToPayloadTransformersDictionary => {
|
|
15
|
-
return Object.fromEntries(
|
|
16
|
-
Object.entries(schemaTransforms).map(([schema, jsonPathTransformerExpressions]) => {
|
|
17
|
-
const transformers = jsonPathTransformerExpressions.map(toPayloadTransformer)
|
|
18
|
-
return [schema, transformers]
|
|
19
|
-
}),
|
|
20
|
-
)
|
|
21
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { SchemaToPayloadTransformersDictionary } from '@xyo-network/diviner-temporal-indexing-model'
|
|
2
|
-
import { PayloadHasher } from '@xyo-network/hash'
|
|
3
|
-
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
4
|
-
import { Payload, PayloadFields } from '@xyo-network/payload-model'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Reduces the payloads to a single payload using the supplied transformers
|
|
8
|
-
* @param payloads The payloads to reduce
|
|
9
|
-
* @param payloadTransformers The transformers to use to convert the payloads to the destination payload
|
|
10
|
-
* @param destinationSchema The schema of the destination payload
|
|
11
|
-
* @returns The reduced payload
|
|
12
|
-
*/
|
|
13
|
-
export const reducePayloads = async <T extends Payload = Payload>(
|
|
14
|
-
payloads: Payload[],
|
|
15
|
-
payloadTransformers: SchemaToPayloadTransformersDictionary,
|
|
16
|
-
destinationSchema: string,
|
|
17
|
-
): Promise<T> => {
|
|
18
|
-
// Use the payload transformers to convert the fields from the source payloads to the destination fields
|
|
19
|
-
const indexFields = payloads.flatMap<PayloadFields[]>((payload) => {
|
|
20
|
-
// Find the transformers for this payload
|
|
21
|
-
const transformers = payloadTransformers[payload.schema]
|
|
22
|
-
// If transformers exist, apply them to the payload otherwise return an empty array
|
|
23
|
-
return transformers ? transformers.map((transform) => transform(payload)) : []
|
|
24
|
-
})
|
|
25
|
-
// Include all the sources for reference
|
|
26
|
-
const sources = (await PayloadHasher.hashPairs([...payloads])).map(([, hash]) => hash)
|
|
27
|
-
// Build and return the index
|
|
28
|
-
return await new PayloadBuilder<T>({ schema: destinationSchema }).fields(Object.assign({ sources }, ...indexFields)).build()
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Reduces the arrays of payload arrays to an array of payloads using the supplied transformers
|
|
33
|
-
* @param payloadsArray The arrays of payloads to reduce
|
|
34
|
-
* @param payloadTransformers The transformers to use to convert the payloads to the destination payloads
|
|
35
|
-
* @param destinationSchema The schema of the destination payloads
|
|
36
|
-
* @returns The reduced payloads
|
|
37
|
-
*/
|
|
38
|
-
export const reducePayloadsArray = async <T extends Payload = Payload>(
|
|
39
|
-
payloadsArray: Payload[][],
|
|
40
|
-
payloadTransformers: SchemaToPayloadTransformersDictionary,
|
|
41
|
-
destinationSchema: string,
|
|
42
|
-
): Promise<T[]> => {
|
|
43
|
-
return await Promise.all(
|
|
44
|
-
payloadsArray.map(async (payloads) => {
|
|
45
|
-
return await reducePayloads<T>(payloads, payloadTransformers, destinationSchema)
|
|
46
|
-
}),
|
|
47
|
-
)
|
|
48
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { JsonPathTransformExpression, PayloadTransformer } from '@xyo-network/diviner-temporal-indexing-model'
|
|
2
|
-
import { Payload } from '@xyo-network/payload-model'
|
|
3
|
-
import jsonpath from 'jsonpath'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Converts a JSON Path transform expression to a payload transformer
|
|
7
|
-
* @param transformExpression The transform expression to convert
|
|
8
|
-
* @returns The payload transformer for the JSON Path transform expression
|
|
9
|
-
*/
|
|
10
|
-
export const toPayloadTransformer = (transformExpression: JsonPathTransformExpression) => {
|
|
11
|
-
const { defaultValue, destinationField, sourcePathExpression } = transformExpression
|
|
12
|
-
const transformer: PayloadTransformer = (x: Payload) => {
|
|
13
|
-
// eslint-disable-next-line import/no-named-as-default-member
|
|
14
|
-
const source = jsonpath.value(x, sourcePathExpression)
|
|
15
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
-
const transformed = {} as { [key: string]: any }
|
|
17
|
-
// Assign the source value to the destination field or the default value if the source is undefined
|
|
18
|
-
const destinationValue = source === undefined ? defaultValue : source
|
|
19
|
-
if (destinationValue !== undefined) transformed[destinationField] = destinationValue
|
|
20
|
-
return transformed
|
|
21
|
-
}
|
|
22
|
-
return transformer
|
|
23
|
-
}
|