import * as Either from 'fp-ts/lib/Either' ; import { SavedObjectsRawDoc } from '../..' ; import { createBulkOperationBody } from '../actions/bulk_overwrite_transformed_documents' ; /**
 * Creates batches of documents to be used by the bulk API. Each batch will
 * have a request body content length that's <= maxBatchSizeBytes
 */ export function createBatches ( docs , index , maxBatchSizeBytes ) { const BRACKETS_BYTES = 2 ; const NDJSON_NEW_LINE_BYTES = 1 ; const batches = [ [ ] ] as [ SavedObjectsRawDoc [ ] ] ; let currBatch = 0 ; let currBatchSizeBytes = 0 ; for ( const doc of docs ) { const bulkOperationBody = createBulkOperationBody ( doc , index ) ; const docSizeBytes = Buffer . byteLength ( JSON . stringify ( bulkOperationBody ) , 'utf8' ) - BRACKETS_BYTES + NDJSON_NEW_LINE_BYTES ; if ( docSizeBytes > maxBatchSizeBytes ) { return Either . left ( { type : 'document_exceeds_batch_size_bytes' , docSizeBytes , maxBatchSizeBytes , document : doc , } ) ; } else if ( currBatchSizeBytes + docSizeBytes <= maxBatchSizeBytes ) { batches [ currBatch ] . push ( doc ) ; currBatchSizeBytes = currBatchSizeBytes + docSizeBytes ; } else { currBatch ++ ; batches [ currBatch ] = [ doc ] ; currBatchSizeBytes = docSizeBytes ; } } return Either . right ( batches ) ; }