Skip to content

Commit

Permalink
0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
KlonD90 committed Feb 15, 2016
1 parent 4690fca commit 4507244
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ language: node_js
node_js:
- "0.12"
- "iojs"
- "stable"
- "4.1"
- "4.0"
cache: false

before_script:
Expand Down
35 changes: 32 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,38 @@ conn.connect()
});
```


##Msgpack implentation

You can yse any implementation that can be duck typing with next interface:

```
//msgpack implementation example
/*
@interface
decode: (Buffer buf)
encode: (Object obj)
*/
var exampleCustomMsgpack = {
encode: function(obj){
return yourmsgpack.encode(obj);
},
decode: function(buf){
return yourmsgpack.decode(obj);
}
};
```

##API

**class TarantoolConnection(options)**
```
var defaultOptions = {
host: 'localhost',
port: '3301',
log: false
log: false,
msgpack: require('msgpack-lite')
};
```
You can overrid default options with options.
Expand Down Expand Up @@ -89,12 +113,11 @@ Promise resolve a new tuple.

**upsert(spaceId: Number or String, key: tuple, ops: array of operations, tuple: tuple) : Promise()**

About operation: https://github.com/tarantool/tarantool/issues/905
About operation: http://tarantool.org/doc/book/box/box_space.html#lua-function.space_object.upsert

Ops: http://tarantool.org/doc/book/box/box_space.html (search for update here).

Promise resolve nothing.
Knowing issues if it cannot be updated by your ops it wouldn't return error just only at tarantool side but not at protocol. See https://github.com/tarantool/tarantool/issues/966

**replace(spaceId: Number or String, tuple: tuple) : Promise(Tuple)**

Expand Down Expand Up @@ -149,6 +172,12 @@ It's ok you can do whatever you need. I add log options for some technical infor

##Changelog

###0.4.0

Change msgpack5 to msgpack-lite(thx to @arusakov).
Add msgpack as option for connection.
Bump msgpack5 for work at new version.

###0.3.0
Add upsert operation.
Key is now can be just a number.
Expand Down
67 changes: 42 additions & 25 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,25 @@ var tarantoolConstants = require('./const');
var net = require('net');
var _ = require('underscore');
var EventEmitter = require('events');
var msgpack = require('msgpack5')();
var msgpack = require('msgpack-lite');
var crypto = require('crypto');
var xor = require('bitwise-xor');

//msgpack implementation example
/*
@interface
decode: (Buffer buf)
encode: (Object obj)
*/
var exampleCustomMsgpack = {
encode: function(obj){
return msgpack.encode(obj);
},
decode: function(buf){
return msgpack.decode(obj);
}
};

var shatransform = function(t){
return crypto.createHash('sha1').update(t).digest();
};
Expand Down Expand Up @@ -41,6 +56,7 @@ function TarantoolConnection (options){
readable: true,
writable: true
});
this.msgpack = options.msgpack || msgpack;
this.state = states.INITED;
this.options = _.extend({}, defaultOptions, options);
this.commandsQueue = [];
Expand Down Expand Up @@ -224,7 +240,7 @@ TarantoolConnection.prototype._responseBufferTrack = function(buffer, length){
TarantoolConnection.prototype._processResponse = function(buffer){
// add fixarraymap with 2 objects before main object
var dataBuffer = Buffer.concat([new Buffer([0x92]), buffer]);
var obj = msgpack.decode(dataBuffer);
var obj = this.msgpack.decode(dataBuffer);

var reqId = obj[0][1];
for(var i = 0; i<this.commandsQueue.length; i++)
Expand Down Expand Up @@ -330,11 +346,11 @@ TarantoolConnection.prototype.select = function(spaceId, indexId, limit, offset,
if (iterator == 'all')
key = [];
var buffered = {
spaceId: msgpack.encode(spaceId),
indexId: msgpack.encode(indexId),
limit: msgpack.encode(limit),
offset: msgpack.encode(offset),
key: msgpack.encode(key)
spaceId: this.msgpack.encode(spaceId),
indexId: this.msgpack.encode(indexId),
limit: this.msgpack.encode(limit),
offset: this.msgpack.encode(offset),
key: this.msgpack.encode(key)
};
var body = Buffer.concat([new Buffer([0x86,tarantoolConstants.KeysCode.space_id]), buffered.spaceId,
new Buffer([tarantoolConstants.KeysCode.index_id]), buffered.indexId,
Expand Down Expand Up @@ -367,9 +383,9 @@ TarantoolConnection.prototype.delete = function(spaceId, indexId, key){
var reqId = requestId.getId();
var header = this._header(tarantoolConstants.RequestCode.rqDelete, reqId);
var buffered = {
spaceId: msgpack.encode(spaceId),
indexId: msgpack.encode(indexId),
key: msgpack.encode(key)
spaceId: this.msgpack.encode(spaceId),
indexId: this.msgpack.encode(indexId),
key: this.msgpack.encode(key)
};
var body = Buffer.concat([new Buffer([0x86,tarantoolConstants.KeysCode.space_id]), buffered.spaceId,
new Buffer([tarantoolConstants.KeysCode.index_id]), buffered.indexId,
Expand Down Expand Up @@ -398,11 +414,12 @@ TarantoolConnection.prototype.update = function(spaceId, indexId, key, ops){
}
var reqId = requestId.getId();
var header = this._header(tarantoolConstants.RequestCode.rqUpdate, reqId);

var buffered = {
spaceId: msgpack.encode(spaceId),
indexId: msgpack.encode(indexId),
ops: msgpack.encode(ops),
key: msgpack.encode(key)
spaceId: this.msgpack.encode(spaceId),
indexId: this.msgpack.encode(indexId),
ops: this.msgpack.encode(ops),
key: this.msgpack.encode(key)
};
var body = Buffer.concat([new Buffer([0x84,tarantoolConstants.KeysCode.space_id]), buffered.spaceId,
new Buffer([tarantoolConstants.KeysCode.index_id]), buffered.indexId,
Expand Down Expand Up @@ -435,11 +452,11 @@ TarantoolConnection.prototype.upsert = function(spaceId, key, ops, tuple){
var reqId = requestId.getId();
var header = this._header(tarantoolConstants.RequestCode.rqUpsert, reqId);
var buffered = {
spaceId: msgpack.encode(spaceId),
spaceId: this.msgpack.encode(spaceId),
//indexId: msgpack.encode(indexId),
ops: msgpack.encode(ops),
key: msgpack.encode(key),
tuple: msgpack.encode(tuple)
ops: this.msgpack.encode(ops),
key: this.msgpack.encode(key),
tuple: this.msgpack.encode(tuple)
};
var body = Buffer.concat([new Buffer([0x84,tarantoolConstants.KeysCode.space_id]), buffered.spaceId,
//new Buffer([tarantoolConstants.KeysCode.index_id]), buffered.indexId,
Expand All @@ -463,8 +480,8 @@ TarantoolConnection.prototype.eval = function(expression){
var reqId = requestId.getId();
var header = this._header(tarantoolConstants.RequestCode.rqEval, reqId);
var buffered = {
expression: msgpack.encode(expression),
tuple: msgpack.encode(tuple)
expression: this.msgpack.encode(expression),
tuple: this.msgpack.encode(tuple)
};
var body = Buffer.concat([new Buffer([0x82,tarantoolConstants.KeysCode.expression]), buffered.expression,
new Buffer([tarantoolConstants.KeysCode.tuple]), buffered.tuple]);
Expand All @@ -479,8 +496,8 @@ TarantoolConnection.prototype.call = function(functionName){
var reqId = requestId.getId();
var header = this._header(tarantoolConstants.RequestCode.rqCall, reqId);
var buffered = {
functionName: msgpack.encode(functionName),
tuple: msgpack.encode(tuple ? tuple : [])
functionName: this.msgpack.encode(functionName),
tuple: this.msgpack.encode(tuple ? tuple : [])
};
var body = Buffer.concat([new Buffer([0x82,tarantoolConstants.KeysCode.function_name]), buffered.functionName,
new Buffer([tarantoolConstants.KeysCode.tuple]), buffered.tuple]);
Expand Down Expand Up @@ -513,8 +530,8 @@ TarantoolConnection.prototype._replaceInsert = function(cmd, reqId, spaceId, tup
}
var header = this._header(cmd, reqId);
var buffered = {
spaceId: msgpack.encode(spaceId),
tuple: msgpack.encode(tuple)
spaceId: this.msgpack.encode(spaceId),
tuple: this.msgpack.encode(tuple)
};
var body = Buffer.concat([new Buffer([0x82,tarantoolConstants.KeysCode.space_id]), buffered.spaceId,
new Buffer([tarantoolConstants.KeysCode.tuple]), buffered.tuple]);
Expand All @@ -531,7 +548,7 @@ TarantoolConnection.prototype.auth = function(username, password){
var reqId = requestId.getId();
var header = this._header(tarantoolConstants.RequestCode.rqAuth, reqId);
var buffered = {
username: msgpack.encode(username)
username: this.msgpack.encode(username)
};
var scrambled = scramble(password, this.salt);
var body = Buffer.concat([new Buffer([0x82, tarantoolConstants.KeysCode.username]), buffered.username,
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tarantool-driver",
"version": "0.3.0",
"version": "0.4.0",
"description": "Tarantool driver for 1.6",
"main": "index.js",
"scripts": {
Expand All @@ -24,10 +24,12 @@
"homepage": "https://github.com/KlonD90/node-tarantool-driver",
"dependencies": {
"bitwise-xor": "0.0.0",
"msgpack5": "^3.0.0",
"msgpack-lite": "^0.1.15",
"msgpack5": "^3.3.0",
"underscore": "^1.8.3"
},
"devDependencies": {
"benchmark": "^2.1.0",
"mocha": "^2.2.4"
}
}
33 changes: 32 additions & 1 deletion test/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var fs = require('fs');
var assert = require('assert');
var TarantoolConnection = require('../lib/connection');
var mlite = require('msgpack-lite');

describe('Tarantool Connection tests', function(){
before(function(){
Expand Down Expand Up @@ -167,7 +168,6 @@ describe('Tarantool Connection tests', function(){
return conn.call('myget', 4)
})
.then(function(value){
console.log('value', value);
done()
})
.catch(function(e){
Expand Down Expand Up @@ -297,4 +297,35 @@ describe('Tarantool Connection tests', function(){
})
});
});
describe('connection test with custom msgpack implementation', function(){
var customConn;
beforeEach(function(){
customConn = new TarantoolConnection(
{
port: 33013,
msgpack: {
encode: function(obj){
return mlite.encode(obj);
},
decode: function(buf){
return mlite.decode(buf);
}
}
}
);
});
it('connect', function(done){
customConn.connect().then(function(){
done();
}, function(e){ throw 'not connected'; done();});
});
it('auth', function(done){
customConn.connect().then(function(){
return customConn.auth('test', 'test');
}, function(e){ throw 'not connected'; done();})
.then(function(){
done();
}, function(e){ throw 'not auth'; done();})
});
});
});

0 comments on commit 4507244

Please sign in to comment.