add initialize

This commit is contained in:
Pierre-Henri
2024-04-21 11:30:09 +02:00
parent c2b86a44e6
commit 68e4689fe8
4 changed files with 35 additions and 16 deletions
+11
View File
@@ -73,3 +73,14 @@ Contributions to Meta-Hash are welcome! Please read our contributing guidelines
## License ## License
Meta-Hash is released under the [MIT License](LICENSE.md). Meta-Hash is released under the [MIT License](LICENSE.md).
Example of usage:
```typescript
const packageEntryPath = (path.dirname(import.meta.resolve("@metazla/meta-hash"))) + "/worker.js";
console.log("Sha Worker Path", packageEntryPath);
if (!config.INDEX_FOLDER_PATH) {
throw new Error("INDEX_FOLDER_PATH not set");
}
this.hashComputer = new HashComputerIndexCache(config.INDEX_FOLDER_PATH, targetHash, packageEntryPath);
```
@@ -1,4 +1,6 @@
export interface FileProcessorInterface { export interface FileProcessorInterface {
initialize?(): Promise<void>;
processFile(current: number, queueSize: number, nfoFilePath: string): Promise<void>; processFile(current: number, queueSize: number, nfoFilePath: string): Promise<void>;
canProcessFile(filePath: string): Promise<boolean>; canProcessFile(filePath: string): Promise<boolean>;
+4
View File
@@ -163,6 +163,10 @@ export class FolderWatcher {
} }
let folderList = folders.split(','); let folderList = folders.split(',');
if(this.fileProcessor.initialize){
await this.fileProcessor.initialize();
}
this.chokidarWatch(folderList); this.chokidarWatch(folderList);
//start the initial scan //start the initial scan
+14 -12
View File
@@ -18,10 +18,10 @@ export const INDEX_HEADERS = ['path', 'size', 'mtime'];
export class HashIndexManager { export class HashIndexManager {
private cache: Map<string, IndexLine> = new Map<string, IndexLine>(); private cache: Map<string, IndexLine> = new Map<string, IndexLine>();
private intervalId: NodeJS.Timeout; private intervalId: any;
private intervalTime: number = 30000; private intervalTime: number = 30000;
private lastIndexFileSize: number = 0; //size of the index file last time it was read private lastIndexFileSize: { [key in CID_ALGORITHM_NAMES]?: number } = {}; //size of the index file last time it was read
private lastCacheFile: IndexLine[]; //state of the file last time it was read private lastCacheFile: { [key in CID_ALGORITHM_NAMES]?: IndexLine[] } = {}; //state of the file last time it was read
private indexOpsInProgress: boolean = false; private indexOpsInProgress: boolean = false;
private hasChanged: boolean = false; private hasChanged: boolean = false;
private initialLoad: Promise<void>; private initialLoad: Promise<void>;
@@ -48,7 +48,7 @@ export class HashIndexManager {
throw new Error(`Invalid index folder path ${this.indexFolderPath}`); throw new Error(`Invalid index folder path ${this.indexFolderPath}`);
} }
for (const hash of this.targetHash) { for (const hash of this.targetHash) {
this.filePaths[hash] = path.join(this.indexFolderPath,`index-${hash}.csv`); this.filePaths[hash] = path.join(this.indexFolderPath, `index-${hash}.csv`);
console.log(`Index file path for ${hash} is ${this.filePaths[hash]}`); console.log(`Index file path for ${hash} is ${this.filePaths[hash]}`);
if (!this.filePaths[hash]) { if (!this.filePaths[hash]) {
throw new Error(`Invalid index file path for ${hash}`); throw new Error(`Invalid index file path for ${hash}`);
@@ -113,23 +113,23 @@ export class HashIndexManager {
if (await existsAsync(this.filePaths[hash])) { if (await existsAsync(this.filePaths[hash])) {
// check the file size and if it did not change, do not read the file // check the file size and if it did not change, do not read the file
const stats = await fs.stat(this.filePaths[hash]); const stats = await fs.stat(this.filePaths[hash]);
if (this.lastIndexFileSize !== stats.size) { if (this.lastIndexFileSize[hash] !== stats.size) {
// Read existing file content and parse it // Read existing file content and parse it
const records: IndexLine[] = await this.readCsv(hash); const records: IndexLine[] = await this.readCsv(hash);
for (const record of records) { for (const record of records) {
let indexLine = this.cache.get(record.path); let indexLine = this.cache.get(record.path);
if (!indexLine) { if (!indexLine) {
this.cache.set(record.path, record); this.cache.set(record.path, record);
} else { } else if(record[hash]){
//update the cache with the latest data //update the cache with the latest data
indexLine[hash] = record[hash]; indexLine[hash] = record[hash];
} }
} }
this.lastIndexFileSize = stats.size; this.lastIndexFileSize[hash] = stats.size;
this.lastCacheFile = records; this.lastCacheFile[hash] = records;
return records; return records;
} else { } else {
return this.lastCacheFile; return this.lastCacheFile[hash];
} }
} }
return []; return [];
@@ -152,15 +152,17 @@ export class HashIndexManager {
const records: IndexLine[] = []; const records: IndexLine[] = [];
return new Promise((resolve, reject) => { return new Promise<IndexLine[]>((resolve, reject) => {
createReadStream(this.filePaths[hash]) createReadStream(this.filePaths[hash])
.pipe(parser) .pipe(parser)
.on('data', (record) => { .on('data', (record) => {
records.push(record); records.push(record);
}).on('end', () => { })
.on('end', () => {
resolve(records); resolve(records);
console.log(`Index read ${hash} time ${performance.now() - start}ms`); console.log(`Index read ${hash} time ${performance.now() - start}ms`);
}).on('error', (err) => { })
.on('error', (err) => {
reject(err); reject(err);
}); });
}); });