Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/serializer' into feature…
Browse files Browse the repository at this point in the history
…/serializer
  • Loading branch information
vanatd committed Oct 5, 2020
2 parents 10656a6 + 08e05f4 commit 6096216
Show file tree
Hide file tree
Showing 5 changed files with 809 additions and 1,008 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Note that values are always returned as `Buffer`s, regardless of whether a

## Contributing

The best way to contribut to the project is by reporting bugs and testing unpublished
The best way to contribute to the project is by reporting bugs and testing unpublished
versions. If you have a staging or development app, the easiest way to do this is
using the git repository as your `memjs` package dependency---in `package.json`:

Expand Down
37 changes: 23 additions & 14 deletions lib/memjs/memjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Client.prototype.get = function(key, callback) {
});
}
var logger = this.options.logger;
this.seq++;
this.incrSeq();
var request = makeRequestBuffer(0, key, '', '', this.seq);
this.perform(key, request, this.seq, function(err, response) {
if (err) {
Expand Down Expand Up @@ -187,7 +187,7 @@ Client.prototype.set = function(key, value, options, callback) {
expires = options.expires;

// TODO: support flags, support version (CAS)
this.seq++;
this.incrSeq();
var expiration = makeExpiration(expires || this.options.expires);
var extras = Buffer.concat([Buffer.from('00000000', 'hex'), expiration]);

Expand Down Expand Up @@ -242,7 +242,7 @@ Client.prototype.add = function(key, value, options, callback) {
expires = options.expires;

// TODO: support flags, support version (CAS)
this.seq++;
this.incrSeq();
var expiration = makeExpiration(expires || this.options.expires);
var extras = Buffer.concat([Buffer.from('00000000', 'hex'), expiration]);

Expand Down Expand Up @@ -300,7 +300,7 @@ Client.prototype.replace = function(key, value, options, callback) {
expires = options.expires;

// TODO: support flags, support version (CAS)
this.seq++;
this.incrSeq();
var expiration = makeExpiration(expires || this.options.expires);
var extras = Buffer.concat([Buffer.from('00000000', 'hex'), expiration]);

Expand Down Expand Up @@ -342,7 +342,7 @@ Client.prototype.delete = function(key, callback) {
}
// TODO: Support version (CAS)
var logger = this.options.logger;
this.seq++;
this.incrSeq();
var request = makeRequestBuffer(4, key, '', '', this.seq);
this.perform(key, request, this.seq, function(err, response) {
if (err) {
Expand Down Expand Up @@ -402,7 +402,7 @@ Client.prototype.increment = function(key, amount, options, callback) {
expires = options.expires;

// TODO: support version (CAS)
this.seq++;
this.incrSeq();
initial = initial || 0;
expires = expires || this.options.expires;
var extras = makeAmountInitialAndExpiration(amount, initial, expires);
Expand Down Expand Up @@ -464,7 +464,7 @@ Client.prototype.decrement = function(key, amount, options, callback) {
initial = options.initial;
expires = options.expires;

this.seq++;
this.incrSeq();
initial = initial || 0;
expires = expires || this.options.expires;
var extras = makeAmountInitialAndExpiration(amount, initial, expires);
Expand Down Expand Up @@ -501,8 +501,7 @@ Client.prototype.append = function(key, value, callback) {
}
// TODO: support version (CAS)
var logger = this.options.logger;
this.seq++;

this.incrSeq();
var opcode = 0x0E;
var serialized = this.serializer.serialize(opcode, value, '');
var request = makeRequestBuffer(opcode, key, serialized.extras, serialized.value, this.seq);
Expand Down Expand Up @@ -540,7 +539,7 @@ Client.prototype.prepend = function(key, value, callback) {
}
// TODO: support version (CAS)
var logger = this.options.logger;
this.seq++;
this.incrSeq();

var opcode = 0x0E;
var serialized = this.serializer.serialize(opcode, value, '');
Expand Down Expand Up @@ -579,7 +578,7 @@ Client.prototype.touch = function(key, expires, callback) {
}
// TODO: support version (CAS)
var logger = this.options.logger;
this.seq++;
this.incrSeq();
var extras = makeExpiration(expires || this.options.expires);
var request = makeRequestBuffer(0x1C, key, extras, '', this.seq);
this.perform(key, request, this.seq, function(err, response) {
Expand Down Expand Up @@ -617,7 +616,7 @@ Client.prototype.flush = function(callback) {
return promisify(function(callback) { self.flush(function(err, results) { callback(err, results); }); });
}
// TODO: support expiration
this.seq++;
this.incrSeq();
var request = makeRequestBuffer(0x08, '', '', '', this.seq);
var count = this.servers.length;
var result = {};
Expand Down Expand Up @@ -659,7 +658,7 @@ Client.prototype.flush = function(callback) {
// mapping the stat name to the value of the statistic as a string.
Client.prototype.statsWithKey = function(key, callback) {
var logger = this.options.logger;
this.seq++;
this.incrSeq();
var request = makeRequestBuffer(0x10, key, '', '', this.seq);
var i;

Expand Down Expand Up @@ -735,7 +734,7 @@ Client.prototype.resetStats = function(callback) {
// fail and are retried, leading to the quit command winning and closing the
// connection before the retries complete.
Client.prototype.quit = function() {
this.seq++;
this.incrSeq();
// TODO: Nicer perhaps to do QUITQ (0x17) but need a new callback for when
// write is done.
var request = makeRequestBuffer(0x07, '', '', '', this.seq); // QUIT
Expand Down Expand Up @@ -819,6 +818,16 @@ Client.prototype.perform = function(key, request, seq, callback, retries) {
serv.write(request);
};

// Increment the seq value
Client.prototype.incrSeq = function() {
// Wrap `this.seq` to 32-bits since the field we fit it into is only 32-bits.
if (this.seq == 0xffffffff) {
this.seq = 0;
} else {
this.seq++;
}
};

exports.Client = Client;
exports.Server = Server;
exports.Utils = require('./utils');
Expand Down
Loading

0 comments on commit 6096216

Please sign in to comment.