-
Notifications
You must be signed in to change notification settings - Fork 643
/
Copy pathprocessor.ts
299 lines (286 loc) · 8.75 KB
/
processor.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import { Insertable } from 'kysely'
import { CID } from 'multiformats/cid'
import { AtUri } from '@atproto/syntax'
import { jsonStringToLex, stringifyLex } from '@atproto/lexicon'
import DatabaseSchema from '../../db/database-schema'
import { lexicons } from '../../lexicon/lexicons'
import { Notification } from '../../db/tables/notification'
import { chunkArray } from '@atproto/common'
import { PrimaryDatabase } from '../../db'
import { BackgroundQueue } from '../../background'
import { NotificationServer } from '../../notifications'
import { dbLogger } from '../../logger'
// @NOTE re: insertions and deletions. Due to how record updates are handled,
// (insertFn) should have the same effect as (insertFn -> deleteFn -> insertFn).
type RecordProcessorParams<T, S> = {
lexId: string
insertFn: (
db: DatabaseSchema,
uri: AtUri,
cid: CID,
obj: T,
timestamp: string,
) => Promise<S | null>
findDuplicate: (
db: DatabaseSchema,
uri: AtUri,
obj: T,
) => Promise<AtUri | null>
deleteFn: (db: DatabaseSchema, uri: AtUri) => Promise<S | null>
notifsForInsert: (obj: S) => Notif[]
notifsForDelete: (
prev: S,
replacedBy: S | null,
) => { notifs: Notif[]; toDelete: string[] }
updateAggregates?: (db: DatabaseSchema, obj: S) => Promise<void>
}
type Notif = Insertable<Notification>
export class RecordProcessor<T, S> {
collection: string
db: DatabaseSchema
constructor(
private appDb: PrimaryDatabase,
private backgroundQueue: BackgroundQueue,
private notifServer: NotificationServer | undefined,
private params: RecordProcessorParams<T, S>,
) {
this.db = appDb.db
this.collection = this.params.lexId
}
matchesSchema(obj: unknown): obj is T {
try {
this.assertValidRecord(obj)
return true
} catch {
return false
}
}
assertValidRecord(obj: unknown): asserts obj is T {
lexicons.assertValidRecord(this.params.lexId, obj)
}
async insertRecord(
uri: AtUri,
cid: CID,
obj: unknown,
timestamp: string,
opts?: { disableNotifs?: boolean },
) {
this.assertValidRecord(obj)
await this.db
.insertInto('record')
.values({
uri: uri.toString(),
cid: cid.toString(),
did: uri.host,
json: stringifyLex(obj),
indexedAt: timestamp,
})
.onConflict((oc) => oc.doNothing())
.execute()
const inserted = await this.params.insertFn(
this.db,
uri,
cid,
obj,
timestamp,
)
if (inserted) {
this.aggregateOnCommit(inserted)
if (!opts?.disableNotifs) {
await this.handleNotifs({ inserted })
}
return
}
// if duplicate, insert into duplicates table with no events
const found = await this.params.findDuplicate(this.db, uri, obj)
if (found && found.toString() !== uri.toString()) {
await this.db
.insertInto('duplicate_record')
.values({
uri: uri.toString(),
cid: cid.toString(),
duplicateOf: found.toString(),
indexedAt: timestamp,
})
.onConflict((oc) => oc.doNothing())
.execute()
}
}
// Currently using a very simple strategy for updates: purge the existing index
// for the uri then replace it. The main upside is that this allows the indexer
// for each collection to avoid bespoke logic for in-place updates, which isn't
// straightforward in the general case. We still get nice control over notifications.
async updateRecord(
uri: AtUri,
cid: CID,
obj: unknown,
timestamp: string,
opts?: { disableNotifs?: boolean },
) {
this.assertValidRecord(obj)
await this.db
.updateTable('record')
.where('uri', '=', uri.toString())
.set({
cid: cid.toString(),
json: stringifyLex(obj),
indexedAt: timestamp,
})
.execute()
// If the updated record was a dupe, update dupe info for it
const dupe = await this.params.findDuplicate(this.db, uri, obj)
if (dupe) {
await this.db
.updateTable('duplicate_record')
.where('uri', '=', uri.toString())
.set({
cid: cid.toString(),
duplicateOf: dupe.toString(),
indexedAt: timestamp,
})
.execute()
} else {
await this.db
.deleteFrom('duplicate_record')
.where('uri', '=', uri.toString())
.execute()
}
const deleted = await this.params.deleteFn(this.db, uri)
if (!deleted) {
// If a record was updated but hadn't been indexed yet, treat it like a plain insert.
return this.insertRecord(uri, cid, obj, timestamp)
}
this.aggregateOnCommit(deleted)
const inserted = await this.params.insertFn(
this.db,
uri,
cid,
obj,
timestamp,
)
if (!inserted) {
throw new Error(
'Record update failed: removed from index but could not be replaced',
)
}
this.aggregateOnCommit(inserted)
if (!opts?.disableNotifs) {
await this.handleNotifs({ inserted, deleted })
}
}
async deleteRecord(uri: AtUri, cascading = false) {
await this.db
.deleteFrom('record')
.where('uri', '=', uri.toString())
.execute()
await this.db
.deleteFrom('duplicate_record')
.where('uri', '=', uri.toString())
.execute()
const deleted = await this.params.deleteFn(this.db, uri)
if (!deleted) return
this.aggregateOnCommit(deleted)
if (cascading) {
await this.db
.deleteFrom('duplicate_record')
.where('duplicateOf', '=', uri.toString())
.execute()
return this.handleNotifs({ deleted })
} else {
const found = await this.db
.selectFrom('duplicate_record')
.innerJoin('record', 'record.uri', 'duplicate_record.uri')
.where('duplicateOf', '=', uri.toString())
.orderBy('duplicate_record.indexedAt', 'asc')
.limit(1)
.selectAll()
.executeTakeFirst()
if (!found) {
return this.handleNotifs({ deleted })
}
const record = jsonStringToLex(found.json)
if (!this.matchesSchema(record)) {
return this.handleNotifs({ deleted })
}
const inserted = await this.params.insertFn(
this.db,
new AtUri(found.uri),
CID.parse(found.cid),
record,
found.indexedAt,
)
if (inserted) {
this.aggregateOnCommit(inserted)
}
await this.handleNotifs({ deleted, inserted: inserted ?? undefined })
}
}
async handleNotifs(op: { deleted?: S; inserted?: S }) {
let notifs: Notif[] = []
const runOnCommit: ((db: PrimaryDatabase) => Promise<void>)[] = []
const sendOnCommit: (() => Promise<void>)[] = []
if (op.deleted) {
const forDelete = this.params.notifsForDelete(
op.deleted,
op.inserted ?? null,
)
if (forDelete.toDelete.length > 0) {
// Notifs can be deleted in background: they are expensive to delete and
// listNotifications already excludes notifs with missing records.
runOnCommit.push(async (db) => {
await db.db
.deleteFrom('notification')
.where('recordUri', 'in', forDelete.toDelete)
.execute()
})
}
notifs = forDelete.notifs
} else if (op.inserted) {
notifs = this.params.notifsForInsert(op.inserted)
}
for (const chunk of chunkArray(notifs, 500)) {
runOnCommit.push(async (db) => {
await db.db.insertInto('notification').values(chunk).execute()
})
if (this.notifServer) {
const notifServer = this.notifServer
sendOnCommit.push(async () => {
try {
const preparedNotifs = await notifServer.prepareNotifications(chunk)
await notifServer.processNotifications(preparedNotifs)
} catch (error) {
dbLogger.error({ error }, 'error sending push notifications')
}
})
}
}
if (runOnCommit.length) {
// Need to ensure notif deletion always happens before creation, otherwise delete may clobber in a race.
this.appDb.onCommit(() => {
this.backgroundQueue.add(async (db) => {
for (const fn of runOnCommit) {
await fn(db)
}
})
})
}
if (sendOnCommit.length) {
// Need to ensure notif deletion always happens before creation, otherwise delete may clobber in a race.
this.appDb.onCommit(() => {
this.backgroundQueue.add(async () => {
for (const fn of sendOnCommit) {
await fn()
}
})
})
}
}
aggregateOnCommit(indexed: S) {
const { updateAggregates } = this.params
if (!updateAggregates) return
this.appDb.onCommit(() => {
this.backgroundQueue.add((db) => updateAggregates(db.db, indexed))
})
}
}
export default RecordProcessor