|
| 1 | +import { createReadStream, createWriteStream, promises as fs } from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import readline from 'node:readline/promises'; |
| 4 | +import stream from 'node:stream/promises'; |
| 5 | + |
| 6 | +import { driver, EJSON, type mongodb, PARALLEL_DIRECTORY, TEMP_DIRECTORY } from '../../driver.mjs'; |
| 7 | + |
| 8 | +export const taskSize = 565; |
| 9 | + |
| 10 | +let collection: mongodb.Collection; |
| 11 | + |
| 12 | +export async function before() { |
| 13 | + await driver.drop(); |
| 14 | + await driver.create(); |
| 15 | + await driver.resetTmpDir(); |
| 16 | + |
| 17 | + collection = driver.client.db(driver.DB_NAME).collection(driver.COLLECTION_NAME); |
| 18 | + |
| 19 | + const ldjson_multi = path.resolve(PARALLEL_DIRECTORY, 'ldjson_multi'); |
| 20 | + |
| 21 | + const files = (await fs.readdir(ldjson_multi)).map(fileName => |
| 22 | + path.resolve(ldjson_multi, fileName) |
| 23 | + ); |
| 24 | + |
| 25 | + const uploads = files.map(async fileName => { |
| 26 | + const fileStream = createReadStream(fileName); |
| 27 | + const lineReader = readline.createInterface({ |
| 28 | + input: fileStream |
| 29 | + }); |
| 30 | + |
| 31 | + const operations = []; |
| 32 | + |
| 33 | + for await (const line of lineReader) { |
| 34 | + operations.push({ |
| 35 | + insertOne: { |
| 36 | + document: JSON.parse(line) |
| 37 | + } |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + fileStream.close(); |
| 42 | + lineReader.close(); |
| 43 | + |
| 44 | + return await collection.bulkWrite(operations); |
| 45 | + }); |
| 46 | + |
| 47 | + await Promise.all(uploads); |
| 48 | +} |
| 49 | + |
| 50 | +export async function beforeEach() { |
| 51 | + await driver.resetTmpDir(); |
| 52 | +} |
| 53 | + |
| 54 | +export async function run() { |
| 55 | + const skips = Array.from({ length: 100 }, (_, index) => index * 5000); |
| 56 | + |
| 57 | + const promises = skips.map(async skip => { |
| 58 | + const documentCursor = collection.find({}, { skip, limit: 5000 }); |
| 59 | + documentCursor.map(doc => EJSON.stringify(doc)); |
| 60 | + const outputStream = createWriteStream(path.resolve(TEMP_DIRECTORY, `tmp-${skip}.txt`)); |
| 61 | + return await stream.pipeline(documentCursor.stream(), outputStream); |
| 62 | + }); |
| 63 | + |
| 64 | + await Promise.all(promises); |
| 65 | +} |
| 66 | + |
| 67 | +export async function afterEach() { |
| 68 | + await driver.resetTmpDir(); |
| 69 | +} |
| 70 | + |
| 71 | +export async function after() { |
| 72 | + await driver.drop(); |
| 73 | + await driver.close(); |
| 74 | +} |
0 commit comments