<commit_before>var Level = require("level");
var nThen = require('nthen');

var getIndex = function(db, cName, cb) {
    db.get(cName+'=>index', function(e, out){
        if (e && !e.notFound) { throw e; }
        cb(parseInt(out || 0));
    });
};

var insert = function (db, channelName, content, cb) {
    var index;
    nThen(function (waitFor) {
        getIndex(db, channelName, waitFor(function (i) { index = i; }));
    }).nThen(function (waitFor) {
        db.put(channelName+'=>index', ''+index, waitFor(function (e) { if (e) { throw e; } }));
    }).nThen(function (waitFor) {
        db.put(channelName+'=>'+index, content, waitFor(function (e) { if (e) { throw e; } }));
    }).nThen(cb);
};

var getMessages = function (db, channelName, msgHandler) {
    var index;
    nThen(function (waitFor) {
        getIndex(db, channelName, waitFor(function (i) { index = i; }));
    }).nThen(function (waitFor) {
        var again = function (i) {
            db.get(channelName + '=>' + i, waitFor(function (e, out) {
                if (e) { throw e; }
                msgHandler(out);
                if (i < index) { again(i+1); }
            }));
        };
        again(0);
    });
};

module.exports.create = function (conf, cb) {
    var db = Level(conf.levelPath || './test.level.db');
    cb({
        message: function (channelName, content, cb) {
            insert(db, channelName, content, cb);
        },
        getMessages: function (channelName, msgHandler) {
            getMessages(db, channelName, msgHandler);
        }
    });
};
<commit_msg>I'm an idiot - killer off-by-one ate the storage<commit_after>var Level = require("level");
var nThen = require('nthen');

var getIndex = function(db, cName, cb) {
    db.get(cName+'=>index', function(e, out){
        if (e) {
            if (e.notFound) {
                cb(-1);
            } else {
                throw e;
            }
            return;
        }
        cb(parseInt(out));
    });
};

var insert = function (db, channelName, content, cb) {
    var index;
    nThen(function (waitFor) {
        getIndex(db, channelName, waitFor(function (i) { index = i+1; }));
    }).nThen(function (waitFor) {
        db.put(channelName+'=>'+index, content, waitFor(function (e) { if (e) { throw e; } }));
    }).nThen(function (waitFor) {
        db.put(channelName+'=>index', ''+index, waitFor(function (e) { if (e) { throw e; } }));
        console.log(index);
    }).nThen(cb);
};

var getMessages = function (db, channelName, msgHandler) {
    var index;
    nThen(function (waitFor) {
        getIndex(db, channelName, waitFor(function (i) { index = i; }));
    }).nThen(function (waitFor) {
        var again = function (i) {
            db.get(channelName + '=>' + i, waitFor(function (e, out) {
                if (e) { throw e; }
                msgHandler(out);
                if (i < index) { again(i+1); }
            }));
        };
        again(0);
    });
};

module.exports.create = function (conf, cb) {
    var db = Level(conf.levelPath || './test.level.db');
    cb({
        message: function (channelName, content, cb) {
            insert(db, channelName, content, cb);
        },
        getMessages: function (channelName, msgHandler) {
            getMessages(db, channelName, msgHandler);
        }
    });
};

{'fraction_non_alphanumeric': 0.17379518072289157, 'fraction_numerical': 0.002108433734939759, 'mean_word_length': 2.230544747081712, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '6836636', 'n_tokens_mistral': 1070, 'n_tokens_neox': 1071, 'n_words': 253}