| 1143 | |
| 1144 | |
| 1145 | static uint64 |
| 1146 | CallInsertWorkerForInsertOne(MongoCollection *collection, int64 shardKeyHash, |
| 1147 | pgbson *document, text *transactionId) |
| 1148 | { |
| 1149 | int argCount = 6; |
| 1150 | Datum argValues[6]; |
| 1151 | |
| 1152 | /* whitespace means not null, n means null */ |
| 1153 | char argNulls[6] = { ' ', ' ', ' ', ' ', 'n', ' ' }; |
| 1154 | Oid argTypes[6] = { INT8OID, INT8OID, REGCLASSOID, BYTEAOID, BYTEAOID, TEXTOID }; |
| 1155 | |
| 1156 | const char *updateQuery = FormatSqlQuery( |
| 1157 | " SELECT %s.insert_worker($1, $2, $3, $4::%s.bson, $5::%s.bsonsequence, $6) FROM %s.documents_" |
| 1158 | UINT64_FORMAT " WHERE shard_key_value = %ld", |
| 1159 | DocumentDBApiInternalSchemaName, CoreSchemaNameV2, CoreSchemaNameV2, |
| 1160 | ApiDataSchemaName, collection->collectionId, |
| 1161 | shardKeyHash); |
| 1162 | |
| 1163 | argValues[0] = UInt64GetDatum(collection->collectionId); |
| 1164 | |
| 1165 | /* p_shard_key_value */ |
| 1166 | argValues[1] = Int64GetDatum(shardKeyHash); |
| 1167 | |
| 1168 | /* p_shard_oid */ |
| 1169 | argValues[2] = ObjectIdGetDatum(InvalidOid); |
| 1170 | |
| 1171 | /* We just send the document for now */ |
| 1172 | pgbsonelement element = { 0 }; |
| 1173 | element.path = "insertOne"; |
| 1174 | element.pathLength = 9; |
| 1175 | element.bsonValue = ConvertPgbsonToBsonValue(document); |
| 1176 | argValues[3] = PointerGetDatum(PgbsonElementToPgbson(&element)); |
| 1177 | |
| 1178 | argValues[5] = PointerGetDatum(transactionId); |
| 1179 | argNulls[5] = ' '; |
| 1180 | |
| 1181 | bool readOnly = false; |
| 1182 | |
| 1183 | Datum resultDatum[1] = { 0 }; |
| 1184 | bool isNulls[1] = { false }; |
| 1185 | int numResults = 1; |
| 1186 | |
| 1187 | /* forceDelegation assumes nested distribution */ |
| 1188 | RunMultiValueQueryWithNestedDistribution(updateQuery, argCount, argTypes, argValues, |
| 1189 | argNulls, |
| 1190 | readOnly, SPI_OK_SELECT, resultDatum, |
| 1191 | isNulls, numResults); |
| 1192 | |
| 1193 | if (isNulls[0]) |
| 1194 | { |
| 1195 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_INTERNALERROR), |
| 1196 | errmsg("insert_worker should not return null"))); |
| 1197 | } |
| 1198 | |
| 1199 | /* If we got here, then it succeeded and inserted */ |
| 1200 | return 1; |
| 1201 | } |
| 1202 |
no test coverage detected