* Write and truncate an array into the target array writer accounting * for the index term size limit. * * dataSize: currentsize of the arrayWriter * indexTermSoftLimit: Before writing any new element we check if it will take us over the soft limit. * If it does, we don't write it and return as truncated. This is to make sure that the index terms * are comparable irrespective of
| 1572 | * |
| 1573 | */ |
| 1574 | static bool |
| 1575 | TruncateArrayTerm(int32_t dataSize, int32_t indexTermSoftLimit, int32_t |
| 1576 | indexTermHardLimit, |
| 1577 | bson_iter_t *arrayIterator, pgbson_array_writer *arrayWriter) |
| 1578 | { |
| 1579 | bool forceAsNotTruncated = false; |
| 1580 | int32_t currentLength = 0; |
| 1581 | bson_type_t lastType = BSON_TYPE_MINKEY; |
| 1582 | |
| 1583 | pgbson_element_writer elementWriter; |
| 1584 | PgbsonInitArrayElementWriter(arrayWriter, &elementWriter); |
| 1585 | while (bson_iter_next(arrayIterator)) |
| 1586 | { |
| 1587 | /* Get the current size */ |
| 1588 | currentLength = (int32_t) PgbsonArrayWriterGetSize(arrayWriter); |
| 1589 | |
| 1590 | /* We stop writing if we are over the soft limit. */ |
| 1591 | if (currentLength + dataSize > indexTermSoftLimit) |
| 1592 | { |
| 1593 | /* We've exceeded the limit - mark as truncated */ |
| 1594 | if (forceAsNotTruncated) |
| 1595 | { |
| 1596 | ereport(LOG, (errmsg( |
| 1597 | "Truncation limit reached and LastType %s is requested as ForceAsNotTruncated", |
| 1598 | BsonTypeName(lastType)))); |
| 1599 | } |
| 1600 | |
| 1601 | return !forceAsNotTruncated; |
| 1602 | } |
| 1603 | else |
| 1604 | { |
| 1605 | /* We're under the size limit reset */ |
| 1606 | forceAsNotTruncated = false; |
| 1607 | } |
| 1608 | |
| 1609 | const bson_value_t *iterValue = bson_iter_value(arrayIterator); |
| 1610 | |
| 1611 | /* Leave up to 4 bytes from the hard limit for the path\0 */ |
| 1612 | int32_t valueLengthLeft = indexTermHardLimit - dataSize - 4; |
| 1613 | lastType = iterValue->value_type; |
| 1614 | |
| 1615 | /* Values can go until the hard limit. */ |
| 1616 | bool truncated = TruncateNestedEntry(&elementWriter, iterValue, |
| 1617 | &forceAsNotTruncated, |
| 1618 | valueLengthLeft, currentLength); |
| 1619 | if (truncated) |
| 1620 | { |
| 1621 | return true; |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | currentLength = (int32_t) PgbsonArrayWriterGetSize(arrayWriter); |
| 1626 | if (currentLength + dataSize > indexTermSoftLimit) |
| 1627 | { |
| 1628 | /* We've exceeded the limit - mark as truncated */ |
| 1629 | return !forceAsNotTruncated; |
| 1630 | } |
| 1631 |
no test coverage detected