-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Irys-xyz/feat/tx-data-prefetch
feat: ✨ workers: add prefetch-data worker
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* eslint-disable header/header */ | ||
import { default as fastq } from 'fastq'; | ||
import type { queueAsPromised } from 'fastq'; | ||
import * as winston from 'winston'; | ||
|
||
import { ContiguousDataSource, MatchableItem } from '../types.js'; | ||
|
||
const DEFAULT_WORKER_COUNT = 4; | ||
|
||
export class DataPrefetcher { | ||
// Dependencies | ||
private log: winston.Logger; | ||
private contiguousDataSource: ContiguousDataSource; | ||
// private indexWriter: DataItemIndexWriter; | ||
|
||
// Data indexing queue | ||
private queue: queueAsPromised<MatchableItem, void>; | ||
|
||
constructor({ | ||
log, | ||
// indexWriter, | ||
contiguousDataSource, | ||
workerCount = DEFAULT_WORKER_COUNT, | ||
}: { | ||
log: winston.Logger; | ||
// indexWriter: DataItemIndexWriter; | ||
contiguousDataSource: ContiguousDataSource; | ||
workerCount?: number; | ||
}) { | ||
this.log = log.child({ class: 'DataPrefetcher' }); | ||
// this.indexWriter = indexWriter; | ||
this.contiguousDataSource = contiguousDataSource; | ||
|
||
this.queue = fastq.promise(this.prefetchData.bind(this), workerCount); | ||
} | ||
|
||
async queuePrefetchData(item: MatchableItem): Promise<void> { | ||
const log = this.log.child({ | ||
method: 'queueDataItem', | ||
id: item.id, | ||
}); | ||
log.debug('Queueing item for prefetching...'); | ||
this.queue.push(item); | ||
log.debug('Data item queued for prefetching.'); | ||
} | ||
|
||
async prefetchData(item: MatchableItem): Promise<void> { | ||
const log = this.log.child({ | ||
method: 'indexDataItem', | ||
id: item.id, | ||
}); | ||
|
||
try { | ||
log.debug('Prefetching data item...'); | ||
const res = await this.contiguousDataSource.getData(item.id); | ||
const stream = res.stream; | ||
// you have to consume the stream so it actually caches the item fully. | ||
for await (const _ of stream) { | ||
true; // void it | ||
} | ||
log.debug('Data item prefetched.'); | ||
} catch (error) { | ||
log.error('Failed to prefetch data item data:', error); | ||
} | ||
} | ||
} |
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,8 @@ | ||
for i in `find ./data -maxdepth 1 -mindepth 1 -type d ` # Finds all the subfolders and loop. | ||
do | ||
echo $i | ||
sudo rm -rf ${i} | ||
mkdir ${i} | ||
done | ||
|
||
yarn db:migrate up |