You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
1.7 KiB
83 lines
1.7 KiB
/* Copyright (c) 2012-2016 LevelUP contributors |
|
* See list at <https://github.com/level/levelup#contributing> |
|
* MIT License |
|
* <https://github.com/level/levelup/blob/master/LICENSE.md> |
|
*/ |
|
|
|
var util = require('./util') |
|
var WriteError = require('level-errors').WriteError |
|
var getOptions = util.getOptions |
|
var dispatchError = util.dispatchError |
|
|
|
function Batch (levelup, codec) { |
|
this._levelup = levelup |
|
this._codec = codec |
|
this.batch = levelup.db.batch() |
|
this.ops = [] |
|
this.length = 0 |
|
} |
|
|
|
Batch.prototype.put = function (key_, value_, options) { |
|
options = getOptions(options) |
|
|
|
var key = this._codec.encodeKey(key_, options) |
|
var value = this._codec.encodeValue(value_, options) |
|
|
|
try { |
|
this.batch.put(key, value) |
|
} catch (e) { |
|
throw new WriteError(e) |
|
} |
|
|
|
this.ops.push({ type: 'put', key: key, value: value }) |
|
this.length++ |
|
|
|
return this |
|
} |
|
|
|
Batch.prototype.del = function (key_, options) { |
|
options = getOptions(options) |
|
|
|
var key = this._codec.encodeKey(key_, options) |
|
|
|
try { |
|
this.batch.del(key) |
|
} catch (err) { |
|
throw new WriteError(err) |
|
} |
|
|
|
this.ops.push({ type: 'del', key: key }) |
|
this.length++ |
|
|
|
return this |
|
} |
|
|
|
Batch.prototype.clear = function () { |
|
try { |
|
this.batch.clear() |
|
} catch (err) { |
|
throw new WriteError(err) |
|
} |
|
|
|
this.ops = [] |
|
this.length = 0 |
|
|
|
return this |
|
} |
|
|
|
Batch.prototype.write = function (callback) { |
|
var levelup = this._levelup |
|
var ops = this.ops |
|
|
|
try { |
|
this.batch.write(function (err) { |
|
if (err) { return dispatchError(levelup, new WriteError(err), callback) } |
|
levelup.emit('batch', ops) |
|
if (callback) { callback() } |
|
}) |
|
} catch (err) { |
|
throw new WriteError(err) |
|
} |
|
} |
|
|
|
module.exports = Batch
|
|
|