Permalink
Browse files

use messagepack as output format

  • Loading branch information...
1 parent f3f2904 commit accd781b3db2491b5ba90124d8a2253c1fed9b04 @darashi darashi committed Jan 16, 2012
Showing with 48 additions and 25 deletions.
  1. +20 −4 lib/nroonga.js
  2. +1 −0 package.json
  3. +13 −17 src/nroonga.cc
  4. +14 −4 src/nroonga.coffee
View
@@ -1,8 +1,10 @@
(function() {
- var nroonga, optionsToCommandString;
+ var msgpack, nroonga, optionsToCommandString, overrideOutputType;
nroonga = module.exports = require('./nroonga_bindings.node');
+ msgpack = require('msgpack2');
+
optionsToCommandString = function(command, options) {
var args, key, value;
args = [command];
@@ -16,11 +18,24 @@
return args.join(' ');
};
+ overrideOutputType = function(optionsGiven, type) {
+ var key, options, value;
+ options = {};
+ for (key in optionsGiven) {
+ value = optionsGiven[key];
+ options[key] = value;
+ }
+ options.output_type = type;
+ return options;
+ };
+
nroonga.Database.prototype.commandSync = function(command, options) {
var result;
+ if (options == null) options = {};
+ options = overrideOutputType(options, 'msgpack');
result = this.commandSyncString(optionsToCommandString(command, options));
if (result.length > 0) {
- return JSON.parse(result);
+ return msgpack.unpack(result);
} else {
return;
}
@@ -30,13 +45,14 @@
var wrappedCallback;
if (arguments.length === 2) {
callback = options;
- options = void 0;
+ options = {};
}
+ options = overrideOutputType(options, 'msgpack');
wrappedCallback = callback != null ? function(error, data) {
if (error != null) {
return callback(error);
} else {
- return callback(void 0, JSON.parse(data));
+ return callback(void 0, msgpack.unpack(data));
}
} : void 0;
return this.commandString(optionsToCommandString(command, options), wrappedCallback);
View
@@ -18,6 +18,7 @@
"expresso":""
},
"dependencies": {
+ "msgpack2":""
},
"scripts": {
"test": "expresso",
View
@@ -1,5 +1,6 @@
#include <v8.h>
#include <node.h>
+#include <node_buffer.h>
#include <groonga.h>
#include "nroonga.h"
@@ -86,25 +87,20 @@ void Database::CommandWork(uv_work_t* req) {
void Database::CommandAfter(uv_work_t* req) {
HandleScope scope;
Baton* baton = static_cast<Baton*>(req->data);
+ Handle<Value> argv[2];
if (baton->error) {
- Local<Value> error = Exception::Error(String::New(baton->context.errbuf));
- Local<Value> argv[] = { error };
- TryCatch try_catch;
- baton->callback->Call(v8::Context::GetCurrent()->Global(), 1, argv);
- if (try_catch.HasCaught()) {
- node::FatalException(try_catch);
- }
+ argv[0] = Exception::Error(String::New(baton->context.errbuf));
+ argv[1] = Null();
} else {
- Local<Value> argv[] = {
- Local<Value>::New(Undefined()),
- Local<Value>::New(String::New(baton->result, baton->result_length))
- };
- TryCatch try_catch;
- baton->callback->Call(v8::Context::GetCurrent()->Global(), 2, argv);
- if (try_catch.HasCaught()) {
- node::FatalException(try_catch);
- }
+ argv[0] = Null();
+ argv[1] = Buffer::New(baton->result, baton->result_length)->handle_;
}
+ TryCatch try_catch;
+ baton->callback->Call(v8::Context::GetCurrent()->Global(), 2, argv);
+ if (try_catch.HasCaught()) {
+ node::FatalException(try_catch);
+ }
+
grn_ctx_fin(&baton->context);
baton->callback.Dispose();
delete baton;
@@ -169,7 +165,7 @@ Handle<Value> Database::CommandSyncString(const Arguments& args) {
return ThrowException(Exception::Error(String::New("grn_ctx_recv returned error")));
}
- return scope.Close(String::New(result, result_length));
+ return scope.Close(Buffer::New(result, result_length)->handle_);
}
void InitNroonga(Handle<Object> target) {
View
@@ -1,4 +1,5 @@
nroonga = module.exports = require('./nroonga_bindings.node')
+msgpack = require('msgpack2')
optionsToCommandString = (command, options) ->
args = [command]
@@ -8,24 +9,33 @@ optionsToCommandString = (command, options) ->
args.push JSON.stringify(value)
args.join(' ')
-nroonga.Database.prototype.commandSync = (command, options) ->
+overrideOutputType = (optionsGiven, type) ->
+ options = {}
+ for key, value of optionsGiven
+ options[key] = value
+ options.output_type = type
+ options
+
+nroonga.Database.prototype.commandSync = (command, options={}) ->
+ options = overrideOutputType(options, 'msgpack')
result = this.commandSyncString(optionsToCommandString(command, options))
if result.length > 0
- JSON.parse(result)
+ msgpack.unpack(result)
else
undefined
nroonga.Database.prototype.command = (command, options, callback) ->
if arguments.length == 2
callback = options
- options = undefined
+ options = {}
+ options = overrideOutputType(options, 'msgpack')
wrappedCallback = if callback?
(error, data) ->
if error?
callback error
else
- callback undefined, JSON.parse(data)
+ callback undefined, msgpack.unpack(data)
else
undefined

0 comments on commit accd781

Please sign in to comment.