Skip to content

Commit

Permalink
Merge branch 'master' into performance
Browse files Browse the repository at this point in the history
Conflicts:
	README.md
	lib/thrift/binary_parser.js
	lib/thrift/connection.js
	lib/thrift/server.js
	lib/thrift/transport.js
	package.json
  • Loading branch information
wadey committed Oct 4, 2011
2 parents f5ee6a5 + 3a512e4 commit da653de
Show file tree
Hide file tree
Showing 21 changed files with 2,887 additions and 124 deletions.
21 changes: 11 additions & 10 deletions README.md
Expand Up @@ -5,23 +5,20 @@ protocol has been implemented. A Thrift compiler that will generate the .js
files from a Thrift specification is being implemented as well, see the
Thrift Compiler section below.

NOTE: you must use the framed thrift transport, TFramedTransport in most
implementations, on the server side. Using a popular example, this is enabled
by default in Cassandra 0.7 (but configuration must be changed in Cassandra
0.6.x and earlier).
NOTE: By default, node-thrift uses TFramedTransport. Using a popular
example, this is enabled by default in Cassandra 0.7 (but configuration must be
changed in Cassandra 0.6.x and earlier). See the
[examples](https://github.com/wadey/node-thrift/tree/master/examples) folder
to see how to enable TBufferedTransport (added in 0.7.0).

## Install

npm install thrift

## Thrift Compiler

A Thrift compiler is being built in a forked version of the upstream thrift
library. You can check it out here:
[https://github.com/wadey/thrift](http://github.com/wadey/thrift)

Once you build this patched version of Thrift, you can compile nodejs sources
by running the following:
A Thrift compiler is included in the 0.6.0 release of Thrift. You can
compile nodejs sources by running the following:

thrift --gen js:node thrift_file

Expand Down Expand Up @@ -54,6 +51,10 @@ Here is a Cassandra example:

Since JavaScript represents all numbers as doubles, int64 values cannot be accurately represented naturally. To solve this, int64 values in responses will be wrapped with Thirft.Int64 objects. The Int64 implementation used is [broofa/node-int64](https://github.com/broofa/node-int64).

## Libraries using node-thrift

* [yukim/node_cassandra](https://github.com/yukim/node_cassandra)

## Custom client and server example

An example based on the one shown on the Thrift front page is included in the [examples](https://github.com/wadey/node-thrift/tree/master/examples) folder.
32 changes: 23 additions & 9 deletions examples/README.md
@@ -1,15 +1,29 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# Running the user example

To run the user example, first start up the server in one terminal:
#Generate the bindings:
../../../compiler/cpp/thrift --gen js:node user.thrift

node server.js
#To run the user example, first start up the server in one terminal:
NODE_PATH=../lib:../lib/thrift node server.js

Now run the client:
#Now run the client:
NODE_PATH=../lib:../lib/thrift node client.js

node client.js

# Regenerating the bindings

If you want to regenerated the bindings, you can run:

thrift --gen js:node user.thrift

22 changes: 20 additions & 2 deletions examples/client.js
@@ -1,3 +1,21 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var thrift = require('thrift');

var UserStorage = require('./gen-nodejs/UserStorage.js'),
Expand All @@ -18,12 +36,12 @@ client.store(user, function(err, response) {
if (err) {
console.error(err);
} else {
console.log("stored:", user.uid);
console.log("client stored:", user.uid);
client.retrieve(user.uid, function(err, responseUser) {
if (err) {
console.error(err);
} else {
console.log("retrieved:", responseUser.uid);
console.log("client retrieved:", responseUser.uid);
connection.end();
}
});
Expand Down
40 changes: 40 additions & 0 deletions examples/client_multitransport.js
@@ -0,0 +1,40 @@
var thrift = require('thrift'),
ttransport = require('thrift/transport');

var UserStorage = require('./gen-nodejs/UserStorage'),
ttypes = require('./gen-nodejs/user_types');

var f_conn = thrift.createConnection('localhost', 9090), // default: framed
f_client = thrift.createClient(UserStorage, f_conn);
var b_conn = thrift.createConnection('localhost', 9091, {transport: ttransport.TBufferedTransport}),
b_client = thrift.createClient(UserStorage, b_conn);
var user1 = new ttypes.UserProfile({uid: 1,
name: "Mark Slee",
blurb: "I'll find something to put here."});
var user2 = new ttypes.UserProfile({uid: 2,
name: "Satoshi Tagomori",
blurb: "ok, let's test with buffered transport."});

f_conn.on('error', function(err) {
console.error("framed:", err);
});

f_client.store(user1, function(err, response) {
if (err) { console.error(err); return; }

console.log("stored:", user1.uid, " as ", user1.name);
b_client.retrieve(user1.uid, function(err, responseUser) {
if (err) { console.error(err); return; }
console.log("retrieved:", responseUser.uid, " as ", responseUser.name);
});
});

b_client.store(user2, function(err, response) {
if (err) { console.error(err); return; }

console.log("stored:", user2.uid, " as ", user2.name);
f_client.retrieve(user2.uid, function(err, responseUser) {
if (err) { console.error(err); return; }
console.log("retrieved:", responseUser.uid, " as ", responseUser.name);
});
});
224 changes: 224 additions & 0 deletions examples/gen-nodejs/BucketStoreMapping.js
@@ -0,0 +1,224 @@
//
// Autogenerated by Thrift
//
// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
//
var Thrift = require('thrift').Thrift;

var ttypes = require('./bucketupdater_types');
//HELPER FUNCTIONS AND STRUCTURES

var BucketStoreMapping_getMapping_args = function(args){
this.category = null
if( args != null ){ if (null != args.category)
this.category = args.category
}}
BucketStoreMapping_getMapping_args.prototype = {}
BucketStoreMapping_getMapping_args.prototype.read = function(input){
var ret = input.readStructBegin()
while (1)
{
var ret = input.readFieldBegin()
var fname = ret.fname
var ftype = ret.ftype
var fid = ret.fid
if (ftype == Thrift.Type.STOP)
break
switch(fid)
{
case 1: if (ftype == Thrift.Type.STRING) {
this.category = input.readString()
} else {
input.skip(ftype)
}
break
default:
input.skip(ftype)
}
input.readFieldEnd()
}
input.readStructEnd()
return
}

BucketStoreMapping_getMapping_args.prototype.write = function(output){
output.writeStructBegin('BucketStoreMapping_getMapping_args')
if (null != this.category) {
output.writeFieldBegin('category', Thrift.Type.STRING, 1)
output.writeString(this.category)
output.writeFieldEnd()
}
output.writeFieldStop()
output.writeStructEnd()
return
}

var BucketStoreMapping_getMapping_result = function(args){
this.success = null
this.e = null
if( args != null ){ if (null != args.success)
this.success = args.success
if (null != args.e)
this.e = args.e
}}
BucketStoreMapping_getMapping_result.prototype = {}
BucketStoreMapping_getMapping_result.prototype.read = function(input){
var ret = input.readStructBegin()
while (1)
{
var ret = input.readFieldBegin()
var fname = ret.fname
var ftype = ret.ftype
var fid = ret.fid
if (ftype == Thrift.Type.STOP)
break
switch(fid)
{
case 0: if (ftype == Thrift.Type.MAP) {
{
var _size0 = 0
var rtmp3
this.success = {}
var _ktype1 = 0
var _vtype2 = 0
rtmp3 = input.readMapBegin()
_ktype1= rtmp3.ktype
_vtype2= rtmp3.vtype
_size0= rtmp3.size
for (var _i4 = 0; _i4 < _size0; ++_i4)
{
key5 = null
val6 = null
key5 = input.readI32()
val6 = new ttypes.HostPort()
val6.read(input)
this.success[key5] = val6
}
input.readMapEnd()
}
} else {
input.skip(ftype)
}
break
case 1: if (ftype == Thrift.Type.STRUCT) {
this.e = new ttypes.BucketStoreMappingException()
this.e.read(input)
} else {
input.skip(ftype)
}
break
default:
input.skip(ftype)
}
input.readFieldEnd()
}
input.readStructEnd()
return
}

BucketStoreMapping_getMapping_result.prototype.write = function(output){
output.writeStructBegin('BucketStoreMapping_getMapping_result')
if (null != this.success) {
output.writeFieldBegin('success', Thrift.Type.MAP, 0)
{
output.writeMapBegin(Thrift.Type.I32, Thrift.Type.STRUCT, Thrift.objectLength(this.success))
{
for(var kiter7 in this.success) {
if (this.success.hasOwnProperty(kiter7))
{
var viter8 = this.success[kiter7]
output.writeI32(kiter7)
viter8.write(output)
}
}
}
output.writeMapEnd()
}
output.writeFieldEnd()
}
if (null != this.e) {
output.writeFieldBegin('e', Thrift.Type.STRUCT, 1)
this.e.write(output)
output.writeFieldEnd()
}
output.writeFieldStop()
output.writeStructEnd()
return
}

var BucketStoreMappingClient = exports.Client = function(output, pClass) {
this.output = output;
this.pClass = pClass;
this.seqid = 0;
this._reqs = {}
}
BucketStoreMappingClient.prototype = {}
BucketStoreMappingClient.prototype.getMapping = function(category,callback){
this.seqid += 1;
this._reqs[this.seqid] = callback;
this.send_getMapping(category)
}

BucketStoreMappingClient.prototype.send_getMapping = function(category){
var output = new this.pClass(this.output);
output.writeMessageBegin('getMapping', Thrift.MessageType.CALL, this.seqid)
var args = new BucketStoreMapping_getMapping_args()
args.category = category
args.write(output)
output.writeMessageEnd()
return this.output.flush()
}

BucketStoreMappingClient.prototype.recv_getMapping = function(input,mtype,rseqid){
var callback = this._reqs[rseqid] || function() {};
delete this._reqs[rseqid];
if (mtype == Thrift.MessageType.EXCEPTION) {
var x = new Thrift.TApplicationException()
x.read(input)
input.readMessageEnd()
return callback(x);
}
var result = new BucketStoreMapping_getMapping_result()
result.read(input)
input.readMessageEnd()

if (null != result.e) {
return callback(result.e);
}
if (null != result.success ) {
return callback(null, result.success);
}
return callback("getMapping failed: unknown result");
}
var BucketStoreMappingProcessor = exports.Processor = function(handler) {
this._handler = handler
}
BucketStoreMappingProcessor.prototype.process = function(input, output) {
var r = input.readMessageBegin()
if (this['process_' + r.fname]) {
return this['process_' + r.fname].call(this, r.rseqid, input, output)
} else {
input.skip(Thrift.Type.STRUCT)
input.readMessageEnd()
var x = new Thrift.TApplicationException(Thrift.TApplicationExceptionType.UNKNOWN_METHOD, 'Unknown function ' + r.fname)
output.writeMessageBegin(r.fname, Thrift.MessageType.Exception, r.rseqid)
x.write(output)
output.writeMessageEnd()
output.flush()
}
}

BucketStoreMappingProcessor.prototype.process_getMapping = function(seqid, input, output) {
var args = new BucketStoreMapping_getMapping_args()
args.read(input)
input.readMessageEnd()
var result = new BucketStoreMapping_getMapping_result()
this._handler.getMapping(args.category, function(success) {
result.success = success
output.writeMessageBegin("getMapping", Thrift.MessageType.REPLY, seqid)
result.write(output)
output.writeMessageEnd()
output.flush()
})
}

0 comments on commit da653de

Please sign in to comment.