Object.defineProperty(exports, "__esModule", { value: true });
const levelup = require("levelup");
const leveldown = require("leveldown");
const OperationLog_1 = require("../helperFunctions/OperationLog");
const LogServer_1 = require("./LogServer");
const cacheLevelDBGet_1 = require("../cacheLevelDBGet");
class LevelDBLogServer extends LogServer_1.LogServer {
    constructor(dbPath, locStore) {
        super(locStore);
        this.levelDownDb = leveldown(dbPath);
        this.db = levelup(this.levelDownDb);
        this._getCached = cacheLevelDBGet_1.cacheLevelDBGet(this.db);
    }
    storeLog(log) {
        this.db.put(log.index.toString(), JSON.stringify(log), function (err) {
            if (err)
                return console.log("Ooops! (put)", err); // some kind of I/O error
        });
    }
    storeLogs(logs, callback = function () { }) {
        var ops = [];
        logs.forEach(log => {
            ops.push({
                type: "put",
                key: log.index.toString(),
                value: JSON.stringify(log)
            });
        });
        // levelDownDb._batch vs db.batch:
        // _batch bypasses some validation code which should help performance a bit
        this.levelDownDb._batch(ops, function (err) {
            if (err)
                return console.log("Ooops!  - level db error (logs)", err);
            callback();
        });
    }
    getLog(index, fn) {
        this._getCached(index.toString(), (err, value) => {
            if (err) {
                fn(err, null);
                return;
            }
            value = JSON.parse(value);
            const ret = new OperationLog_1.default(value);
            fn(null, ret);
        });
    }
}
exports.default = LevelDBLogServer;
//# sourceMappingURL=LevelDBLogServer.js.map