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

Function TruncateDocumentTerm

pg_documentdb/src/opclass/gin_index_term.c:1641–1737  ·  view source on GitHub ↗

* Write and truncate a document into the target document writer accounting * for the index term size limit. */

Source from the content-addressed store, hash-verified

1639 * for the index term size limit.
1640 */
1641static bool
1642TruncateDocumentTerm(int32_t existingTermSize, int32_t softLimit, int32_t hardLimit,
1643 bson_iter_t *documentIterator, pgbson_writer *documentWriter)
1644{
1645 bool forceAsNotTruncated = false;
1646 int32_t currentLength = 0;
1647 bson_type_t lastType = BSON_TYPE_MINKEY;
1648
1649 pgbson_element_writer elementWriter;
1650 while (bson_iter_next(documentIterator))
1651 {
1652 /* Get the current size */
1653 currentLength = (int32_t) PgbsonWriterGetSize(documentWriter);
1654 if (currentLength + existingTermSize > softLimit)
1655 {
1656 /* We've exceeded the limit - mark as truncated */
1657 if (forceAsNotTruncated)
1658 {
1659 ereport(LOG, (errmsg(
1660 "Truncation limit reached and LastType %s is requested as ForceAsNotTruncated",
1661 BsonTypeName(lastType))));
1662 }
1663
1664 return !forceAsNotTruncated;
1665 }
1666 else
1667 {
1668 /* We're under the size limit reset */
1669 forceAsNotTruncated = false;
1670 }
1671
1672 const bson_value_t *iterValue = bson_iter_value(documentIterator);
1673 const StringView pathView = bson_iter_key_string_view(documentIterator);
1674 lastType = iterValue->value_type;
1675
1676 /* Determine how we're going to write this value */
1677 int32_t requiredLengthWithPath = currentLength + existingTermSize +
1678 (int32_t) pathView.length + 2;
1679
1680 bool truncated;
1681 if (requiredLengthWithPath < softLimit)
1682 {
1683 /* Path at least fits under the soft limit, write the path */
1684 PgbsonInitObjectElementWriter(documentWriter, &elementWriter, pathView.string,
1685 pathView.length);
1686
1687 /* Since the path is under the limit, the value for this path can go until the hard limit */
1688 int32_t valueLengthLeft = hardLimit - existingTermSize - pathView.length - 2;
1689 truncated = TruncateNestedEntry(&elementWriter, iterValue,
1690 &forceAsNotTruncated,
1691 valueLengthLeft, currentLength);
1692 }
1693 else if (requiredLengthWithPath < hardLimit)
1694 {
1695 /* The path fits between the soft limit and hard limit
1696 * Write the path only but with MaxKey and mark this path as truncated
1697 */
1698 bson_value_t maxKeyValue = { 0 };

Callers 2

TruncateNestedEntryFunction · 0.85
SerializeTermToWriterFunction · 0.85

Calls 6

PgbsonWriterGetSizeFunction · 0.85
BsonTypeNameFunction · 0.85
TruncateNestedEntryFunction · 0.85
PgbsonWriterAppendValueFunction · 0.85

Tested by

no test coverage detected