@natlibfi/melinda-record-matching 2.2.1-alpha.1 → 2.2.1-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/dist/candidate-search/candidate-search-utils.js +55 -0
  2. package/dist/candidate-search/candidate-search-utils.js.map +1 -0
  3. package/dist/candidate-search/index.js +258 -0
  4. package/dist/candidate-search/index.js.map +1 -0
  5. package/dist/candidate-search/index.spec.js +148 -0
  6. package/dist/candidate-search/index.spec.js.map +1 -0
  7. package/dist/candidate-search/query-list/bib.js +230 -0
  8. package/dist/candidate-search/query-list/bib.js.map +1 -0
  9. package/dist/candidate-search/query-list/bib.spec.js +63 -0
  10. package/dist/candidate-search/query-list/bib.spec.js.map +1 -0
  11. package/dist/candidate-search/query-list/index.js +61 -0
  12. package/dist/candidate-search/query-list/index.js.map +1 -0
  13. package/dist/index.js +559 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/index.spec.js +125 -0
  16. package/dist/index.spec.js.map +1 -0
  17. package/dist/match-detection/features/bib/all-source-ids.js +116 -0
  18. package/dist/match-detection/features/bib/all-source-ids.js.map +1 -0
  19. package/dist/match-detection/features/bib/authors.js +107 -0
  20. package/dist/match-detection/features/bib/authors.js.map +1 -0
  21. package/dist/match-detection/features/bib/bibliographic-level.js +39 -0
  22. package/dist/match-detection/features/bib/bibliographic-level.js.map +1 -0
  23. package/dist/match-detection/features/bib/host-component.js +39 -0
  24. package/dist/match-detection/features/bib/host-component.js.map +1 -0
  25. package/dist/match-detection/features/bib/index.js +91 -0
  26. package/dist/match-detection/features/bib/index.js.map +1 -0
  27. package/dist/match-detection/features/bib/index.spec.js +87 -0
  28. package/dist/match-detection/features/bib/index.spec.js.map +1 -0
  29. package/dist/match-detection/features/bib/isbn.js +50 -0
  30. package/dist/match-detection/features/bib/isbn.js.map +1 -0
  31. package/dist/match-detection/features/bib/issn.js +50 -0
  32. package/dist/match-detection/features/bib/issn.js.map +1 -0
  33. package/dist/match-detection/features/bib/language.js +98 -0
  34. package/dist/match-detection/features/bib/language.js.map +1 -0
  35. package/dist/match-detection/features/bib/melinda-id.js +47 -0
  36. package/dist/match-detection/features/bib/melinda-id.js.map +1 -0
  37. package/dist/match-detection/features/bib/melinda-identifier-factory.js +88 -0
  38. package/dist/match-detection/features/bib/melinda-identifier-factory.js.map +1 -0
  39. package/dist/match-detection/features/bib/other-standard-identifier.js +50 -0
  40. package/dist/match-detection/features/bib/other-standard-identifier.js.map +1 -0
  41. package/dist/match-detection/features/bib/publication-time.js +47 -0
  42. package/dist/match-detection/features/bib/publication-time.js.map +1 -0
  43. package/dist/match-detection/features/bib/record-type.js +41 -0
  44. package/dist/match-detection/features/bib/record-type.js.map +1 -0
  45. package/dist/match-detection/features/bib/standard-identifier-factory.js +112 -0
  46. package/dist/match-detection/features/bib/standard-identifier-factory.js.map +1 -0
  47. package/dist/match-detection/features/bib/title.js +78 -0
  48. package/dist/match-detection/features/bib/title.js.map +1 -0
  49. package/dist/match-detection/features/index.js +11 -0
  50. package/dist/match-detection/features/index.js.map +1 -0
  51. package/dist/match-detection/index.js +162 -0
  52. package/dist/match-detection/index.js.map +1 -0
  53. package/dist/match-detection/index.spec.js +88 -0
  54. package/dist/match-detection/index.spec.js.map +1 -0
  55. package/dist/matching-utils.js +95 -0
  56. package/dist/matching-utils.js.map +1 -0
  57. package/package.json +18 -17
  58. package/src/candidate-search/query-list/bib.js +19 -18
  59. package/src/candidate-search/query-list/index.js +1 -1
  60. package/src/match-detection/features/bib/authors.js +5 -2
  61. package/src/match-detection/features/bib/language.js +2 -2
  62. package/src/match-detection/features/bib/publication-time.js +8 -3
  63. package/src/match-detection/features/bib/record-type.js +2 -0
  64. package/src/match-detection/features/bib/standard-identifier-factory.js +4 -3
  65. package/src/match-detection/features/bib/title.js +6 -4
  66. package/src/matching-utils.js +13 -4
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.toQueries = toQueries;
7
+ var _debug = _interopRequireDefault(require("debug"));
8
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
+ /**
10
+ *
11
+ * @licstart The following is the entire license notice for the JavaScript code in this file.
12
+ *
13
+ * Melinda record matching modules for Javascript
14
+ *
15
+ * Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)
16
+ *
17
+ * This file is part of melinda-record-matching-js
18
+ *
19
+ * melinda-record-matching-js program is free software: you can redistribute it and/or modify
20
+ * it under the terms of the GNU Lesser General Public License as
21
+ * published by the Free Software Foundation, either version 3 of the
22
+ * License, or (at your option) any later version.
23
+ *
24
+ * melinda-record-matching-js is distributed in the hope that it will be useful,
25
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
+ * GNU Lesser General Public License for more details.
28
+ *
29
+ * You should have received a copy of the GNU Affero General Public License
30
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
31
+ *
32
+ * @licend The above is the entire license notice
33
+ * for the JavaScript code in this file.
34
+ *
35
+ */
36
+
37
+ function toQueries(identifiers, queryString) {
38
+ const debug = (0, _debug.default)('@natlibfi/melinda-record-matching:toQueries');
39
+ const debugData = debug.extend('data');
40
+ const quotedIdentifiers = identifiers.map(identifier => identifier.match(/\//u) ? `"${identifier}"` : `${identifier}`);
41
+
42
+ // Aleph supports only two queries with or -operator (This is not actually true)
43
+ const pairs = toPairs(quotedIdentifiers);
44
+ const queries = pairs.map(([a, b]) => b ? `${queryString}=${a} or ${queryString}=${b}` : `${queryString}=${a}`);
45
+ debugData(`Pairs (${pairs.length}): ${JSON.stringify(pairs)}`);
46
+ debugData(`Queries (${queries.length}): ${JSON.stringify(queries)}`);
47
+ return queries;
48
+ }
49
+ function toPairs(array) {
50
+ if (array.length === 0) {
51
+ return [];
52
+ }
53
+ return [array.slice(0, 2)].concat(toPairs(array.slice(2), 2));
54
+ }
55
+ //# sourceMappingURL=candidate-search-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"candidate-search-utils.js","names":["toQueries","identifiers","queryString","debug","createDebugLogger","debugData","extend","quotedIdentifiers","map","identifier","match","pairs","toPairs","queries","a","b","length","JSON","stringify","array","slice","concat"],"sources":["../../src/candidate-search/candidate-search-utils.js"],"sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Melinda record matching modules for Javascript\n*\n* Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-record-matching-js\n*\n* melinda-record-matching-js program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Lesser General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-record-matching-js is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Lesser General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport createDebugLogger from 'debug';\n\nexport function toQueries(identifiers, queryString) {\n\n const debug = createDebugLogger('@natlibfi/melinda-record-matching:toQueries');\n const debugData = debug.extend('data');\n\n const quotedIdentifiers = identifiers.map(identifier => identifier.match(/\\//u) ? `\"${identifier}\"` : `${identifier}`);\n\n // Aleph supports only two queries with or -operator (This is not actually true)\n const pairs = toPairs(quotedIdentifiers);\n const queries = pairs.map(([a, b]) => b ? `${queryString}=${a} or ${queryString}=${b}` : `${queryString}=${a}`);\n\n debugData(`Pairs (${pairs.length}): ${JSON.stringify(pairs)}`);\n debugData(`Queries (${queries.length}): ${JSON.stringify(queries)}`);\n\n return queries;\n}\n\nfunction toPairs(array) {\n if (array.length === 0) {\n return [];\n }\n return [array.slice(0, 2)].concat(toPairs(array.slice(2), 2));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AAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAC/D"}
@@ -0,0 +1,258 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.CandidateSearchError = void 0;
7
+ Object.defineProperty(exports, "searchTypes", {
8
+ enumerable: true,
9
+ get: function () {
10
+ return _queryList.searchTypes;
11
+ }
12
+ });
13
+ var _debug = _interopRequireDefault(require("debug"));
14
+ var _sruClient = _interopRequireWildcard(require("@natlibfi/sru-client"));
15
+ var _marcRecord = require("@natlibfi/marc-record");
16
+ var _marcRecordSerializers = require("@natlibfi/marc-record-serializers");
17
+ var _queryList = _interopRequireWildcard(require("./query-list"));
18
+ var _melindaCommons = require("@natlibfi/melinda-commons");
19
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
20
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
21
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
+ /**
23
+ *
24
+ * @licstart The following is the entire license notice for the JavaScript code in this file.
25
+ *
26
+ * Melinda record matching modules for Javascript
27
+ *
28
+ * Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)
29
+ *
30
+ * This file is part of melinda-record-matching-js
31
+ *
32
+ * melinda-record-matching-js program is free software: you can redistribute it and/or modify
33
+ * it under the terms of the GNU Lesser General Public License as
34
+ * published by the Free Software Foundation, either version 3 of the
35
+ * License, or (at your option) any later version.
36
+ *
37
+ * melinda-record-matching-js is distributed in the hope that it will be useful,
38
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
39
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40
+ * GNU Lesser General Public License for more details.
41
+ *
42
+ * You should have received a copy of the GNU Affero General Public License
43
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
44
+ *
45
+ * @licend The above is the entire license notice
46
+ * for the JavaScript code in this file.
47
+ *
48
+ */
49
+
50
+ class CandidateSearchError extends Error {}
51
+
52
+ // serverMaxResults : maximum size of total search result available from the server, defaults to Aleph's 20000
53
+
54
+ // eslint-disable-next-line max-statements
55
+ exports.CandidateSearchError = CandidateSearchError;
56
+ var _default = ({
57
+ record,
58
+ searchSpec,
59
+ url,
60
+ maxCandidates,
61
+ maxRecordsPerRequest = 50,
62
+ serverMaxResult = 20000
63
+ }) => {
64
+ _marcRecord.MarcRecord.setValidationOptions({
65
+ subfieldValues: false
66
+ });
67
+ const debug = (0, _debug.default)('@natlibfi/melinda-record-matching:candidate-search');
68
+ const debugData = debug.extend('data');
69
+ debugData(`SearchSpec: ${JSON.stringify(searchSpec)}`);
70
+ debugData(`Url: ${url}`);
71
+ debugData(`MaxRecordsPerRequest ${maxRecordsPerRequest}`);
72
+ debugData(`ServerMaxResult: ${serverMaxResult}`);
73
+ debugData(`MaxCandidates: ${maxCandidates}`);
74
+
75
+ // Do not retrieve more candidates than defined in maxCandidates
76
+ const adjustedMaxRecordsPerRequest = maxRecordsPerRequest >= maxCandidates ? maxCandidates : maxRecordsPerRequest;
77
+ const inputRecordId = getRecordId(record);
78
+ const queryList = (0, _queryList.default)(record, searchSpec);
79
+ const client = (0, _sruClient.default)({
80
+ url,
81
+ maxRecordsPerRequest: adjustedMaxRecordsPerRequest,
82
+ version: '2.0',
83
+ retrieveAll: false
84
+ });
85
+ debug(`Searching matches for ${inputRecordId}`);
86
+ debug(`Generated queryList ${JSON.stringify(queryList)}`);
87
+
88
+ // if generateQueryList errored we should throw 422
89
+
90
+ if (queryList.length === 0) {
91
+ // eslint-disable-line functional/no-conditional-statement
92
+ throw new CandidateSearchError(`Generated query list contains no queries`);
93
+ }
94
+
95
+ // state.totalRecords : amount of candidate records available to the current query (undefined, if there was no queries left)
96
+ // state.query : current query (undefined if there was no queries left)
97
+ // state.searchCounter : sequence for current search for current query (undefined, if there we no queries left)
98
+ // state.queryCandidateCounter: amount of candidates (records+failures) retrieved from SRU for matching for current query, including the current record+failure set (undefined if there were no queries left)
99
+ // state.queriesLeft : amount of queries left
100
+ // state.queryCounter : sequence for current query
101
+ // state.maxedQueries : queries that resulted in more than serverMaxResults hits
102
+
103
+ return async ({
104
+ queryOffset = 0,
105
+ resultSetOffset = 1,
106
+ totalRecords = 0,
107
+ searchCounter = 0,
108
+ queryCandidateCounter = 0,
109
+ queryCounter = 0,
110
+ maxedQueries = []
111
+ }) => {
112
+ const query = queryList[queryOffset];
113
+ if (query) {
114
+ const {
115
+ records,
116
+ failures,
117
+ nextOffset,
118
+ total
119
+ } = await retrieveRecords();
120
+
121
+ // If resultSetOffset === 1 this is the first search for the current query
122
+ debugData(`ResultSetOffset: ${resultSetOffset}`);
123
+ const newTotalRecords = resultSetOffset === 1 ? total : totalRecords;
124
+ const newQueryCounter = resultSetOffset === 1 ? queryCounter + 1 : queryCounter;
125
+ const newSearchCounter = resultSetOffset === 1 ? 1 : searchCounter + 1;
126
+ const newQueryCandidateCounter = resultSetOffset === 1 ? records.length + failures.length : queryCandidateCounter + records.length + failures.length;
127
+ const maxedQuery = resultSetOffset === 1 ? checkMaxedQuery(query, total, serverMaxResult) : undefined;
128
+ const newMaxedQueries = maxedQuery ? maxedQueries.concat(maxedQuery) : maxedQueries;
129
+ if (typeof nextOffset === 'number') {
130
+ debug(`Next search will be for query ${queryOffset} ${query}, starting from record ${nextOffset}`);
131
+ return {
132
+ records,
133
+ failures,
134
+ queryOffset,
135
+ resultSetOffset: nextOffset,
136
+ queriesLeft: queryList.length - (queryOffset + 1),
137
+ totalRecords: newTotalRecords,
138
+ query,
139
+ searchCounter: newSearchCounter,
140
+ queryCandidateCounter: newQueryCandidateCounter,
141
+ queryCounter: newQueryCounter,
142
+ maxedQueries: newMaxedQueries
143
+ };
144
+ }
145
+ debug(`Query ${queryOffset} ${query} done.`);
146
+ debug(`There are (${queryList.length - (queryOffset + 1)} queries left)`);
147
+ return {
148
+ records,
149
+ failures,
150
+ queryOffset: queryOffset + 1,
151
+ queriesLeft: queryList.length - (queryOffset + 1),
152
+ totalRecords: newTotalRecords,
153
+ query,
154
+ searchCounter: newSearchCounter,
155
+ queryCandidateCounter: newQueryCandidateCounter,
156
+ queryCounter: newQueryCounter,
157
+ maxedQueries: newMaxedQueries
158
+ };
159
+ }
160
+ debug(`All ${queryList.length} queries done, there's no query for ${queryOffset}`);
161
+ return {
162
+ records: [],
163
+ failures: [],
164
+ queriesLeft: 0,
165
+ queryCounter,
166
+ maxedQueries
167
+ };
168
+ function retrieveRecords() {
169
+ return new Promise((resolve, reject) => {
170
+ const promises = [];
171
+ // eslint-disable-next-line functional/no-let
172
+ let totalRecords = 0;
173
+ debug(`Searching for candidates with query: ${query} (Offset ${resultSetOffset})`);
174
+ client.searchRetrieve(query, {
175
+ startRecord: resultSetOffset
176
+ }).on('error', err => {
177
+ // eslint-disable-next-line functional/no-conditional-statement
178
+ if (err instanceof _sruClient.SruSearchError) {
179
+ debug(`SRU SruSearchError for query: ${query}: ${err}`);
180
+ reject(new CandidateSearchError(`SRU SruSearchError for query: ${query}: ${err}`));
181
+ }
182
+ debug(`SRU error for query: ${query}: ${err}`);
183
+ reject(new CandidateSearchError(`SRU error for query: ${query}: ${err}`));
184
+ }).on('total', total => {
185
+ debug(`Got total: ${total}`);
186
+ totalRecords += total;
187
+ }).on('end', async nextOffset => {
188
+ try {
189
+ const recordPromises = await Promise.allSettled(promises);
190
+ debug(`All recordPromises: ${JSON.stringify(recordPromises)}`);
191
+ const filtered = recordPromises.filter(r => r.status === 'fulfilled').map(r => r.value);
192
+ const failures = recordPromises.filter(r => r.status === 'rejected').map(r => ({
193
+ status: r.reason.status,
194
+ payload: r.reason.payload
195
+ }));
196
+ debug(`Found ${JSON.stringify(recordPromises)} records`);
197
+ debug(`Found ${filtered.length} convertable candidates`);
198
+ debug(`Found ${failures.length} NON-convertable candidates`);
199
+ debug(`Converted: ${JSON.stringify(filtered)}.`);
200
+ debug(`Not converted: ${JSON.stringify(failures)}.`);
201
+ resolve({
202
+ nextOffset,
203
+ records: filtered,
204
+ failures,
205
+ total: totalRecords
206
+ });
207
+ } catch (err) {
208
+ debug(`Error caught on END`);
209
+ reject(err);
210
+ }
211
+ }).on('record', recordXML => {
212
+ promises.push(handleRecord()); // eslint-disable-line functional/immutable-data
213
+
214
+ async function handleRecord() {
215
+ try {
216
+ const recordMarc = await _marcRecordSerializers.MARCXML.from(recordXML, {
217
+ subfieldValues: false
218
+ });
219
+ const recordId = getRecordId(recordMarc);
220
+ return {
221
+ record: recordMarc,
222
+ id: recordId
223
+ };
224
+ } catch (err) {
225
+ // What should this do?
226
+ const idFromXML = getRecordIdFromXML(recordXML);
227
+ debugData(`Failed converting record: ${err.message}, id: ${idFromXML}, data: ${recordXML}`);
228
+ //return {message: `Failed converting record: ${err.message}`, id: idFromXML, data: recordXML};
229
+ throw new _melindaCommons.Error(422, {
230
+ message: `Failed converting record: ${err.message}`,
231
+ id: idFromXML || '000000000',
232
+ data: recordXML
233
+ });
234
+ }
235
+ }
236
+ });
237
+ });
238
+ }
239
+ };
240
+ function checkMaxedQuery(query, total, serverMaxResult) {
241
+ // eslint-disable-next-line functional/no-conditional-statement
242
+ if (total >= serverMaxResult) {
243
+ debug(`WARNING: Query ${query} resulted in ${total} hits which meets the serverMaxResult (${serverMaxResult}) `);
244
+ return query;
245
+ }
246
+ }
247
+ function getRecordId(record) {
248
+ const [field] = record.get(/^001$/u);
249
+ return field ? field.value : '';
250
+ }
251
+ function getRecordIdFromXML(recordXML) {
252
+ //<controlfield tag=\"001\">015376846</controlfield
253
+ debug(`Cannot yet find possible database record id from recordXML (length ${recordXML.length})`);
254
+ return undefined;
255
+ }
256
+ };
257
+ exports.default = _default;
258
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["CandidateSearchError","Error","record","searchSpec","url","maxCandidates","maxRecordsPerRequest","serverMaxResult","MarcRecord","setValidationOptions","subfieldValues","debug","createDebugLogger","debugData","extend","JSON","stringify","adjustedMaxRecordsPerRequest","inputRecordId","getRecordId","queryList","generateQueryList","client","createClient","version","retrieveAll","length","queryOffset","resultSetOffset","totalRecords","searchCounter","queryCandidateCounter","queryCounter","maxedQueries","query","records","failures","nextOffset","total","retrieveRecords","newTotalRecords","newQueryCounter","newSearchCounter","newQueryCandidateCounter","maxedQuery","checkMaxedQuery","undefined","newMaxedQueries","concat","queriesLeft","Promise","resolve","reject","promises","searchRetrieve","startRecord","on","err","SruSearchError","recordPromises","allSettled","filtered","filter","r","status","map","value","reason","payload","recordXML","push","handleRecord","recordMarc","MARCXML","from","recordId","id","idFromXML","getRecordIdFromXML","message","MatchingError","data","field","get"],"sources":["../../src/candidate-search/index.js"],"sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Melinda record matching modules for Javascript\n*\n* Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-record-matching-js\n*\n* melinda-record-matching-js program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Lesser General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-record-matching-js is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Lesser General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport createDebugLogger from 'debug';\nimport createClient, {SruSearchError} from '@natlibfi/sru-client';\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport {MARCXML} from '@natlibfi/marc-record-serializers';\nimport generateQueryList from './query-list';\nimport {Error as MatchingError} from '@natlibfi/melinda-commons';\n\nexport {searchTypes} from './query-list';\n\nexport class CandidateSearchError extends Error {}\n\n// serverMaxResults : maximum size of total search result available from the server, defaults to Aleph's 20000\n\n// eslint-disable-next-line max-statements\nexport default ({record, searchSpec, url, maxCandidates, maxRecordsPerRequest = 50, serverMaxResult = 20000}) => {\n MarcRecord.setValidationOptions({subfieldValues: false});\n\n const debug = createDebugLogger('@natlibfi/melinda-record-matching:candidate-search');\n const debugData = debug.extend('data');\n\n debugData(`SearchSpec: ${JSON.stringify(searchSpec)}`);\n debugData(`Url: ${url}`);\n debugData(`MaxRecordsPerRequest ${maxRecordsPerRequest}`);\n debugData(`ServerMaxResult: ${serverMaxResult}`);\n debugData(`MaxCandidates: ${maxCandidates}`);\n\n // Do not retrieve more candidates than defined in maxCandidates\n const adjustedMaxRecordsPerRequest = maxRecordsPerRequest >= maxCandidates ? maxCandidates : maxRecordsPerRequest;\n\n const inputRecordId = getRecordId(record);\n const queryList = generateQueryList(record, searchSpec);\n const client = createClient({\n url,\n maxRecordsPerRequest: adjustedMaxRecordsPerRequest,\n version: '2.0',\n retrieveAll: false\n });\n\n debug(`Searching matches for ${inputRecordId}`);\n debug(`Generated queryList ${JSON.stringify(queryList)}`);\n\n // if generateQueryList errored we should throw 422\n\n if (queryList.length === 0) { // eslint-disable-line functional/no-conditional-statement\n throw new CandidateSearchError(`Generated query list contains no queries`);\n }\n\n // state.totalRecords : amount of candidate records available to the current query (undefined, if there was no queries left)\n // state.query : current query (undefined if there was no queries left)\n // state.searchCounter : sequence for current search for current query (undefined, if there we no queries left)\n // state.queryCandidateCounter: amount of candidates (records+failures) retrieved from SRU for matching for current query, including the current record+failure set (undefined if there were no queries left)\n // state.queriesLeft : amount of queries left\n // state.queryCounter : sequence for current query\n // state.maxedQueries : queries that resulted in more than serverMaxResults hits\n\n\n return async ({queryOffset = 0, resultSetOffset = 1, totalRecords = 0, searchCounter = 0, queryCandidateCounter = 0, queryCounter = 0, maxedQueries = []}) => {\n const query = queryList[queryOffset];\n\n if (query) {\n const {records, failures, nextOffset, total} = await retrieveRecords();\n\n // If resultSetOffset === 1 this is the first search for the current query\n debugData(`ResultSetOffset: ${resultSetOffset}`);\n const newTotalRecords = resultSetOffset === 1 ? total : totalRecords;\n const newQueryCounter = resultSetOffset === 1 ? queryCounter + 1 : queryCounter;\n const newSearchCounter = resultSetOffset === 1 ? 1 : searchCounter + 1;\n const newQueryCandidateCounter = resultSetOffset === 1 ? records.length + failures.length : queryCandidateCounter + records.length + failures.length;\n\n const maxedQuery = resultSetOffset === 1 ? checkMaxedQuery(query, total, serverMaxResult) : undefined;\n const newMaxedQueries = maxedQuery ? maxedQueries.concat(maxedQuery) : maxedQueries;\n\n if (typeof nextOffset === 'number') {\n debug(`Next search will be for query ${queryOffset} ${query}, starting from record ${nextOffset}`);\n return {records, failures, queryOffset, resultSetOffset: nextOffset, queriesLeft: queryList.length - (queryOffset + 1), totalRecords: newTotalRecords, query, searchCounter: newSearchCounter, queryCandidateCounter: newQueryCandidateCounter, queryCounter: newQueryCounter, maxedQueries: newMaxedQueries};\n }\n debug(`Query ${queryOffset} ${query} done.`);\n debug(`There are (${queryList.length - (queryOffset + 1)} queries left)`);\n return {records, failures, queryOffset: queryOffset + 1, queriesLeft: queryList.length - (queryOffset + 1), totalRecords: newTotalRecords, query, searchCounter: newSearchCounter, queryCandidateCounter: newQueryCandidateCounter, queryCounter: newQueryCounter, maxedQueries: newMaxedQueries};\n }\n\n debug(`All ${queryList.length} queries done, there's no query for ${queryOffset}`);\n return {records: [], failures: [], queriesLeft: 0, queryCounter, maxedQueries};\n\n function retrieveRecords() {\n return new Promise((resolve, reject) => {\n const promises = [];\n // eslint-disable-next-line functional/no-let\n let totalRecords = 0;\n\n debug(`Searching for candidates with query: ${query} (Offset ${resultSetOffset})`);\n\n client.searchRetrieve(query, {startRecord: resultSetOffset})\n .on('error', err => {\n // eslint-disable-next-line functional/no-conditional-statement\n if (err instanceof SruSearchError) {\n debug(`SRU SruSearchError for query: ${query}: ${err}`);\n reject(new CandidateSearchError(`SRU SruSearchError for query: ${query}: ${err}`));\n }\n debug(`SRU error for query: ${query}: ${err}`);\n reject(new CandidateSearchError(`SRU error for query: ${query}: ${err}`));\n })\n .on('total', total => {\n debug(`Got total: ${total}`);\n totalRecords += total;\n })\n .on('end', async nextOffset => {\n try {\n const recordPromises = await Promise.allSettled(promises);\n debug(`All recordPromises: ${JSON.stringify(recordPromises)}`);\n const filtered = recordPromises.filter(r => r.status === 'fulfilled').map(r => r.value);\n const failures = recordPromises.filter(r => r.status === 'rejected').map(r => ({status: r.reason.status, payload: r.reason.payload}));\n\n debug(`Found ${JSON.stringify(recordPromises)} records`);\n debug(`Found ${filtered.length} convertable candidates`);\n debug(`Found ${failures.length} NON-convertable candidates`);\n debug(`Converted: ${JSON.stringify(filtered)}.`);\n debug(`Not converted: ${JSON.stringify(failures)}.`);\n\n\n resolve({nextOffset, records: filtered, failures, total: totalRecords});\n } catch (err) {\n debug(`Error caught on END`);\n reject(err);\n }\n })\n .on('record', recordXML => {\n promises.push(handleRecord()); // eslint-disable-line functional/immutable-data\n\n async function handleRecord() {\n try {\n const recordMarc = await MARCXML.from(recordXML, {subfieldValues: false});\n const recordId = getRecordId(recordMarc);\n\n return {record: recordMarc, id: recordId};\n } catch (err) {\n // What should this do?\n const idFromXML = getRecordIdFromXML(recordXML);\n debugData(`Failed converting record: ${err.message}, id: ${idFromXML}, data: ${recordXML}`);\n //return {message: `Failed converting record: ${err.message}`, id: idFromXML, data: recordXML};\n throw new MatchingError(422, {message: `Failed converting record: ${err.message}`, id: idFromXML || '000000000', data: recordXML});\n }\n }\n });\n });\n }\n };\n\n function checkMaxedQuery(query, total, serverMaxResult) {\n // eslint-disable-next-line functional/no-conditional-statement\n if (total >= serverMaxResult) {\n debug(`WARNING: Query ${query} resulted in ${total} hits which meets the serverMaxResult (${serverMaxResult}) `);\n return query;\n }\n }\n\n\n function getRecordId(record) {\n const [field] = record.get(/^001$/u);\n return field ? field.value : '';\n }\n\n function getRecordIdFromXML(recordXML) {\n //<controlfield tag=\\\"001\\\">015376846</controlfield\n debug(`Cannot yet find possible database record id from recordXML (length ${recordXML.length})`);\n return undefined;\n }\n\n};\n"],"mappings":";;;;;;;;;;;;AA4BA;AACA;AACA;AACA;AACA;AACA;AAAiE;AAAA;AAAA;AAjCjE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAWO,MAAMA,oBAAoB,SAASC,KAAK,CAAC;;AAEhD;;AAEA;AAAA;AAAA,eACe,CAAC;EAACC,MAAM;EAAEC,UAAU;EAAEC,GAAG;EAAEC,aAAa;EAAEC,oBAAoB,GAAG,EAAE;EAAEC,eAAe,GAAG;AAAK,CAAC,KAAK;EAC/GC,sBAAU,CAACC,oBAAoB,CAAC;IAACC,cAAc,EAAE;EAAK,CAAC,CAAC;EAExD,MAAMC,KAAK,GAAG,IAAAC,cAAiB,EAAC,oDAAoD,CAAC;EACrF,MAAMC,SAAS,GAAGF,KAAK,CAACG,MAAM,CAAC,MAAM,CAAC;EAEtCD,SAAS,CAAE,eAAcE,IAAI,CAACC,SAAS,CAACb,UAAU,CAAE,EAAC,CAAC;EACtDU,SAAS,CAAE,QAAOT,GAAI,EAAC,CAAC;EACxBS,SAAS,C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MAAM,CAACiF,GAAG,CAAC,QAAQ,CAAC;IACpC,OAAOD,KAAK,GAAGA,KAAK,CAAChB,KAAK,GAAG,EAAE;EACjC;EAEA,SAASY,kBAAkB,CAACT,SAAS,EAAE;IACrC;IACA1D,KAAK,CAAE,sEAAqE0D,SAAS,CAAC3C,MAAO,GAAE,CAAC;IAChG,OAAOoB,SAAS;EAClB;AAEF,CAAC;AAAA"}
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+
3
+ var _chai = require("chai");
4
+ var _fixura = require("@natlibfi/fixura");
5
+ var _fixugenHttpClient = _interopRequireDefault(require("@natlibfi/fixugen-http-client"));
6
+ var _marcRecord = require("@natlibfi/marc-record");
7
+ var _melindaCommons = require("@natlibfi/melinda-commons");
8
+ var _ = _interopRequireWildcard(require("."));
9
+ var _debug = _interopRequireDefault(require("debug"));
10
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
11
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+ /**
14
+ *
15
+ * @licstart The following is the entire license notice for the JavaScript code in this file.
16
+ *
17
+ * Melinda record matching modules for Javascript
18
+ *
19
+ * Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)
20
+ *
21
+ * This file is part of melinda-record-matching-js
22
+ *
23
+ * melinda-record-matching-js program is free software: you can redistribute it and/or modify
24
+ * it under the terms of the GNU Lesser General Public License as
25
+ * published by the Free Software Foundation, either version 3 of the
26
+ * License, or (at your option) any later version.
27
+ *
28
+ * melinda-record-matching-js is distributed in the hope that it will be useful,
29
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31
+ * GNU Lesser General Public License for more details.
32
+ *
33
+ * You should have received a copy of the GNU Affero General Public License
34
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
35
+ *
36
+ * @licend The above is the entire license notice
37
+ * for the JavaScript code in this file.
38
+ *
39
+ */
40
+
41
+ const debug = (0, _debug.default)('@natlibfi/melinda-record-matching:candidate-search:test');
42
+ describe('candidate-search', () => {
43
+ (0, _fixugenHttpClient.default)({
44
+ callback,
45
+ path: [__dirname, '..', '..', 'test-fixtures', 'candidate-search', 'index'],
46
+ recurse: false,
47
+ fixura: {
48
+ reader: _fixura.READERS.JSON
49
+ }
50
+ });
51
+
52
+ // eslint-disable-next-line max-statements
53
+ async function callback({
54
+ getFixture,
55
+ factoryOptions,
56
+ searchOptions,
57
+ expectedFactoryError = false,
58
+ expectedSearchError = false,
59
+ enabled = true
60
+ }) {
61
+ const url = 'http://foo.bar';
62
+ if (!enabled) {
63
+ return;
64
+ }
65
+ if (expectedFactoryError) {
66
+ if (expectedFactoryError.isCandidateSearchError) {
67
+ (0, _chai.expect)(() => (0, _.default)({
68
+ ...formatFactoryOptions(),
69
+ url
70
+ })).to.throw(_.CandidateSearchError, new RegExp(expectedFactoryError, 'u'));
71
+ return;
72
+ }
73
+ (0, _chai.expect)(() => (0, _.default)({
74
+ ...formatFactoryOptions(),
75
+ url
76
+ })).to.throw(new RegExp(expectedFactoryError, 'u'));
77
+ return;
78
+ }
79
+ const search = (0, _.default)({
80
+ ...formatFactoryOptions(),
81
+ url
82
+ });
83
+ await iterate({
84
+ searchOptions,
85
+ expectedSearchError
86
+ });
87
+ function formatFactoryOptions() {
88
+ debug(`Using factoryOptions: ${JSON.stringify(factoryOptions)}`);
89
+ return {
90
+ ...factoryOptions,
91
+ maxRecordsPerRequest: factoryOptions.maxRecordsPerRequest || 1,
92
+ maxServerResults: factoryOptions.maxServerResults || undefined,
93
+ record: new _marcRecord.MarcRecord(factoryOptions.record, {
94
+ subfieldValues: false
95
+ })
96
+ };
97
+ }
98
+
99
+ // eslint-disable-next-line max-statements
100
+ async function iterate({
101
+ searchOptions,
102
+ expectedSearchError,
103
+ expectedErrorStatus,
104
+ count = 1
105
+ }) {
106
+ const expectedResults = getFixture(`expectedResults${count}.json`);
107
+ if (expectedSearchError) {
108
+ // eslint-disable-line functional/no-conditional-statement
109
+ try {
110
+ await search(searchOptions);
111
+ throw new Error('Expected an error');
112
+ } catch (err) {
113
+ debug(`Got an error: ${err}`);
114
+ (0, _chai.expect)(err).to.be.an('error');
115
+ const errorMessage = err instanceof _melindaCommons.Error ? err.payload.message : err.message;
116
+ const errorStatus = err instanceof _melindaCommons.Error ? err.status : undefined;
117
+ debug(`errorMessage: ${errorMessage}, errorStatus: ${errorStatus}`);
118
+ (0, _chai.expect)(errorMessage).to.match(new RegExp(expectedSearchError, 'u'));
119
+ if (expectedErrorStatus) {
120
+ (0, _chai.expect)(errorStatus).to.be(expectedErrorStatus);
121
+ return;
122
+ }
123
+ return;
124
+ }
125
+ }
126
+
127
+ // eslint-disable-next-line functional/no-conditional-statement
128
+ if (!expectedSearchError) {
129
+ const results = await search(searchOptions);
130
+ (0, _chai.expect)(formatResults(results)).to.eql(expectedResults);
131
+ }
132
+ function formatResults(results) {
133
+ debug(results); //eslint-disable-line
134
+ return {
135
+ ...results,
136
+ records: results.records.map(({
137
+ record,
138
+ id
139
+ }) => ({
140
+ id,
141
+ record: record.toObject()
142
+ }))
143
+ };
144
+ }
145
+ }
146
+ }
147
+ });
148
+ //# sourceMappingURL=index.spec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.spec.js","names":["debug","createDebugLogger","describe","generateTests","callback","path","__dirname","recurse","fixura","reader","READERS","JSON","getFixture","factoryOptions","searchOptions","expectedFactoryError","expectedSearchError","enabled","url","isCandidateSearchError","expect","createSearchInterface","formatFactoryOptions","to","throw","CandidateSearchError","RegExp","search","iterate","stringify","maxRecordsPerRequest","maxServerResults","undefined","record","MarcRecord","subfieldValues","expectedErrorStatus","count","expectedResults","Error","err","be","an","errorMessage","MatchingError","payload","message","errorStatus","status","match","results","formatResults","eql","records","map","id","toObject"],"sources":["../../src/candidate-search/index.spec.js"],"sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Melinda record matching modules for Javascript\n*\n* Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-record-matching-js\n*\n* melinda-record-matching-js program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Lesser General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-record-matching-js is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Lesser General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport {expect} from 'chai';\nimport {READERS} from '@natlibfi/fixura';\nimport generateTests from '@natlibfi/fixugen-http-client';\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport {Error as MatchingError} from '@natlibfi/melinda-commons';\nimport createSearchInterface, {CandidateSearchError} from '.';\nimport createDebugLogger from 'debug';\n\nconst debug = createDebugLogger('@natlibfi/melinda-record-matching:candidate-search:test');\n\ndescribe('candidate-search', () => {\n generateTests({\n callback,\n path: [__dirname, '..', '..', 'test-fixtures', 'candidate-search', 'index'],\n recurse: false,\n fixura: {\n reader: READERS.JSON\n }\n });\n\n // eslint-disable-next-line max-statements\n async function callback({getFixture, factoryOptions, searchOptions, expectedFactoryError = false, expectedSearchError = false, enabled = true}) {\n const url = 'http://foo.bar';\n\n if (!enabled) {\n return;\n }\n\n if (expectedFactoryError) {\n if (expectedFactoryError.isCandidateSearchError) {\n expect(() => createSearchInterface({...formatFactoryOptions(), url})).to.throw(CandidateSearchError, new RegExp(expectedFactoryError, 'u'));\n return;\n }\n\n expect(() => createSearchInterface({...formatFactoryOptions(), url})).to.throw(new RegExp(expectedFactoryError, 'u'));\n return;\n }\n\n const search = createSearchInterface({...formatFactoryOptions(), url});\n await iterate({searchOptions, expectedSearchError});\n\n function formatFactoryOptions() {\n debug(`Using factoryOptions: ${JSON.stringify(factoryOptions)}`);\n return {\n ...factoryOptions,\n maxRecordsPerRequest: factoryOptions.maxRecordsPerRequest || 1,\n maxServerResults: factoryOptions.maxServerResults || undefined,\n record: new MarcRecord(factoryOptions.record, {subfieldValues: false})\n };\n }\n\n // eslint-disable-next-line max-statements\n async function iterate({searchOptions, expectedSearchError, expectedErrorStatus, count = 1}) {\n const expectedResults = getFixture(`expectedResults${count}.json`);\n\n if (expectedSearchError) { // eslint-disable-line functional/no-conditional-statement\n try {\n await search(searchOptions);\n throw new Error('Expected an error');\n } catch (err) {\n debug(`Got an error: ${err}`);\n expect(err).to.be.an('error');\n const errorMessage = err instanceof MatchingError ? err.payload.message : err.message;\n const errorStatus = err instanceof MatchingError ? err.status : undefined;\n debug(`errorMessage: ${errorMessage}, errorStatus: ${errorStatus}`);\n expect(errorMessage).to.match(new RegExp(expectedSearchError, 'u'));\n\n if (expectedErrorStatus) {\n expect(errorStatus).to.be(expectedErrorStatus);\n return;\n }\n return;\n }\n }\n\n // eslint-disable-next-line functional/no-conditional-statement\n if (!expectedSearchError) {\n const results = await search(searchOptions);\n expect(formatResults(results)).to.eql(expectedResults);\n }\n\n function formatResults(results) {\n debug(results); //eslint-disable-line\n return {\n ...results,\n records: results.records.map(({record, id}) => ({id, record: record.toObject()}))\n };\n }\n\n }\n }\n});\n"],"mappings":";;AA4BA;AACA;AACA;AACA;AACA;AACA;AACA;AAAsC;AAAA;AAAA;AAlCtC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAUA,MAAMA,KAAK,GAAG,IAAAC,cAAiB,EAAC,yDAAyD,CAAC;AAE1FC,QAAQ,CAAC,kBAAkB,EAAE,MAAM;EACjC,IAAAC,0BAAa,EAAC;IACZC,QAAQ;IACRC,IAAI,EAAE,CAACC,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,eAAe,EAAE,kBAAkB,EAAE,OAAO,CAAC;IAC3EC,OAAO,EAAE,KAAK;IACdC,MAAM,EAAE;MACNC,MAAM,EAAEC,eAAO,CAACC;IAClB;EACF,CAAC,CAAC;;EAEF;EACA,eAAeP,QAAQ,CAAC;IAACQ,UAAU;IAAEC,cAAc;IAAEC,aAAa;IAAEC,oBAAoB,GAAG,KAAK;IAAEC,mBAAmB,GAAG,KAAK;IAAEC,OAAO,GAAG;EAAI,CAAC,EAAE;IAC9I,MAAMC,GAAG,GAAG,gBAAgB;IAE5B,IAAI,CAACD,OAAO,EAAE;MACZ;IACF;IAEA,IAAIF,oBAAoB,EAAE;MACxB,IAAIA,oBAAoB,CAACI,sBAAsB,EAAE;QAC/C,IAAAC,YAAM,EAAC,MAAM,IAAAC,SAAqB,EAAC;UAAC,GAAGC,oBAAoB,EAAE;UAAEJ;QAAG,CAAC,CAAC,CAAC,CAACK,EAAE,CAACC,KAAK,CAACC,sBAAoB,EAAE,IAAIC,MAAM,CAACX,oBAAoB,EAAE,GAAG,CAAC,CAAC;QAC3I;MACF;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