* Serializes a given term to a pgbson writer honoring the truncation limits. * Returns whether the term was truncated or not. */
| 1742 | * Returns whether the term was truncated or not. |
| 1743 | */ |
| 1744 | static bool |
| 1745 | SerializeTermToWriter(pgbson_writer *writer, pgbsonelement *indexElement, |
| 1746 | const IndexTermCreateMetadata *termMetadata, |
| 1747 | bool allowValueOnly, bool *isValueOnly) |
| 1748 | { |
| 1749 | /* Bson size + \0 overhead + path + \0 + type marker */ |
| 1750 | StringView indexPath = |
| 1751 | { |
| 1752 | .length = indexElement->pathLength, |
| 1753 | .string = indexElement->path |
| 1754 | }; |
| 1755 | |
| 1756 | char *newPath = NULL; |
| 1757 | |
| 1758 | if (termMetadata->pathPrefix.length > 0 && indexPath.length > 0 && |
| 1759 | !termMetadata->isWildcard) |
| 1760 | { |
| 1761 | if (!StringViewEquals(&indexPath, &termMetadata->pathPrefix)) |
| 1762 | { |
| 1763 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_INTERNALERROR), |
| 1764 | errmsg( |
| 1765 | "Wildcard Prefix path encountered with non-wildcard index - path %s, prefix %s", |
| 1766 | indexPath.string, termMetadata->pathPrefix.string))); |
| 1767 | } |
| 1768 | |
| 1769 | /* Index term should occupy a minimal amount of space */ |
| 1770 | if (termMetadata->allowValueOnly && allowValueOnly) |
| 1771 | { |
| 1772 | indexPath.length = 0; |
| 1773 | indexPath.string = ""; |
| 1774 | *isValueOnly = true; |
| 1775 | } |
| 1776 | else |
| 1777 | { |
| 1778 | indexPath.length = 1; |
| 1779 | indexPath.string = "$"; |
| 1780 | } |
| 1781 | } |
| 1782 | else if (termMetadata->indexTermSizeLimit > 0 && termMetadata->isWildcard) |
| 1783 | { |
| 1784 | /* If it is a single path wildcard index with a non root projection we can trim down the key by removing the prefix. */ |
| 1785 | if (!termMetadata->isWildcardProjection && termMetadata->pathPrefix.length > 0 && |
| 1786 | indexPath.length > 0) |
| 1787 | { |
| 1788 | int32_t newIndexPathLength = indexPath.length - |
| 1789 | termMetadata->pathPrefix.length; |
| 1790 | |
| 1791 | /* We only proceed with the optimization if the new index path length >= 0, otherwise we lose the root term */ |
| 1792 | if (newIndexPathLength == 0) |
| 1793 | { |
| 1794 | indexPath.string = "$"; |
| 1795 | indexPath.length = 1; |
| 1796 | } |
| 1797 | if (newIndexPathLength > 0) |
| 1798 | { |
| 1799 | newPath = palloc0(sizeof(char) * (newIndexPathLength + 2)); |
| 1800 | |
| 1801 | /* Build the $.suffix string i.e if the pathPrefix is a.b.c.d.e.$** and the index element path is a.b.c.d.e.f.g we can transform to $.f.g */ |
no test coverage detected