<commit_before>var fs = require('fs');

var Source = require('./source');
var utils = require('./utils');


/**
 * @param {string} file
 * @constructor
 */
var Cache = module.exports = function(file) {
  /** @type {string} */
  this.file = file;
  /** @private {Object.<Source>} */
  this._sourcesMap = {};
  /** @private {Object.<string>} */
  this._modifiedDates = {};

  try {
    if (fs.existsSync(file)) {
      var content = fs.readFileSync(file, 'utf8');
      var json = JSON.parse(content);

      if ('object' == typeof(json)) {
        for (var jsFile in json) {
          var jsonSource = json[jsFile];

          if (
            jsonSource.provides &&
            jsonSource.requires &&
            jsonSource.modifiedDates
          ) {
            this._sourcesMap[jsFile] = new Source(jsFile, jsonSource.provides,
              jsonSource.requires, jsonSource.syntaxTree || {});
            this._modifiedDates[jsFile] = jsonSource.modifiedDates;
          }
        }
      }
    }
  } catch (e) {
    console.error(file);

    throw e;
  }
};


/**
 * @param {string} path
 * @return {Source}
 */
Cache.prototype.getSource = function(path) {
  if (this._sourcesMap[path]) {
    if (this._modifiedDates[path] == (+fs.statSync(path).mtime)) {
      return this._sourcesMap[path];
    } else {
      this.removeSource(path);
    }
  }

  return null;
};

/**
 * @param {string} path
 */
Cache.prototype.removeSource = function(path) {
  if (this._sourcesMap[path]) {
    delete this._sourcesMap[path];
  }
};

/**
 * @param {string} path
 * @param {Source} source
 */
Cache.prototype.setSource = function(path, source) {
  this._sourcesMap[path] = source;
};

/**
 * @param {function(Error)=} opt_callback
 */
Cache.prototype.save = function(opt_callback) {
  var json = {};

  for (var jsFile in this._sourcesMap) {
    json[jsFile] = {
      modifiedDates: +fs.statSync(jsFile).mtime,
      provides: this._sourcesMap[jsFile].provides,
      requires: this._sourcesMap[jsFile].requires
    };
  }
<commit_msg>Small fix in cache class.<commit_after>var fs = require('fs');

var Source = require('./source');
var utils = require('./utils');


/**
 * @param {string} file
 * @constructor
 */
var Cache = module.exports = function(file) {
  /** @type {string} */
  this.file = file;
  /** @private {Object.<Source>} */
  this._sourcesMap = {};
  /** @private {Object.<string>} */
  this._modifiedDates = {};

  try {
    if (fs.existsSync(file)) {
      var content = fs.readFileSync(file, 'utf8');
      var json = JSON.parse(content);

      if ('object' == typeof(json)) {
        for (var jsFile in json) {
          var jsonSource = json[jsFile];

          if (
            jsonSource.provides &&
            jsonSource.requires &&
            jsonSource.modifiedDates
          ) {
            this._sourcesMap[jsFile] = new Source(jsFile, jsonSource.provides,
              jsonSource.requires, jsonSource.syntaxTree || {});
            this._modifiedDates[jsFile] = jsonSource.modifiedDates;
          }
        }
      }
    }
  } catch (e) {
    console.error(file);

    throw e;
  }
};


/**
 * @param {string} path
 * @return {Source}
 */
Cache.prototype.getSource = function(path) {
  if (this._sourcesMap[path]) {
    if (this._modifiedDates[path] == (+fs.statSync(path).mtime)) {
      return this._sourcesMap[path];
    } else {
      this.removeSource(path);
    }
  }

  return null;
};

/**
 * @param {string} path
 */
Cache.prototype.removeSource = function(path) {
  if (this._sourcesMap[path]) {
    delete this._sourcesMap[path];
  }
};

/**
 * @param {string} path
 * @param {Source} source
 */
Cache.prototype.setSource = function(path, source) {
  this._sourcesMap[path] = source;
};

/**
 * @param {function(Error)=} opt_callback
 */
Cache.prototype.save = function(opt_callback) {
  var json = {};

  for (var jsFile in this._sourcesMap) {
    if (fs.existsSync(jsFile)) {
      json[jsFile] = {
        modifiedDates: +fs.statSync(jsFile).mtime,
        provides: this._sourcesMap[jsFile].provides,
        requires: this._sourcesMap[jsFile].requires
      };
    }
  }

{'fraction_non_alphanumeric': 0.16281895504252733, 'fraction_numerical': 0.0004860267314702309, 'mean_word_length': 2.5822454308093996, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1488118', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1320, 'n_words': 250}