@omegup/msync 0.1.15 → 0.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.esm.js +45 -24
  2. package/index.js +45 -24
  3. package/package.json +1 -1
package/index.esm.js CHANGED
@@ -1331,13 +1331,50 @@ const mergeIterators = (params) => {
1331
1331
  };
1332
1332
  };
1333
1333
 
1334
+ const T = (s) => `Timestamp(${parseInt(`${BigInt(s) / 2n ** 32n}`)}, ${parseInt(`${BigInt(s) % 2n ** 32n}`)})`;
1335
+ const replace = (s) => s.replace(/\{"\$timestamp":"(\d+)"\}/g, (_, d) => T(d));
1336
+ const json = (a) => replace(JSON.stringify(a));
1337
+ const log = (...args) => console.log(new Date(), ...args.map(a => (typeof a === 'function' ? a(replace) : a && typeof a === 'object' ? json(a) : a)));
1338
+
1339
+ const state = { steady: false };
1340
+ let timeout = null;
1341
+ const aggregate = (streamName, input, snapshot = true, start = Date.now()) => input(({ coll, input }) => {
1342
+ const req = {
1343
+ aggregate: coll.collectionName,
1344
+ pipeline: input,
1345
+ cursor: {},
1346
+ ...(snapshot && { readConcern: { level: 'snapshot' } }),
1347
+ };
1348
+ if (timeout !== null) {
1349
+ clearTimeout(timeout);
1350
+ timeout = null;
1351
+ }
1352
+ log('exec', streamName, req);
1353
+ return coll.s.db.command(req).then(result => {
1354
+ const r = result;
1355
+ log('execed', streamName, (replace) => replace(JSON.stringify(req).replaceAll('$$CLUSTER_TIME', JSON.stringify(r.cursor.atClusterTime))), result, 'took', Date.now() - start);
1356
+ if (!state.steady) {
1357
+ if (timeout !== null)
1358
+ throw new Error('timeout should be null');
1359
+ timeout = setTimeout(() => {
1360
+ state.steady = true;
1361
+ console.log('steady');
1362
+ }, 1000);
1363
+ }
1364
+ return r;
1365
+ }, err => {
1366
+ log('err', req, err);
1367
+ throw new Error(err);
1368
+ });
1369
+ });
1370
+
1334
1371
  const firstWorksMerge = (iters) => {
1335
1372
  const iterator = () => {
1336
1373
  const results = iters.map(iter => iter());
1337
1374
  const sources = { ...results };
1338
1375
  return mergeIterators({
1339
1376
  sources,
1340
- interrupt: key => false,
1377
+ interrupt: key => state.steady
1341
1378
  });
1342
1379
  };
1343
1380
  return iterator;
@@ -1501,29 +1538,6 @@ const $insertPart = (out, ext) => {
1501
1538
  const $insert = (out) => $insertPart(out, {});
1502
1539
  const assertNotNull = (expr) => expr;
1503
1540
 
1504
- const T = (s) => `Timestamp(${parseInt(`${BigInt(s) / 2n ** 32n}`)}, ${parseInt(`${BigInt(s) % 2n ** 32n}`)})`;
1505
- const replace = (s) => s.replace(/\{"\$timestamp":"(\d+)"\}/g, (_, d) => T(d));
1506
- const json = (a) => replace(JSON.stringify(a));
1507
- const log = (...args) => console.log(new Date(), ...args.map(a => (typeof a === 'function' ? a(replace) : a && typeof a === 'object' ? json(a) : a)));
1508
-
1509
- const aggregate = (streamName, input, snapshot = true, start = Date.now()) => input(({ coll, input }) => {
1510
- const req = {
1511
- aggregate: coll.collectionName,
1512
- pipeline: input,
1513
- cursor: {},
1514
- ...(snapshot && { readConcern: { level: 'snapshot' } }),
1515
- };
1516
- log('exec', streamName, req);
1517
- return coll.s.db.command(req).then(result => {
1518
- const r = result;
1519
- log('execed', streamName, (replace) => replace(JSON.stringify(req).replaceAll('$$CLUSTER_TIME', JSON.stringify(r.cursor.atClusterTime))), result, 'took', Date.now() - start);
1520
- return r;
1521
- }, err => {
1522
- log('err', req, err);
1523
- throw new Error(err);
1524
- });
1525
- });
1526
-
1527
1541
  const addTeardown = (it, tr) => {
1528
1542
  if (!tr)
1529
1543
  return it;
@@ -1755,6 +1769,9 @@ const executes$2 = (view, input, streamName, skip = false, after, needs = {}) =>
1755
1769
  })).stages;
1756
1770
  const r = await aggregate(streamName, c => c({ coll: collection, input: cloneIntoNew }));
1757
1771
  await snapshotCollection.deleteMany({ updated: true, after: null, before: null });
1772
+ const start = Date.now();
1773
+ const count = await snapshotCollection.countDocuments({ updated: true });
1774
+ log(streamName, `documents to update ${snapshotCollection.collectionName}`, count, 'took', Date.now() - start);
1758
1775
  return next(step4({ result: r, ts: lastTS?.ts }), 'run the aggregation');
1759
1776
  };
1760
1777
  const makeStream = (startAt) => makeWatchStream(db, view, startAt, streamName);
@@ -1934,6 +1951,10 @@ const executes$1 = (view, input, streamName, needs) => {
1934
1951
  }));
1935
1952
  const stream = makeStream(aggResult.cursor.atClusterTime);
1936
1953
  const nextRes = stream.tryNext();
1954
+ const intoColl = raw.at(-1).$merge.into.coll;
1955
+ db.collection(intoColl)
1956
+ .countDocuments({ touchedAt: { $gte: aggResult.cursor.atClusterTime } })
1957
+ .then(count => log(`documents updated ${intoColl}`, count));
1937
1958
  return next(step7({ aggResult, ts: aggResult.cursor.atClusterTime, stream, nextRes }), 'update __last', () => stream.close());
1938
1959
  };
1939
1960
  const step7 = (l) => async () => {
package/index.js CHANGED
@@ -1333,13 +1333,50 @@ const mergeIterators = (params) => {
1333
1333
  };
1334
1334
  };
1335
1335
 
1336
+ const T = (s) => `Timestamp(${parseInt(`${BigInt(s) / 2n ** 32n}`)}, ${parseInt(`${BigInt(s) % 2n ** 32n}`)})`;
1337
+ const replace = (s) => s.replace(/\{"\$timestamp":"(\d+)"\}/g, (_, d) => T(d));
1338
+ const json = (a) => replace(JSON.stringify(a));
1339
+ const log = (...args) => console.log(new Date(), ...args.map(a => (typeof a === 'function' ? a(replace) : a && typeof a === 'object' ? json(a) : a)));
1340
+
1341
+ const state = { steady: false };
1342
+ let timeout = null;
1343
+ const aggregate = (streamName, input, snapshot = true, start = Date.now()) => input(({ coll, input }) => {
1344
+ const req = {
1345
+ aggregate: coll.collectionName,
1346
+ pipeline: input,
1347
+ cursor: {},
1348
+ ...(snapshot && { readConcern: { level: 'snapshot' } }),
1349
+ };
1350
+ if (timeout !== null) {
1351
+ clearTimeout(timeout);
1352
+ timeout = null;
1353
+ }
1354
+ log('exec', streamName, req);
1355
+ return coll.s.db.command(req).then(result => {
1356
+ const r = result;
1357
+ log('execed', streamName, (replace) => replace(JSON.stringify(req).replaceAll('$$CLUSTER_TIME', JSON.stringify(r.cursor.atClusterTime))), result, 'took', Date.now() - start);
1358
+ if (!state.steady) {
1359
+ if (timeout !== null)
1360
+ throw new Error('timeout should be null');
1361
+ timeout = setTimeout(() => {
1362
+ state.steady = true;
1363
+ console.log('steady');
1364
+ }, 1000);
1365
+ }
1366
+ return r;
1367
+ }, err => {
1368
+ log('err', req, err);
1369
+ throw new Error(err);
1370
+ });
1371
+ });
1372
+
1336
1373
  const firstWorksMerge = (iters) => {
1337
1374
  const iterator = () => {
1338
1375
  const results = iters.map(iter => iter());
1339
1376
  const sources = { ...results };
1340
1377
  return mergeIterators({
1341
1378
  sources,
1342
- interrupt: key => false,
1379
+ interrupt: key => state.steady
1343
1380
  });
1344
1381
  };
1345
1382
  return iterator;
@@ -1503,29 +1540,6 @@ const $insertPart = (out, ext) => {
1503
1540
  const $insert = (out) => $insertPart(out, {});
1504
1541
  const assertNotNull = (expr) => expr;
1505
1542
 
1506
- const T = (s) => `Timestamp(${parseInt(`${BigInt(s) / 2n ** 32n}`)}, ${parseInt(`${BigInt(s) % 2n ** 32n}`)})`;
1507
- const replace = (s) => s.replace(/\{"\$timestamp":"(\d+)"\}/g, (_, d) => T(d));
1508
- const json = (a) => replace(JSON.stringify(a));
1509
- const log = (...args) => console.log(new Date(), ...args.map(a => (typeof a === 'function' ? a(replace) : a && typeof a === 'object' ? json(a) : a)));
1510
-
1511
- const aggregate = (streamName, input, snapshot = true, start = Date.now()) => input(({ coll, input }) => {
1512
- const req = {
1513
- aggregate: coll.collectionName,
1514
- pipeline: input,
1515
- cursor: {},
1516
- ...(snapshot && { readConcern: { level: 'snapshot' } }),
1517
- };
1518
- log('exec', streamName, req);
1519
- return coll.s.db.command(req).then(result => {
1520
- const r = result;
1521
- log('execed', streamName, (replace) => replace(JSON.stringify(req).replaceAll('$$CLUSTER_TIME', JSON.stringify(r.cursor.atClusterTime))), result, 'took', Date.now() - start);
1522
- return r;
1523
- }, err => {
1524
- log('err', req, err);
1525
- throw new Error(err);
1526
- });
1527
- });
1528
-
1529
1543
  const addTeardown = (it, tr) => {
1530
1544
  if (!tr)
1531
1545
  return it;
@@ -1757,6 +1771,9 @@ const executes$2 = (view, input, streamName, skip = false, after, needs = {}) =>
1757
1771
  })).stages;
1758
1772
  const r = await aggregate(streamName, c => c({ coll: collection, input: cloneIntoNew }));
1759
1773
  await snapshotCollection.deleteMany({ updated: true, after: null, before: null });
1774
+ const start = Date.now();
1775
+ const count = await snapshotCollection.countDocuments({ updated: true });
1776
+ log(streamName, `documents to update ${snapshotCollection.collectionName}`, count, 'took', Date.now() - start);
1760
1777
  return next(step4({ result: r, ts: lastTS?.ts }), 'run the aggregation');
1761
1778
  };
1762
1779
  const makeStream = (startAt) => makeWatchStream(db, view, startAt, streamName);
@@ -1936,6 +1953,10 @@ const executes$1 = (view, input, streamName, needs) => {
1936
1953
  }));
1937
1954
  const stream = makeStream(aggResult.cursor.atClusterTime);
1938
1955
  const nextRes = stream.tryNext();
1956
+ const intoColl = raw.at(-1).$merge.into.coll;
1957
+ db.collection(intoColl)
1958
+ .countDocuments({ touchedAt: { $gte: aggResult.cursor.atClusterTime } })
1959
+ .then(count => log(`documents updated ${intoColl}`, count));
1939
1960
  return next(step7({ aggResult, ts: aggResult.cursor.atClusterTime, stream, nextRes }), 'update __last', () => stream.close());
1940
1961
  };
1941
1962
  const step7 = (l) => async () => {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "module": "index.esm.js",
4
4
  "typings": "index.d.ts",
5
5
  "name": "@omegup/msync",
6
- "version": "0.1.15",
6
+ "version": "0.1.16",
7
7
  "dependencies": {
8
8
  "dayjs": "^1.11.9",
9
9
  "dotenv": "^16.3.1",