-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump-with-extra-filler.js
143 lines (121 loc) · 3.94 KB
/
dump-with-extra-filler.js
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
'use strict';
// Fills in `events`, `current_state_events` tables in Synapse
// with the given events plus a bunch of filler data.
const assert = require('assert');
const crypto = require('crypto');
const { Client } = require('pg');
const pgFormat = require('pg-format');
const client = new Client({
host: 'localhost',
// port: 5334,
database: 'synapse',
// user: 'database-user',
// password: 'secretpassword!!',
});
// State from https://matrix-client.matrix.org/_matrix/client/r0/rooms/!OGEhHVWSdvArJzumhm:matrix.org/state
const stateEvents = require('/Users/eric/Downloads/matrixhq-state.json');
console.log('stateEvents', stateEvents.length);
const DUPLICATION_FACTOR = 100;
const CHUNK_SIZE = 1000;
function eventToEventsRecord(event) {
assert(event);
return {
event_id: event.event_id,
type: event.type,
room_id: event.room_id,
processed: true,
outlier: false,
origin_server_ts: event.origin_server_ts,
received_ts: event.origin_server_ts,
sender: event.sender,
state_key: event.state_key,
topological_ordering: event.topological_ordering,
depth: event.depth,
stream_ordering: event.stream_ordering,
};
}
function eventToCurrentStateEventsRecord(event) {
assert(event);
return {
event_id: event.event_id,
room_id: event.room_id,
type: event.type,
state_key: event.state_key,
membership: event.content?.membership,
};
}
function generateFakeStateEvent(index) {
assert(index);
// Give a random depth in the range where we current are
const depth = Math.floor(Math.random() * index);
const mxid = `@fake-user:fake-homeserver-${crypto.randomBytes(8).toString('hex')}`;
return {
event_id: `$${crypto.randomBytes(43).toString('hex')}`,
type: 'm.room.member',
state_key: mxid,
sender: mxid,
content: {
membership: 'join',
},
origin_server_ts: 1633370210924,
room_id: `!fake-room:fake-homeserver`,
topological_ordering: depth,
depth: depth,
stream_ordering: index,
};
}
async function exec() {
await client.connect();
const maxStreamOrderingResult = await client.query(`SELECT MAX(stream_ordering) FROM events;`);
const maxStreamOrdering = maxStreamOrderingResult.rows[0].max;
console.log('maxStreamOrdering', maxStreamOrdering);
const eventsToPersist = [];
for (let [stateEventIndex, stateEvent] of stateEvents.entries()) {
const eventWithInfoWeNeed = {
...stateEvent,
topological_ordering: stateEventIndex,
depth: stateEventIndex,
stream_ordering: maxStreamOrdering + stateEventIndex,
};
eventsToPersist.push(eventWithInfoWeNeed);
// Generate filler events
for (let i = 0; i < DUPLICATION_FACTOR; i++) {
eventsToPersist.push(
generateFakeStateEvent(maxStreamOrdering + stateEventIndex * DUPLICATION_FACTOR + i)
);
}
}
console.log('Done assembling data');
let insertCount = 0;
for (let i = 0; i < eventsToPersist.length; i += CHUNK_SIZE) {
const eventsInChunk = eventsToPersist.slice(i, i + CHUNK_SIZE);
const eventsInsertQuery = pgFormat(
`
INSERT into events
(${Object.keys(eventToEventsRecord(eventsInChunk[0])).join(', ')})
VALUES %L
`,
eventsInChunk.map((event) => Object.values(eventToEventsRecord(event)))
);
await client.query(eventsInsertQuery);
const currentStateEventsInsertQuery = pgFormat(
`
INSERT into current_state_events
(${Object.keys(eventToCurrentStateEventsRecord(eventsInChunk[0])).join(', ')})
VALUES %L
`,
eventsInChunk.map((event) => Object.values(eventToCurrentStateEventsRecord(event)))
);
await client.query(currentStateEventsInsertQuery);
if (insertCount % 10 === 0) {
process.stdout.write('.');
}
insertCount++;
}
const result = await client.query(
`SELECT COUNT(*) from current_state_events where room_id = '!OGEhHVWSdvArJzumhm:matrix.org';`
);
console.log('result', result);
await client.end();
}
exec();