-
Notifications
You must be signed in to change notification settings - Fork 4
/
CommitTransformation.ts
171 lines (151 loc) · 4.44 KB
/
CommitTransformation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* @module @ddes/store-transformations
*/
import {AggregateType, Commit, EventStore, MarshalledCommit} from '@ddes/core'
import * as debug from 'debug'
import {TransformationWorkerInput} from '.'
import Transformation from './Transformation'
/**
* @hidden
*/
const log = debug('@ddes/store-transformations:CommitTransformation')
/**
* For transformations operating on the [[Commit]] level.
* Is most useful when used in conjunction with a [[Transformer]].
*
* ```typescript
* export default new CommitTransformation({
* name: 'Do something',
*
* source: new AwsEventStore({tableName: 'tableA'}),
* target: new AwsEventStore({tableName: 'tableB'}),
*
* async transform(commit: Commit) {
* // return the output commits of this commit transformation
* return [commit]
* },
* })
* ```
*/
class CommitTransformation extends Transformation {
public aggregateTypes?: AggregateType[]
public marshalled?: boolean
public marshalledKeyProps?: [string, string]
public transform!: (
commit: MarshalledCommit | Commit
) => Promise<Array<MarshalledCommit | Commit>>
protected softInternalDeadline: number
protected hardInternalDeadline: number
constructor(transformationSpec: {
name: string
source: EventStore
target: EventStore
transform: (
commit: MarshalledCommit | Commit
) => Promise<Array<MarshalledCommit | Commit>>
transformerConfig?: any
aggregateTypes?: AggregateType[]
marshalled?: boolean
marshalledKeyProps?: [string, string]
softInternalDeadline?: number
hardInternalDeadline?: number
}) {
const {
aggregateTypes,
marshalled,
transform,
softInternalDeadline = 10000,
hardInternalDeadline = 1000,
marshalledKeyProps,
...superParams
} = transformationSpec
super(superParams)
if (marshalled && !marshalledKeyProps) {
throw new Error(
'You need to specify marshalledKeyProps when marshalled= true'
)
}
this.aggregateTypes = aggregateTypes
this.marshalled = marshalled
this.marshalledKeyProps = marshalledKeyProps
this.transform = transform
this.softInternalDeadline = softInternalDeadline
this.hardInternalDeadline = hardInternalDeadline
}
public async perform(params: TransformationWorkerInput) {
const {
totalSegments,
segment,
state,
deadline,
readCapacityLimit,
writeCapacityLimit,
} = params
const {aggregateTypes, target, marshalled} = this
const targetMutator = target.createBatchMutator({
capacityLimit: writeCapacityLimit,
})
let completed = true
let newState = null
let commitsScanned = 0
let commitsRead = 0
let throttledReads = 0
resultSets: for await (const resultSet of this.source.scan({
totalSegments,
segment,
startKey: state,
filterAggregateTypes: aggregateTypes,
capacityLimit: readCapacityLimit,
})) {
commitsScanned += resultSet.scannedCount
throttledReads += resultSet.throttleCount
for await (const commit of marshalled
? resultSet.items
: resultSet.commits) {
commitsRead++
const outputCommits = await this.transform(commit)
if (!Array.isArray(outputCommits)) {
throw new Error(
'CommitTransformation#transform() must return an array'
)
}
if (this.isInPlaceTransformation) {
if (
!outputCommits.find(outputCommit =>
Commit.hasSameKey(outputCommit, commit)
)
) {
await targetMutator.delete(commit)
}
}
await targetMutator.put(outputCommits)
if (commit instanceof Commit) {
newState = commit.storeKey
} else {
const [p, s] = this.marshalledKeyProps!
newState = {[p]: commit[p], [s]: commit[s]}
}
if (deadline - Date.now() < this.hardInternalDeadline) {
completed = false
break resultSets
}
}
if (deadline - Date.now() < this.softInternalDeadline) {
completed = false
break resultSets
}
}
await targetMutator.drained
return {
completed,
state: newState,
commitsScanned,
commitsRead,
throttledReads,
commitsWritten: targetMutator.writeCount,
commitsDeleted: targetMutator.deleteCount,
throttledWrites: targetMutator.throttleCount,
}
}
}
export default CommitTransformation