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

Function checkIndexes

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

Source from the content-addressed store, hash-verified

331}
332
333async function checkIndexes(stream: GridFSBucketWriteStream): Promise<void> {
334 let remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow(
335 `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms`
336 );
337 const doc = await stream.files.findOne(
338 {},
339 {
340 projection: { _id: 1 },
341 timeoutMS: remainingTimeMS
342 }
343 );
344 if (doc != null) {
345 // If at least one document exists assume the collection has the required index
346 return;
347 }
348
349 const index = { filename: 1, uploadDate: 1 };
350
351 let indexes;
352 remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow(
353 `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms`
354 );
355 const listIndexesOptions = {
356 timeoutMode: remainingTimeMS != null ? CursorTimeoutMode.LIFETIME : undefined,
357 timeoutMS: remainingTimeMS
358 };
359 try {
360 indexes = await stream.files.listIndexes(listIndexesOptions).toArray();
361 } catch (error) {
362 if (error instanceof MongoError && error.code === MONGODB_ERROR_CODES.NamespaceNotFound) {
363 indexes = [];
364 } else {
365 throw error;
366 }
367 }
368
369 const hasFileIndex = !!indexes.find(index => {
370 const keys = Object.keys(index.key);
371 if (keys.length === 2 && index.key.filename === 1 && index.key.uploadDate === 1) {
372 return true;
373 }
374 return false;
375 });
376
377 if (!hasFileIndex) {
378 remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow(
379 `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms`
380 );
381
382 await stream.files.createIndex(index, { background: false, timeoutMS: remainingTimeMS });
383 }
384
385 await checkChunksIndex(stream);
386}
387
388function createFilesDoc(
389 _id: ObjectId,

Callers 1

_constructMethod · 0.85

Calls 7

checkChunksIndexFunction · 0.85
findOneMethod · 0.80
toArrayMethod · 0.80
listIndexesMethod · 0.80
findMethod · 0.45
createIndexMethod · 0.45

Tested by

no test coverage detected