-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve memory usage of record export (#311)
- Loading branch information
Showing
82 changed files
with
1,095 additions
and
452 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,18 @@ | ||
import { CsvError } from "csv-parse"; | ||
import { CliKintoneError } from "../../../utils/error"; | ||
|
||
export class ParserError extends Error { | ||
private readonly cause: unknown; | ||
|
||
export class ParserError extends CliKintoneError { | ||
constructor(cause: unknown) { | ||
const message = "Failed to parse input"; | ||
super(message); | ||
|
||
super(message, cause); | ||
this.name = "ParserError"; | ||
this.message = message; | ||
this.cause = cause; | ||
|
||
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work | ||
// Set the prototype explicitly. | ||
Object.setPrototypeOf(this, ParserError.prototype); | ||
} | ||
|
||
toString(): string { | ||
let errorMessage = ""; | ||
errorMessage += this.message + "\n"; | ||
protected _toStringCause(): string { | ||
if (this.cause instanceof CsvError) { | ||
errorMessage += `${this.cause.code}: ${this.cause.message}\n`; | ||
} else { | ||
errorMessage += this.cause + "\n"; | ||
return `${this.cause.code}: ${this.cause.message}\n`; | ||
} | ||
return errorMessage; | ||
return super._toStringCause(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,12 @@ | ||
export class ValidatorError extends Error { | ||
private readonly cause: unknown; | ||
import { CliKintoneError } from "../../../utils/error"; | ||
|
||
export class ValidatorError extends CliKintoneError { | ||
constructor(cause: unknown) { | ||
const message = "Failed to delete records"; | ||
super(message); | ||
super(message, cause); | ||
|
||
this.name = "ValidatorError"; | ||
this.message = message; | ||
this.cause = cause; | ||
|
||
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work | ||
// Set the prototype explicitly. | ||
Object.setPrototypeOf(this, ValidatorError.prototype); | ||
} | ||
|
||
toString(): string { | ||
let errorMessage = ""; | ||
errorMessage += this.message + "\n"; | ||
errorMessage += this.cause + "\n"; | ||
|
||
return errorMessage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/record/export/repositories/localRecordRepositoryFromStream.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { LocalRecordRepository } from "../usecases/interface"; | ||
import type { RecordSchema } from "../types/schema"; | ||
import { stringifierFactory } from "./stringifiers"; | ||
|
||
export class LocalRecordRepositoryFromStream implements LocalRecordRepository { | ||
readonly format = "csv"; | ||
|
||
private readonly openWritableSink: () => NodeJS.WritableStream; | ||
private readonly schema: RecordSchema; | ||
private readonly useLocalFilePath: boolean; | ||
|
||
constructor( | ||
openWritableSink: () => NodeJS.WritableStream, | ||
schema: RecordSchema, | ||
useLocalFilePath: boolean | ||
) { | ||
this.openWritableSink = openWritableSink; | ||
this.schema = schema; | ||
this.useLocalFilePath = useLocalFilePath; | ||
} | ||
writer() { | ||
const stringifier = stringifierFactory({ | ||
format: this.format, | ||
schema: this.schema, | ||
useLocalFilePath: this.useLocalFilePath, | ||
}); | ||
stringifier.pipe(this.openWritableSink()); | ||
return stringifier; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/record/export/repositories/localRecordRepositoryMock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { LocalRecordRepository, Writer } from "../usecases/interface"; | ||
import type { LocalRecord } from "../types/record"; | ||
|
||
export class LocalRecordRepositoryMock implements LocalRecordRepository { | ||
readonly format = "csv"; | ||
|
||
private underlyingSink: WritableMock = new WritableMock(); | ||
|
||
writer() { | ||
this.underlyingSink = new WritableMock(); | ||
return this.underlyingSink; | ||
} | ||
receivedRecords() { | ||
return this.underlyingSink.underlyingSink; | ||
} | ||
} | ||
|
||
class WritableMock implements Writer { | ||
public underlyingSink: LocalRecord[] = []; | ||
async write(chunk: LocalRecord[]) { | ||
this.underlyingSink.push(...chunk); | ||
} | ||
async end() { | ||
/* noop */ | ||
} | ||
} |
File renamed without changes.
9 changes: 9 additions & 0 deletions
9
...export/repositories/stringifiers/__tests__/fixtures/canStringifyRecordsCorrectly/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { input } from "./input"; | ||
import { expected } from "./expected_csv"; | ||
import { schema } from "./schema"; | ||
|
||
export const pattern = { | ||
input: input, | ||
expected: expected, | ||
schema: schema, | ||
}; |
4 changes: 2 additions & 2 deletions
4
.../stringifiers/__tests__/fixtures/input.ts → ...res/canStringifyRecordsCorrectly/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...stringifiers/__tests__/fixtures/schema.ts → ...es/canStringifyRecordsCorrectly/schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...ies/stringifiers/__tests__/fixtures/canStringifyRecordsCorrectlyIterative/expected_csv.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const expected = `"レコード番号","文字列__1行_","作成日時" | ||
"1","文字列123","2022-05-02T07:58:00Z" | ||
"2","文字列234","2022-05-02T07:58:00Z" | ||
"3","文字列345","2022-05-02T07:58:00Z" | ||
`; |
9 changes: 9 additions & 0 deletions
9
...positories/stringifiers/__tests__/fixtures/canStringifyRecordsCorrectlyIterative/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { input } from "./input"; | ||
import { expected } from "./expected_csv"; | ||
import { schema } from "./schema"; | ||
|
||
export const pattern = { | ||
input: input, | ||
expected: expected, | ||
schema: schema, | ||
}; |
Oops, something went wrong.