|
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; |
14 | 8 | }
|
15 | 9 |
|
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); |
18 | 20 | };
|
19 | 21 |
|
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); |
22 | 26 | };
|
23 | 27 |
|
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); |
26 | 30 | };
|
27 | 31 |
|
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 | +}; |
29 | 39 |
|
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); |
32 | 42 | };
|
33 | 43 |
|
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 | + }); |
36 | 60 | };
|
37 | 61 |
|
38 |
| -Codec.prototype.keyAsBuffer = function(options){ |
39 |
| - return keyAsBuffer([options, this.options]); |
| 62 | +Codec.prototype.keyAsBuffer = function(opts){ |
| 63 | + return this._keyEncoding(opts).buffer; |
40 | 64 | };
|
41 | 65 |
|
42 |
| -Codec.prototype.valueAsBuffer = function(options){ |
43 |
| - return valueAsBuffer([options, this.options]); |
| 66 | +Codec.prototype.valueAsBuffer = function(opts){ |
| 67 | + return this._valueEncoding(opts).buffer; |
44 | 68 | };
|
45 | 69 |
|
0 commit comments