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

Function MergeTextIndexWeights

pg_documentdb/src/metadata/index.c:2024–2084  ·  view source on GitHub ↗

* Merges the weights field with the set of paths used in the key declaration. * If the path is there, it updates the weight. Otherwise, adds the weight * as a new path. * Wildcard weights need special handling. When merging weights, if we are * building the model definition (an IndexDef struct), then we do not add the * wildcard to the weights. This is because the IndexDef is used to construc

Source from the content-addressed store, hash-verified

2022 * the spec. So we add the wildcard to the weights.
2023 */
2024List *
2025MergeTextIndexWeights(List *textIndexes, const bson_value_t *weights, bool *isWildCard,
2026 bool includeWildCardInWeights)
2027{
2028 if (weights->value_type != BSON_TYPE_DOCUMENT)
2029 {
2030 ereport(ERROR, (errmsg("weights must be a valid document")));
2031 }
2032
2033 bson_iter_t weightsIter;
2034 BsonValueInitIterator(weights, &weightsIter);
2035
2036 while (bson_iter_next(&weightsIter))
2037 {
2038 const char *weightPath = bson_iter_key(&weightsIter);
2039 const bson_value_t *currentValue = bson_iter_value(&weightsIter);
2040 bool isWeightWildCard = false;
2041 if (!BsonValueIsNumber(currentValue))
2042 {
2043 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_INVALIDINDEXSPECIFICATIONOPTION),
2044 errmsg("weight for text index needs numeric type")));
2045 }
2046
2047 if (strcmp(weightPath, "$**") == 0)
2048 {
2049 isWeightWildCard = true;
2050 if (isWildCard != NULL)
2051 {
2052 *isWildCard |= isWeightWildCard;
2053 }
2054 }
2055
2056 double weight = BsonValueAsDouble(currentValue);
2057
2058 ListCell *indexCell;
2059 bool found = false;
2060 if (textIndexes != NIL)
2061 {
2062 foreach(indexCell, textIndexes)
2063 {
2064 TextIndexWeights *weightEntry = lfirst(indexCell);
2065 if (strcmp(weightEntry->path, weightPath) == 0)
2066 {
2067 weightEntry->weight = weight;
2068 found = true;
2069 break;
2070 }
2071 }
2072 }
2073
2074 if (!found && (includeWildCardInWeights || !isWeightWildCard))
2075 {
2076 TextIndexWeights *weightEntry = palloc0(sizeof(TextIndexWeights));
2077 weightEntry->path = weightPath;
2078 weightEntry->weight = weight;
2079 textIndexes = lappend(textIndexes, weightEntry);
2080 }
2081 }

Callers 2

SerializeIndexSpecFunction · 0.85

Calls 4

BsonValueInitIteratorFunction · 0.85
BsonValueIsNumberFunction · 0.85
BsonValueAsDoubleFunction · 0.85
foreachFunction · 0.50

Tested by

no test coverage detected