<commit_before>'use strict';

// Simple function wrapper that batches Node-style calls.

// Batching is achieved by wrapping a function in logic that records calls in
// a batch for some time, then calls that function once with the accumulated
// batch, giving it an opportunity to process all the calls efficiently at
// once. Unbatching takes a batch of calls, applies them individually to a
// function, then returns a batch of results.

const _ = require('underscore');
const yieldable = require('abacus-yieldable');
const transform = require('abacus-transform');

const map = _.map;
const identity = _.identity;
const initial = _.initial;
const zip = _.zip;
const last = _.last;
const extend = _.extend;
const isFunction = _.isFunction;
const object = _.object;
const pairs = _.pairs;
const functions = _.functions;
const bind = _.bind;
const filter = _.filter;
const values = _.values;
const flatten = _.flatten;
const groupBy = _.groupBy;
const sortBy = _.sortBy;

// Setup debug log
const debug = require('abacus-debug')('abacus-batch');

// Return a function that records calls for some time and presents the whole
// list of calls later to the given batch function
const batchifyfn = (fn, delay, max) => {
  // Use a default batch time window of 20 msec
  const timeout = delay === undefined ? 20 : delay;
  const bsize = max === undefined ? 100 : max;

  // Convert to a function with callback
  const fcb = yieldable.functioncb(fn);

  // Warning: calls is a mutable variable, but that's by design as we're
  // using it to record and accumulate function calls
  let calls = [];

  // Warning: tid is a mutable variable, by design again as it's used to
  // record the id of the batching timeout
  let tid;

  // Flush a batch of recorded calls
  const flush = () => {
    const bcalls = calls;
    // Warning: mutating variable calls, by design again
    calls = [];

    // Pass the list of calls to the batch function
    debug(
      'Calling batch function %s with %d batched calls', name, bcalls.length);
    fcb(map(bcalls, (call) => initial(call)), (err, vals) => {
      // Pass each call result to the corresponding callback
      debug('Returning %d batch function results with error %o',
        vals ? vals.length : 0, err);
      return map(zip(bcalls, vals),
        (call) => last(call[0]).apply(undefined, err ? [err] : call[1]));
    });
  };

  // Determine the application function name
  const name = fcb.fname || fcb.name;

  const wrapper = function() {
    // Record each call arguments in our list of calls
    const call = map(arguments, identity);
    debug('Recording function call to %s', name);
    // Warning: mutating variable calls, by design
    calls.push(call);
<commit_msg>Minor debug log message improvement<commit_after>'use strict';

// Simple function wrapper that batches Node-style calls.

// Batching is achieved by wrapping a function in logic that records calls in
// a batch for some time, then calls that function once with the accumulated
// batch, giving it an opportunity to process all the calls efficiently at
// once. Unbatching takes a batch of calls, applies them individually to a
// function, then returns a batch of results.

const _ = require('underscore');
const yieldable = require('abacus-yieldable');
const transform = require('abacus-transform');

const map = _.map;
const identity = _.identity;
const initial = _.initial;
const zip = _.zip;
const last = _.last;
const extend = _.extend;
const isFunction = _.isFunction;
const object = _.object;
const pairs = _.pairs;
const functions = _.functions;
const bind = _.bind;
const filter = _.filter;
const values = _.values;
const flatten = _.flatten;
const groupBy = _.groupBy;
const sortBy = _.sortBy;

// Setup debug log
const debug = require('abacus-debug')('abacus-batch');

// Return a function that records calls for some time and presents the whole
// list of calls later to the given batch function
const batchifyfn = (fn, delay, max) => {
  // Use a default batch time window of 20 msec
  const timeout = delay === undefined ? 20 : delay;
  const bsize = max === undefined ? 100 : max;

  // Convert to a function with callback
  const fcb = yieldable.functioncb(fn);

  // Warning: calls is a mutable variable, but that's by design as we're
  // using it to record and accumulate function calls
  let calls = [];

  // Warning: tid is a mutable variable, by design again as it's used to
  // record the id of the batching timeout
  let tid;

  // Flush a batch of recorded calls
  const flush = () => {
    const bcalls = calls;
    // Warning: mutating variable calls, by design again
    calls = [];

    // Pass the list of calls to the batch function
    debug(
      'Calling batch function %s with %d batched calls', name, bcalls.length);
    fcb(map(bcalls, (call) => initial(call)), (err, vals) => {
      if(err)
        debug('Returning %d batch function results with error %o',
          vals ? vals.length : 0, err);
        else
        debug('Returning %d batch function results',
          vals ? vals.length : 0);

      // Pass each call result to the corresponding callback
      return map(zip(bcalls, vals),
        (call) => last(call[0]).apply(undefined, err ? [err] : call[1]));
    });
  };

  // Determine the application function name
  const name = fcb.fname || fcb.name;

  const wrapper = function() {
    // Record each call arguments in our list of calls
    const call = map(arguments, identity);
    debug('Recording function call to %s', name);
    // Warning: mutating variable calls, by design
    calls.push(call);

{'fraction_non_alphanumeric': 0.10526315789473684, 'fraction_numerical': 0.0037593984962406013, 'mean_word_length': 3.261632341723875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7363591', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1541, 'n_words': 692}