lru. buffer reader zeroCopy. utils.wrap.

This commit is contained in:
Christopher Jeffrey 2016-03-18 20:12:17 -07:00
parent ca4fe76fae
commit 4f805ce52a
4 changed files with 31 additions and 12 deletions

View File

@ -376,14 +376,6 @@ Iterator.prototype.end = function end(callback) {
return ret;
};
utils.wrap = function wrap(callback, unlock) {
return function(err, result) {
unlock();
if (callback)
callback(err, result);
};
};
DataStore.prototype.putData = function putData(data, callback, force) {
var self = this;
var offset;

View File

@ -247,6 +247,16 @@ LRU.prototype.keys = function keys() {
return keys;
};
LRU.prototype.items = function items() {
var items = [];
var item;
for (item = this.head; item; item = item.next)
items.push(item);
return items;
};
LRU.prototype.iterator = function iterator() {
return {
item: { next: this.head },

View File

@ -11,11 +11,12 @@ var assert = utils.assert;
* BufferReader
*/
function BufferReader(data, offset) {
function BufferReader(data, zeroCopy) {
if (data instanceof BufferReader)
return data;
this.data = data;
this.offset = offset || 0;
this.offset = 0;
this.zeroCopy = zeroCopy;
this.stack = [];
}
@ -157,10 +158,18 @@ BufferReader.prototype.read64BE = function read64BE() {
};
BufferReader.prototype.readBytes = function readBytes(size) {
var ret;
assert(size >= 0);
assert(this.offset + size <= this.data.length);
var ret = utils.slice(this.data, this.offset, this.offset + size);
if (this.zeroCopy)
ret = this.data.slice(this.offset, this.offset + size);
else
ret = utils.slice(this.data, this.offset, this.offset + size);
this.offset += size;
return ret;
};
@ -174,7 +183,7 @@ BufferReader.prototype.readString = function readString(enc, size) {
BufferReader.prototype.readHash = function readHash(enc) {
if (enc)
return this.readBytes(32).toString(enc);
return this.readString(enc, 32);
return this.readBytes(32);
};

View File

@ -1745,6 +1745,14 @@ utils.pad32 = function pad32(num) {
return num;
};
utils.wrap = function wrap(callback, unlock) {
return function(err, result) {
unlock();
if (callback)
callback(err, result);
};
};
function SyncBatch(db) {
this.db = db;
this.ops = [];