-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateOrderingIndex.ts
62 lines (60 loc) · 1.68 KB
/
createOrderingIndex.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
import { Database } from "https://deno.land/x/[email protected]/mod.ts";
import {
getRidFromParallelIdAndSchema,
sortParallelIdsBySchema,
} from "./getParallelId.ts";
const BATCH_SIZE = 25000;
export default function (
db: Database,
versificationSchemas: { [key: string]: number },
) {
db.exec(`
DROP TABLE IF EXISTS ordering_index;
CREATE TABLE ordering_index (
parallel_id INTEGER NOT NULL,
versification_schema_id INTEGER NOT NULL,
rid INTEGER,
order_in_schema INTEGER NOT NULL
);
`);
const allPids = db.prepare("SELECT DISTINCT parallel_id FROM parallel;")
.all()
.map((p) => p.parallel_id);
console.log("Parallel ids to order:", allPids.length);
Object.keys(versificationSchemas).forEach((vs) => {
const pids = allPids.slice().sort(
sortParallelIdsBySchema(vs as VersificationSchema),
);
// write to file
const orderIndex = pids.map((pid, i) => ({
parallel_id: pid,
versification_schema_id: versificationSchemas[vs],
rid: getRidFromParallelIdAndSchema(pid, vs),
order_in_schema: i + 1,
}));
const insertOrderIndex = db.prepare(`
INSERT INTO ordering_index (
parallel_id,
versification_schema_id,
rid,
order_in_schema
) VALUES (
:parallel_id,
:versification_schema_id,
:rid,
:order_in_schema
);
`);
const insertOrderIndexBatch = db.transaction((batch) => {
for (const v of batch) {
insertOrderIndex.run(v);
}
});
let i = 0;
while (orderIndex.length) {
const batch = orderIndex.splice(0, BATCH_SIZE);
insertOrderIndexBatch(batch);
console.log(` - Inserted ${i += batch.length} verses`);
}
});
}