|
@@ -740,17 +740,14 @@ class PageService {
|
|
|
|
|
|
|
|
async v5RecursiveMigration(grant, rootPath = null) {
|
|
async v5RecursiveMigration(grant, rootPath = null) {
|
|
|
const BATCH_SIZE = 100;
|
|
const BATCH_SIZE = 100;
|
|
|
|
|
+ const PAGES_LIMIT = 1000;
|
|
|
const Page = this.crowi.model('Page');
|
|
const Page = this.crowi.model('Page');
|
|
|
const { PageQueryBuilder } = Page;
|
|
const { PageQueryBuilder } = Page;
|
|
|
|
|
|
|
|
- const randomPagesStream = await Page
|
|
|
|
|
|
|
+ const total = await Page.countDocuments({ grant, parent: null });
|
|
|
|
|
+
|
|
|
|
|
+ let baseAggregation = Page
|
|
|
.aggregate([
|
|
.aggregate([
|
|
|
- // TODO: randomize somehow sample does not work when the result is under 100?
|
|
|
|
|
- // {
|
|
|
|
|
- // $sample: {
|
|
|
|
|
- // size: BATCH_SIZE,
|
|
|
|
|
- // },
|
|
|
|
|
- // },
|
|
|
|
|
{
|
|
{
|
|
|
$match: {
|
|
$match: {
|
|
|
grant,
|
|
grant,
|
|
@@ -763,13 +760,20 @@ class PageService {
|
|
|
path: 1,
|
|
path: 1,
|
|
|
},
|
|
},
|
|
|
},
|
|
},
|
|
|
- ])
|
|
|
|
|
- .cursor({ batchSize: BATCH_SIZE }) // get stream
|
|
|
|
|
- .exec();
|
|
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ // limit pages to get
|
|
|
|
|
+ if (total > PAGES_LIMIT) {
|
|
|
|
|
+ baseAggregation = baseAggregation.limit(Math.floor(total * 0.3));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const randomPagesStream = await baseAggregation.cursor({ batchSize: BATCH_SIZE }).exec();
|
|
|
|
|
|
|
|
// use batch stream
|
|
// use batch stream
|
|
|
const batchStream = createBatchStream(BATCH_SIZE);
|
|
const batchStream = createBatchStream(BATCH_SIZE);
|
|
|
|
|
|
|
|
|
|
+ let countPages = 0;
|
|
|
|
|
+
|
|
|
// migrate all siblings for each page
|
|
// migrate all siblings for each page
|
|
|
const migratePagesStream = new Writable({
|
|
const migratePagesStream = new Writable({
|
|
|
objectMode: true,
|
|
objectMode: true,
|
|
@@ -813,7 +817,6 @@ class PageService {
|
|
|
// modify to adjust for RegExp
|
|
// modify to adjust for RegExp
|
|
|
const parentPath = parent.path === '/' ? '' : parent.path;
|
|
const parentPath = parent.path === '/' ? '' : parent.path;
|
|
|
|
|
|
|
|
- // TODO: consider filter to improve the target selection
|
|
|
|
|
return {
|
|
return {
|
|
|
updateMany: {
|
|
updateMany: {
|
|
|
filter: {
|
|
filter: {
|
|
@@ -828,7 +831,9 @@ class PageService {
|
|
|
};
|
|
};
|
|
|
});
|
|
});
|
|
|
try {
|
|
try {
|
|
|
- await Page.bulkWrite(updateManyOperations);
|
|
|
|
|
|
|
+ const res = await Page.bulkWrite(updateManyOperations);
|
|
|
|
|
+ countPages += (res.items || []).length;
|
|
|
|
|
+ logger.info(`Page migration processing: (count=${countPages}, errors=${res.errors}, took=${res.took}ms)`);
|
|
|
}
|
|
}
|
|
|
catch (err) {
|
|
catch (err) {
|
|
|
logger.error('Failed to update page.parent.', err);
|
|
logger.error('Failed to update page.parent.', err);
|