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

Function ProcessInsertion

pg_documentdb/src/commands/insert.c:972–1055  ·  view source on GitHub ↗

* ProcessInsertion processes a single insertion operation. */

Source from the content-addressed store, hash-verified

970 * ProcessInsertion processes a single insertion operation.
971 */
972static uint64
973ProcessInsertion(MongoCollection *collection,
974 Oid optionalInsertShardOid,
975 const bson_value_t *documentValue,
976 text *transactionId, ExprEvalState *evalState)
977{
978 if (transactionId != NULL &&
979 collection->shardKey != NULL &&
980 !DocumentBsonValueHasDocumentId(documentValue) &&
981 PgbsonHasDocumentId(collection->shardKey))
982 {
983 RetryableWriteResult writeResult;
984
985 /*
986 * This edge case is slightly problematic: We have a collection that is
987 * sharded by _id, but the document does not specify an _id so we will
988 * generate one randomly. If this is the second try, we do not know which
989 * shard holds the retry record, so we search all of them.
990 *
991 * Clients can prevent this by setting the object ID.
992 */
993 if (FindRetryRecordInAnyShard(collection->collectionId, transactionId,
994 &writeResult))
995 {
996 return writeResult.rowsAffected;
997 }
998 }
999
1000 int64 shardKeyHash;
1001 pgbson *objectIdPtr = NULL;
1002 pgbson *insertDoc = PreprocessInsertionDoc(documentValue, collection, &shardKeyHash,
1003 &objectIdPtr, evalState);
1004
1005 /* make sure the document has an _id and it is in the right place */
1006 if (transactionId == NULL)
1007 {
1008 /*
1009 * If retry is NULL then we don't really need to call insert_one on the worker, and then
1010 * have that call INSERT - we can just do that directly from the coordinator (which probably
1011 * saves one query parsing and planning per document).
1012 */
1013 Const *shardKeyConst = makeConst(INT8OID, -1, InvalidOid, 8,
1014 Int64GetDatum(shardKeyHash), false, true);
1015
1016 List *singleInsertList = NULL;
1017 uint64_t insertResult = 0;
1018
1019 ParamListInfo paramListInfo = makeParamList(2);
1020 paramListInfo->numParams = 2;
1021 Expr *objectidParam = CreateBsonParam(0, paramListInfo, objectIdPtr);
1022 Expr *documentParam = CreateBsonParam(1, paramListInfo, insertDoc);
1023
1024 singleInsertList = CreateValuesListForInsert(shardKeyConst, objectidParam,
1025 documentParam,
1026 collection->
1027 mongoDataCreationTimeVarAttrNumber);
1028
1029 if (optionalInsertShardOid == InvalidOid)

Callers 2

DoSingleInsertFunction · 0.85
DoSingleInsertWithSubTxnFunction · 0.85

Calls 12

PgbsonHasDocumentIdFunction · 0.85
PreprocessInsertionDocFunction · 0.85
CreateBsonParamFunction · 0.85
CreateInsertQueryFunction · 0.85
RunInsertQueryFunction · 0.85

Tested by

no test coverage detected