-
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 import (#218)
- Loading branch information
Showing
99 changed files
with
943 additions
and
404 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 was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
src/record/import/parsers/parseCsv/__tests__/fixtures/emptyCsv/expected.ts
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
src/record/import/parsers/parseCsv/__tests__/fixtures/withNoRecord/expected.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 { ParserError } from "./parsers/error"; | ||
|
||
export class RepositoryError extends Error { | ||
private readonly cause: unknown; | ||
|
||
constructor(cause: unknown) { | ||
const message = | ||
"An error occurred while loading records from the data source"; | ||
super(message); | ||
|
||
this.name = "RepositoryError"; | ||
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, RepositoryError.prototype); | ||
} | ||
|
||
toString(): string { | ||
let errorMessage = ""; | ||
errorMessage += this.message + "\n"; | ||
if (this.cause instanceof ParserError) { | ||
errorMessage += this.cause.toString(); | ||
} else { | ||
errorMessage += this.cause + "\n"; | ||
} | ||
return errorMessage; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/record/import/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,31 @@ | ||
import type { LocalRecordRepository } from "../usecases/interface"; | ||
import type { RecordSchema } from "../types/schema"; | ||
import { RepositoryError } from "./error"; | ||
import { countRecordsFromCsv, csvReader } from "./parsers/parseCsv"; | ||
import type { LocalRecord } from "../types/record"; | ||
|
||
export class LocalRecordRepositoryFromStream implements LocalRecordRepository { | ||
readonly format: string; | ||
readonly length: () => Promise<number>; | ||
|
||
readonly reader: () => AsyncGenerator<LocalRecord, void, undefined>; | ||
|
||
constructor( | ||
source: () => NodeJS.ReadableStream, | ||
format: string, | ||
schema: RecordSchema | ||
) { | ||
this.format = format; | ||
this.length = () => countRecordsFromCsv(source()); | ||
|
||
switch (format) { | ||
case "csv": | ||
this.reader = () => csvReader(source, schema); | ||
break; | ||
default: | ||
throw new RepositoryError( | ||
`Unexpected file type: ${format} is unacceptable.` | ||
); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/record/import/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,22 @@ | ||
import type { LocalRecordRepository } from "../usecases/interface"; | ||
import type { LocalRecord } from "../types/record"; | ||
|
||
export class LocalRecordRepositoryMock implements LocalRecordRepository { | ||
readonly format: string; | ||
readonly length: () => Promise<number>; | ||
|
||
readonly reader: () => AsyncGenerator<LocalRecord, void, undefined>; | ||
|
||
constructor(source: LocalRecord[], format: string) { | ||
this.format = format; | ||
this.length = async () => source.length; | ||
this.reader = () => asyncGeneratorFromStream(source); | ||
} | ||
} | ||
|
||
// eslint-disable-next-line func-style | ||
async function* asyncGeneratorFromStream( | ||
source: LocalRecord[] | ||
): AsyncGenerator<LocalRecord, void, undefined> { | ||
yield* source; | ||
} |
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...ers/parseCsv/__tests__/fieldValue.test.ts → ...ers/parseCsv/__tests__/fieldValue.test.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
3 changes: 3 additions & 0 deletions
3
src/record/import/repositories/parsers/parseCsv/__tests__/fixtures/emptyCsv/expected.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,3 @@ | ||
import type { LocalRecord } from "../../../../../../types/record"; | ||
|
||
export const expected: LocalRecord[] = []; |
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...__tests__/fixtures/withNoRecord/schema.ts → ...Csv/__tests__/fixtures/emptyCsv/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
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
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...seCsv/__tests__/fixtures/withCr/schema.ts → ...seCsv/__tests__/fixtures/withCr/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
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
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...seCsv/__tests__/fixtures/withLf/schema.ts → ...Csv/__tests__/fixtures/withCrLf/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
Oops, something went wrong.