-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfolder2sql.mjs
More file actions
152 lines (145 loc) · 4.34 KB
/
Copy pathfolder2sql.mjs
File metadata and controls
152 lines (145 loc) · 4.34 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
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
import * as Path from "node:path";
import { readdir, open, mkdir } from "node:fs/promises";
import { createGzip } from "node:zlib";
import { createWriteStream } from "node:fs";
import { isMainThread, threadId } from "node:worker_threads";
import { pipeline } from "node:stream/promises";
import { v4 as uuidv4 } from "uuid";
import sqlLint from "sql-lint";
import { decode } from "html-entities";
import Piscina from "piscina";
const SEGMENT_LENGTH = Math.pow(10, 3);
async function generateKindList(dirName, kind, kindNameList) {
const kindSQLName = `${kind}.sql.gz`;
const gzip = createGzip();
const writeTo = createWriteStream(Path.resolve(dirName, kindSQLName), {
autoClose: false,
flush: true,
});
console.log(`[${threadId}] Opening: ${kindSQLName}`);
await pipeline(async function* () {
let joinedHeaders;
let rowList = [];
async function flush() {
const sql = `INSERT INTO ${kind}
(uuid,${joinedHeaders})
VALUES
(${rowList.join("),\n (")});\n`;
rowList.length = 0;
const errors = await sqlLint.default({
sql,
});
if (errors.length) {
console.error(errors);
setTimeout(() => {
throw new Error(sql);
});
}
return sql;
}
for await (const kindName of kindNameList) {
let filehandle;
try {
filehandle = await open(Path.resolve("..", "data-warehouse", dirName, kindName));
let skipCount = 0;
for await (const line of filehandle.readLines("utf-8")) {
if (!joinedHeaders) {
joinedHeaders = line.trim();
skipCount += 1;
continue;
}
if (2 > skipCount) {
skipCount += 1;
continue;
}
if (SEGMENT_LENGTH === rowList.length) {
yield await flush();
}
let cleanedLine = line.trim();
// cleanedLine = decode(cleanedLine);
cleanedLine = cleanedLine.replaceAll(/�/g, "");
cleanedLine = cleanedLine.replaceAll(
/&#(\d+);/g,
(src, what) =>
({
128: "",
65533: "",
151: "—",
223: "ß",
12316: "~",
24304: "廰",
27114: "概",
37921: "鐵",
})[what] || src
);
cleanedLine = cleanedLine.replaceAll(/\s*;\s*/g, ";");
cleanedLine = cleanedLine.replaceAll("(", "(");
cleanedLine = cleanedLine.replaceAll(")", ")");
rowList.push([uuidv4(), cleanedLine].join(","));
}
} finally {
await filehandle?.close();
}
}
yield await flush();
}, gzip, writeTo);
writeTo.end();
console.log(`[${threadId}] Written: ${kindSQLName}`);
}
export default async function generateDir(dirName) {
const [csvNameList] = await Promise.all([
readdir(Path.resolve("..", "data-warehouse", dirName)),
mkdir(Path.resolve(dirName), { recursive: true })
]);
const buildNameList = [];
const integratedNameList = [];
const landNameList = [];
const parkNameList = [];
const restNameList = [];
for (let index = 0; index < csvNameList.length; index++) {
const csvName = csvNameList[index];
if (!csvName.match("_lvr_land_")) {
restNameList.push(csvName);
continue;
}
switch (csvName.slice(-6, -1)) {
case "ld.cs": {
buildNameList.push(csvName);
break;
}
case "nd.cs": {
landNameList.push(csvName);
break;
}
case "rk.cs": {
parkNameList.push(csvName);
break;
}
default: {
integratedNameList.push(csvName);
break;
}
}
}
buildNameList.sort();
integratedNameList.sort();
landNameList.sort();
parkNameList.sort();
await generateKindList(dirName, "build", buildNameList);
await generateKindList(dirName, "integrated", integratedNameList);
await generateKindList(dirName, "land", landNameList);
await generateKindList(dirName, "park", parkNameList);
}
if (isMainThread) {
main(process.argv.slice(2)).catch(async (e) => {
console.error(e);
process.exit(1);
});
}
async function main(dirNameList) {
const piscina = new Piscina({
filename: import.meta.url,
});
console.log("Batch with: ", dirNameList);
await Promise.all(dirNameList.map((dirName) => piscina.run(dirName)));
}