|
| 1 | +import { createReadStream, createWriteStream, promises as fs } from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { pipeline } from 'node:stream/promises'; |
| 4 | + |
| 5 | +import { driver, type mongodb, PARALLEL_DIRECTORY, TEMP_DIRECTORY } from '../../driver.mjs'; |
| 6 | + |
| 7 | +export const taskSize = 262.144; |
| 8 | + |
| 9 | +let bucket: mongodb.GridFSBucket; |
| 10 | + |
| 11 | +export async function before() { |
| 12 | + await driver.drop(); |
| 13 | + await driver.create(); |
| 14 | + await driver.resetTmpDir(); |
| 15 | + |
| 16 | + bucket = driver.bucket(driver.client.db(driver.DB_NAME)); |
| 17 | + await bucket.drop().catch(() => null); |
| 18 | + |
| 19 | + const gridfs_multi = path.resolve(PARALLEL_DIRECTORY, 'gridfs_multi'); |
| 20 | + |
| 21 | + const files = (await fs.readdir(gridfs_multi)).map(filename => |
| 22 | + path.resolve(gridfs_multi, filename) |
| 23 | + ); |
| 24 | + |
| 25 | + const uploadPromises = files.map(async filename => { |
| 26 | + const fileStream = createReadStream(filename); |
| 27 | + const uploadStream = bucket.openUploadStream(filename); |
| 28 | + return await pipeline(fileStream, uploadStream); |
| 29 | + }); |
| 30 | + |
| 31 | + await Promise.all(uploadPromises); |
| 32 | +} |
| 33 | + |
| 34 | +export async function beforeEach() { |
| 35 | + await driver.resetTmpDir(); |
| 36 | +} |
| 37 | + |
| 38 | +export async function run() { |
| 39 | + const files = await bucket |
| 40 | + .find() |
| 41 | + .map(({ _id }) => ({ |
| 42 | + path: path.resolve(TEMP_DIRECTORY, `${_id}.txt`), |
| 43 | + _id |
| 44 | + })) |
| 45 | + .toArray(); |
| 46 | + |
| 47 | + const downloads = files.map(async ({ _id, path }) => { |
| 48 | + const fileStream = createWriteStream(path); |
| 49 | + const downloadStream = bucket.openDownloadStream(_id); |
| 50 | + return await pipeline(downloadStream, fileStream); |
| 51 | + }); |
| 52 | + |
| 53 | + await Promise.all(downloads); |
| 54 | +} |
| 55 | + |
| 56 | +export async function afterEach() { |
| 57 | + await driver.resetTmpDir(); |
| 58 | +} |
| 59 | + |
| 60 | +export async function after() { |
| 61 | + await driver.drop(); |
| 62 | + await driver.close(); |
| 63 | +} |
0 commit comments