MCPcopy
hub / github.com/mongodb/node-mongodb-native / checkChunksIndex

Function checkChunksIndex

src/gridfs/upload.ts:239–282  ·  view source on GitHub ↗
(stream: GridFSBucketWriteStream)

Source from the content-addressed store, hash-verified

237}
238
239async function checkChunksIndex(stream: GridFSBucketWriteStream): Promise<void> {
240 const index = { files_id: 1, n: 1 };
241
242 let remainingTimeMS;
243 remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow(
244 `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms`
245 );
246
247 let indexes;
248 try {
249 indexes = await stream.chunks
250 .listIndexes({
251 timeoutMode: remainingTimeMS != null ? CursorTimeoutMode.LIFETIME : undefined,
252 timeoutMS: remainingTimeMS
253 })
254 .toArray();
255 } catch (error) {
256 if (error instanceof MongoError && error.code === MONGODB_ERROR_CODES.NamespaceNotFound) {
257 indexes = [];
258 } else {
259 throw error;
260 }
261 }
262
263 const hasChunksIndex = !!indexes.find(index => {
264 const keys = Object.keys(index.key);
265 if (keys.length === 2 && index.key.files_id === 1 && index.key.n === 1) {
266 return true;
267 }
268 return false;
269 });
270
271 if (!hasChunksIndex) {
272 remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow(
273 `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms`
274 );
275 await stream.chunks.createIndex(index, {
276 ...stream.writeConcern,
277 background: true,
278 unique: true,
279 timeoutMS: remainingTimeMS
280 });
281 }
282}
283
284function checkDone(stream: GridFSBucketWriteStream, callback: Callback): void {
285 if (stream.done) {

Callers 1

checkIndexesFunction · 0.85

Calls 5

toArrayMethod · 0.80
listIndexesMethod · 0.80
findMethod · 0.45
createIndexMethod · 0.45

Tested by

no test coverage detected