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

Function FillWeightsSpec

pg_documentdb/src/opclass/bson_text_gin.c:1080–1203  ·  view source on GitHub ↗

* Populates the index option weights into the index spec * This includes the path and associated weights encoded as follows * [ ]+ */

Source from the content-addressed store, hash-verified

1078 * <numPaths><Datum[4] of weights>[<pathLength><path><weightIndex>]+
1079 */
1080static Size
1081FillWeightsSpec(const char *weightsSpec, void *buffer)
1082{
1083 /* Weights count + Weight array (for rank) */
1084 Size sizeRequired = sizeof(uint32_t) + (sizeof(Datum) * 4);
1085
1086 pgbson *bson;
1087 if (weightsSpec != NULL && strlen(weightsSpec) > 0)
1088 {
1089 bson = PgbsonInitFromJson(weightsSpec);
1090 }
1091 else
1092 {
1093 bson = PgbsonInitEmpty();
1094 }
1095
1096 bson_iter_t weightsIter;
1097 PgbsonInitIterator(bson, &weightsIter);
1098
1099 int pathCount = 0;
1100 float4 maxWeight = 0;
1101 while (bson_iter_next(&weightsIter))
1102 {
1103 pathCount++;
1104
1105 StringView pathView = bson_iter_key_string_view(&weightsIter);
1106 if (pathView.length == 0)
1107 {
1108 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE), errmsg(
1109 "filter must have a valid path")));
1110 }
1111
1112 /* add the prefixed path length */
1113 sizeRequired += sizeof(uint32_t);
1114
1115 /* add the path size */
1116 sizeRequired += pathView.length;
1117
1118 /* Add 1 character for 'weight' char (A, B, C, D)*/
1119 sizeRequired++;
1120
1121 float4 weight = (float4) BsonValueAsDouble(bson_iter_value(&weightsIter));
1122 maxWeight = Max(weight, maxWeight);
1123 }
1124
1125 maxWeight = Max(maxWeight, 1.0);
1126 if (buffer != NULL)
1127 {
1128 char *bufferPtr = (char *) buffer;
1129
1130 /* Use memcpy to avoid misaligned memory access - buffer may not be 4-byte aligned */
1131 memcpy(bufferPtr, &pathCount, sizeof(uint32_t));
1132 bufferPtr += sizeof(uint32_t);
1133
1134 /* Save space for the weights array */
1135 Datum *weightDatums = (Datum *) bufferPtr;
1136 bufferPtr += (sizeof(Datum) * 4);
1137

Callers

nothing calls this directly

Calls 5

PgbsonInitFromJsonFunction · 0.85
PgbsonInitEmptyFunction · 0.85
PgbsonInitIteratorFunction · 0.85
BsonValueAsDoubleFunction · 0.85

Tested by

no test coverage detected