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

Function ValidateWeightsSpec

pg_documentdb/src/opclass/bson_text_gin.c:1209–1246  ·  view source on GitHub ↗

* Validates the incoming weight spec into create_indexes */

Source from the content-addressed store, hash-verified

1207 * Validates the incoming weight spec into create_indexes
1208 */
1209static void
1210ValidateWeightsSpec(const char *weightsSpec)
1211{
1212 if (weightsSpec == NULL || strlen(weightsSpec) == 0)
1213 {
1214 /* Wildcard */
1215 return;
1216 }
1217
1218 pgbson *bson = PgbsonInitFromJson(weightsSpec);
1219
1220 int numCustomWeights = 0;
1221 bson_iter_t weightsIter;
1222 PgbsonInitIterator(bson, &weightsIter);
1223
1224 while (bson_iter_next(&weightsIter))
1225 {
1226 if (bson_iter_key_len(&weightsIter) == 0)
1227 {
1228 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_INVALIDINDEXSPECIFICATIONOPTION),
1229 errmsg("Weights must have a valid path")));
1230 }
1231
1232 double value = BsonValueAsDouble(bson_iter_value(&weightsIter));
1233 if (value != 1.0)
1234 {
1235 numCustomWeights++;
1236 }
1237 }
1238
1239 /* Postgres supports only 4 weights A, B, C, D */
1240 /* Since we reserve "D" as weight 1, we can only have 3 custom weights */
1241 if (numCustomWeights > 3)
1242 {
1243 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_COMMANDNOTSUPPORTED),
1244 errmsg("Cannot have more than 3 custom weights in the index")));
1245 }
1246}
1247
1248
1249/*

Callers

nothing calls this directly

Calls 3

PgbsonInitFromJsonFunction · 0.85
PgbsonInitIteratorFunction · 0.85
BsonValueAsDoubleFunction · 0.85

Tested by

no test coverage detected