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

Function fetchBatch

src/cursor/abstract_cursor.ts:940–984  ·  view source on GitHub ↗

@internal Attempt to obtain more documents

()

Source from the content-addressed store, hash-verified

938
939 /** @internal Attempt to obtain more documents */
940 private async fetchBatch(): Promise<void> {
941 if (this.isClosed) {
942 return;
943 }
944
945 if (this.isDead) {
946 // if the cursor is dead, we clean it up
947 // cleanupCursor should never throw, but if it does it indicates a bug in the driver
948 // and we should surface the error
949 await this.cleanup();
950 return;
951 }
952
953 if (this.cursorId == null) {
954 await this.cursorInit();
955 // If the cursor died or returned documents, return
956 if ((this.documents?.length ?? 0) !== 0 || this.isDead) return;
957 }
958
959 // Otherwise, run a getMore
960 try {
961 const response = await this.getMore();
962 this.cursorId = response.id;
963 this.documents = response;
964 } catch (error) {
965 try {
966 await this.cleanup(undefined, error);
967 } catch (cleanupError) {
968 // `cleanupCursor` should never throw, squash and throw the original error
969 squashError(cleanupError);
970 }
971 throw error;
972 }
973
974 if (this.isDead) {
975 // If we successfully received a response from a cursor BUT the cursor indicates that it is exhausted,
976 // we intentionally clean up the cursor to release its session back into the pool before the cursor
977 // is iterated. This prevents a cursor that is exhausted on the server from holding
978 // onto a session indefinitely until the AbstractCursor is iterated.
979 //
980 // cleanupCursorAsync should never throw, but if it does it indicates a bug in the driver
981 // and we should surface the error
982 await this.cleanup();
983 }
984 }
985
986 /** @internal */
987 private async cleanup(timeoutMS?: number, error?: Error) {

Callers

nothing calls this directly

Calls 3

squashErrorFunction · 0.90
cleanupMethod · 0.45
getMoreMethod · 0.45

Tested by

no test coverage detected