mirror of
https://github.com/FerretDB/documentdb.git
synced 2026-11-25 19:59:54 -05:00
### Does this PR have any customer impact? Yes ### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc) Perf ### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes) No ### Are you introducing any new config? If yes, do you have tests with and without them being set? ### ChangeLog (Refer [Template](../oss/CHANGELOG.md)) ### Description Currently for Update/Delete, we currently parse the update spec, query spec, etc into pgbsons on startup. Then reuse it per commands. This has a few issues - for large document requests, this causes us to duplicate memory and persist it for the duration of the update and also transcode all up front - this is worse for multi-node when all we need to do is build yet another update spec and push it to the worker. Instead, we now only cache pointers to the spec document; then when we need it we convert to pgbson only if necessary. A consequence of this is that on a single node update, if we're doing a simple updateOne on _id then we would never serialize the updateSpec at all (for instance). We also more eagerly free memory on the intermediate update specs. ---- #### AI description (iteration 1) #### PR Classification Performance optimization for update parsing by refactoring BSON value handling. #### PR Summary This PR streamlines update parsing by replacing heavy BSON initialization routines with lightweight wrappers and converting update-related types from `pgbson*` to `bson_value_t*`, which reduces overhead and improves coordinator/worker compatibility. - `src/commands/update.c`: Replaced multiple `PgbsonInitFromDocumentBsonValue` and similar calls with efficient wrappers like `CreateBsonValueFrom` and updated serialization (including back-compatible array filter and update document formats). - `include/commands/update.h`: Updated update structure fields to use `bson_value_t*` instead of `pgbson*`, aligning interfaces with the new parsing approach. - `src/update/bson_update.c` and `bson_update_operators_workflow.c`: Refactored update type determination and replace document logic to use new BSON value iterators and helper functions. - Other modules (e.g. `commands/find_and_modify.c`, `commands/delete.c`, query operators, sharding) were updated to leverage the lighter-weight BSON representation and its associated helper routines. <!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot -->
95 lines
2.2 KiB
C
95 lines
2.2 KiB
C
/*-------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
*
|
|
* include/commands/update.h
|
|
*
|
|
* Exports related to implementation of a single-document update.
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
#ifndef UPDATE_H
|
|
#define UPDATE_H
|
|
|
|
#include <postgres.h>
|
|
|
|
#include "metadata/collection.h"
|
|
#include "operators/bson_expr_eval.h"
|
|
|
|
|
|
/*
|
|
* UpdateReturnValue specifies whether an update should return
|
|
* no document, the old document, or the new document.
|
|
*/
|
|
typedef enum
|
|
{
|
|
UPDATE_RETURNS_NONE,
|
|
UPDATE_RETURNS_OLD,
|
|
UPDATE_RETURNS_NEW
|
|
} UpdateReturnValue;
|
|
|
|
/*
|
|
* UpdateOneParams describes update operation for a single document.
|
|
*/
|
|
typedef struct
|
|
{
|
|
/* update only documents matching this query */
|
|
const bson_value_t *query;
|
|
|
|
/* apply this update */
|
|
const bson_value_t *update;
|
|
|
|
/* whether to use upsert if no documents match */
|
|
int isUpsert;
|
|
|
|
/* sort order to use when selecting 1 row */
|
|
const bson_value_t *sort;
|
|
|
|
/* whether to return a document */
|
|
UpdateReturnValue returnDocument;
|
|
|
|
/* fields to return if returning a document */
|
|
const bson_value_t *returnFields;
|
|
|
|
/* array filters specified in the update */
|
|
const bson_value_t *arrayFilters;
|
|
|
|
/* whether to bypass document validation */
|
|
bool bypassDocumentValidation;
|
|
} UpdateOneParams;
|
|
|
|
|
|
/*
|
|
* UpdateOneResult reflects the result of a single-row update
|
|
* on a single shard, which may be a delete.
|
|
*/
|
|
typedef struct
|
|
{
|
|
/* whether one row matched the query and was updated */
|
|
bool isRowUpdated;
|
|
|
|
/* whether we found a document but it was not affected by the update spec */
|
|
bool updateSkipped;
|
|
|
|
/* update result came from a retry record */
|
|
bool isRetry;
|
|
|
|
/* shard key value changed, reinsertDocument document needs to be inserted */
|
|
pgbson *reinsertDocument;
|
|
|
|
/*
|
|
* Value of the (maybe projected) original or new document, if requested
|
|
* and matched any.
|
|
*/
|
|
pgbson *resultDocument;
|
|
|
|
/* upserted document ID */
|
|
pgbson *upsertedObjectId;
|
|
} UpdateOneResult;
|
|
|
|
|
|
void UpdateOne(MongoCollection *collection, UpdateOneParams *updateOneParams,
|
|
int64 shardKeyHash, text *transactionId, UpdateOneResult *result,
|
|
bool forceInlineWrites, ExprEvalState *state);
|
|
|
|
#endif
|