<commit_before>/* This module provides an interface into whatever mechanism we might
 * want to use to cache and retrieve bundles.
 */

//var request = require('superagent');

var cacheFile = 'cache.dat';

/* Cache objects provide a uniform interface to access and store data from and to
 * any of the kinds of media we may want to use to cache bundles.
 * A Cache object's read method accepts:
 *   1. A description of the source to read from (typically a URL)
 *   2. A callback for handling the read data
 * The write method accepts:
 *   1. A description of the data to write
 *   2. A callback for handling the result of writing data
 * In both cases, callbacks should be passed 
 *   1. An error object/description if one occurred
 *   2. Any result data
 * Note that the `from` and `data` arguments to `read` and `write` respectively
 * can be any kind of value- a string, object, etc. and it is up to the
 * implementations of the reader and writer supplied to the Cache object
 * to determine how to interpret them and provide data to the callbacks.
 */
function Cache(reader, writer) {
  this.read = function (from, callback) {
    reader(from, callback);
  };

  this.write = function (data, callback) {
    writer(data, callback); 
  };
}

/* Each retriever and storer function provide closures over any configuration
 * data they may need to retain. This allows us to simply export from this
 * module configuration functions that close over whatever configuration data
 * may be required.
 */

/* We may have the local retriever and storer functions be configured with
 * a directory and file to read and write cached data to and from.
 */
function localRetriever() {
  return function (from, callback) {
    console.log('Reading data from ' + from);
    callback(null, 'Hello world');
  };
}

function localStorer() {
  return function (data, callback) {
    console.log('Writing data');
    console.log(data);
    callback(null, 'Goodbye!');
  };
}

function freenetRetriever(fnAddr) {
  return function (url, callback) {
    console.log('Requesting URL ' + url + ' from freenet at ' + fnAddr);
    callback(null, 'Woah');
  };
}

function freenetStorer(fnAddr) {
  return function (data, callback) {
    console.log('Writing to freenet at ' + fnAddr);
    console.log(data);
    callback(null, 'Dude');
  };
}

module.exports = {
  local: function () {
    return new Cache(localRetriever(), localStorer());
  },

  freenet: function (fnAddr) {
    return new Cache(freenetRetriever(fnAddr), freenetStorer(fnAddr));
  }
  // etc.
};
<commit_msg>Create the Cache object by extending prototype<commit_after>/* This module provides an interface into whatever mechanism we might
 * want to use to cache and retrieve bundles.
 */

//var request = require('superagent');
var _ = require('lodash');

var cacheFile = 'cache.dat';

/* Cache objects provide a uniform interface to access and store data from and to
 * any of the kinds of media we may want to use to cache bundles.
 * A Cache object's read method accepts:
 *   1. A description of the source to read from (typically a URL)
 *   2. A callback for handling the read data
 * The write method accepts:
 *   1. A description of the data to write
 *   2. A callback for handling the result of writing data
 * In both cases, callbacks should be passed 
 *   1. An error object/description if one occurred
 *   2. Any result data
 * Note that the `from` and `data` arguments to `read` and `write` respectively
 * can be any kind of value- a string, object, etc. and it is up to the
 * implementations of the reader and writer supplied to the Cache object
 * to determine how to interpret them and provide data to the callbacks.
 */
var Cache = function () {
  this.initialize.apply(this, arguments);
};

_.extend(Cache.prototype, {
  initialize: function (reader, writer) {
    this.read = function (from, callback) {
      reader(from, callback);
    };
    
    this.write = function (data, callback) {
      writer(data, callback);
    };
  }
});

/* Each retriever and storer function provide closures over any configuration
 * data they may need to retain. This allows us to simply export from this
 * module configuration functions that close over whatever configuration data
 * may be required.
 */

/* We may have the local retriever and storer functions be configured with
 * a directory and file to read and write cached data to and from.
 */
function localRetriever() {
  return function (from, callback) {
    console.log('Reading data from ' + from);
    callback(null, 'Hello world');
  };
}

function localStorer() {
  return function (data, callback) {
    console.log('Writing data');
    console.log(data);
    callback(null, 'Goodbye!');
  };
}

function freenetRetriever(fnAddr) {
  return function (url, callback) {
    console.log('Requesting URL ' + url + ' from freenet at ' + fnAddr);
    callback(null, 'Woah');
  };
}

function freenetStorer(fnAddr) {
  return function (data, callback) {
    console.log('Writing to freenet at ' + fnAddr);
    console.log(data);
    callback(null, 'Dude');
  };
}

module.exports = {
  local: function () {
    return new Cache(localRetriever(), localStorer());
  },

  freenet: function (fnAddr) {
    return new Cache(freenetRetriever(fnAddr), freenetStorer(fnAddr));
  }
  // etc.
};

{'fraction_non_alphanumeric': 0.09917199849454272, 'fraction_numerical': 0.002258185923974407, 'mean_word_length': 3.5349829351535837, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1512351', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1491, 'n_words': 643}