<commit_before>var redis = require('redis'),
    client = redis.createClient();

client.on('error', function (err) {
    console.log('Error ' + err);
});

// If no key specified, use filename for some degree of uniqueness
var key = process.env.DBKEY || __filename;

exports.get = function(id, fn) {
  client.get(key, function(err, reply) {
    if (err) return fn(err);
    console.log(reply);
    reply = JSON.parse(reply);
    if (!reply || !reply[id]) return fn(new Error('Not found'));
    fn(null, reply[id]);
  });
};

exports.post = function(docs, fn) {
  // uncomment to reset
  //client.del(key);
  client.get(key, function(err, reply) {
    if (err) return fn(err);
    reply = JSON.parse(reply) || {};
    merge(reply, docs);
    client.set(key, JSON.stringify(reply), function(err, setReply) {
      if (err) return fn(err);
      fn(null, setReply);
    });
  });
};

exports.near = function(lat, lon, limit, fn) {
  var _limit;
  if (fn) {
    _limit = limit || 20;
  } else {
    fn = limit;
    _limit = 20;
  }
  client.get(key, function(err, reply) {
    if (err) return fn(err);
    var db = JSON.parse(reply);
    var doc, distance, distances = [];
    for (var id in db) {
      doc = db[id];
      if (doc.location) {
        distance = getDistance(lat, doc.location.lat, lon, doc.location.lon)
        distances.push(distance)
        distances.sort()
        // TODO link distances to IDs
        if (distances.length > _limit) distances.pop();
      }
      //locObj[id].id = id;
      //locations.push(locObj[id]);
    }
    fn(null, distances);
  });
};

exports.loc = function(idOrSlug, fn) {
  exports.get(idOrSlug, function(err, data) {
    if (err) return exports.getBySlug(idOrSlug, fn);
    fn(null, data);
  });
};

exports.getBySlug = function(slug, fn) {
  //request({
  //  url: encodeURI('https://api.cloudmine.me/v1/app/'+process.env.CMID+'/search?q=[slug="'+slug+'"]'),
  //  headers: {'X-CloudMine-ApiKey': process.env.CMKEY}
  //}, function(err, res, body) {
  //  if (err) return fn(err);
  //  if (_.isEmpty(JSON.parse(body).success)) return fn(body);
  //  fn(null, body);
  //});
};

exports.settings = function(doc, fn) {
  // get settings if no doc param
  if (!fn) {
    fn = doc;
    return exports.get('settings', function(err, data){
      if (err) return fn(err);
      fn(null, data);
    });
  }
  exports.post({settings: doc}, fn);<commit_msg>Changed fn to cb in db<commit_after>var redis = require('redis'),
    client = redis.createClient();

client.on('error', function (err) {
    console.log('Error ' + err);
});

// If no key specified, use filename for some degree of uniqueness
var key = process.env.DBKEY || __filename;

exports.get = function(id, cb) {
  client.get(key, function(err, reply) {
    if (err) return cb(err);
    console.log(reply);
    reply = JSON.parse(reply);
    if (!reply || !reply[id]) return cb(new Error('Not found'));
    cb(null, reply[id]);
  });
};

exports.post = function(docs, cb) {
  // uncomment to reset
  //client.del(key);
  client.get(key, function(err, reply) {
    if (err) return cb(err);
    reply = JSON.parse(reply) || {};
    merge(reply, docs);
    client.set(key, JSON.stringify(reply), function(err, setReply) {
      if (err) return cb(err);
      cb(null, setReply);
    });
  });
};

exports.near = function(lat, lon, limit, cb) {
  var _limit;
  if (cb) {
    _limit = limit || 20;
  } else {
    cb = limit;
    _limit = 20;
  }
  client.get(key, function(err, reply) {
    if (err) return cb(err);
    var db = JSON.parse(reply);
    var doc, distance, distances = [];
    for (var id in db) {
      doc = db[id];
      if (doc.location) {
        distance = getDistance(lat, doc.location.lat, lon, doc.location.lon)
        distances.push(distance)
        distances.sort()
        // TODO link distances to IDs
        if (distances.length > _limit) distances.pop();
      }
      //locObj[id].id = id;
      //locations.push(locObj[id]);
    }
    cb(null, distances);
  });
};

exports.loc = function(idOrSlug, cb) {
  exports.get(idOrSlug, function(err, data) {
    if (err) return exports.getBySlug(idOrSlug, cb);
    cb(null, data);
  });
};

exports.getBySlug = function(slug, cb) {
  //request({
  //  url: encodeURI('https://api.cloudmine.me/v1/app/'+process.env.CMID+'/search?q=[slug="'+slug+'"]'),
  //  headers: {'X-CloudMine-ApiKey': process.env.CMKEY}
  //}, function(err, res, body) {
  //  if (err) return cb(err);
  //  if (_.isEmpty(JSON.parse(body).success)) return cb(body);
  //  cb(null, body);
  //});
};

exports.settings = function(doc, cb) {
  // get settings if no doc param
  if (!cb) {
    cb = doc;
    return exports.get('settings', function(err, data){
      if (err) return cb(err);
      cb(null, data);
    });
  }
  exports.post({settings: doc}, cb);
{'fraction_non_alphanumeric': 0.18397502601456817, 'fraction_numerical': 0.002081165452653486, 'mean_word_length': 2.8758064516129034, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '3039273', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1703, 'n_words': 398}