add initialize
This commit is contained in:
@@ -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>;
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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>;
|
||||||
@@ -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);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user