monsqlize 1.3.1 → 2.0.1
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.
- package/CHANGELOG.md +506 -235
- package/LICENSE +201 -21
- package/README.md +542 -928
- package/changelogs/README.md +163 -0
- package/changelogs/v2.0.0.md +222 -0
- package/changelogs/v2.0.1.md +39 -0
- package/dist/cjs/index.cjs +10788 -0
- package/dist/cjs/mongodb/common/transaction-aware.cjs +10 -0
- package/dist/cjs/transaction/CacheLockManager.cjs +100 -0
- package/dist/cjs/transaction/Transaction.cjs +158 -0
- package/dist/cjs/transaction/TransactionManager.cjs +298 -0
- package/dist/esm/index.mjs +10838 -0
- package/dist/types/base.d.ts +81 -0
- package/dist/types/collection.d.ts +1031 -0
- package/dist/types/expression.d.ts +115 -0
- package/dist/types/index.d.ts +23 -0
- package/dist/types/lock.d.ts +74 -0
- package/dist/types/model.d.ts +530 -0
- package/dist/types/mongodb.d.ts +49 -0
- package/dist/types/monsqlize.d.ts +491 -0
- package/dist/types/pool.d.ts +84 -0
- package/dist/types/runtime.d.ts +362 -0
- package/dist/types/saga.d.ts +143 -0
- package/dist/types/slow-query-log.d.ts +126 -0
- package/dist/types/sync.d.ts +103 -0
- package/dist/types/transaction.d.ts +132 -0
- package/package.json +120 -117
- package/index.d.ts +0 -1289
- package/lib/cache.js +0 -491
- package/lib/common/cursor.js +0 -58
- package/lib/common/docs-urls.js +0 -72
- package/lib/common/index-options.js +0 -222
- package/lib/common/log.js +0 -60
- package/lib/common/namespace.js +0 -21
- package/lib/common/normalize.js +0 -33
- package/lib/common/page-result.js +0 -42
- package/lib/common/runner.js +0 -56
- package/lib/common/server-features.js +0 -231
- package/lib/common/shape-builders.js +0 -26
- package/lib/common/validation.js +0 -112
- package/lib/connect.js +0 -76
- package/lib/constants.js +0 -54
- package/lib/count-queue.js +0 -187
- package/lib/distributed-cache-invalidator.js +0 -259
- package/lib/errors.js +0 -167
- package/lib/index.js +0 -461
- package/lib/infrastructure/ssh-tunnel-ssh2.js +0 -211
- package/lib/infrastructure/ssh-tunnel.js +0 -40
- package/lib/infrastructure/uri-parser.js +0 -35
- package/lib/lock/Lock.js +0 -66
- package/lib/lock/errors.js +0 -27
- package/lib/lock/index.js +0 -12
- package/lib/logger.js +0 -224
- package/lib/model/examples/test.js +0 -114
- package/lib/mongodb/common/accessor-helpers.js +0 -58
- package/lib/mongodb/common/agg-pipeline.js +0 -32
- package/lib/mongodb/common/iid.js +0 -27
- package/lib/mongodb/common/lexicographic-expr.js +0 -52
- package/lib/mongodb/common/shape.js +0 -31
- package/lib/mongodb/common/sort.js +0 -38
- package/lib/mongodb/common/transaction-aware.js +0 -24
- package/lib/mongodb/connect.js +0 -233
- package/lib/mongodb/index.js +0 -591
- package/lib/mongodb/management/admin-ops.js +0 -199
- package/lib/mongodb/management/bookmark-ops.js +0 -166
- package/lib/mongodb/management/cache-ops.js +0 -49
- package/lib/mongodb/management/collection-ops.js +0 -386
- package/lib/mongodb/management/database-ops.js +0 -201
- package/lib/mongodb/management/index-ops.js +0 -474
- package/lib/mongodb/management/index.js +0 -16
- package/lib/mongodb/management/namespace.js +0 -30
- package/lib/mongodb/management/validation-ops.js +0 -267
- package/lib/mongodb/queries/aggregate.js +0 -142
- package/lib/mongodb/queries/chain.js +0 -630
- package/lib/mongodb/queries/count.js +0 -98
- package/lib/mongodb/queries/distinct.js +0 -77
- package/lib/mongodb/queries/find-and-count.js +0 -192
- package/lib/mongodb/queries/find-by-ids.js +0 -235
- package/lib/mongodb/queries/find-one-by-id.js +0 -170
- package/lib/mongodb/queries/find-one.js +0 -70
- package/lib/mongodb/queries/find-page.js +0 -577
- package/lib/mongodb/queries/find.js +0 -170
- package/lib/mongodb/queries/index.js +0 -50
- package/lib/mongodb/queries/watch.js +0 -537
- package/lib/mongodb/writes/delete-many.js +0 -190
- package/lib/mongodb/writes/delete-one.js +0 -182
- package/lib/mongodb/writes/find-one-and-delete.js +0 -202
- package/lib/mongodb/writes/find-one-and-replace.js +0 -238
- package/lib/mongodb/writes/find-one-and-update.js +0 -239
- package/lib/mongodb/writes/increment-one.js +0 -252
- package/lib/mongodb/writes/index.js +0 -41
- package/lib/mongodb/writes/insert-batch.js +0 -507
- package/lib/mongodb/writes/insert-many.js +0 -227
- package/lib/mongodb/writes/insert-one.js +0 -180
- package/lib/mongodb/writes/replace-one.js +0 -215
- package/lib/mongodb/writes/result-handler.js +0 -236
- package/lib/mongodb/writes/update-many.js +0 -221
- package/lib/mongodb/writes/update-one.js +0 -223
- package/lib/mongodb/writes/upsert-one.js +0 -206
- package/lib/multi-level-cache.js +0 -189
- package/lib/operators.js +0 -330
- package/lib/redis-cache-adapter.js +0 -237
- package/lib/slow-query-log/base-storage.js +0 -69
- package/lib/slow-query-log/batch-queue.js +0 -96
- package/lib/slow-query-log/config-manager.js +0 -195
- package/lib/slow-query-log/index.js +0 -237
- package/lib/slow-query-log/mongodb-storage.js +0 -323
- package/lib/slow-query-log/query-hash.js +0 -38
- package/lib/transaction/CacheLockManager.js +0 -161
- package/lib/transaction/DistributedCacheLockManager.js +0 -474
- package/lib/transaction/Transaction.js +0 -314
- package/lib/transaction/TransactionManager.js +0 -266
- package/lib/transaction/index.js +0 -10
- package/lib/utils/objectid-converter.js +0 -566
|
@@ -1,507 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* insertBatch 操作实现(改进版 - 支持重试机制)
|
|
3
|
-
* 分批批量插入大量文档到集合
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const { createError, ErrorCodes } = require('../../errors');
|
|
7
|
-
const CacheFactory = require('../../cache');
|
|
8
|
-
const { isInTransaction, getTransactionFromSession } = require('../common/transaction-aware');
|
|
9
|
-
const { convertObjectIdStrings } = require('../../utils/objectid-converter');
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 创建 insertBatch 操作
|
|
13
|
-
* @param {Object} context - 模块上下文
|
|
14
|
-
* @param {Object} context.insertMany - insertMany 方法实例
|
|
15
|
-
* @param {Object} context.cache - 缓存实例
|
|
16
|
-
* @param {Object} context.logger - 日志实例
|
|
17
|
-
* @param {Object} context.defaults - 默认配置
|
|
18
|
-
* @param {string} context.collectionName - 集合名称
|
|
19
|
-
* @param {string} context.effectiveDbName - 数据库名称
|
|
20
|
-
* @param {string} context.instanceId - 实例ID
|
|
21
|
-
* @returns {Object} 包含 insertBatch 方法的对象
|
|
22
|
-
*/
|
|
23
|
-
function createInsertBatchOps(context) {
|
|
24
|
-
const { insertMany, cache, logger, defaults, collectionName, effectiveDbName: databaseName, instanceId } = context;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* 分批批量插入多个文档
|
|
28
|
-
* @param {Array<Object>} documents - 要插入的文档数组(必需)
|
|
29
|
-
* @param {Object} [options] - 操作选项
|
|
30
|
-
* @param {number} [options.batchSize=1000] - 每批插入的文档数量
|
|
31
|
-
* @param {number} [options.concurrency=1] - 并发批次数(1=串行,>1=并行)
|
|
32
|
-
* @param {boolean} [options.ordered=false] - 批次内是否按顺序插入
|
|
33
|
-
* @param {Function} [options.onProgress] - 进度回调函数 (progress) => {}
|
|
34
|
-
* @param {string} [options.onError='stop'] - 错误处理策略: 'stop'/'skip'/'collect'/'retry'
|
|
35
|
-
* @param {number} [options.retryAttempts=3] - 失败批次最大重试次数(onError='retry'时有效)
|
|
36
|
-
* @param {number} [options.retryDelay=1000] - 重试延迟时间(毫秒)
|
|
37
|
-
* @param {Function} [options.onRetry] - 重试回调函数 (retryInfo) => {}
|
|
38
|
-
* @param {Object} [options.writeConcern] - 写关注选项
|
|
39
|
-
* @param {boolean} [options.bypassDocumentValidation] - 是否绕过文档验证
|
|
40
|
-
* @param {string} [options.comment] - 操作注释(用于日志追踪)
|
|
41
|
-
* @returns {Promise<Object>} 插入结果 { acknowledged, totalCount, insertedCount, batchCount, errors, retries, insertedIds }
|
|
42
|
-
*/
|
|
43
|
-
const insertBatch = async function insertBatch(documents, options = {}) {
|
|
44
|
-
const startTime = Date.now();
|
|
45
|
-
|
|
46
|
-
// 1. 参数验证
|
|
47
|
-
if (!Array.isArray(documents)) {
|
|
48
|
-
throw createError(
|
|
49
|
-
ErrorCodes.DOCUMENTS_REQUIRED,
|
|
50
|
-
'documents 必须是数组类型',
|
|
51
|
-
[{ field: 'documents', type: 'array.required', message: 'documents 是必需参数且必须是数组' }]
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (documents.length === 0) {
|
|
56
|
-
throw createError(
|
|
57
|
-
ErrorCodes.DOCUMENTS_REQUIRED,
|
|
58
|
-
'documents 数组不能为空',
|
|
59
|
-
[{ field: 'documents', type: 'array.min', message: 'documents 至少需要包含一个文档' }]
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// ✅ v1.3.0: 自动转换 ObjectId 字符串
|
|
64
|
-
const convertedDocuments = documents.map(doc => convertObjectIdStrings(doc, 'document', 0, new WeakSet(), {
|
|
65
|
-
logger: context.logger,
|
|
66
|
-
excludeFields: context.autoConvertConfig?.excludeFields,
|
|
67
|
-
customFieldPatterns: context.autoConvertConfig?.customFieldPatterns,
|
|
68
|
-
maxDepth: context.autoConvertConfig?.maxDepth
|
|
69
|
-
}));
|
|
70
|
-
|
|
71
|
-
// 2. 提取选项
|
|
72
|
-
const {
|
|
73
|
-
batchSize = 1000,
|
|
74
|
-
concurrency = 1,
|
|
75
|
-
ordered = false,
|
|
76
|
-
onProgress,
|
|
77
|
-
onError = 'stop',
|
|
78
|
-
retryAttempts = 3,
|
|
79
|
-
retryDelay = 1000,
|
|
80
|
-
onRetry,
|
|
81
|
-
...insertOptions
|
|
82
|
-
} = options;
|
|
83
|
-
|
|
84
|
-
// 验证参数
|
|
85
|
-
if (typeof batchSize !== 'number' || batchSize < 1) {
|
|
86
|
-
throw createError(
|
|
87
|
-
ErrorCodes.INVALID_PARAMETER,
|
|
88
|
-
'batchSize 必须是大于 0 的数字',
|
|
89
|
-
[{ field: 'batchSize', type: 'number.min', message: 'batchSize 必须大于 0' }]
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (typeof concurrency !== 'number' || concurrency < 1) {
|
|
94
|
-
throw createError(
|
|
95
|
-
ErrorCodes.INVALID_PARAMETER,
|
|
96
|
-
'concurrency 必须是大于 0 的数字',
|
|
97
|
-
[{ field: 'concurrency', type: 'number.min', message: 'concurrency 必须大于 0' }]
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const validErrorStrategies = ['stop', 'skip', 'collect', 'retry'];
|
|
102
|
-
if (!validErrorStrategies.includes(onError)) {
|
|
103
|
-
throw createError(
|
|
104
|
-
ErrorCodes.INVALID_PARAMETER,
|
|
105
|
-
`onError 必须是以下值之一: ${validErrorStrategies.join(', ')}`,
|
|
106
|
-
[{ field: 'onError', type: 'enum', message: `有效值: ${validErrorStrategies.join(', ')}` }]
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (typeof retryAttempts !== 'number' || retryAttempts < 0) {
|
|
111
|
-
throw createError(
|
|
112
|
-
ErrorCodes.INVALID_PARAMETER,
|
|
113
|
-
'retryAttempts 必须是非负数',
|
|
114
|
-
[{ field: 'retryAttempts', type: 'number.min', message: 'retryAttempts 必须 >= 0' }]
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (typeof retryDelay !== 'number' || retryDelay < 0) {
|
|
119
|
-
throw createError(
|
|
120
|
-
ErrorCodes.INVALID_PARAMETER,
|
|
121
|
-
'retryDelay 必须是非负数',
|
|
122
|
-
[{ field: 'retryDelay', type: 'number.min', message: 'retryDelay 必须 >= 0' }]
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// 3. 构建操作上下文
|
|
127
|
-
const operation = 'insertBatch';
|
|
128
|
-
const ns = `${databaseName}.${collectionName}`;
|
|
129
|
-
const totalCount = convertedDocuments.length;
|
|
130
|
-
const totalBatches = Math.ceil(totalCount / batchSize);
|
|
131
|
-
|
|
132
|
-
// 结果统计
|
|
133
|
-
const result = {
|
|
134
|
-
acknowledged: true,
|
|
135
|
-
totalCount,
|
|
136
|
-
insertedCount: 0,
|
|
137
|
-
batchCount: totalBatches,
|
|
138
|
-
errors: [],
|
|
139
|
-
retries: [], // 新增:重试记录
|
|
140
|
-
insertedIds: {}
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
logger.debug(`[${operation}] 开始分批插入`, {
|
|
144
|
-
ns,
|
|
145
|
-
totalCount,
|
|
146
|
-
batchSize,
|
|
147
|
-
totalBatches,
|
|
148
|
-
concurrency,
|
|
149
|
-
onError,
|
|
150
|
-
retryAttempts: onError === 'retry' ? retryAttempts : 0
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
try {
|
|
154
|
-
// 4. 分割文档为批次
|
|
155
|
-
const batches = [];
|
|
156
|
-
for (let i = 0; i < convertedDocuments.length; i += batchSize) {
|
|
157
|
-
batches.push({
|
|
158
|
-
index: batches.length,
|
|
159
|
-
documents: convertedDocuments.slice(i, Math.min(i + batchSize, convertedDocuments.length)),
|
|
160
|
-
startIndex: i
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// 5. 执行批次插入
|
|
165
|
-
if (concurrency === 1) {
|
|
166
|
-
await executeSequential(batches, result, insertOptions, onProgress, {
|
|
167
|
-
onError,
|
|
168
|
-
retryAttempts,
|
|
169
|
-
retryDelay,
|
|
170
|
-
onRetry,
|
|
171
|
-
logger,
|
|
172
|
-
insertMany
|
|
173
|
-
});
|
|
174
|
-
} else {
|
|
175
|
-
await executeConcurrent(batches, result, insertOptions, onProgress, concurrency, {
|
|
176
|
-
onError,
|
|
177
|
-
retryAttempts,
|
|
178
|
-
retryDelay,
|
|
179
|
-
onRetry,
|
|
180
|
-
logger,
|
|
181
|
-
insertMany
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// 6. 最终缓存失效
|
|
186
|
-
if (result.insertedCount > 0 && cache) {
|
|
187
|
-
try {
|
|
188
|
-
const nsObj = {
|
|
189
|
-
iid: instanceId,
|
|
190
|
-
type: 'mongodb',
|
|
191
|
-
db: databaseName,
|
|
192
|
-
collection: collectionName
|
|
193
|
-
};
|
|
194
|
-
const pattern = CacheFactory.buildNamespacePattern(nsObj);
|
|
195
|
-
const deleted = await cache.delPattern(pattern);
|
|
196
|
-
|
|
197
|
-
if (deleted > 0) {
|
|
198
|
-
logger.debug(`[${operation}] 自动失效缓存: ${nsObj.db}.${nsObj.collection}, 删除 ${deleted} 个缓存键`);
|
|
199
|
-
}
|
|
200
|
-
} catch (cacheErr) {
|
|
201
|
-
logger.warn(`[${operation}] 缓存失效失败: ${cacheErr.message}`, { ns, error: cacheErr });
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// 7. 记录完成日志
|
|
206
|
-
const duration = Date.now() - startTime;
|
|
207
|
-
const slowThreshold = defaults.slowThreshold || 5000;
|
|
208
|
-
|
|
209
|
-
if (duration > slowThreshold) {
|
|
210
|
-
logger.warn(`[${operation}] 慢操作警告`, {
|
|
211
|
-
ns,
|
|
212
|
-
duration,
|
|
213
|
-
threshold: slowThreshold,
|
|
214
|
-
totalCount,
|
|
215
|
-
insertedCount: result.insertedCount,
|
|
216
|
-
batchCount: result.batchCount,
|
|
217
|
-
errorCount: result.errors.length,
|
|
218
|
-
retryCount: result.retries.length,
|
|
219
|
-
comment: insertOptions.comment
|
|
220
|
-
});
|
|
221
|
-
} else {
|
|
222
|
-
logger.debug(`[${operation}] 操作完成`, {
|
|
223
|
-
ns,
|
|
224
|
-
duration,
|
|
225
|
-
totalCount,
|
|
226
|
-
insertedCount: result.insertedCount,
|
|
227
|
-
batchCount: result.batchCount,
|
|
228
|
-
errorCount: result.errors.length,
|
|
229
|
-
retryCount: result.retries.length
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
// 8. 如果有错误且策略是 stop,抛出错误
|
|
234
|
-
if (result.errors.length > 0 && onError === 'stop') {
|
|
235
|
-
throw createError(
|
|
236
|
-
ErrorCodes.WRITE_ERROR,
|
|
237
|
-
`insertBatch 操作失败: 在批次 ${result.errors[0].batchIndex + 1}/${totalBatches} 遇到错误`,
|
|
238
|
-
result.errors,
|
|
239
|
-
result.errors[0].error
|
|
240
|
-
);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
return result;
|
|
244
|
-
|
|
245
|
-
} catch (error) {
|
|
246
|
-
const duration = Date.now() - startTime;
|
|
247
|
-
|
|
248
|
-
logger.error(`[${operation}] 操作失败`, {
|
|
249
|
-
ns,
|
|
250
|
-
duration,
|
|
251
|
-
error: error.message,
|
|
252
|
-
code: error.code,
|
|
253
|
-
totalCount,
|
|
254
|
-
insertedCount: result.insertedCount,
|
|
255
|
-
errorCount: result.errors.length,
|
|
256
|
-
retryCount: result.retries.length
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
if (error.code === ErrorCodes.WRITE_ERROR && result.errors.length > 0) {
|
|
260
|
-
throw error;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
throw createError(
|
|
264
|
-
ErrorCodes.WRITE_ERROR,
|
|
265
|
-
`insertBatch 操作失败: ${error.message}`,
|
|
266
|
-
null,
|
|
267
|
-
error
|
|
268
|
-
);
|
|
269
|
-
}
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* 执行单个批次插入(带重试)
|
|
274
|
-
*/
|
|
275
|
-
async function executeBatchWithRetry(batch, insertOptions, retryContext) {
|
|
276
|
-
const { onError, retryAttempts, retryDelay, onRetry, logger, insertMany } = retryContext;
|
|
277
|
-
|
|
278
|
-
let lastError = null;
|
|
279
|
-
let attempts = 0;
|
|
280
|
-
const maxAttempts = onError === 'retry' ? retryAttempts + 1 : 1;
|
|
281
|
-
|
|
282
|
-
while (attempts < maxAttempts) {
|
|
283
|
-
try {
|
|
284
|
-
const batchResult = await insertMany(batch.documents, {
|
|
285
|
-
...insertOptions,
|
|
286
|
-
ordered: insertOptions.ordered !== undefined ? insertOptions.ordered : false
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
// 成功
|
|
290
|
-
return {
|
|
291
|
-
success: true,
|
|
292
|
-
batch,
|
|
293
|
-
result: batchResult,
|
|
294
|
-
attempts
|
|
295
|
-
};
|
|
296
|
-
|
|
297
|
-
} catch (error) {
|
|
298
|
-
lastError = error;
|
|
299
|
-
attempts++;
|
|
300
|
-
|
|
301
|
-
// 如果还可以重试
|
|
302
|
-
if (attempts < maxAttempts) {
|
|
303
|
-
logger.warn(`[insertBatch] 批次 ${batch.index + 1} 失败,准备重试 (${attempts}/${retryAttempts})`, {
|
|
304
|
-
error: error.message,
|
|
305
|
-
batchIndex: batch.index
|
|
306
|
-
});
|
|
307
|
-
|
|
308
|
-
// 触发重试回调
|
|
309
|
-
if (onRetry) {
|
|
310
|
-
onRetry({
|
|
311
|
-
batchIndex: batch.index,
|
|
312
|
-
attempt: attempts,
|
|
313
|
-
maxAttempts: retryAttempts,
|
|
314
|
-
error,
|
|
315
|
-
nextRetryDelay: retryDelay
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
// 延迟后重试
|
|
320
|
-
if (retryDelay > 0) {
|
|
321
|
-
await new Promise(resolve => setTimeout(resolve, retryDelay));
|
|
322
|
-
}
|
|
323
|
-
} else {
|
|
324
|
-
// 重试次数用尽
|
|
325
|
-
logger.error(`[insertBatch] 批次 ${batch.index + 1} 重试失败,已达最大重试次数`, {
|
|
326
|
-
error: error.message,
|
|
327
|
-
attempts,
|
|
328
|
-
batchIndex: batch.index
|
|
329
|
-
});
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
// 所有重试都失败
|
|
335
|
-
return {
|
|
336
|
-
success: false,
|
|
337
|
-
batch,
|
|
338
|
-
error: lastError,
|
|
339
|
-
attempts
|
|
340
|
-
};
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
* 串行执行批次插入
|
|
345
|
-
*/
|
|
346
|
-
async function executeSequential(batches, result, insertOptions, onProgress, retryContext) {
|
|
347
|
-
for (const batch of batches) {
|
|
348
|
-
const batchResult = await executeBatchWithRetry(batch, insertOptions, retryContext);
|
|
349
|
-
|
|
350
|
-
if (batchResult.success) {
|
|
351
|
-
// 更新结果
|
|
352
|
-
result.insertedCount += batchResult.result.insertedCount;
|
|
353
|
-
|
|
354
|
-
// 合并 insertedIds
|
|
355
|
-
if (batchResult.result.insertedIds) {
|
|
356
|
-
for (const [localIndex, id] of Object.entries(batchResult.result.insertedIds)) {
|
|
357
|
-
const globalIndex = batch.startIndex + parseInt(localIndex);
|
|
358
|
-
result.insertedIds[globalIndex] = id;
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
// 记录重试信息
|
|
363
|
-
if (batchResult.attempts > 0) {
|
|
364
|
-
result.retries.push({
|
|
365
|
-
batchIndex: batch.index,
|
|
366
|
-
attempts: batchResult.attempts,
|
|
367
|
-
success: true
|
|
368
|
-
});
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
} else {
|
|
372
|
-
const errorInfo = {
|
|
373
|
-
batchIndex: batch.index,
|
|
374
|
-
batchStartIndex: batch.startIndex,
|
|
375
|
-
batchSize: batch.documents.length,
|
|
376
|
-
error: batchResult.error,
|
|
377
|
-
code: batchResult.error.code,
|
|
378
|
-
message: batchResult.error.message,
|
|
379
|
-
attempts: batchResult.attempts
|
|
380
|
-
};
|
|
381
|
-
|
|
382
|
-
result.errors.push(errorInfo);
|
|
383
|
-
|
|
384
|
-
// 记录重试信息
|
|
385
|
-
if (batchResult.attempts > 1) {
|
|
386
|
-
result.retries.push({
|
|
387
|
-
batchIndex: batch.index,
|
|
388
|
-
attempts: batchResult.attempts,
|
|
389
|
-
success: false
|
|
390
|
-
});
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
// 根据错误策略处理
|
|
394
|
-
if (retryContext.onError === 'stop') {
|
|
395
|
-
throw batchResult.error;
|
|
396
|
-
} else if (retryContext.onError === 'skip' || retryContext.onError === 'retry') {
|
|
397
|
-
retryContext.logger.warn(`[insertBatch] 跳过失败批次 ${batch.index + 1}/${batches.length}`, errorInfo);
|
|
398
|
-
} else if (retryContext.onError === 'collect') {
|
|
399
|
-
retryContext.logger.warn(`[insertBatch] 收集错误,继续执行 ${batch.index + 1}/${batches.length}`, errorInfo);
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
// 触发进度回调
|
|
404
|
-
if (onProgress) {
|
|
405
|
-
onProgress({
|
|
406
|
-
currentBatch: batch.index + 1,
|
|
407
|
-
totalBatches: batches.length,
|
|
408
|
-
inserted: result.insertedCount,
|
|
409
|
-
total: result.totalCount,
|
|
410
|
-
percentage: Math.round((result.insertedCount / result.totalCount) * 100),
|
|
411
|
-
errors: result.errors.length,
|
|
412
|
-
retries: result.retries.length
|
|
413
|
-
});
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
/**
|
|
419
|
-
* 并发执行批次插入
|
|
420
|
-
*/
|
|
421
|
-
async function executeConcurrent(batches, result, insertOptions, onProgress, concurrency, retryContext) {
|
|
422
|
-
const executing = [];
|
|
423
|
-
const results = [];
|
|
424
|
-
|
|
425
|
-
for (let i = 0; i < batches.length; i++) {
|
|
426
|
-
const batch = batches[i];
|
|
427
|
-
|
|
428
|
-
const promise = executeBatchWithRetry(batch, insertOptions, retryContext);
|
|
429
|
-
|
|
430
|
-
results.push(promise);
|
|
431
|
-
executing.push(promise);
|
|
432
|
-
|
|
433
|
-
// 控制并发数量
|
|
434
|
-
if (executing.length >= concurrency) {
|
|
435
|
-
await Promise.race(executing);
|
|
436
|
-
executing.splice(0, executing.findIndex(p => p === promise) + 1);
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
// 等待当前批次
|
|
440
|
-
const batchResult = await promise;
|
|
441
|
-
|
|
442
|
-
// 更新结果
|
|
443
|
-
if (batchResult.success) {
|
|
444
|
-
result.insertedCount += batchResult.result.insertedCount;
|
|
445
|
-
|
|
446
|
-
if (batchResult.result.insertedIds) {
|
|
447
|
-
for (const [localIndex, id] of Object.entries(batchResult.result.insertedIds)) {
|
|
448
|
-
const globalIndex = batch.startIndex + parseInt(localIndex);
|
|
449
|
-
result.insertedIds[globalIndex] = id;
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
if (batchResult.attempts > 0) {
|
|
454
|
-
result.retries.push({
|
|
455
|
-
batchIndex: batch.index,
|
|
456
|
-
attempts: batchResult.attempts,
|
|
457
|
-
success: true
|
|
458
|
-
});
|
|
459
|
-
}
|
|
460
|
-
} else {
|
|
461
|
-
const errorInfo = {
|
|
462
|
-
batchIndex: batch.index,
|
|
463
|
-
batchStartIndex: batch.startIndex,
|
|
464
|
-
batchSize: batch.documents.length,
|
|
465
|
-
error: batchResult.error,
|
|
466
|
-
code: batchResult.error.code,
|
|
467
|
-
message: batchResult.error.message,
|
|
468
|
-
attempts: batchResult.attempts
|
|
469
|
-
};
|
|
470
|
-
|
|
471
|
-
result.errors.push(errorInfo);
|
|
472
|
-
|
|
473
|
-
if (batchResult.attempts > 1) {
|
|
474
|
-
result.retries.push({
|
|
475
|
-
batchIndex: batch.index,
|
|
476
|
-
attempts: batchResult.attempts,
|
|
477
|
-
success: false
|
|
478
|
-
});
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
if (retryContext.onError === 'stop') {
|
|
482
|
-
throw batchResult.error;
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
// 触发进度回调
|
|
487
|
-
if (onProgress) {
|
|
488
|
-
onProgress({
|
|
489
|
-
currentBatch: i + 1,
|
|
490
|
-
totalBatches: batches.length,
|
|
491
|
-
inserted: result.insertedCount,
|
|
492
|
-
total: result.totalCount,
|
|
493
|
-
percentage: Math.round((result.insertedCount / result.totalCount) * 100),
|
|
494
|
-
errors: result.errors.length,
|
|
495
|
-
retries: result.retries.length
|
|
496
|
-
});
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
await Promise.all(results);
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
return { insertBatch };
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
module.exports = { createInsertBatchOps };
|
|
507
|
-
|