<commit_before>var _ = require("lodash");
var cuid = require("cuid");
var crypto = require("crypto");
var levelup = require("levelup");
var bytewise = require("bytewise");
var safeJsonCodec = require("level-json-coerce-null");
var extractRulesetID = require("./extractRulesetID");

var dbToObj = function(ldb, callback){
  var db_data = {};
  ldb.createReadStream()
    .on("data", function(data){
      if(!_.isArray(data.key)){
        return;
      }
      _.set(db_data, data.key, data.value);
    })
    .on("end", function(){
      callback(undefined, db_data);
    });
};

module.exports = function(opts){

  var ldb = levelup(opts.location, {
    db: opts.db,
    keyEncoding: bytewise,
    valueEncoding: safeJsonCodec
  });

  var newID = _.isFunction(opts.newID) ? opts.newID : cuid;

  return {
    toObj: function(callback){
      dbToObj(ldb, callback);
    },
    getPicoIDByECI: function(eci, callback){
      ldb.get(["channel", eci, "pico_id"], callback);
    },
    getPico: function(id, callback){
      var pico = {};
      ldb.createReadStream({
        gte: ["pico", id],
        lte: ["pico", id, undefined]//bytewise sorts with null at the bottom and undefined at the top
      })
        .on("data", function(data){
          _.set(pico, data.key, data.value);
        })
        .on("end", function(){
          callback(undefined, pico.pico[id]);
        });
    },
    newPico: function(opts, callback){
      var new_pico = {
        id: newID()
      };
      ldb.put(["pico", new_pico.id], new_pico, function(err){
        if(err) return callback(err);<commit_msg>add new function to get owner channel<commit_after>var _ = require("lodash");
var cuid = require("cuid");
var crypto = require("crypto");
var levelup = require("levelup");
var bytewise = require("bytewise");
var safeJsonCodec = require("level-json-coerce-null");
var extractRulesetID = require("./extractRulesetID");

var dbToObj = function(ldb, callback){
  var db_data = {};
  ldb.createReadStream()
    .on("data", function(data){
      if(!_.isArray(data.key)){
        return;
      }
      _.set(db_data, data.key, data.value);
    })
    .on("end", function(){
      callback(undefined, db_data);
    });
};

module.exports = function(opts){

  var ldb = levelup(opts.location, {
    db: opts.db,
    keyEncoding: bytewise,
    valueEncoding: safeJsonCodec
  });

  var newID = _.isFunction(opts.newID) ? opts.newID : cuid;

  return {
    toObj: function(callback){
      dbToObj(ldb, callback);
    },
    getPicoIDByECI: function(eci, callback){
      ldb.get(["channel", eci, "pico_id"], callback);
    },
    getFirstChannel: function(callback){
      var channel = {};
      ldb.createReadStream({
        gte: ["channel", ""],
        limit: 1
      })
        .on("data", function(data){
          _.set(channel, data.key, data.value);
        })
        .on("end", function(){
          callback(undefined, channel);
        });
    },
    getPico: function(id, callback){
      var pico = {};
      ldb.createReadStream({
        gte: ["pico", id],
        lte: ["pico", id, undefined]//bytewise sorts with null at the bottom and undefined at the top
      })
        .on("data", function(data){
          _.set(pico, data.key, data.value);
        })
        .on("end", function(){
          callback(undefined, pico.pico[id]);
        });
    },
    newPico: function(opts, callback){
      var new_pico = {
        id: newID()
      };
      ldb.put(["pico", new_pico.id], new_pico, function(err){
        if(err) return callback(err);
{'fraction_non_alphanumeric': 0.1650239369191777, 'fraction_numerical': 0.000281610813855252, 'mean_word_length': 2.7, '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': '2386511', 'n_tokens_mistral': 1173, 'n_tokens_neox': 1167, 'n_words': 249}