improvement
This commit is contained in:
@@ -5,15 +5,15 @@ import {stat} from "fs/promises";
|
||||
import path from "path";
|
||||
|
||||
export class ComputeHashIndexCache implements ComputeInterface {
|
||||
hashIndexManager: HashIndexManager;
|
||||
hashComputer: HashComputer;
|
||||
private hashIndexManager: HashIndexManager;
|
||||
private hashComputer: HashComputer;
|
||||
|
||||
constructor(indexFilePath: string,private targetHash: CID_ALGORITHM_NAMES[] = [CID_ALGORITHM_NAMES.sha1, CID_ALGORITHM_NAMES.sha256],workerPath?:string) {
|
||||
this.hashComputer = new HashComputer(targetHash,workerPath);
|
||||
this.hashIndexManager = new HashIndexManager(indexFilePath,targetHash);
|
||||
}
|
||||
|
||||
async computeMissingHash(filePath: string, metadata: MultiHashData): Promise<void> {
|
||||
public async computeMissingHash(filePath: string, metadata: MultiHashData): Promise<void> {
|
||||
await this.hashIndexManager.init();
|
||||
let stats = await stat(filePath);
|
||||
if (this.hashIndexManager.getCache().has(path.basename(filePath))) {
|
||||
@@ -29,4 +29,9 @@ export class ComputeHashIndexCache implements ComputeInterface {
|
||||
await this.hashComputer.computeMissingHash(filePath, metadata);
|
||||
this.hashIndexManager.addFileCid(filePath, stats.size, stats.mtime.toISOString(), metadata);
|
||||
}
|
||||
|
||||
async getHashIndexManager(): Promise<HashIndexManager> {
|
||||
await this.hashIndexManager.init();
|
||||
return this.hashIndexManager;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import {existsAsync} from "./ExistsAsync";
|
||||
import path from "path";
|
||||
import {clearInterval} from "node:timers";
|
||||
import {CID_ALGORITHM_NAMES} from "./MultiHashData";
|
||||
import {stat} from "fs/promises";
|
||||
|
||||
interface IndexLine extends Partial<Record<CID_ALGORITHM_NAMES, string>> {
|
||||
path: string;
|
||||
@@ -163,7 +164,7 @@ export class HashIndexManager {
|
||||
});
|
||||
}
|
||||
|
||||
private async saveCacheToFile(): Promise<void> {
|
||||
public async saveCacheToFile(): Promise<void> {
|
||||
if (this.indexOpsInProgress || !this.hasChanged) {
|
||||
return;
|
||||
}
|
||||
@@ -205,6 +206,34 @@ export class HashIndexManager {
|
||||
this.indexOpsInProgress = false;
|
||||
}
|
||||
|
||||
public async getCidForFileAsync(filePath: string): Promise<IndexLine> {
|
||||
const fileName = path.basename(filePath);
|
||||
const stats = await stat(filePath);
|
||||
let fileNameIndex = this.cache.get(fileName);
|
||||
if (fileNameIndex) {
|
||||
if (fileNameIndex.mtime) {
|
||||
//if we have a mtime, we need to check it
|
||||
if (fileNameIndex.size === (stats.size + "") && fileNameIndex.mtime === stats.mtime.toISOString()) {
|
||||
return fileNameIndex;
|
||||
}
|
||||
} else {
|
||||
//mtime is optional
|
||||
if (fileNameIndex.size === (stats.size + "")) {
|
||||
return fileNameIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3 - if not found, delete the entry (keeps the index clean)
|
||||
/*if (fileNameIndex && fileNameIndex.size !== (fileSize + "") && pathIndex && pathIndex.size !== (fileSize + "")) {
|
||||
this.cache.delete(fileName);
|
||||
this.cache.delete(filePath);
|
||||
}*/
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public getCidForFile(filePath: string, fileSize: number, mtime: string): IndexLine {
|
||||
const fileName = path.basename(filePath);
|
||||
let fileNameIndex = this.cache.get(fileName);
|
||||
|
||||
Reference in New Issue
Block a user