<commit_before>/*
 * Memcing
 * Copyright (c) 2014 <NAME> (http://github.com/cmfatih/memcing)
 * For the full copyright and license information, please view the LICENSE.txt file.
 */

// cache module implements caching.

// Init reqs
/* jslint node: true */
'use strict';

var mUtilex = require('utilex');

// Init the module
exports = module.exports = function(iParam) {

  // Init vars
  var gConfig       = {isDebug: false},

      gDataSet      = {},
      gDataLen      = 0,

      gCacheOpt     = {
        limitInKB: 16384,   // cache limit in kilobytes
        limitInEntry: 0,    // cache limit in number of entry
        keyLIC: 64,         // key limit in char
        valLIC: 256,        // val limit in char
        vacuum: {
          ival: 30,         // vacuum interval in seconds
          running: false    // whether vacuum running or not
        },
        eviction: {
          enabled: false,   // eviction enabled or not
          limitInEntry: 0,  // limit in number of entry
        },
        ts: {
          outOfLimit: 0,    // time stamp for last out of limit
          eviction: 0       // eviction process
        }
      },
      gTimers       = {
        vacuum: null        // timer for vacuum
      },

      avlbEntry,    // available space in entry - function
      stats,        // stats - function
      dump,         // dump - function
      vacuum,       // vacuum - function

      get,          // get - function
      set,          // set - function
      add,          // add - function
      del,          // delete - function
      incdec,       // increment or decrement value - function 
      increment,    // increment value - function
      decrement     // decrement value - function
  ;

  // Check params
  if(iParam && iParam.isDebug === true)   gConfig.isDebug             = true;
  if(iParam && !isNaN(iParam.limitInKB))  gCacheOpt.limitInKB         = iParam.limitInKB;
  if(iParam && !isNaN(iParam.vacuumIval)) gCacheOpt.vacuum.ival       = iParam.vacuumIval;
  if(iParam && iParam.eviction === true)  gCacheOpt.eviction.enabled  = true;

  // Calculate the entry limit.
  // Empty space should be guaranteed for each key. Otherwise will fail on updates.
  // This calculation might be important for eviction policies.
  // Also UTF-8 considered for the calculation. (4 bytes for each char.)
  gCacheOpt.limitInEntry = Math.floor((gCacheOpt.limitInKB*1024)/((gCacheOpt.keyLIC+gCacheOpt.valLIC)*4));

  // Calculate the eviction limit.
  gCacheOpt.eviction.limitInEntry = Math.floor((gCacheOpt.limitInEntry*2)/100); // 2%
  if(gCacheOpt.eviction.limitInEntry < 1) gCacheOpt.eviction.limitInEntry = 1;

  // Check and init the vacuum timer.
  if(gCacheOpt.vacuum.ival < 1) gCacheOpt.vacuum.ival = 30; // Do not allowed <= 0
  gTimers.vacuum = setInterval(function() {
    if(!gCacheOpt.vacuum.running) vacuum({all: true});
  }, gCacheOpt.vacuum.ival*1000);

  // Returns available space in entry.
  avlbEntry = function avlbEntry() {
    return (gCacheOpt.limitInEntry-gDataLen);
  };


  // Returns the stats.
  stats = function stats() {
    return {
      options: gCacheOpt,
      numberOfEntry: gDataLen,
      numberOfAvlbEntry: avlbEntry(),
      usageInPercent: Math.floor((gDataLen*100)/gCacheOpt.limitInEntry)
    };
  };

  // Dump the cached data.
  // This is not an export process. It is for small data sets.
  dump = function stats() {
    return gDataSet;
  };

  // Vacuum the data by the given options.
  vacuum = function vacuum(iParam) {<commit_msg>Removing blank line<commit_after>/*
 * Memcing
 * Copyright (c) 2014 <NAME> (http://github.com/cmfatih/memcing)
 * For the full copyright and license information, please view the LICENSE.txt file.
 */

// cache module implements caching.

// Init reqs
/* jslint node: true */
'use strict';

var mUtilex = require('utilex');

// Init the module
exports = module.exports = function(iParam) {

  // Init vars
  var gConfig       = {isDebug: false},

      gDataSet      = {},
      gDataLen      = 0,

      gCacheOpt     = {
        limitInKB: 16384,   // cache limit in kilobytes
        limitInEntry: 0,    // cache limit in number of entry
        keyLIC: 64,         // key limit in char
        valLIC: 256,        // val limit in char
        vacuum: {
          ival: 30,         // vacuum interval in seconds
          running: false    // whether vacuum running or not
        },
        eviction: {
          enabled: false,   // eviction enabled or not
          limitInEntry: 0,  // limit in number of entry
        },
        ts: {
          outOfLimit: 0,    // time stamp for last out of limit
          eviction: 0       // eviction process
        }
      },
      gTimers       = {
        vacuum: null        // timer for vacuum
      },

      avlbEntry,    // available space in entry - function
      stats,        // stats - function
      dump,         // dump - function
      vacuum,       // vacuum - function

      get,          // get - function
      set,          // set - function
      add,          // add - function
      del,          // delete - function
      incdec,       // increment or decrement value - function 
      increment,    // increment value - function
      decrement     // decrement value - function
  ;

  // Check params
  if(iParam && iParam.isDebug === true)   gConfig.isDebug             = true;
  if(iParam && !isNaN(iParam.limitInKB))  gCacheOpt.limitInKB         = iParam.limitInKB;
  if(iParam && !isNaN(iParam.vacuumIval)) gCacheOpt.vacuum.ival       = iParam.vacuumIval;
  if(iParam && iParam.eviction === true)  gCacheOpt.eviction.enabled  = true;

  // Calculate the entry limit.
  // Empty space should be guaranteed for each key. Otherwise will fail on updates.
  // This calculation might be important for eviction policies.
  // Also UTF-8 considered for the calculation. (4 bytes for each char.)
  gCacheOpt.limitInEntry = Math.floor((gCacheOpt.limitInKB*1024)/((gCacheOpt.keyLIC+gCacheOpt.valLIC)*4));

  // Calculate the eviction limit.
  gCacheOpt.eviction.limitInEntry = Math.floor((gCacheOpt.limitInEntry*2)/100); // 2%
  if(gCacheOpt.eviction.limitInEntry < 1) gCacheOpt.eviction.limitInEntry = 1;

  // Check and init the vacuum timer.
  if(gCacheOpt.vacuum.ival < 1) gCacheOpt.vacuum.ival = 30; // Do not allowed <= 0
  gTimers.vacuum = setInterval(function() {
    if(!gCacheOpt.vacuum.running) vacuum({all: true});
  }, gCacheOpt.vacuum.ival*1000);

  // Returns available space in entry.
  avlbEntry = function avlbEntry() {
    return (gCacheOpt.limitInEntry-gDataLen);
  };

  // Returns the stats.
  stats = function stats() {
    return {
      options: gCacheOpt,
      numberOfEntry: gDataLen,
      numberOfAvlbEntry: avlbEntry(),
      usageInPercent: Math.floor((gDataLen*100)/gCacheOpt.limitInEntry)
    };
  };

  // Dump the cached data.
  // This is not an export process. It is for small data sets.
  dump = function stats() {
    return gDataSet;
  };

  // Vacuum the data by the given options.
  vacuum = function vacuum(iParam) {
{'fraction_non_alphanumeric': 0.10939492702281423, 'fraction_numerical': 0.013036701147796514, 'mean_word_length': 2.092900964066608, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '6145792', 'n_tokens_mistral': 2223, 'n_tokens_neox': 2099, 'n_words': 620}