-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PB-1907] feat: cache file contents (#472)
* chore: extract upload logic to application service * chore: use offlineFileUploader service * feat: read chunks from cached files * feat: delete buffer from cache when released * chore: remove dead code
- Loading branch information
1 parent
760c5fd
commit b43a5b6
Showing
19 changed files
with
210 additions
and
41 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
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
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,28 +1,41 @@ | ||
import { OfflineDriveDependencyContainer } from '../dependency-injection/offline/OfflineDriveDependencyContainer'; | ||
import { VirtualDriveDependencyContainer } from '../dependency-injection/virtual-drive/VirtualDriveDependencyContainer'; | ||
import { NotifyFuseCallback } from './FuseCallback'; | ||
import { FuseIOError } from './FuseErrors'; | ||
|
||
export class ReleaseCallback extends NotifyFuseCallback { | ||
constructor(private readonly container: OfflineDriveDependencyContainer) { | ||
constructor( | ||
private readonly offlineDrive: OfflineDriveDependencyContainer, | ||
private readonly virtualDrive: VirtualDriveDependencyContainer | ||
) { | ||
super('Release'); | ||
} | ||
|
||
async execute(path: string, _fd: number) { | ||
const file = await this.container.offlineFileSearcher.run({ path }); | ||
const offlineFile = await this.offlineDrive.offlineFileSearcher.run({ | ||
path, | ||
}); | ||
|
||
if (!file) { | ||
if (offlineFile) { | ||
await this.offlineDrive.offlineContentsUploader.run( | ||
offlineFile.id, | ||
offlineFile.path | ||
); | ||
return this.right(); | ||
} | ||
|
||
try { | ||
await this.container.offlineContentsUploader.run(file.id, file.path); | ||
return this.right(); | ||
} catch (err: unknown) { | ||
if (err instanceof Error) { | ||
return this.left(new FuseIOError()); | ||
} | ||
const virtualFile = await this.virtualDrive.filesSearcher.run({ path }); | ||
|
||
if (virtualFile) { | ||
const contentsPath = | ||
this.virtualDrive.relativePathToAbsoluteConverter.run( | ||
virtualFile.contentsId | ||
); | ||
|
||
await this.offlineDrive.offlineContentsCacheCleaner.run(contentsPath); | ||
|
||
return this.left(new FuseIOError()); | ||
return this.right(); | ||
} | ||
|
||
return this.right(); | ||
} | ||
} |
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
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
22 changes: 22 additions & 0 deletions
22
src/context/offline-drive/contents/application/ContentsChunkReader.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 { Optional } from '../../../../shared/types/Optional'; | ||
import { ContentsRepository } from '../domain/ContentsRepository'; | ||
|
||
export class ContentsChunkReader { | ||
constructor(private readonly repository: ContentsRepository) {} | ||
|
||
async run( | ||
contentsPath: string, | ||
length: number, | ||
position: number | ||
): Promise<Optional<Buffer>> { | ||
const data = await this.repository.read(contentsPath); | ||
|
||
if (position >= data.length) { | ||
return Optional.empty(); | ||
} | ||
|
||
const chunk = data.slice(position, position + length); | ||
|
||
return Optional.of(chunk); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/context/offline-drive/contents/application/OfflineContentsCacheCleaner.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 { ContentsRepository } from '../domain/ContentsRepository'; | ||
|
||
export class OfflineContentsCacheCleaner { | ||
constructor(private readonly repository: ContentsRepository) {} | ||
|
||
run(contentsPath: string): Promise<void> { | ||
return this.repository.forget(contentsPath); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/context/offline-drive/contents/domain/ContentsRepository.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 interface ContentsRepository { | ||
read(path: string): Promise<Buffer>; | ||
|
||
forget(path: string): Promise<void>; | ||
} |
28 changes: 28 additions & 0 deletions
28
src/context/offline-drive/contents/infrastructure/cache/CachedFSContentsRepository.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,28 @@ | ||
import { ContentsRepository } from '../../domain/ContentsRepository'; | ||
import fs from 'fs/promises'; | ||
import Logger from 'electron-log'; | ||
import { basename } from 'path'; | ||
export class CachedFSContentsRepository implements ContentsRepository { | ||
private buffers: Map<string, Buffer> = new Map(); | ||
|
||
async read(path: string): Promise<Buffer> { | ||
const cached = this.buffers.get(path); | ||
|
||
if (cached) { | ||
return cached; | ||
} | ||
|
||
const read = await fs.readFile(path); | ||
this.buffers.set(path, read); | ||
|
||
return read; | ||
} | ||
|
||
async forget(path: string): Promise<void> { | ||
const deleted = this.buffers.delete(path); | ||
|
||
if (deleted) { | ||
Logger.debug(`Buffer from ${basename(path)} deleted from cache`); | ||
} | ||
} | ||
} |
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,23 @@ | ||
export class Optional<T> { | ||
private constructor(private readonly value: T | undefined) {} | ||
|
||
static of<T>(value: T): Optional<T> { | ||
return new Optional(value); | ||
} | ||
|
||
static empty<T>(): Optional<T> { | ||
return new Optional<T>(undefined); | ||
} | ||
|
||
get(): T { | ||
if (!this.value) { | ||
throw new Error('Element not found'); | ||
} | ||
|
||
return this.value; | ||
} | ||
|
||
isPresent(): boolean { | ||
return this.value !== undefined; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
tests/context/offline-drive/boundaryBridge/__mocks__/GivenFileOfflineFileSearcher.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,16 @@ | ||
import { OfflineFileSearcher } from '../../../../../src/context/offline-drive/files/application/OfflineFileSearcher'; | ||
import { | ||
OfflineFile, | ||
OfflineFileAttributes, | ||
} from '../../../../../src/context/offline-drive/files/domain/OfflineFile'; | ||
import { OfflineFileRepository } from '../../../../../src/context/offline-drive/files/domain/OfflineFileRepository'; | ||
|
||
export class GivenFileOfflineFileSearcher extends OfflineFileSearcher { | ||
constructor(private readonly file: OfflineFile) { | ||
super({} as OfflineFileRepository); | ||
} | ||
|
||
async run(_partial: Partial<OfflineFileAttributes>) { | ||
return this.file; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/context/offline-drive/boundaryBridge/__mocks__/NoFileOfflineFileSearcher.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,13 @@ | ||
import { OfflineFileSearcher } from '../../../../../src/context/offline-drive/files/application/OfflineFileSearcher'; | ||
import { OfflineFileAttributes } from '../../../../../src/context/offline-drive/files/domain/OfflineFile'; | ||
import { OfflineFileRepository } from '../../../../../src/context/offline-drive/files/domain/OfflineFileRepository'; | ||
|
||
export class NoFileOfflineFileSearcher extends OfflineFileSearcher { | ||
constructor() { | ||
super({} as OfflineFileRepository); | ||
} | ||
|
||
async run(_partial: Partial<OfflineFileAttributes>) { | ||
return undefined; | ||
} | ||
} |
Oops, something went wrong.