Skip to content

Commit

Permalink
feat: delete buffer from cache when released
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanVicens committed Mar 20, 2024
1 parent e6afb04 commit 38d7c8d
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 79 deletions.
3 changes: 2 additions & 1 deletion src/apps/fuse/FuseApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export class FuseApp {
);
const write = new WriteCallback(this.fuseContainer.offlineDriveContainer);
const release = new ReleaseCallback(
this.fuseContainer.offlineDriveContainer
this.fuseContainer.offlineDriveContainer,
this.fuseContainer.virtualDriveContainer
);

return {
Expand Down
2 changes: 1 addition & 1 deletion src/apps/fuse/callbacks/ReadCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class ReadCallback {
length: number,
position: number
): Promise<number> {
Logger.debug('READING ', length, 'MB FROM ', filePath);
// Logger.debug('READING ', length, 'MB FROM ', filePath);

const readResult = await this.offlineDrive.contentsChunkReader.run(
filePath,
Expand Down
33 changes: 28 additions & 5 deletions src/apps/fuse/callbacks/ReleaseCallback.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
import { OfflineDriveDependencyContainer } from '../dependency-injection/offline/OfflineDriveDependencyContainer';
import { VirtualDriveDependencyContainer } from '../dependency-injection/virtual-drive/VirtualDriveDependencyContainer';
import { NotifyFuseCallback } from './FuseCallback';

export class ReleaseCallback extends NotifyFuseCallback {
constructor(private readonly container: OfflineDriveDependencyContainer) {
super('Release', { input: true });
constructor(
private readonly offlineDrive: OfflineDriveDependencyContainer,
private readonly virtualDrive: VirtualDriveDependencyContainer
) {
super('Release');
}

async execute(path: string, _fd: number) {
const uploadError = await this.container.offlineFileUploader.run(path);
const offlineFile = await this.offlineDrive.offlineFileSearcher.run({
path,
});

if (uploadError) {
return this.left(uploadError);
if (offlineFile) {
await this.offlineDrive.offlineContentsUploader.run(
offlineFile.id,
offlineFile.path
);
return this.right();
}

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.right();
}

return this.right();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { OfflineFileAndContentsCreator } from '../../../../../context/offline-drive/boundaryBridge/application/OfflineFileAndContentsCreator';
import { OfflineFileUploader } from '../../../../../context/offline-drive/boundaryBridge/application/OfflineFileUploader';

export interface BoundaryBridgeContainer {
offlineFileAndContentsCreator: OfflineFileAndContentsCreator;
offlineFileUploader: OfflineFileUploader
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { OfflineFileAndContentsCreator } from '../../../../../context/offline-drive/boundaryBridge/application/OfflineFileAndContentsCreator';
import { OfflineFileUploader } from '../../../../../context/offline-drive/boundaryBridge/application/OfflineFileUploader';
import { OfflineContentsDependencyContainer } from '../OfflineContents/OfflineDriveDependencyContainer';
import { OfflineFilesContainer } from '../OfflineFiles/OfflineFilesContainer';
import { BoundaryBridgeContainer } from './BoundaryBridgeContainer';
Expand All @@ -13,13 +12,7 @@ export async function buildBoundaryBridgeContainer(
offlineContentsContainer.offlineContentsCreator
);

const offlineFileUploader = new OfflineFileUploader(
offlineFileContainer.offlineFileSearcher,
offlineContentsContainer.offlineContentsUploader
);

return {
offlineFileAndContentsCreator,
offlineFileUploader,
};
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { OfflineContentsCacheCleaner } from '../../../../../context/offline-drive/contents/application/OfflineContentsCacheCleaner';
import { ContentsChunkReader } from '../../../../../context/offline-drive/contents/application/ContentsChunkReader';
import { OfflineContentsAppender } from '../../../../../context/offline-drive/contents/application/OfflineContentsAppender';
import { OfflineContentsCreator } from '../../../../../context/offline-drive/contents/application/OfflineContentsCreator';
Expand All @@ -8,4 +9,5 @@ export interface OfflineContentsDependencyContainer {
offlineContentsAppender: OfflineContentsAppender;
offlineContentsUploader: OfflineContentsUploader;
contentsChunkReader: ContentsChunkReader;
offlineContentsCacheCleaner: OfflineContentsCacheCleaner;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ContentsChunkReader } from '../../../../../context/offline-drive/contents/application/ContentsChunkReader';
import { OfflineContentsAppender } from '../../../../../context/offline-drive/contents/application/OfflineContentsAppender';
import { OfflineContentsCacheCleaner } from '../../../../../context/offline-drive/contents/application/OfflineContentsCacheCleaner';
import { OfflineContentsCreator } from '../../../../../context/offline-drive/contents/application/OfflineContentsCreator';
import { OfflineContentsUploader } from '../../../../../context/offline-drive/contents/application/OfflineContentsUploader';
import { EnvironmentOfflineContentsManagersFactory } from '../../../../../context/offline-drive/contents/infrastructure/EnvironmentRemoteFileContentsManagersFactory';
Expand Down Expand Up @@ -55,10 +56,15 @@ export async function buildOfflineContentsContainer(
const contentsRepository = new CachedFSContentsRepository();
const contentsChunkReader = new ContentsChunkReader(contentsRepository);

const offlineContentsCacheCleaner = new OfflineContentsCacheCleaner(
contentsRepository
);

return {
offlineContentsCreator,
offlineContentsAppender,
offlineContentsUploader,
contentsChunkReader,
offlineContentsCacheCleaner,
};
}

This file was deleted.

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);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface ContentsRepository {
read(path: string): Promise<Buffer>;

forget(path: string): void;
forget(path: string): Promise<void>;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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();

Expand All @@ -17,7 +18,11 @@ export class CachedFSContentsRepository implements ContentsRepository {
return read;
}

forget(_path: string): void {
//
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 was deleted.

0 comments on commit 38d7c8d

Please sign in to comment.