<commit_before>var levelup = require('levelup')
var join    = require('path').join
var LevelScuttlebutt 
            = require('level-scuttlebutt')
var udid    = require('udid')('rumours')
var shasum  = require('shasum')

var schema  = require('./schema')

var dbs = {}

function whenReady (db, cb) {
  if(db.isOpen()) cb(null, db)
  function onReady () {
    db.removeListener('error', onError)
     cb(null, db)
  }
  function onError (err) {
    db.removeListener('ready', onReady)
    cb(err)
  }
  db.once('ready', onReady)
  db.once('error', onError)
  return db
}

module.exports = function (config) {
  config = config || {
    root: '/tmp/rumours-default'
  }

  config.schema = config.schema || require('./schema')

  //just a simple count of all items.
  var views  = config.views = config.views || [
    {
      name: 'all',
      map: function (key, model, emit) {
        if(model.name) {
          emit(model.name.split('!'), 1)
        }
      },
      reduce: function (acc, item) {
        return Number(acc) + Number(item)
      },
      initial: 0
    }
  ]

  return function (name, cb) {

    if(dbs[name]) {
      return whenReady(dbs[name], cb)
    }

    var dir = join(config.root, name)
    var db = levelup(dir, {createIfMissing: true})

    //don't give each database the same hash,
    //that will reveal that they are on the same server to attackers.<commit_msg>ensure dir exists<commit_after>var levelup = require('levelup')
var join    = require('path').join
var LevelScuttlebutt 
            = require('level-scuttlebutt')
var udid    = require('udid')('rumours')
var shasum  = require('shasum')
var mkdirp  = require('mkdirp')

var schema  = require('./schema')

var dbs = {}

function whenReady (db, cb) {
  if(db.isOpen()) cb(null, db)
  function onReady () {
    db.removeListener('error', onError)
     cb(null, db)
  }
  function onError (err) {
    db.removeListener('ready', onReady)
    cb(err)
  }
  db.once('ready', onReady)
  db.once('error', onError)
  return db
}

module.exports = function (config) {
  config = config || {
    root: '/tmp/rumours-default'
  }

  config.schema = config.schema || require('./schema')

  mkdirp.sync(config.root)

  //just a simple count of all items.
  var views  = config.views = config.views || [
    {
      name: 'all',
      map: function (key, model, emit) {
        if(model.name) {
          emit(model.name.split('!'), 1)
        }
      },
      reduce: function (acc, item) {
        return Number(acc) + Number(item)
      },
      initial: 0
    }
  ]

  return function (name, cb) {

    if(dbs[name]) {
      return whenReady(dbs[name], cb)
    }

    var dir = join(config.root, name)
    var db = levelup(dir, {createIfMissing: true})

    //don't give each database the same hash,
    //that will reveal that they are on the same server to attackers.
{'fraction_non_alphanumeric': 0.14912280701754385, 'fraction_numerical': 0.0014035087719298245, 'mean_word_length': 2.6457800511508953, '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': '3116740', 'n_tokens_mistral': 976, 'n_tokens_neox': 946, 'n_words': 247}