Sfoglia il codice sorgente

modify comments and method name

yohei0125 4 anni fa
parent
commit
ec62a864d3

+ 6 - 15
packages/app/src/server/models/page.ts

@@ -342,9 +342,9 @@ schema.statics.findAncestorsChildrenByPathAndViewer = async function(path: strin
 };
 };
 
 
 /**
 /**
- * Aggregate pages with paths starting with the provided string
+ * return aggregation to get pages with paths starting with the provided path
  */
  */
-schema.statics.getAggrationForPagesByPathInDescOrder = function(path) {
+schema.statics.getAggrConditionForPagesStartingWithProvidedPath = function(path:string) {
   const match = {
   const match = {
     $match: {
     $match: {
       $or: [
       $or: [
@@ -371,12 +371,12 @@ schema.statics.getAggrationForPagesByPathInDescOrder = function(path) {
   ];
   ];
 };
 };
 
 
-schema.statics.recountPage = async function(document) {
+schema.statics.recountPage = async function(id:mongoose.Types.ObjectId):Promise<void> {
   const res = await this.aggregate(
   const res = await this.aggregate(
     [
     [
       {
       {
         $match: {
         $match: {
-          parent: document._id,
+          parent: id,
         },
         },
       },
       },
       {
       {
@@ -407,19 +407,10 @@ schema.statics.recountPage = async function(document) {
     ],
     ],
   );
   );
 
 
-  if (res.length === 0) {
-    await this.findByIdAndUpdate(document._id, {
-      descendantCount: 0,
-    });
-  }
-  else {
-    await this.findByIdAndUpdate(document._id, {
-      descendantCount: res[0].descendantCount,
-    });
-  }
+  const query = { descendantCount: res.length === 0 ? 0 : res[0].descendantCount };
+  await this.findByIdAndUpdate(id, query);
 };
 };
 
 
-
 /*
 /*
  * Merge obsolete page model methods and define new methods which depend on crowi instance
  * Merge obsolete page model methods and define new methods which depend on crowi instance
  */
  */

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

@@ -1248,19 +1248,20 @@ class PageService {
     return Page.count({ parent: null, creator: user, grant: { $ne: Page.GRANT_PUBLIC } });
     return Page.count({ parent: null, creator: user, grant: { $ne: Page.GRANT_PUBLIC } });
   }
   }
 
 
-  async updateDescendantCount(path = '/') {
-    const BATCH_SIZE = 10;
+  // update descendantCount of all pages with path starting with provided string
+  async updateSelfAndDescendantCount(path = '/') {
+    const BATCH_SIZE = 200;
     const Page = this.crowi.model('Page');
     const Page = this.crowi.model('Page');
 
 
-    const aggregation = Page.getAggrationForPagesByPathInDescOrder(path);
-    const aggregatedPages = await Page.aggregate(aggregation).cursor({ batchSize: BATCH_SIZE });
+    const aggregateCondition = Page.getAggrConditionForPagesStartingWithProvidedPath(path);
+    const aggregatedPages = await Page.aggregate(aggregateCondition).cursor({ batchSize: BATCH_SIZE });
 
 
     const recountWriteStream = new Writable({
     const recountWriteStream = new Writable({
       objectMode: true,
       objectMode: true,
       async write(pageDocuments, encoding, callback) {
       async write(pageDocuments, encoding, callback) {
         for (const document of pageDocuments) {
         for (const document of pageDocuments) {
           // eslint-disable-next-line no-await-in-loop
           // eslint-disable-next-line no-await-in-loop
-          await Page.recountPage(document);
+          await Page.recountPage(document._id);
         }
         }
         callback();
         callback();
       },
       },