Skip to content

Commit 6ccbec1

Browse files
committed
add space name and index name operations
1 parent f364694 commit 6ccbec1

File tree

4 files changed

+212
-20
lines changed

4 files changed

+212
-20
lines changed

README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,40 @@ Resolve if connected. Or reject if not.
5252

5353
Auth with using chap-sha1(http://tarantool.org/doc/book/box/box_space.html). About authenthication more here: http://tarantool.org/doc/book/box/authentication.html
5454

55-
**select(spaceId: Number, indexId: Number, limit: Number, offset: Number, iterator: Iterator, key: tuple) : Promise( Array of tuples)**
55+
**select(spaceId: Number or String, indexId: Number or String, limit: Number, offset: Number, iterator: Iterator, key: tuple) : Promise( Array of tuples)**
5656

5757
Iterators: http://tarantool.org/doc/book/box/box_index.html. Available iterators: 'eq', 'req', 'all', 'lt', 'le', 'ge', 'gt', 'bitsAllSet', 'bitsAnySet', 'bitsAllNotSet'.
5858

5959
It's just select. Promise resolve array of tuples.
6060

61-
**delete(spaceId: Number, indexId: Number, key: tuple) : Promise(Array of tuples)**
61+
Some examples:
62+
63+
```
64+
conn.select(512, 0, 1, 0, 'eq', [50]);
65+
//same as
66+
conn.select('test', 'primary', 1, 0, 'eq', [50]);
67+
```
68+
69+
You can use space name or index name instead if id but it will some requests for get this metadata. That information actual for delete, replace, insert, update too.
70+
71+
**delete(spaceId: Number or String, indexId: Number or String, key: tuple) : Promise(Array of tuples)**
6272

6373
Promise resolve an array of deleted tuples.
6474

65-
**update(spaceId: Number, indexId: Number, key: tuple, ops) : Promise(Array of tuples)**
75+
**update(spaceId: Number or String, indexId: Number or String, key: tuple, ops) : Promise(Array of tuples)**
6676

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

6979
Promise resolve an array of updated tuples.
7080

71-
**insert(spaceId: Number, tuple: tuple) : Promise(Tuple)**
81+
**insert(spaceId: Number or String, tuple: tuple) : Promise(Tuple)**
7282

7383
So it's insert. More you can read here: http://tarantool.org/doc/book/box/box_space.html
7484

7585
Promise resolve a new tuple.
7686

7787

78-
**replace(spaceId: Number, tuple: tuple) : Promise(Tuple)**
88+
**replace(spaceId: Number or String, tuple: tuple) : Promise(Tuple)**
7989

8090
So it's replace. More you can read here: http://tarantool.org/doc/book/box/box_space.html
8191

lib/connection.js

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,71 @@ function TarantoolConnection (options){
5151
this.socket.on('data', this.onData.bind(this));
5252
}
5353

54-
TarantoolConnection.prototype._getMetadataNamespace = function(){
54+
TarantoolConnection.prototype._getSpaceId = function(name){
55+
return this.select(tarantoolConstants.Space.space, tarantoolConstants.IndexSpace.name, 1, 0,
56+
tarantoolConstants.IteratorsType.all, [name])
57+
.then(function(value){
58+
var spaceId = value[0][0];
59+
this.namespace[name] = {
60+
id: spaceId,
61+
name: name,
62+
indexes: {}
63+
};
64+
this.namespace[spaceId] = {
65+
id: spaceId,
66+
name: name,
67+
indexes: {}
68+
};
69+
return spaceId;
70+
}.bind(this))
71+
.catch(function(e){
72+
reject(new Error('cannot detect space by name'));
73+
});
74+
};
5575

76+
TarantoolConnection.prototype._getIndexId = function(spaceId, indexName){
77+
return this.select(tarantoolConstants.Space.index, tarantoolConstants.IndexSpace.indexName, 1, 0,
78+
tarantoolConstants.IteratorsType.all, [spaceId, indexName])
79+
.then(function(value){
80+
var indexId = value[0][1];
81+
var space = this.namespace[spaceId];
82+
if (space)
83+
{
84+
this.namespace[space.name].indexes[indexName] = indexId;
85+
this.namespace[space.id].indexes[indexName] = indexId;
86+
}
87+
return indexId;
88+
}.bind(this));
89+
};
90+
91+
TarantoolConnection.prototype._getMetadata = function(spaceName, indexName){
92+
if (this.namespace[spaceName])
93+
{
94+
spaceName = this.namespace[spaceName].id;
95+
}
96+
if (typeof(this.namespace[spaceName]) != 'undefined' && typeof(this.namespace[spaceName].indexes[indexName])!='undefined')
97+
{
98+
indexName = this.namespace[spaceName].indexes[indexName];
99+
}
100+
if (typeof(spaceName)=='string' && typeof(indexName)=='string')
101+
{
102+
return this._getSpaceId(spaceName)
103+
.then(function(spaceId){
104+
return Promise.all([spaceId, this._getIndexId(spaceId, indexName)]);
105+
}.bind(this))
106+
}
107+
var promises = [];
108+
if (typeof(spaceName) == 'string')
109+
promises.push(this._getSpaceId(spaceName));
110+
else
111+
promises.push(spaceName);
112+
if (typeof(indexName) == 'string')
113+
promises.push(this._getIndexId(spaceName, indexName));
114+
else
115+
promises.push(indexName);
116+
if(this.options.log)
117+
console.log('promises', promises);
118+
return Promise.all(promises);
56119
};
57120

58121
TarantoolConnection.prototype.onData = function(data){
@@ -207,9 +270,9 @@ TarantoolConnection.prototype._interupt = function(error){
207270
};
208271

209272
TarantoolConnection.prototype.connect = function(){
210-
console.log('connect call');
273+
if (this.options.log)
274+
console.log('connect call');
211275
return new Promise(function (resolve, reject) {
212-
console.log('I am in promise');
213276
if (this.state == states.INITED)
214277
{
215278
this.state = states.CONNECTING;
@@ -243,6 +306,15 @@ TarantoolConnection.prototype.ping = function(){
243306

244307
TarantoolConnection.prototype.select = function(spaceId, indexId, limit, offset, iterator, key){
245308
return new Promise(function(resolve, reject){
309+
if (typeof(spaceId)=='string' || typeof(indexId)=='string')
310+
{
311+
return this._getMetadata(spaceId, indexId)
312+
.then(function(info){
313+
return this.select(info[0], info[1], limit, offset, iterator, key);
314+
}.bind(this))
315+
.then(resolve)
316+
.catch(reject);
317+
}
246318
var reqId = requestId.getId();
247319
var header = this._header(tarantoolConstants.RequestCode.rqSelect, reqId);
248320
//don't need a key for all iterator
@@ -272,6 +344,15 @@ TarantoolConnection.prototype.delete = function(spaceId, indexId, key){
272344
return new Promise(function (resolve, reject) {
273345
if (Array.isArray(key))
274346
{
347+
if (typeof(spaceId)=='string' || typeof(indexId)=='string')
348+
{
349+
return this._getMetadata(spaceId, indexId)
350+
.then(function(info){
351+
return this.delete(info[0], info[1], key);
352+
}.bind(this))
353+
.then(resolve)
354+
.catch(reject);
355+
}
275356
var reqId = requestId.getId();
276357
var header = this._header(tarantoolConstants.RequestCode.rqDelete, reqId);
277358
var buffered = {
@@ -293,6 +374,15 @@ TarantoolConnection.prototype.delete = function(spaceId, indexId, key){
293374
TarantoolConnection.prototype.update = function(spaceId, indexId, key, ops){
294375
return new Promise(function (resolve, reject) {
295376
if (Array.isArray(ops) && Array.isArray(key)){
377+
if (typeof(spaceId)=='string' || typeof(indexId)=='string')
378+
{
379+
return this._getMetadata(spaceId, indexId)
380+
.then(function(info){
381+
return this.update(info[0], info[1], key, ops);
382+
}.bind(this))
383+
.then(resolve)
384+
.catch(reject);
385+
}
296386
var reqId = requestId.getId();
297387
var header = this._header(tarantoolConstants.RequestCode.rqUpdate, reqId);
298388
var buffered = {
@@ -358,6 +448,15 @@ TarantoolConnection.prototype.replace = function(spaceId, tuple){
358448
TarantoolConnection.prototype._replaceInsert = function(cmd, reqId, spaceId, tuple){
359449
return new Promise(function (resolve, reject) {
360450
if (Array.isArray(tuple)){
451+
if (typeof(spaceId)=='string')
452+
{
453+
return this._getMetadata(spaceId, 0)
454+
.then(function(info){
455+
return this._replaceInsert(cmd, reqId, info[0], tuple);
456+
}.bind(this))
457+
.then(resolve)
458+
.catch(reject);
459+
}
361460
var header = this._header(cmd, reqId);
362461
var buffered = {
363462
spaceId: msgpack.encode(spaceId),
@@ -451,4 +550,6 @@ TarantoolConnection.prototype._stubMethods = function(){
451550
this[requestMethods[i]] = this._notAvailableMethod;
452551
};
453552

553+
TarantoolConnection.prototype.IteratorsType = tarantoolConstants.IteratorsType;
554+
454555
module.exports = TarantoolConnection;

lib/const.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,31 @@ const TimeoutErrCode = 0xfffffff2; // fake code to wrap timeout error into r
5353

5454
const PacketLengthBytes = 5;
5555

56+
const Space = {
57+
schema: 272,
58+
space: 280,
59+
index: 288,
60+
func: 296,
61+
user: 304,
62+
priv: 312,
63+
cluster: 320
64+
};
65+
66+
const IndexSpace = {
67+
primary: 0,
68+
name: 2,
69+
indexPrimary: 0,
70+
indexName: 2
71+
};
72+
5673
const ExportPackage = {
5774
RequestCode: RequestCode,
5875
KeysCode: KeysCode,
5976
IteratorsType: IteratorsType,
6077
OkCode: OkCode,
61-
passEnter: msgpack.encode('chap-sha1')
78+
passEnter: msgpack.encode('chap-sha1'),
79+
Space: Space,
80+
IndexSpace: IndexSpace
6281
};
6382

6483
module.exports = ExportPackage;

test/app.js

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,19 @@ describe('Tarantool Connection tests', function(){
3232
});
3333
describe('requests', function(){
3434
var insertTuple = [50, 10, 'my key', 30];
35+
var conn;
3536
before(function(done){
3637
console.log('before call');
3738
try{
38-
Promise.all([conn.delete(514, 0, [1]),conn.delete(514, 0, [2]),conn.delete(514, 0, [3]),conn.delete(514, 0, [4] )])
39+
conn = new TarantoolConnection({port: 33013, log: true});
40+
conn.connect().then(function(){
41+
return conn.auth('test', 'test');
42+
}, function(e){ throw 'not connected'; done();})
43+
.then(function(){
44+
return Promise.all([conn.delete(514, 0, [1]),conn.delete(514, 0, [2]),
45+
conn.delete(514, 0, [3]),conn.delete(514, 0, [4]),
46+
conn.delete(512, 0, [999])]);
47+
})
3948
.then(function(){
4049
return conn.call('clearaddmore');
4150
})
@@ -44,20 +53,12 @@ describe('Tarantool Connection tests', function(){
4453
})
4554
.catch(function(e){
4655
done(e);
47-
})
56+
});
4857
}
4958
catch(e){
5059
console.log(e);
5160
}
5261
});
53-
beforeEach(function(done){
54-
conn.connect().then(function(){
55-
return conn.auth('test', 'test');
56-
}, function(e){ throw 'not connected'; done();})
57-
.then(function(){
58-
done();
59-
}, function(e){ throw 'not auth'; done();})
60-
});
6162
it('replace', function(done){
6263
conn.replace(512, insertTuple)
6364
.then(function(a){
@@ -176,6 +177,67 @@ describe('Tarantool Connection tests', function(){
176177
done(e);
177178
})
178179
});
179-
180+
it('get metadata space by name', function(done){
181+
conn._getSpaceId('batched')
182+
.then(function(v){
183+
assert.equal(v, 514);
184+
done();
185+
})
186+
.catch(function(e){
187+
done(e);
188+
})
189+
});
190+
it('get metadata index by name', function(done){
191+
conn._getIndexId(514, 'primary')
192+
.then(function(v){
193+
assert.equal(v, 0);
194+
done();
195+
})
196+
.catch(function(e){
197+
done(e);
198+
})
199+
});
200+
it('insert with space name', function(done){
201+
conn.insert('test', [999, 999, 'fear'])
202+
.then(function(v){
203+
done();
204+
})
205+
.catch(done);
206+
});
207+
it('select with space name and index name', function(done){
208+
conn.select('test', 'primary', 0, 0, conn.IteratorsType.all, [999])
209+
.then(function(){
210+
done();
211+
})
212+
.catch(done);
213+
});
214+
it('select with space name and index number', function(done){
215+
conn.select('test', 0, 0, 0, 'eq', [999])
216+
.then(function(){
217+
done();
218+
})
219+
.catch(done);
220+
});
221+
it('select with space number and index name', function(done){
222+
conn.select(512, 'primary', 0, 0, 'eq', [999])
223+
.then(function(){
224+
done();
225+
})
226+
.catch(done);
227+
});
228+
it('delete with name', function(done){
229+
conn.delete('test', 'primary', [999])
230+
.then(function(){
231+
done();
232+
})
233+
.catch(done);
234+
});
235+
it('update with name', function(done){
236+
conn.update('test', 'primary', [999], ['+', 1, 10])
237+
.then(function(){
238+
done();
239+
})
240+
.catch(done);
241+
});
180242
});
181243
});

0 commit comments

Comments
 (0)