<commit_before>const levelup = require('levelup');
const encodingDown = require('encoding-down');
const leveldown = require('leveldown');

const crypto = require('crypto');
const path = require('path');

const Conference = require('./conference');

let main;
const confs = new Map();
let confList;

function startDB(dir, cb) {
  const levelBackend = encodingDown(
    leveldown(dir),
    { valueEncoding: 'json' },
  );

  return levelup(levelBackend, null, cb);
}

function init(cb) {
  main = startDB(path.resolve(__dirname, 'storage', 'main.db'), err => {
    if(err) return void cb(err);

    main.get('list', (err, list) => {
      if(err)
        if(err.notFound) {
          confList = [];
          return void main.put('list', [], cb);
        } else return void cb(err);
      else {
        confList = list;
        for(const conf of list) {
          const db = startDB(`${__dirname}/storage/${conf.id}.db`);
          const filedir = `${__dirname}/storage/${conf.id}.files`;
          confs.set(conf.id, new Conference(conf.name, db, filedir));
        }
        return void cb(null);
      }
    });
  });
}

function shutdown(cb) {
  main.close(err => {
    if(err && cb) return void cb(err);
    else return void Promise.all([...confs.values()].map(
      e => new Promise((resolve, reject) => e.db.close(err => err ? reject(err) : resolve())),
    ))
      .then(() => {
        if(cb) cb();
      })
      .catch(e => {
        if(cb) cb(e);<commit_msg>Fixed: incorrectly handled callback for shutdown hooks<commit_after>const levelup = require('levelup');
const encodingDown = require('encoding-down');
const leveldown = require('leveldown');

const crypto = require('crypto');
const path = require('path');

const Conference = require('./conference');

let main;
const confs = new Map();
let confList;

function startDB(dir, cb) {
  const levelBackend = encodingDown(
    leveldown(dir),
    { valueEncoding: 'json' },
  );

  return levelup(levelBackend, null, cb);
}

function init(cb) {
  main = startDB(path.resolve(__dirname, 'storage', 'main.db'), err => {
    if(err) return void cb(err);

    main.get('list', (err, list) => {
      if(err)
        if(err.notFound) {
          confList = [];
          return void main.put('list', [], cb);
        } else return void cb(err);
      else {
        confList = list;
        for(const conf of list) {
          const db = startDB(`${__dirname}/storage/${conf.id}.db`);
          const filedir = `${__dirname}/storage/${conf.id}.files`;
          confs.set(conf.id, new Conference(conf.name, db, filedir));
        }
        return void cb(null);
      }
    });
  });
}

function shutdown(cb) {
  main.close(err => {
    if(err) {
      if(cb) return void cb(err);
      return;
    }

    Promise.all([...confs.values()].map(
      e => new Promise((resolve, reject) => e.db.close(err => err ? reject(err) : resolve())),
    ))
      .then(() => {
        if(cb) cb();
      })
      .catch(e => {
        if(cb) cb(e);
{'fraction_non_alphanumeric': 0.17757009345794392, 'fraction_numerical': 0.0, 'mean_word_length': 2.668298653610771, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '6777817', 'n_tokens_mistral': 956, 'n_tokens_neox': 958, 'n_words': 225}