UNPKG

@drip_sync/drip

Version:

Scalable incremental sync for MongoDB aggregation pipelines

54 lines (53 loc) 2.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.expirePCSEvents = expirePCSEvents; const mongodb_1 = require("mongodb"); const derive_pcs_coll_name_1 = require("../cea/derive_pcs_coll_name"); const zod_1 = __importDefault(require("zod")); async function expirePCSEvents(collectionName, client, metadataDbName, until) { const pcsColl = client .db(metadataDbName) .collection((0, derive_pcs_coll_name_1.derivePCSCollName)(collectionName)); const maxCT = zod_1.default .object({ ct: zod_1.default.instanceof(mongodb_1.Timestamp) }) .optional() .parse((await pcsColl .find({}, { readConcern: mongodb_1.ReadConcernLevel.majority }) .sort({ ct: -1 }) .project({ _id: 0, ct: 1 }) .limit(1) .toArray())[0])?.ct; if (!maxCT) { // No persisted events return; } const lastExpiredCT_ = zod_1.default .object({ ct: zod_1.default.instanceof(mongodb_1.Timestamp) }) .optional() .parse((await pcsColl .find({ w: { $lt: until } }, { readConcern: mongodb_1.ReadConcernLevel.majority }) .sort({ w: -1 }) .project({ _id: 0, ct: 1 }) .limit(1) .toArray())[0])?.ct; if (!lastExpiredCT_) { // No expired events return; } // Avoid cleaning the tail of the PCS const lastExpiredCT = lastExpiredCT_.gte(maxCT) ? new mongodb_1.Timestamp(maxCT.subtract(1)) : lastExpiredCT_; // Use a transaction to guarantee that // we never delete an event with a higher // [cluster time, id] followed by a lower one. await client.withSession((session) => session.withTransaction(async (session) => { await pcsColl.deleteMany({ ct: { $lte: lastExpiredCT } }, { session }); }, { readConcern: mongodb_1.ReadConcernLevel.majority, writeConcern: { w: "majority" }, })); }