mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
starting working on faster buffer writer
This commit is contained in:
parent
66add6defc
commit
e93cb644e8
@ -1,57 +1,121 @@
|
||||
require(__dirname + "/test-helper");
|
||||
|
||||
|
||||
BufferList.prototype.compare = function(expected) {
|
||||
var buf = this.join();
|
||||
assert.equalBuffers(buf, expected);
|
||||
var ElasticBuffer = function(size) {
|
||||
this.size = size || 1024;
|
||||
this.buffer = new Buffer(this.size);
|
||||
this.offset = 0;
|
||||
};
|
||||
|
||||
test('adds int16', function() {
|
||||
new BufferList().addInt16(5).compare([0, 5]);
|
||||
});
|
||||
var p = ElasticBuffer.prototype;
|
||||
|
||||
test('adds two int16s', function() {
|
||||
new BufferList().addInt16(5).addInt16(3).compare([0,5,0,3]);
|
||||
});
|
||||
p._remaining = function() {
|
||||
return this.buffer.length - this.offset;
|
||||
}
|
||||
|
||||
test('adds int32', function() {
|
||||
new BufferList().addInt32(1).compare([0,0,0,1]);
|
||||
new BufferList().addInt32(1).addInt32(3).compare([0,0,0,1,0,0,0,3]);
|
||||
});
|
||||
p._resize = function() {
|
||||
var oldBuffer = this.buffer;
|
||||
this.buffer = Buffer(oldBuffer.length + this.size);
|
||||
oldBuffer.copy(this.buffer);
|
||||
}
|
||||
|
||||
test('adds CStrings', function() {
|
||||
new BufferList().addCString('').compare([0]);
|
||||
new BufferList().addCString('!!').compare([33,33,0]);
|
||||
new BufferList().addCString('!').addCString('!').compare([33,0,33,0]);
|
||||
});
|
||||
//resizes internal buffer if not enough size left
|
||||
p._ensure = function(size) {
|
||||
if(this._remaining() < size) {
|
||||
this._resize()
|
||||
}
|
||||
}
|
||||
|
||||
test('computes length', function() {
|
||||
var buf = new BufferList().join(true);
|
||||
assert.equalBuffers(buf, [0,0,0,4]);
|
||||
});
|
||||
p.addInt32 = function(num) {
|
||||
this._ensure(4)
|
||||
this.buffer[this.offset++] = (num >>> 24 & 0xFF)
|
||||
this.buffer[this.offset++] = (num >>> 16 & 0xFF)
|
||||
this.buffer[this.offset++] = (num >>> 8 & 0xFF)
|
||||
this.buffer[this.offset++] = (num >>> 0 & 0xFF)
|
||||
return this;
|
||||
}
|
||||
|
||||
test('appends character', function() {
|
||||
var buf = new BufferList().join(false,'!');
|
||||
assert.equalBuffers(buf,[33]);
|
||||
});
|
||||
p.addInt16 = function(num) {
|
||||
this._ensure(2)
|
||||
this.buffer[this.offset++] = (num >>> 8 & 0xFF)
|
||||
this.buffer[this.offset++] = (num >>> 0 & 0xFF)
|
||||
return this;
|
||||
}
|
||||
|
||||
test('appends char and length', function() {
|
||||
var buf = new BufferList().join(true,'!');
|
||||
assert.equalBuffers(buf,[33,0,0,0,4]);
|
||||
});
|
||||
p.addCString = function(string) {
|
||||
var string = string || "";
|
||||
var len = Buffer.byteLength(string) + 1;
|
||||
this._ensure(len);
|
||||
|
||||
}
|
||||
|
||||
test('does complicated buffer', function() {
|
||||
var buf = new BufferList()
|
||||
.addInt32(1)
|
||||
.addInt16(2)
|
||||
.addCString('!')
|
||||
.join(true,'!');
|
||||
assert.equalBuffers(buf, [33, 0, 0, 0, 0x0c, 0, 0, 0, 1, 0, 2, 33, 0]);
|
||||
});
|
||||
p.join = function() {
|
||||
return this.buffer.slice(0, this.offset)
|
||||
}
|
||||
|
||||
test('concats', function() {
|
||||
var buf1 = new BufferList().addInt32(8).join(false,'!');
|
||||
var buf2 = new BufferList().addInt16(1).join();
|
||||
var result = BufferList.concat(buf1, buf2);
|
||||
assert.equalBuffers(result, [33, 0, 0, 0, 8, 0, 1]);
|
||||
});
|
||||
test('adding int32', function() {
|
||||
var testAddingInt32 = function(int, expectedBuffer) {
|
||||
test('writes ' + int, function() {
|
||||
var subject = new ElasticBuffer();
|
||||
var result = subject.addInt32(int).join();
|
||||
assert.equalBuffers(result, expectedBuffer);
|
||||
})
|
||||
}
|
||||
|
||||
testAddingInt32(0, [0, 0, 0, 0]);
|
||||
testAddingInt32(1, [0, 0, 0, 1]);
|
||||
testAddingInt32(256, [0, 0, 1, 0]);
|
||||
test('writes largest int32', function() {
|
||||
//todo need to find largest int32 when I have internet access
|
||||
return false;
|
||||
})
|
||||
|
||||
test('writing multiple int32s', function() {
|
||||
var subject = new ElasticBuffer();
|
||||
var result = subject.addInt32(1).addInt32(10).addInt32(0).join();
|
||||
assert.equalBuffers(result, [0, 0, 0, 1, 0, 0, 0, 0x0a, 0, 0, 0, 0]);
|
||||
})
|
||||
|
||||
test('having to resize the buffer', function() {
|
||||
test('after resize correct result returned', function() {
|
||||
var subject = new ElasticBuffer(10);
|
||||
subject.addInt32(1).addInt32(1).addInt32(1)
|
||||
assert.equalBuffers(subject.join(), [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('int16', function() {
|
||||
test('writes 0', function() {
|
||||
var subject = new ElasticBuffer();
|
||||
var result = subject.addInt16(0).join();
|
||||
assert.equalBuffers(result, [0,0]);
|
||||
})
|
||||
|
||||
test('writes 400', function() {
|
||||
var subject = new ElasticBuffer();
|
||||
var result = subject.addInt16(400).join();
|
||||
assert.equalBuffers(result, [1, 0x90])
|
||||
})
|
||||
|
||||
test('writes many', function() {
|
||||
var subject = new ElasticBuffer();
|
||||
var result = subject.addInt16(0).addInt16(1).addInt16(2).join();
|
||||
assert.equalBuffers(result, [0, 0, 0, 1, 0, 2])
|
||||
})
|
||||
|
||||
test('resizes if internal buffer fills up', function() {
|
||||
var subject = new ElasticBuffer(3);
|
||||
var result = subject.addInt16(2).addInt16(3).join();
|
||||
assert.equalBuffers(result, [0, 2, 0, 3])
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
test('cString', function() {
|
||||
test('writes empty cstring', function() {
|
||||
var subject = new ElasticBuffer();
|
||||
var result = subject.addCString().join();
|
||||
assert.equalBuffers(result, [0])
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user