Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit 37f103e

Browse files
committed
settle on new api
1 parent dd2b9a0 commit 37f103e

File tree

10 files changed

+63
-117
lines changed

10 files changed

+63
-117
lines changed

index.js

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,69 @@
1-
var encodeKey = exports.encodeKey = require('./lib/encode-key');
2-
var encodeValue = exports.encodeValue = require('./lib/encode-value');
3-
var encodeBatch = exports.encodeBatch = require('./lib/batch');
4-
var encodings = exports.encodings = require('./lib/encodings');
5-
var decodeKey = exports.decodeKey = require('./lib/decode-key');
6-
var decodeValue = exports.decodeValue = require('./lib/decode-value');
7-
var keyAsBuffer = exports.keyAsBuffer = require('./lib/key-as-buffer');
8-
var valueAsBuffer = exports.valueAsBuffer = require('./lib/value-as-buffer');
9-
10-
exports.Codec = Codec;
11-
12-
function Codec(options){
13-
this.options = options;
1+
var encodings = require('./lib/encodings');
2+
3+
module.exports = Codec;
4+
5+
function Codec(opts){
6+
this.opts = opts;
7+
this.encodings = encodings;
148
}
159

16-
Codec.prototype.encodeKey = function(key, options){
17-
return encodeKey(key, [options, this.options]);
10+
Codec.prototype._encoding = function(encoding){
11+
if (typeof encoding == 'string') encoding = encodings[encoding];
12+
if (!encoding) encoding = encodings.id;
13+
return encoding;
14+
};
15+
16+
Codec.prototype._keyEncoding = function(opts, batchOpts){
17+
return this._encoding(batchOpts && batchOpts.keyEncoding
18+
|| opts.keyEncoding
19+
|| this.opts.keyEncoding);
1820
};
1921

20-
Codec.prototype.encodeValue = function(value, options){
21-
return encodeValue(value, [options, this.options]);
22+
Codec.prototype._valueEncoding = function(opts, batchOpts){
23+
return this._encoding(batchOpts && batchOpts.valueEncoding
24+
|| opts.valueEncoding
25+
|| this.opts.valueEncoding);
2226
};
2327

24-
Codec.prototype.encodeBatch = function(ops, options){
25-
return encodeBatch(ops, [options, this.options]);
28+
Codec.prototype.encodeKey = function(key, opts, batchOpts){
29+
return this._keyEncoding(opts, batchOpts).encode(key);
2630
};
2731

28-
Codec.prototype.encodings = encodings;
32+
Codec.prototype.encodeValue = function(value, opts, batchOpts){
33+
return this._valueEncoding(opts, batchOpts).encode(value);
34+
};
35+
36+
Codec.prototype.decodeKey = function(key, opts){
37+
return this._keyEncoding(opts).decode(key);
38+
};
2939

30-
Codec.prototype.decodeKey = function(key, options){
31-
return decodeKey(key, [options, this.options]);
40+
Codec.prototype.decodeValue = function(value, opts){
41+
return this._valueEncoding(opts).decode(value);
3242
};
3343

34-
Codec.prototype.decodeValue = function(value, options){
35-
return decodeValue(value, [options, this.options]);
44+
Codec.prototype.encodeBatch = function(ops, opts){
45+
var self = this;
46+
47+
return ops.map(function(_op){
48+
var op = {
49+
type: _op.type,
50+
key: self.encodeKey(_op.key, opts, _op)
51+
};
52+
if (self.keyAsBuffer(opts, _op)) op.keyEncoding = 'binary';
53+
if (_op.prefix) op.prefix = _op.prefix;
54+
if ('value' in _op) {
55+
op.value = self.encodeValue(_op.value, opts, _op);
56+
if (self.valueAsBuffer(opts, _op)) op.valueEncoding = 'binary';
57+
}
58+
return op;
59+
});
3660
};
3761

38-
Codec.prototype.keyAsBuffer = function(options){
39-
return keyAsBuffer([options, this.options]);
62+
Codec.prototype.keyAsBuffer = function(opts){
63+
return this._keyEncoding(opts).buffer;
4064
};
4165

42-
Codec.prototype.valueAsBuffer = function(options){
43-
return valueAsBuffer([options, this.options]);
66+
Codec.prototype.valueAsBuffer = function(opts){
67+
return this._valueEncoding(opts).buffer;
4468
};
4569

lib/batch.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

lib/decode-key.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/decode-value.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

lib/encode-key.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/encode-value.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

lib/encodings.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ exports.binary = {
2828
type: 'binary'
2929
};
3030

31+
exports.id = {
32+
encode: function(data){
33+
return data;
34+
},
35+
decode: function(data){
36+
return data;
37+
},
38+
buffer: false,
39+
type: 'id'
40+
};
41+
3142
var bufferEncodings = [
3243
'hex',
3344
'ascii',

lib/key-as-buffer.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/value-as-buffer.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/walk.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)