forked from nkuntz1934/matrix-workers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateCompactionWorkflow.ts
More file actions
72 lines (60 loc) · 2.37 KB
/
StateCompactionWorkflow.ts
File metadata and controls
72 lines (60 loc) · 2.37 KB
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
// State Compaction Workflow - Compact auth chains for large rooms
//
// Reduces storage and lookup cost by pruning redundant auth chain entries
// and compacting room state snapshots.
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from 'cloudflare:workers';
import type { Env } from '../types';
export interface CompactionParams {
roomId: string;
maxAuthChainDepth?: number;
}
export interface CompactionResult {
roomId: string;
prunedAuthEntries: number;
compactedStateEvents: number;
success: boolean;
}
export class StateCompactionWorkflow extends WorkflowEntrypoint<Env, CompactionParams> {
async run(event: WorkflowEvent<CompactionParams>, step: WorkflowStep) {
const { roomId } = event.payload;
const maxDepth = event.payload.maxAuthChainDepth ?? 100;
// Step 1: Identify redundant auth chain entries
const redundantEntries = await step.do('find-redundant-auth', async () => {
// Find auth_events entries where the referenced event is also transitively
// referenced through another auth event (making the direct reference redundant)
const result = await this.env.DB.prepare(`
SELECT COUNT(*) as count FROM event_auth_chain
WHERE room_id = ? AND depth > ?
`).bind(roomId, maxDepth).first<{ count: number }>();
return result?.count || 0;
});
// Step 2: Prune deep auth chain entries
const prunedCount = await step.do('prune-auth-chain', async () => {
if (redundantEntries === 0) return 0;
const result = await this.env.DB.prepare(`
DELETE FROM event_auth_chain
WHERE room_id = ? AND depth > ?
`).bind(roomId, maxDepth).run();
return result.meta.changes || 0;
});
// Step 3: Compact room_state by removing superseded entries
const compactedCount = await step.do('compact-state', async () => {
// Remove room_state entries where a newer event exists for the same (type, state_key)
const result = await this.env.DB.prepare(`
DELETE FROM room_state
WHERE room_id = ? AND rowid NOT IN (
SELECT MAX(rowid) FROM room_state
WHERE room_id = ?
GROUP BY event_type, state_key
)
`).bind(roomId, roomId).run();
return result.meta.changes || 0;
});
return {
roomId,
prunedAuthEntries: prunedCount,
compactedStateEvents: compactedCount,
success: true,
};
}
}