Просмотр исходного кода

Moved recursive (resumable) process to at the end of methods

Taichi Masuyama 4 лет назад
Родитель
Сommit
3c489ec46b
2 измененных файлов с 45 добавлено и 35 удалено
  1. 3 1
      packages/app/src/server/models/page.ts
  2. 42 34
      packages/app/src/server/service/page.ts

+ 3 - 1
packages/app/src/server/models/page.ts

@@ -468,7 +468,9 @@ schema.statics.recountDescendantCountOfSelfAndDescendants = async function(id: O
             $sum: '$descendantCount',
           },
           sumOfDocsCount: {
-            $sum: { $cond: { if: { $eq: ['$isEmpty', true] }, then: 0, else: 1 } }, // exclude isEmpty true page from sumOfDocsCount
+            $sum: {
+              $cond: { if: { $eq: ['$isEmpty', true] }, then: 0, else: 1 }, // exclude isEmpty true page from sumOfDocsCount
+            },
           },
         },
       },

+ 42 - 34
packages/app/src/server/service/page.ts

@@ -342,9 +342,6 @@ class PageService {
       }
     }
 
-    // update descendants first
-    await this.renameDescendantsWithStream(page, newPagePath, user, options, shouldUseV4Process);
-
     /*
      * update target
      */
@@ -362,6 +359,10 @@ class PageService {
 
     this.pageEvent.emit('rename', page, user);
 
+    // TODO: resume
+    // update descendants first
+    this.renameDescendantsWithStream(page, newPagePath, user, options, shouldUseV4Process);
+
     return renamedPage;
   }
 
@@ -569,7 +570,7 @@ class PageService {
       .pipe(createBatchStream(BULK_REINDEX_SIZE))
       .pipe(writeStream);
 
-    await streamToPromise(readStream);
+    await streamToPromise(writeStream);
   }
 
   private async renameDescendantsWithStreamV4(targetPage, newPagePath, user, options = {}) {
@@ -678,10 +679,6 @@ class PageService {
       newPagePath, page.revision.body, user, options,
     );
 
-    if (isRecursively) {
-      this.duplicateDescendantsWithStream(page, newPagePath, user, shouldUseV4Process);
-    }
-
     // take over tags
     const originTags = await page.findRelatedTagsById();
     let savedTags = [];
@@ -694,6 +691,11 @@ class PageService {
     const result = serializePageSecurely(createdPage);
     result.tags = savedTags;
 
+    // TODO: resume
+    if (isRecursively) {
+      this.duplicateDescendantsWithStream(page, newPagePath, user, shouldUseV4Process);
+    }
+
     return result;
   }
 
@@ -1001,20 +1003,7 @@ class PageService {
       throw new Error('Page is not deletable.');
     }
 
-    if (isRecursively) {
-      // no await for deleteDescendantsWithStream and updateDescendantCountOfAncestors
-      (async() => {
-        const deletedDescendantCount = await this.deleteDescendantsWithStream(page, user, shouldUseV4Process); // use the same process in both version v4 and v5
-
-        // update descendantCount of ancestors'
-        if (page.parent != null) {
-          await this.updateDescendantCountOfAncestors(page.parent, (deletedDescendantCount + 1) * -1, true);
-
-          // TODO https://redmine.weseek.co.jp/issues/87667 : delete leaf empty pages here
-        }
-      })();
-    }
-    else {
+    if (!isRecursively) {
       // replace with an empty page
       const shouldReplace = await Page.exists({ parent: page._id });
       if (shouldReplace) {
@@ -1050,6 +1039,21 @@ class PageService {
       this.pageEvent.emit('create', deletedPage, user);
     }
 
+    // TODO: resume
+    // no await for deleteDescendantsWithStream and updateDescendantCountOfAncestors
+    if (isRecursively) {
+      (async() => {
+        const deletedDescendantCount = await this.deleteDescendantsWithStream(page, user, shouldUseV4Process); // use the same process in both version v4 and v5
+
+        // update descendantCount of ancestors'
+        if (page.parent != null) {
+          await this.updateDescendantCountOfAncestors(page.parent, (deletedDescendantCount + 1) * -1, true);
+
+          // TODO https://redmine.weseek.co.jp/issues/87667 : delete leaf empty pages here
+        }
+      })();
+    }
+
     return deletedPage;
   }
 
@@ -1274,6 +1278,17 @@ class PageService {
 
     await this.deleteCompletelyOperation(ids, paths);
 
+    if (!isRecursively) {
+      await this.updateDescendantCountOfAncestors(page.parent, -1, true);
+
+      // TODO https://redmine.weseek.co.jp/issues/87667 : delete leaf empty pages here
+    }
+
+    if (!page.isEmpty && !preventEmitting) {
+      this.pageEvent.emit('deleteCompletely', page, user);
+    }
+
+    // TODO: resume
     if (isRecursively) {
       // no await for deleteCompletelyDescendantsWithStream
       (async() => {
@@ -1287,15 +1302,6 @@ class PageService {
         // TODO https://redmine.weseek.co.jp/issues/87667 : delete leaf empty pages here
       })();
     }
-    else {
-      await this.updateDescendantCountOfAncestors(page.parent, -1, true);
-
-      // TODO https://redmine.weseek.co.jp/issues/87667 : delete leaf empty pages here
-    }
-
-    if (!page.isEmpty && !preventEmitting) {
-      this.pageEvent.emit('deleteCompletely', page, user);
-    }
 
     return;
   }
@@ -1440,6 +1446,11 @@ class PageService {
     await PageTagRelation.updateMany({ relatedPage: page._id }, { $set: { isPageTrashed: false } });
 
     if (isRecursively) {
+      await this.updateDescendantCountOfAncestors(parent._id, 1, true);
+    }
+
+    // TODO: resume
+    if (!isRecursively) {
       // no await for revertDeletedDescendantsWithStream
       (async() => {
         const revertedDescendantCount = await this.revertDeletedDescendantsWithStream(page, user, options, shouldUseV4Process);
@@ -1452,9 +1463,6 @@ class PageService {
         }
       })();
     }
-    else {
-      await this.updateDescendantCountOfAncestors(parent._id, 1, true);
-    }
 
     return updatedPage;
   }