MCPcopy Create free account
hub / github.com/documentdb/documentdb / PostProcessCursorPage

Function PostProcessCursorPage

pg_documentdb/src/commands/cursors.c:2515–2590  ·  view source on GitHub ↗

* Writes the end of the cursor page. Write the end array, overwrite the cursorId * with the actual one (if it's drained, replace it with 0). * Also creates the result tuple that's (document, continuation) and returns it. */

Source from the content-addressed store, hash-verified

2513 * Also creates the result tuple that's (document, continuation) and returns it.
2514 */
2515Datum
2516PostProcessCursorPage(pgbson_writer *cursorDoc, pgbson_array_writer *arrayWriter,
2517 pgbson_writer *topLevelWriter, int64_t cursorId,
2518 pgbson *continuation, bool persistConnection,
2519 pgbson *lastContinuationToken,
2520 TupleDesc cursorResultTupleDesc)
2521{
2522 /* Finish the cursor doc*/
2523 PgbsonWriterEndArray(cursorDoc, arrayWriter);
2524
2525 /*
2526 * For tailable cursors, append the last continuation token to the cursor.
2527 */
2528 if (lastContinuationToken != NULL)
2529 {
2530 AppendLastContinuationTokenToCursor(cursorDoc, lastContinuationToken);
2531 }
2532
2533 PgbsonWriterEndDocument(topLevelWriter, cursorDoc);
2534 PgbsonWriterAppendDouble(topLevelWriter, "ok", 2, 1);
2535 if (lastContinuationToken != NULL)
2536 {
2537 /*
2538 * TODO: Currently, the operationTime field is applicable only for change
2539 * stream cursors. In future, if there is another tailable cursor type is
2540 * supported, then update the condition above to check specific cursor type.
2541 */
2542 TimestampTz currentTime = GetCurrentTimestamp();
2543 PgbsonWriterAppendTimestamp(topLevelWriter, "operationTime", 13, currentTime);
2544 }
2545
2546 bool queryFullyDrained = continuation == NULL;
2547
2548 /* If this is a oneshot query (singlePage) mark it as drained. */
2549 if (cursorId == 0)
2550 {
2551 queryFullyDrained = true;
2552 }
2553 else if (queryFullyDrained)
2554 {
2555 /* Write out the cursor_id given that the cursor is not drained */
2556 cursorId = 0;
2557 }
2558
2559 if (cursorId != 0)
2560 {
2561 bson_iter_t cursorDocIter;
2562 PgbsonWriterGetIterator(topLevelWriter, &cursorDocIter);
2563 if (!bson_iter_find_descendant(&cursorDocIter, "cursor.id", &cursorDocIter))
2564 {
2565 ereport(ERROR, (errmsg(
2566 "Could not find cursor.id in cursor document. This is a bug")));
2567 }
2568
2569 bson_iter_overwrite_int64(&cursorDocIter, cursorId);
2570 }
2571
2572 /* Returns (continuation bson, cursorPage bson) */

Callers 2

HandleFirstPageRequestFunction · 0.85

Calls 7

PgbsonWriterEndArrayFunction · 0.85
PgbsonWriterEndDocumentFunction · 0.85
PgbsonWriterAppendDoubleFunction · 0.85
PgbsonWriterGetIteratorFunction · 0.85
PgbsonWriterGetPgbsonFunction · 0.85

Tested by

no test coverage detected