Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented openSync()

  • Loading branch information...
commit b46e2af2de72398805ef65344c6cab4dd859c30e 1 parent 1a258aa
@ulikoehler ulikoehler authored
Showing with 45 additions and 3 deletions.
  1. +20 −1 src/coffee/leveldb/handle.coffee
  2. +25 −2 src/cpp/handle.cc
View
21 src/coffee/leveldb/handle.coffee
@@ -46,6 +46,7 @@ noop = ->
###
+
exports.open = (path, options, callback) ->
# optional options
@@ -59,7 +60,25 @@ exports.open = (path, options, callback) ->
handle = self and new Handle self
callback err, handle
+###
+
+ Synchronous version of open
+
+###
+
+exports.openSync = (path, options) ->
+ # optional options
+ if typeof options is 'function'
+ callback = options
+ options = null
+
+ #openSync throws an exception on error
+ self = binding.openSync path, options
+ handle = self and new Handle self
+ return handle
+
+
###
Destroy a leveldb database.
@@ -107,7 +126,7 @@ exports.repair = (path, options, callback) ->
###
A handle represents an open leveldb database.
-
+hand
###
class Handle
View
27 src/cpp/handle.cc
@@ -165,8 +165,30 @@ class JHandle::OpenAsync : public OpAsync {
};
-
-
+static Handle<Value> openSync(const Arguments& args) {
+ if (args.Length() != 2 || !args[0]->IsString()) {
+ return ThrowTypeError("Invalid arguments");
+ }
+ //Process the arguments
+ std::string name_ = *String::Utf8Value(args[0]);
+ leveldb::Options options_;
+ Persistent<Value> comparator_;
+ UnpackOptions(args[1], options_, &comparator_);
+ //Open the database
+ leveldb::DB* db_;
+ leveldb::Status status_ = leveldb::DB::Open(options_, name_, &db_);
+ //Check if the database has been opened successfully
+ if (!status_.ok()) {
+ Handle<String> errMsg = String::New(status_.ToString().c_str());
+ return ThrowException(errMsg);
+ }
+ //Create and return the handle object
+ Handle<Value> handleArgs[] = { External::New(db_), Undefined() };
+ if (!comparator_.IsEmpty()) {
+ handleArgs[1] = comparator_;
+ }
+ return JHandle::constructor->GetFunction()->NewInstance(2, handleArgs);
+}
/**
@@ -589,6 +611,7 @@ void JHandle::Initialize(Handle<Object> target) {
// Static methods
NODE_SET_METHOD(target, "open", OpenAsync::Hook<OpenAsync>);
+ NODE_SET_METHOD(target, "openSync", openSync);
NODE_SET_METHOD(target, "destroy", OpenAsync::Hook<DestroyAsync>);
NODE_SET_METHOD(target, "repair", OpenAsync::Hook<RepairAsync>);
Please sign in to comment.
Something went wrong with that request. Please try again.