Sfoglia il codice sorgente

update descendantCount one at a time

yohei0125 4 anni fa
parent
commit
bb9a5b3a3e

+ 13 - 32
packages/app/src/server/models/page.ts

@@ -358,7 +358,7 @@ schema.statics.findAncestorsChildrenByPathAndViewer = async function(path: strin
 /**
  * Aggregate pages with paths starting with the provided string
  */
-schema.statics.getAggrationForPagesByMatchConditionInDescOrder = function(path) {
+schema.statics.getAggrationForPagesByPathInDescOrder = function(path) {
   const match = {
     $match: {
       $or: [
@@ -385,14 +385,13 @@ schema.statics.getAggrationForPagesByMatchConditionInDescOrder = function(path)
   ];
 };
 
-schema.statics.recountPage = async function(idsToRecount) {
+schema.statics.recountPage = async function(document) {
   const res = await this.aggregate(
     [
       {
         $match: {
-          parent: { $in: idsToRecount },
+          parent: document._id,
         },
-
       },
       {
         $project: {
@@ -422,34 +421,16 @@ schema.statics.recountPage = async function(idsToRecount) {
     ],
   );
 
-  const idWithChildren = res.map(data => data._id.toString());
-  const pageIdsWithNoChildren = idsToRecount.filter((targetId) => {
-    return !idWithChildren.includes(targetId.toString());
-  });
-
-  const operationForPageWithChildren = res.map((data) => {
-    return {
-      updateOne: {
-        filter: { _id: data._id },
-        update: { $set: { descendantCount: data.descendantCount } },
-      },
-    };
-
-  });
-  const operationsForPageWithoutChildren = pageIdsWithNoChildren.map((data) => {
-    return {
-      updateOne: {
-        filter: { _id: data._id },
-        update: { $set: { descendantCount: 0 } },
-      },
-    };
-  });
-
-  const operations = operationForPageWithChildren.concat(operationsForPageWithoutChildren);
-  operations.forEach((e) => {
-    console.log(e.updateOne);
-  });
-  await this.bulkWrite(operations);
+  if (res.length === 0) {
+    await this.findByIdAndUpdate(document._id, {
+      descendantCount: 0,
+    });
+  }
+  else {
+    await this.findByIdAndUpdate(document._id, {
+      descendantCount: res[0].descendantCount,
+    });
+  }
 };
 
 

+ 6 - 23
packages/app/src/server/service/page.js

@@ -1248,35 +1248,18 @@ class PageService {
   }
 
   async updateDescendantCount(path = '/') {
-    const BATCH_SIZE = 2;
-
-
+    const BATCH_SIZE = 10;
     const Page = this.crowi.model('Page');
-    const aggregation = Page.getAggrationForPagesByMatchConditionInDescOrder(path);
+
+    const aggregation = Page.getAggrationForPagesByPathInDescOrder(path);
     const aggregatedPages = await Page.aggregate(aggregation).cursor({ batchSize: BATCH_SIZE });
 
     const recountWriteStream = new Writable({
       objectMode: true,
-      async write(pages, encoding, callback) {
-        for (const singlePage of pages) {
-          // skip page tagged as sibling. Tagged ones are already updated with other pages.
-          if (singlePage.isTaggedAsSibling) {
-            continue;
-          }
-
-          // filter out pages with different parent
-          const pagesWithSameParent = pages.filter((page) => {
-            const hasSameParent = singlePage.parent === page.parent;
-            // if page with same parent found, tag it as sibling to skip updating from the next iteration.
-            if (hasSameParent) {
-              page.isTaggedAsSibling = true;
-              return true;
-            }
-            return false;
-          });
-          const idsToRecount = pagesWithSameParent.map(page => page._id);
+      async write(pageDocuments, encoding, callback) {
+        for (const document of pageDocuments) {
           // eslint-disable-next-line no-await-in-loop
-          await Page.recountPage(idsToRecount);
+          await Page.recountPage(document);
         }
         callback();
       },