@@ -385,36 +385,6 @@ JavascriptRedisParser.prototype.setStringNumbers = function (stringNumbers) {
385
385
this . optionStringNumbers = stringNumbers
386
386
}
387
387
388
- /**
389
- * Concat a bulk string containing multiple chunks
390
- *
391
- * Notes:
392
- * 1) The first chunk might contain the whole bulk string including the \r
393
- * 2) We are only safe to fully add up elements that are neither the first nor any of the last two elements
394
- *
395
- * @param parser
396
- * @param buffer
397
- * @returns {String }
398
- */
399
- function concatBulkString ( parser ) {
400
- var list = parser . bufferCache
401
- var chunks = list . length
402
- var offset = parser . bigStrSize - parser . totalChunkSize
403
- parser . offset = offset
404
- if ( offset === 1 ) {
405
- if ( chunks === 2 ) {
406
- return list [ 0 ] . toString ( 'utf8' , parser . bigOffset , list [ 0 ] . length - 1 )
407
- }
408
- chunks --
409
- }
410
- var res = decoder . write ( list [ 0 ] . slice ( parser . bigOffset ) )
411
- for ( var i = 1 ; i < chunks - 1 ; i ++ ) {
412
- res += decoder . write ( list [ i ] )
413
- }
414
- res += decoder . end ( list [ i ] . slice ( 0 , offset - 2 ) )
415
- return res
416
- }
417
-
418
388
/**
419
389
* Decrease the bufferPool size over time
420
390
* @returns {undefined }
@@ -444,37 +414,86 @@ function decreaseBufferPool () {
444
414
}
445
415
446
416
/**
447
- * Concat the collected chunks from parser.bufferCache.
448
- *
449
- * Increases the bufferPool size beforehand if necessary.
417
+ * Check if the requested size fits in the current bufferPool.
418
+ * If it does not, reset and increase the bufferPool accordingly.
450
419
*
451
- * @param parser
452
420
* @param length
453
- * @returns {Buffer }
421
+ * @returns {undefined }
454
422
*/
455
- function concatBuffer ( parser , length ) {
456
- var list = parser . bufferCache
457
- var pos = bufferOffset
458
- length -= parser . offset
423
+ function resizeBuffer ( length ) {
459
424
if ( bufferPool . length < length + bufferOffset ) {
460
425
var multiplier = length > 1024 * 1024 * 75 ? 2 : 3
461
- if ( bufferOffset > 1024 * 1024 * 120 ) {
426
+ if ( bufferOffset > 1024 * 1024 * 111 ) {
462
427
bufferOffset = 1024 * 1024 * 50
463
428
}
464
429
bufferPool = new Buffer ( length * multiplier + bufferOffset )
465
430
bufferOffset = 0
466
431
counter ++
467
- pos = 0
468
432
if ( interval === null ) {
469
433
interval = setInterval ( decreaseBufferPool , 50 )
470
434
}
471
435
}
472
- list [ 0 ] . copy ( bufferPool , pos , parser . offset , list [ 0 ] . length )
473
- pos += list [ 0 ] . length - parser . offset
474
- for ( var i = 1 ; i < list . length ; i ++ ) {
436
+ }
437
+
438
+ /**
439
+ * Concat a bulk string containing multiple chunks
440
+ *
441
+ * Notes:
442
+ * 1) The first chunk might contain the whole bulk string including the \r
443
+ * 2) We are only safe to fully add up elements that are neither the first nor any of the last two elements
444
+ *
445
+ * @param parser
446
+ * @returns {String }
447
+ */
448
+ function concatBulkString ( parser ) {
449
+ var list = parser . bufferCache
450
+ var chunks = list . length
451
+ var offset = parser . bigStrSize - parser . totalChunkSize
452
+ parser . offset = offset
453
+ if ( offset === 1 ) {
454
+ if ( chunks === 2 ) {
455
+ return list [ 0 ] . toString ( 'utf8' , parser . bigOffset , list [ 0 ] . length - 1 )
456
+ }
457
+ chunks --
458
+ }
459
+ var res = decoder . write ( list [ 0 ] . slice ( parser . bigOffset ) )
460
+ for ( var i = 1 ; i < chunks - 1 ; i ++ ) {
461
+ res += decoder . write ( list [ i ] )
462
+ }
463
+ res += decoder . end ( list [ i ] . slice ( 0 , offset - 2 ) )
464
+ return res
465
+ }
466
+
467
+ /**
468
+ * Concat the collected chunks from parser.bufferCache.
469
+ *
470
+ * Increases the bufferPool size beforehand if necessary.
471
+ *
472
+ * @param parser
473
+ * @returns {Buffer }
474
+ */
475
+ function concatBulkBuffer ( parser ) {
476
+ var list = parser . bufferCache
477
+ var chunks = list . length
478
+ var length = parser . bigStrSize - parser . bigOffset - 2
479
+ var offset = parser . bigStrSize - parser . totalChunkSize
480
+ parser . offset = offset
481
+ if ( offset === 1 ) {
482
+ if ( chunks === 2 ) {
483
+ return list [ 0 ] . slice ( parser . bigOffset , list [ 0 ] . length - 1 )
484
+ }
485
+ chunks --
486
+ offset = list [ list . length - 1 ] . length + 1
487
+ }
488
+ resizeBuffer ( length )
489
+ var pos = bufferOffset
490
+ list [ 0 ] . copy ( bufferPool , pos , parser . bigOffset , list [ 0 ] . length )
491
+ pos += list [ 0 ] . length - parser . bigOffset
492
+ for ( var i = 1 ; i < list . length - 1 ; i ++ ) {
475
493
list [ i ] . copy ( bufferPool , pos )
476
494
pos += list [ i ] . length
477
495
}
496
+ list [ i ] . copy ( bufferPool , pos , 0 , offset - 2 )
478
497
var buffer = bufferPool . slice ( bufferOffset , length + bufferOffset )
479
498
bufferOffset += length
480
499
return buffer
@@ -486,7 +505,6 @@ function concatBuffer (parser, length) {
486
505
* @returns {undefined }
487
506
*/
488
507
JavascriptRedisParser . prototype . execute = function execute ( buffer ) {
489
- var arr
490
508
if ( this . buffer === null ) {
491
509
this . buffer = buffer
492
510
this . offset = 0
@@ -499,32 +517,26 @@ JavascriptRedisParser.prototype.execute = function execute (buffer) {
499
517
this . buffer = newBuffer
500
518
this . offset = 0
501
519
if ( this . arrayCache . length ) {
502
- arr = parseArrayChunks ( this )
520
+ var arr = parseArrayChunks ( this )
503
521
if ( ! arr ) {
504
522
return
505
523
}
506
524
this . returnReply ( arr )
507
525
}
508
526
} else if ( this . totalChunkSize + buffer . length >= this . bigStrSize ) {
509
527
this . bufferCache . push ( buffer )
510
- if ( this . optionReturnBuffers === false && ! this . arrayCache . length ) {
511
- this . returnReply ( concatBulkString ( this ) )
512
- this . buffer = buffer
513
- } else {
514
- this . buffer = concatBuffer ( this , this . totalChunkSize + buffer . length )
515
- this . offset = 0
516
- if ( this . arrayCache . length ) {
517
- arr = parseArrayChunks ( this )
518
- if ( ! arr ) {
519
- this . bigStrSize = 0
520
- this . bufferCache = [ ]
521
- return
522
- }
523
- this . returnReply ( arr )
524
- }
525
- }
528
+ var tmp = this . optionReturnBuffers ? concatBulkBuffer ( this ) : concatBulkString ( this )
526
529
this . bigStrSize = 0
527
530
this . bufferCache = [ ]
531
+ this . buffer = buffer
532
+ if ( this . arrayCache . length ) {
533
+ this . arrayCache [ 0 ] [ this . arrayPos [ 0 ] ++ ] = tmp
534
+ tmp = parseArrayChunks ( this )
535
+ if ( ! tmp ) {
536
+ return
537
+ }
538
+ }
539
+ this . returnReply ( tmp )
528
540
} else {
529
541
this . bufferCache . push ( buffer )
530
542
this . totalChunkSize += buffer . length
0 commit comments