<commit_before>﻿"use strict";

const _ = require("lodash");
const fs = require("fs-extra");
const path = require("path");
const async = require("async");
const config = require("config");
const cp = require("child_process");
const mongoose = require("mongoose");

let _connected = false;

/**
 * 启动数据库 Server。
 * @param {Function(err)} [p_callback]
 */
module.exports.start = function (p_callback)
{
    const dbpath = path.resolve(__dirname, "../db");
    _prepareMongoDB(dbpath, (err1) =>
    {
        if (err1)
        {
            p_callback(err1);
        }
        else
        {
            const timer = setTimeout(p_callback, 2000);
            cp.exec(`mongod --dbpath "${dbpath}"`, (err2) =>
            {
                if (err2)
                {
                    clearTimeout(timer);
                    p_callback(err2);
                }
            });
        }
    });
};

/**
 * 连接数据库。
 * @param {Function(err)} [p_callback]
 */
module.exports.connect = function (p_callback)
{
    // TODO: check if correct.
    console.log(`Connecting database: ${process.env.MONGODB_CONNECTION || config.db}`);
    if (process.env.MONGODB_CONNECTION)
    {
        const db = mongoose.createConnection();
        db.openSet(
            `mongodb://${process.env.MONGODB_CONNECTION}`,
            {
                server: {
                    auto_reconnect: config.auto_reconnect,
                    poolSize: config.poolSize
                }
            },
            (err) =>
            {
                _connected = !err;
                _monitor();
                if (_.isFunction(p_callback))
                {
                    p_callback(err);
                }
            }
        );
    }
    else
    {
        mongoose.connect(
            config.db,
            {
                server: {
                    auto_reconnect: config.auto_reconnect,
                    poolSize: config.poolSize
                }
            },
            (err) =>
            {
                _connected = !err;
                _monitor();
                if (_.isFunction(p_callback))
                {
                    p_callback(err);
                }
            }
        );
    }
};

/**
 * 检查数据库是否已连接。
 */
module.exports.connected = function ()
{
    return _connected;
};

/**
 * 删除所有集合。
 * @param {Function(err)} [p_callback]
 */
module.exports.dropAllCollections = function (p_callback)
{
    async.each(
        mongoose.connection.collections,
        (collection, done) =>
        {
            collection.drop(done);
        },
        (err) =>
        {
            if (_.isFunction(p_callback))
            {
                if (!err || err.message === "ns not found")
                {
                    p_callback();
                }
                else
                {
                    p_callback(err);<commit_msg>use arrow function<commit_after>﻿"use strict";

const _ = require("lodash");
const fs = require("fs-extra");
const path = require("path");
const async = require("async");
const config = require("config");
const cp = require("child_process");
const mongoose = require("mongoose");

let _connected = false;

/**
 * 启动数据库 Server。
 * @param {Function(err)} [p_callback]
 */
module.exports.start = (p_callback) =>
{
    const dbpath = path.resolve(__dirname, "../db");
    _prepareMongoDB(dbpath, (err1) =>
    {
        if (err1)
        {
            p_callback(err1);
        }
        else
        {
            const timer = setTimeout(p_callback, 2000);
            cp.exec(`mongod --dbpath "${dbpath}"`, (err2) =>
            {
                if (err2)
                {
                    clearTimeout(timer);
                    p_callback(err2);
                }
            });
        }
    });
};

/**
 * 连接数据库。
 * @param {Function(err)} [p_callback]
 */
module.exports.connect = (p_callback) =>
{
    // TODO: check if correct.
    if (process.env.MONGODB_CONNECTION)
    {
        const db = mongoose.createConnection();
        db.openSet(
            `mongodb://${process.env.MONGODB_CONNECTION}`,
            {
                server: {
                    auto_reconnect: config.auto_reconnect,
                    poolSize: config.poolSize
                }
            },
            (err) =>
            {
                _connected = !err;
                _monitor();
                if (_.isFunction(p_callback))
                {
                    p_callback(err);
                }
            }
        );
    }
    else
    {
        mongoose.connect(
            config.db,
            {
                server: {
                    auto_reconnect: config.auto_reconnect,
                    poolSize: config.poolSize
                }
            },
            (err) =>
            {
                _connected = !err;
                _monitor();
                if (_.isFunction(p_callback))
                {
                    p_callback(err);
                }
            }
        );
    }
};

/**
 * 检查数据库是否已连接。
 */
module.exports.connected = () =>
{
    return _connected;
};

/**
 * 删除所有集合。
 * @param {Function(err)} [p_callback]
 */
module.exports.dropAllCollections = (p_callback) =>
{
    async.each(
        mongoose.connection.collections,
        (collection, done) =>
        {
            collection.drop(done);
        },
        (err) =>
        {
            if (_.isFunction(p_callback))
            {
                if (!err || err.message === "ns not found")
                {
                    p_callback();
                }
                else
                {
                    p_callback(err);
{'fraction_non_alphanumeric': 0.12915260259371114, 'fraction_numerical': 0.003553028957186001, 'mean_word_length': 1.2147915027537373, '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': '6250326', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1601, 'n_words': 259}