-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathcopy-assertions.js
154 lines (131 loc) · 5.58 KB
/
copy-assertions.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
144
145
146
147
148
149
150
151
152
153
154
import 'dotenv/config';
import fs from 'fs-extra';
import rc from 'rc';
import appRootPath from 'app-root-path';
import path from 'path';
import { TRIPLE_STORE_REPOSITORIES, SCHEMA_CONTEXT } from '../src/constants/constants.js';
import TripleStoreModuleManager from '../src/modules/triple-store/triple-store-module-manager.js';
import DataService from '../src/service/data-service.js';
import Logger from '../src/logger/logger.js';
const { readFile } = fs;
const generalConfig = JSON.parse(await readFile(path.join(appRootPath.path, 'config/config.json')));
const pjson = JSON.parse(await readFile(path.join(appRootPath.path, 'package.json')));
const defaultConfig = generalConfig[process.env.NODE_ENV];
const config = rc(pjson.name, defaultConfig);
const logger = new Logger(config.loglevel);
const tripleStoreModuleManager = new TripleStoreModuleManager({ config, logger });
await tripleStoreModuleManager.initialize();
const dataService = new DataService({ config, logger });
const repositoryImplementations = {};
for (const implementationName of tripleStoreModuleManager.getImplementationNames()) {
for (const repository in tripleStoreModuleManager.getImplementation(implementationName).module
.repositories) {
repositoryImplementations[repository] = implementationName;
}
}
const fromRepository = TRIPLE_STORE_REPOSITORIES.PUBLIC_CURRENT;
const fromImplementation = repositoryImplementations[TRIPLE_STORE_REPOSITORIES.PUBLIC_CURRENT];
const fromRepositoryName =
tripleStoreModuleManager.getImplementation(fromImplementation).module.repositories[
fromRepository
].name;
const toRepository = TRIPLE_STORE_REPOSITORIES.PRIVATE_CURRENT;
const toImplementation = repositoryImplementations[TRIPLE_STORE_REPOSITORIES.PRIVATE_CURRENT];
const toRepositoryName =
tripleStoreModuleManager.getImplementation(toImplementation).module.repositories[toRepository]
.name;
async function getAssertions(implementation, repository) {
const graphs = await tripleStoreModuleManager.select(
implementation,
repository,
`SELECT DISTINCT ?g
WHERE {
GRAPH ?g { ?s ?p ?o }
}`,
);
return (graphs ?? []).filter(({ g }) => g.startsWith('assertion:')).map(({ g }) => g);
}
function logPercentage(index, max) {
const previousPercentage = (Math.max(0, index - 1) / max) * 100;
const currentPercentage = (index / max) * 100;
if (Math.floor(currentPercentage) - Math.floor(previousPercentage) < 1) return;
logger.debug(`Migration at ${Math.floor(currentPercentage * 10) / 10}%`);
}
let toRepositoryAssertions = await getAssertions(toImplementation, toRepository);
logger.info(
`${toRepositoryAssertions.length} assertions found in ${toRepository} repository before migration`,
);
logger.info(
`Starting to copy assertions from ${fromImplementation} repository ${fromRepository} with name ${fromRepositoryName} to repository ${toImplementation} repository ${toRepository} with name ${toRepositoryName}`,
);
const fromRepositoryAssertions = await getAssertions(fromImplementation, fromRepository);
logger.info(`${fromRepositoryAssertions.length} assertions found in ${fromRepository}`);
let completed = 0;
const copyAssertion = async (g) => {
if (!toRepositoryAssertions.includes(g)) {
let nquads;
try {
nquads = await tripleStoreModuleManager.construct(
fromImplementation,
fromRepository,
`PREFIX schema: <${SCHEMA_CONTEXT}>
CONSTRUCT { ?s ?p ?o }
WHERE {
{
GRAPH <${g}>
{
?s ?p ?o .
}
}
}`,
);
nquads = await dataService.toNQuads(nquads, 'application/n-quads');
} catch (error) {
logger.error(
`Error while getting assertion ${g.substring(
'assertion:'.length,
)} from ${fromImplementation} repository ${fromRepository} with name ${fromRepositoryName}. Error: ${
error.message
}`,
);
process.exit(1);
}
try {
await tripleStoreModuleManager.insertAssertion(
toImplementation,
toRepository,
g.substring('assertion:'.length),
nquads.join('\n'),
);
} catch (error) {
logger.error(
`Error while inserting assertion ${g.substring(
'assertion:'.length,
)} with nquads: ${nquads} in ${toImplementation} repository ${toRepository} with name ${toRepositoryName}. Error: ${
error.message
}`,
);
process.exit(1);
}
}
completed += 1;
logPercentage(completed, fromRepositoryAssertions.length);
};
const start = Date.now();
const concurrency = 10;
let promises = [];
for (let i = 0; i < fromRepositoryAssertions.length; i += 1) {
promises.push(copyAssertion(fromRepositoryAssertions[i]));
if (promises.length > concurrency) {
// eslint-disable-next-line no-await-in-loop
await Promise.all(promises);
promises = [];
}
}
await Promise.all(promises);
const end = Date.now();
logger.info(`Migration completed! Lasted ${(end - start) / 1000} seconds.`);
toRepositoryAssertions = await getAssertions(toImplementation, toRepository);
logger.info(
`${toRepositoryAssertions.length} assertions found in ${toRepository} repository after migration`,
);