Skip to content

Commit

Permalink
Added skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
christkv committed Feb 7, 2013
1 parent 4828454 commit 6e000ff
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -10,5 +10,7 @@ lib-cov
pids
logs
results
build/
node_modules/

npm-debug.log
17 changes: 17 additions & 0 deletions binding.gyp
@@ -0,0 +1,17 @@
{
'targets': [
{
'target_name': 'sync',
'sources': [ 'ext/sync.cc' ],
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
'conditions': [
['OS=="mac"', {
'xcode_settings': {
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES'
}
}]
]
}
]
}
76 changes: 76 additions & 0 deletions ext/sync.cc
@@ -0,0 +1,76 @@
#include <stdarg.h>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <stdlib.h>

#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
#endif

#include <v8.h>

// this and the above block must be around the v8.h header otherwise
// v8 is not happy
#ifdef __clang__
#pragma clang diagnostic pop
#endif

#include <node.h>
#include <node_version.h>
#include <node_buffer.h>

#include <cmath>
#include <iostream>
#include <limits>
#include <vector>

#ifdef __sun
#include <alloca.h>
#endif

#include "sync.h"

using namespace v8;
using namespace node;

Persistent<FunctionTemplate> Sync::constructor_template;


Sync::Sync() : ObjectWrap() {
}

void Sync::Initialize(v8::Handle<v8::Object> target) {
// Grab the scope of the call from Node
HandleScope scope;
// Define a new function template
Local<FunctionTemplate> t = FunctionTemplate::New(New);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("Sync"));

// Instance methods
NODE_SET_PROTOTYPE_METHOD(constructor_template, "execute", Execute);

// Set the class name
target->ForceSet(String::NewSymbol("Sync"), constructor_template->GetFunction());
}

// Create a new instance of BSON and passing it the existing context
Handle<Value> Sync::New(const Arguments &args) {
HandleScope scope;

Sync *sync = new Sync();
sync->Wrap(args.This());
return args.This();
}

Handle<Value> Sync::Execute(const Arguments &args)
{
HandleScope scope;

return scope.Close(Boolean::New(true));
}


42 changes: 42 additions & 0 deletions ext/sync.h
@@ -0,0 +1,42 @@
//===========================================================================

#ifndef SYNC_H_
#define SYNC_H_

//===========================================================================

#include <node.h>
#include <node_object_wrap.h>
#include <v8.h>

using namespace v8;
using namespace node;

//===========================================================================
class Sync : public ObjectWrap {
public:
Sync();
~Sync() {}

static void Initialize(Handle<Object> target);

static Handle<Value> Execute(const Arguments &args);

// Constructor used for creating new BSON objects from C++
static Persistent<FunctionTemplate> constructor_template;

private:
static Handle<Value> New(const Arguments &args);
};

#endif // SYNC_H_

//===========================================================================
// Exporting function
extern "C" void init(Handle<Object> target)
{
HandleScope scope;
Sync::Initialize(target);
}

NODE_MODULE(sync, Sync::Initialize);
25 changes: 25 additions & 0 deletions index.js
@@ -0,0 +1,25 @@
var ConsoleRepl = require("./lib/console_repl").ConsoleRepl;

var options = {
shell: false
, nodb: false
, norc: false
, quiet: false
, port: 27017
, host: "localhost"
, eval: null
, username: null
, password: null
, authenticationMechanism: null
, help: false
, version: false
, verbose: false
, ipv6: false
, ssl: false
, sslCAFile: null
, sslPEMKeyFile: null
, sslPEMKeyPassword: null
}

var console_repl = new ConsoleRepl(options);
console_repl.start();
56 changes: 56 additions & 0 deletions lib/console_repl.js
@@ -0,0 +1,56 @@
var repl = require('repl')
, format = require('util').format
, MongoClient = require('mongodb').MongoClient
, Db = require('./wrappers/db').Db
, Sync = require('../build/Release/sync').Sync;

console.dir(Sync);

ConsoleRepl = function ConsoleRepl(options) {
this.options = options;
console.log("====================== create")
this.sync = new Sync();
console.log("====================== create done")

// Split out the db name if specified
if(this.options.host.match(/\//) != null) {
this.options.db_name = this.options.host.split(/\//)[1];
this.options.host = this.options.host.split(/\//)[0];
} else {
this.options.db_name = "test";
}

// Create connection url
this.options.url = format("mongodb://%s:%s/%s"
, this.options.host
, this.options.port
, this.options.db_name);

// Global context
this.global = global;
}

ConsoleRepl.prototype.start = function() {
// Our script context
var scope = {};
var self = this;
// console.dir(this.options)

console.log("MongoDB shell version: 0.0.1 - node");
console.log(format("connecting to %s", this.options.db_name));

MongoClient.connect(this.options.url, function(err, db) {
if(err) throw err;

// Our db command
this.global.db = new Db(self.sync, db);

repl.start({
prompt: ">"
, global: true
})

});
}

exports.ConsoleRepl = ConsoleRepl;
22 changes: 22 additions & 0 deletions lib/wrappers/db.js
@@ -0,0 +1,22 @@
var Db = function(sync, db) {
this.sync = sync;
this.db = db;
}

Db.prototype.runCommand = function(command) {
var final_command = {};
if(typeof command == "string") {
final_command = {};
final_command[command] = true;
} else {
final_command = command;
}

console.log("=============================== running command")
console.dir(final_command)

// Execute the db command in sync mode
var result = this.sync.execute(db.executeDbCommand, final_command);
}

exports.Db = Db;
21 changes: 21 additions & 0 deletions package.json
@@ -0,0 +1,21 @@
{
"name": "repl",
"version": "0.0.0",
"description": "repl ====",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git://github.com/christkv/repl.git"
},
"dependencies": {
"mongodb": "latest"
, "exec-sync": "latest"
},
"author": "",
"license": "BSD",
"readmeFilename": "README.md",
"gitHead": "482845406fe069985937650a64026f7cd3fbfafa"
}

0 comments on commit 6e000ff

Please sign in to comment.