-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertWordFeatures.ts
87 lines (82 loc) · 1.98 KB
/
insertWordFeatures.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
import { Database } from "https://deno.land/x/[email protected]/mod.ts";
const BATCH_SIZE = 50_000;
let tableIsReady = false;
const ensureTableIsReady = (db: Database) => {
if (!tableIsReady) {
prepareTable(db);
}
};
const prepareTable = (db: Database) => {
db.exec(`
DROP TABLE IF EXISTS word_features;
CREATE TABLE word_features (
word_uid INTEGER PRIMARY KEY,
module_id INTEGER NOT NULL,
wid INTEGER NOT NULL,
leader TEXT,
text TEXT,
trailer TEXT,
rid INTEGER NOT NULL,
parallel_id INTEGER NOT NULL
)
`);
tableIsReady = true;
};
const knownWordFeatureColumns = [
"word_uid",
"module_id",
"wid",
"leader",
"text",
"trailer",
"rid",
"parallel_id",
];
const featureExists = (f: string) => knownWordFeatureColumns.includes(f);
const addFeatureColumn = (db: Database, feature: string) => {
ensureTableIsReady(db);
db.exec(`ALTER TABLE word_features ADD COLUMN ${feature} TEXT`);
knownWordFeatureColumns.push(feature);
};
const insertWordFeatures = (
db: Database,
columns: string[],
wordFeatures: WordFeaturesObject[],
) => {
ensureTableIsReady(db);
const insertWordFeatures = db.prepare(`
INSERT INTO word_features (
word_uid,
module_id,
wid,
leader,
text,
trailer,
rid,
parallel_id,
${columns.join(", ")}
) VALUES (
:word_uid,
:module_id,
:wid,
:leader,
:text,
:trailer,
:rid,
:parallel_id,
${columns.map((f) => ":" + f).join(", ")}
);
`);
const insertWordFeaturesBatch = db.transaction((batch) => {
for (const v of batch) {
insertWordFeatures.run(v);
}
});
let j = 0;
while (wordFeatures.length) {
const batch = wordFeatures.splice(0, BATCH_SIZE);
insertWordFeaturesBatch(batch);
console.log(` - Inserted ${j += batch.length} words`);
}
};
export { addFeatureColumn, featureExists, insertWordFeatures };