Skip to content

Commit

Permalink
remove batch size and beforeBulkDestroy hook
Browse files Browse the repository at this point in the history
  • Loading branch information
yoozo committed Nov 20, 2024
1 parent b84f8c6 commit afaa536
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 34 deletions.
11 changes: 0 additions & 11 deletions packages/node-core/src/indexer/store.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,6 @@ export class StoreService {
item.__block_range = [this.blockHeight, null];
});
});

sequelizeModel.addHook('beforeBulkDestroy', (instance) => {
instance.where = {
...instance.where,
[Op.and]: this.sequelize.where(
this.sequelize.fn('lower', this.sequelize.col('_block_range')),
Op.eq,
this.blockHeight
),
};
});
}
// TODO, remove id and block_range constraint, check id manually
// see https://github.com/subquery/subql/issues/1542
Expand Down
44 changes: 21 additions & 23 deletions packages/node-core/src/indexer/storeModelProvider/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,30 +107,28 @@ export class PlainModel<T extends BaseEntity = BaseEntity> implements IModel<T>
throw new Error(`Currently not supported: update by fields`);
}

// Batch insert every 10000 data
const batchSize = 10000;
for (let i = 0; i < data.length; i += batchSize) {
const batchDatas = data.slice(i, i + batchSize);

if (!this.historical) {
await this._bulkCreate(batchDatas, tx);
continue;
}

await this.model.destroy({
where: {
id: batchDatas.map((v) => v.id),
} as any,
limit: batchSize,
transaction: tx,
});
await this.markAsDeleted(
batchDatas.map((v) => v.id),
blockHeight,
tx
);
await this._bulkCreate(batchDatas, tx);
if (!this.historical) {
await this._bulkCreate(data, tx);
return;
}

await this.model.destroy({
where: {
id: data.map((v) => v.id),
[Op.and]: this.sequelize.where(
this.sequelize.fn('lower', this.sequelize.col('_block_range')),
Op.eq,
blockHeight
),
} as any,
transaction: tx,
});
await this.markAsDeleted(
data.map((v) => v.id),
blockHeight,
tx
);
await this._bulkCreate(data, tx);
}

async bulkRemove(ids: string[], blockHeight: number, tx?: Transaction): Promise<void> {
Expand Down

0 comments on commit afaa536

Please sign in to comment.