Skip to content

Commit

Permalink
feat: use batch insert and batch update
Browse files Browse the repository at this point in the history
  • Loading branch information
JulissaDantes committed Apr 11, 2024
1 parent 14771a4 commit 13f1c5d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
8 changes: 3 additions & 5 deletions src/repositories/anchor-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { parseCountResult } from './parse-count-result.util.js'
import { decode } from 'codeco'

const TABLE_NAME = 'anchor'
const chunkSize = 10000

export class AnchorRepository implements IAnchorRepository {
static inject = ['dbConnection'] as const
Expand All @@ -32,11 +33,8 @@ export class AnchorRepository implements IAnchorRepository {
* @returns A promise that resolve to the number of anchors created
*/
async createAnchors(anchors: Array<FreshAnchor>): Promise<number> {
const result: any = await this.table
.insert(anchors.map((anchor) => FreshAnchor.encode(anchor)))
.onConflict('requestId')
.ignore()
return parseCountResult(result.rowCount)
const result = await this.connection.batchInsert(TABLE_NAME, anchors, chunkSize)
return parseCountResult(result.length)
}

/**
Expand Down
29 changes: 20 additions & 9 deletions src/repositories/request-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const FAILURE_RETRY_INTERVAL = 1000 * 60 * 60 * 6 // 6H
const REPEATED_READ_SERIALIZATION_ERROR = '40001'

const TABLE_NAME = 'request'
const chunkSize = 10000

/**
* Records statistics about the set of requests
Expand Down Expand Up @@ -169,15 +170,25 @@ export class RequestRepository {
*/
async updateRequests(fields: RequestUpdateFields, requests: Request[]): Promise<number> {
const updatedAt = new Date()
const ids = requests.map((r) => r.id)
const result = await this.table
.update({
message: fields.message,
status: fields.status,
pinned: fields.pinned,
updatedAt: date.encode(updatedAt),
})
.whereIn('id', ids)
let result = 0

for (let i = 0; i < requests.length; i += chunkSize) {
const chunk = requests.slice(i, i + chunkSize)
const ids = chunk.map((r) => r.id)
try {
result += await this.table
.update({
message: fields.message,
status: fields.status,
pinned: fields.pinned,
updatedAt: date.encode(updatedAt),
})
.whereIn('id', ids)

} catch (error) {
throw error
}
}

requests.map((request) => {
logEvent.db({
Expand Down

0 comments on commit 13f1c5d

Please sign in to comment.