<commit_before>

// ----------------------------------------------------------------
// class RttEstimator
var RttEstimator = function RttEstimator() {
  this.n_samples = 0;
  this.rtt = 1000.0;
  this.variance = 0;
  this.multiplier = 1;
  this.gain = 0.1;

  this.min_rto = 10.0;
  this.max_multiplier = 16;
};

// public method measurement
RttEstimator.prototype.measurement = function RttEstimator_measurement(m) {
  if (this.n_samples) {
    var err = m - this.rtt;
    this.rtt += err * this.gain;
    this.variance += (Math.abs(err) - this.variance) * this.gain;
  } else {
    this.rtt = m;
    this.variance = m/2;
  }
  ++this.n_samples;
};

// public method increase_multiplier
RttEstimator.prototype.increase_multiplier = function RttEstimator_increase_multiplier() {
  this.multiplier = Math.min(this.max_multiplier, this.multiplier*2);
};

// public method reset_multiplier
RttEstimator.prototype.reset_multiplier = function RttEstimator_reset_multiplier() {
  this.multiplier = 1;
}

// public method rto
RttEstimator.prototype.rto = function RttEstimator_rto() {
  return Math.max(this.min_rto, (this.rtt + 4*this.variance) * this.multiplier);
};


// ----------------------------------------------------------------
// class NamePrefixTable
// NamePrefixTable is solely used for strategy decision support.
// It does not act as FIB or PIT.
var NamePrefixTable = function NamePrefixTable() {
  this.T = {};
};

// private method entry_seek
NamePrefixTable.prototype.entry_seek = function NamePrefixTable_entry_seek(name, expires) {
  var name_key = name.to_uri();
  var entry = this.T[name_key];
  if (expires === false) return entry;
  if (!entry) this.T[name_key] = entry = { _expiry:0, _timer:0 };

  var now = Date.now(), expiry = now + expires;
  entry._expiry = Math.max(entry._expiry, expiry);
  clearTimeout(entry._timer);
  entry._timer = setTimeout(this.entry_expiry.bind(this, name_key), entry._expiry - now);
  
  return entry;
};

// private method entry_expiry
NamePrefixTable.prototype.entry_expiry = function NamePrefixTable_entry_expiry(name_key) {
  var now = Date.now;
  var entry = this.T[name_key];
  if (now >= entry._expiry) {
    delete this.T[name_key];
  } else {
    setTimeout(this.entry_expiry.bind(this, name_key), entry._expiry - now);
  }
};

// public method set
NamePrefixTable.prototype.set = function NamePrefixTable_add(name, key, value, expires) {
  if (!expires) expires = 2000;
  var entry = this.entry_seek(name, expires);
  entry[key] = value;
};

// public method renew_or_set
// If key exists, renew; otherwise, set to new_value or create_new_value().<commit_msg>fix NamePrefixTable memory leak<commit_after>

// ----------------------------------------------------------------
// class RttEstimator
var RttEstimator = function RttEstimator() {
  this.n_samples = 0;
  this.rtt = 1000.0;
  this.variance = 0;
  this.multiplier = 1;
  this.gain = 0.1;

  this.min_rto = 10.0;
  this.max_multiplier = 16;
};

// public method measurement
RttEstimator.prototype.measurement = function RttEstimator_measurement(m) {
  if (this.n_samples) {
    var err = m - this.rtt;
    this.rtt += err * this.gain;
    this.variance += (Math.abs(err) - this.variance) * this.gain;
  } else {
    this.rtt = m;
    this.variance = m/2;
  }
  ++this.n_samples;
};

// public method increase_multiplier
RttEstimator.prototype.increase_multiplier = function RttEstimator_increase_multiplier() {
  this.multiplier = Math.min(this.max_multiplier, this.multiplier*2);
};

// public method reset_multiplier
RttEstimator.prototype.reset_multiplier = function RttEstimator_reset_multiplier() {
  this.multiplier = 1;
}

// public method rto
RttEstimator.prototype.rto = function RttEstimator_rto() {
  return Math.max(this.min_rto, (this.rtt + 4*this.variance) * this.multiplier);
};


// ----------------------------------------------------------------
// class NamePrefixTable
// NamePrefixTable is solely used for strategy decision support.
// It does not act as FIB or PIT.
var NamePrefixTable = function NamePrefixTable() {
  this.T = {};
};

// private method entry_seek
NamePrefixTable.prototype.entry_seek = function NamePrefixTable_entry_seek(name, expires) {
  var name_key = name.to_uri();
  var entry = this.T[name_key];
  if (expires === false) return entry;
  if (!entry) this.T[name_key] = entry = { _expiry:0, _timer:0 };

  var now = Date.now(), expiry = now + expires;
  entry._expiry = Math.max(entry._expiry, expiry);
  clearTimeout(entry._timer);
  entry._timer = setTimeout(this.entry_expiry.bind(this, name_key), entry._expiry - now);
  
  return entry;
};

// private method entry_expiry
NamePrefixTable.prototype.entry_expiry = function NamePrefixTable_entry_expiry(name_key) {
  var now = Date.now();
  var entry = this.T[name_key];
  if (now >= entry._expiry) {
    delete this.T[name_key];
  } else {
    setTimeout(this.entry_expiry.bind(this, name_key), entry._expiry - now);
  }
};

// public method set
NamePrefixTable.prototype.set = function NamePrefixTable_add(name, key, value, expires) {
  if (!expires) expires = 2000;
  var entry = this.entry_seek(name, expires);
  entry[key] = value;
};

// public method renew_or_set
// If key exists, renew; otherwise, set to new_value or create_new_value().
{'fraction_non_alphanumeric': 0.17052791492594, 'fraction_numerical': 0.0094948727687049, 'mean_word_length': 3.2001594896331738, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6228433', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1765, 'n_words': 386}