@forzalabs/remora
Version:
A powerful CLI tool for seamless data translation.
78 lines (77 loc) • 2.76 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const DatasetRecord_1 = __importDefault(require("./DatasetRecord"));
/**
* A pool of DatasetRecord objects to optimize memory allocation during batch processing
*/
class DatasetRecordPool {
constructor(poolSize) {
/**
* Initialize the pool with empty DatasetRecord objects
*/
this._initializePool = () => {
this._pool = [];
for (let i = 0; i < this._poolSize; i++) {
this._pool.push(new DatasetRecord_1.default('', [], ','));
}
this._poolIndex = 0;
};
/**
* Get the next available record from the pool and reinitialize it with new data
* @param line The raw line data
* @param dimensions The dataset dimensions
* @param delimiter The delimiter to use
* @returns A reinitialized DatasetRecord from the pool
*/
this.getNext = (line, dimensions, delimiter) => {
const record = this._pool[this._poolIndex];
record.reinitialize(line, dimensions, delimiter);
this._poolIndex = (this._poolIndex + 1) % this._poolSize;
return record;
};
/**
* Reset the pool index to start from the beginning
* This should be called when starting a new batch
*/
this.reset = () => {
this._poolIndex = 0;
};
/**
* Update the pool size and reinitialize if necessary
* @param newSize The new pool size
*/
this.resize = (newSize) => {
if (newSize !== this._poolSize) {
this._poolSize = newSize;
this._initializePool();
}
};
/**
* Update all pooled records with new dimensions and delimiter
* This should be called when dataset dimensions change
* @param dimensions The new dimensions
* @param delimiter The new delimiter
*/
this.updateDimensions = (dimensions, delimiter) => {
for (const record of this._pool) {
record.reinitialize('', dimensions, delimiter);
}
};
/**
* Get the current pool size
*/
this.getSize = () => this._poolSize;
/**
* Get the current pool index
*/
this.getCurrentIndex = () => this._poolIndex;
this._poolSize = poolSize;
this._poolIndex = 0;
this._pool = [];
this._initializePool();
}
}
exports.default = DatasetRecordPool;