[Pkg-javascript-commits] [node-leveldown] 269/492: initial draft
Andrew Kelley
andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:14:07 UTC 2014
This is an automated email from the git hooks/post-receive script.
andrewrk-guest pushed a commit to annotated tag rocksdb-0.10.1
in repository node-leveldown.
commit 0539ee81abb3d254bf3792f11a07c10c5901f3d1
Author: Julian Gruber <julian at juliangruber.com>
Date: Thu Mar 7 19:39:14 2013 +0100
initial draft
---
binding.gyp | 1 +
index.js | 5 ++-
src/cbatch.cc | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++
src/cbatch.h | 27 ++++++++++++++
src/database.cc | 59 +++++++++++++++++++++++++++++
src/database.h | 12 ++++++
src/leveldown.cc | 2 +
test/cbatch-test.js | 27 ++++++++++++++
8 files changed, 236 insertions(+), 1 deletion(-)
diff --git a/binding.gyp b/binding.gyp
index fa528ff..5c6fe2a 100644
--- a/binding.gyp
+++ b/binding.gyp
@@ -22,6 +22,7 @@
, "sources": [
"src/async.cc"
, "src/batch.cc"
+ , "src/cbatch.cc"
, "src/database.cc"
, "src/database_async.cc"
, "src/iterator.cc"
diff --git a/index.js b/index.js
index 9193802..be43850 100644
--- a/index.js
+++ b/index.js
@@ -1 +1,4 @@
-module.exports = require('bindings')('leveldown.node').leveldown
\ No newline at end of file
+var binding = require('bindings')('leveldown.node')
+var leveldown = binding.leveldown
+leveldown.CBatch = binding.CBatch
+module.exports = leveldown
diff --git a/src/cbatch.cc b/src/cbatch.cc
new file mode 100644
index 0000000..798fd34
--- /dev/null
+++ b/src/cbatch.cc
@@ -0,0 +1,104 @@
+#include "cbatch.h"
+
+using namespace v8;
+using namespace node;
+
+Persistent<FunctionTemplate> CBatch::constructor;
+
+void CBatch::Initialize(v8::Handle<v8::Object> target) {
+ HandleScope scope;
+
+ Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
+
+ constructor = Persistent<FunctionTemplate>::New(tpl);
+ constructor->InstanceTemplate()->SetInternalFieldCount(1);
+ constructor->SetClassName(String::NewSymbol("CBatch"));
+
+ SetPrototypeMethod(constructor, "put", Put);
+ SetPrototypeMethod(constructor, "del", Del);
+ SetPrototypeMethod(constructor, "clear", Clear);
+
+ target->Set(String::NewSymbol("CBatch"), constructor->GetFunction());
+}
+
+Handle<Value> CBatch::New(const Arguments& args) {
+ HandleScope scope;
+
+ CBatch *wrapper = new CBatch();
+ wrapper->Wrap(args.Holder());
+ return scope.Close(args.Holder());
+}
+
+/**
+ * Put
+ *
+ * @param {string} key
+ * @param {string} val
+ * @returns {object} CBatch
+ */
+
+Handle<Value> CBatch::Put(const Arguments& args) {
+ HandleScope scope;
+ CBatch* self = ObjectWrap::Unwrap<CBatch>(args.Holder());
+
+ if (args.Length() < 2) {
+ ThrowException(Exception::Error(String::New("Key and Value required")));
+ return scope.Close(Undefined());
+ }
+ if (!args[1]->IsString()) {
+ ThrowException(Exception::Error(String::New("Only strings as value")));
+ return scope.Close(Undefined());
+ }
+
+ String::Utf8Value key(args[0]->ToString());
+ String::Utf8Value val(args[1]->ToString());
+
+ self->batch.Put(*key, *val);
+
+ return scope.Close(args.Holder());
+}
+
+/**
+ * Delete
+ *
+ * @param {string} key
+ * @returns {object} CBatch
+ */
+
+Handle<Value> CBatch::Del(const Arguments& args) {
+ HandleScope scope;
+ CBatch* self = ObjectWrap::Unwrap<CBatch>(args.Holder());
+
+ if (args.Length() == 0) {
+ ThrowException(Exception::Error(String::New("key required")));
+ return scope.Close(Undefined());
+ }
+
+ String::Utf8Value key(args[0]->ToString());
+ self->batch.Delete(*key);
+
+ return scope.Close(args.Holder());
+}
+
+/**
+ * Clear
+ *
+ * @returns {object} CBatch
+ */
+
+Handle<Value> CBatch::Clear(const Arguments& args) {
+ HandleScope scope;
+ CBatch* self = ObjectWrap::Unwrap<CBatch>(args.Holder());
+
+ self->batch.Clear();
+
+ return scope.Close(args.Holder());
+}
+
+/**
+ * CBatch constructor
+ */
+
+CBatch::CBatch() {}
+
+CBatch::~CBatch() {}
diff --git a/src/cbatch.h b/src/cbatch.h
new file mode 100644
index 0000000..413dcb5
--- /dev/null
+++ b/src/cbatch.h
@@ -0,0 +1,27 @@
+#ifndef BATCH
+#define BATCH
+
+#include <node.h>
+#include <leveldb/write_batch.h>
+
+using namespace v8;
+using namespace node;
+
+class CBatch : public ObjectWrap {
+public:
+ static void Initialize(v8::Handle<v8::Object> target);
+ leveldb::WriteBatch batch;
+private:
+ CBatch();
+ ~CBatch();
+ static Persistent<FunctionTemplate> constructor;
+
+ static Handle<Value> New(const Arguments &args);
+
+ static Handle<Value> Put(const Arguments &args);
+ static Handle<Value> Del(const Arguments &args);
+ static Handle<Value> Clear(const Arguments &args);
+
+};
+
+#endif
diff --git a/src/database.cc b/src/database.cc
index 59b9002..702d177 100644
--- a/src/database.cc
+++ b/src/database.cc
@@ -14,6 +14,7 @@
#include "async.h"
#include "database_async.h"
#include "batch.h"
+#include "cbatch.h"
#include "iterator.h"
namespace leveldown {
@@ -136,6 +137,10 @@ void Database::Init () {
v8::String::NewSymbol("iterator")
, v8::FunctionTemplate::New(Iterator)->GetFunction()
);
+ tpl->PrototypeTemplate()->Set(
+ v8::String::NewSymbol("write")
+ , v8::FunctionTemplate::New(Write)->GetFunction()
+ );
constructor = v8::Persistent<v8::Function>::New(tpl->GetFunction());
}
@@ -242,6 +247,59 @@ v8::Handle<v8::Value> Database::Put (const v8::Arguments& args) {
return v8::Undefined();
}
+Handle<Value> Database::Write(const Arguments& args) {
+ HandleScope scope;
+
+ Database* database = ObjectWrap::Unwrap<Database>(args.This());
+
+ if (args.Length() < 1) {
+ ThrowException(Exception::Error(String::New("batch required")));
+ return scope.Close(Undefined());
+ }
+
+ WriteParams *params = new WriteParams();
+ params->request.data = params;
+ params->db = database->db;
+ params->cb = Persistent<Function>::New(Local<Function>::Cast(args[1]));
+
+ params->batch = ObjectWrap::Unwrap<CBatch>(args[0]->ToObject());
+
+ uv_queue_work(uv_default_loop(), ¶ms->request, WriteDoing, WriteAfter);
+
+ return scope.Close(args.Holder());
+}
+
+void Database::WriteDoing (uv_work_t *req) {
+ WriteParams *params = (WriteParams *)req->data;
+ leveldb::WriteBatch wb = params->batch->batch;
+
+ params->status = params->db->Write(leveldb::WriteOptions(), &wb);
+}
+
+void Database::WriteAfter (uv_work_t *req) {
+ HandleScope scope;
+ WriteParams *params = (WriteParams *)req->data;
+
+ Handle<Value> argv[1];
+
+ if (params->status.ok()) {
+ argv[0] = Local<Value>::New(Undefined());
+ } else {
+ argv[0] = Local<Value>::New(String::New(params->status.ToString().data()));
+ }
+
+ if (params->cb->IsFunction()) {
+ TryCatch try_catch;
+ params->cb->Call(Context::GetCurrent()->Global(), 1, argv);
+ if (try_catch.HasCaught()) {
+ FatalException(try_catch);
+ }
+ }
+
+ params->cb.Dispose();
+ delete params;
+ scope.Close(Undefined());
+}
v8::Handle<v8::Value> Database::Get (const v8::Arguments& args) {
v8::HandleScope scope;
@@ -413,4 +471,5 @@ v8::Handle<v8::Value> Database::Iterator (const v8::Arguments& args) {
return scope.Close(Iterator::NewInstance(args.This(), optionsObj));
}
+
} // namespace leveldown
diff --git a/src/database.h b/src/database.h
index 67f1e2c..257b172 100644
--- a/src/database.h
+++ b/src/database.h
@@ -11,6 +11,7 @@
#include "leveldb/db.h"
#include "leveldown.h"
+#include "cbatch.h"
namespace leveldown {
@@ -65,6 +66,8 @@ private:
char* location;
static v8::Persistent<v8::Function> constructor;
+ static void WriteDoing(uv_work_t *req);
+ static void WriteAfter(uv_work_t *req);
LD_V8_METHOD( New )
LD_V8_METHOD( Open )
@@ -73,10 +76,19 @@ private:
LD_V8_METHOD( Delete )
LD_V8_METHOD( Get )
LD_V8_METHOD( Batch )
+ LD_V8_METHOD( Write )
LD_V8_METHOD( Iterator )
LD_V8_METHOD( ApproximateSize )
};
+struct WriteParams {
+ leveldb::DB* db;
+ CBatch* batch;
+ Persistent<Function> cb;
+ leveldb::Status status;
+ uv_work_t request;
+};
+
} // namespace leveldown
#endif
diff --git a/src/leveldown.cc b/src/leveldown.cc
index 653855b..a2b26d1 100644
--- a/src/leveldown.cc
+++ b/src/leveldown.cc
@@ -8,12 +8,14 @@
#include "leveldown.h"
#include "database.h"
#include "iterator.h"
+#include "cbatch.h"
namespace leveldown {
void Init (v8::Handle<v8::Object> target) {
Database::Init();
leveldown::Iterator::Init();
+ CBatch::Initialize(target);
target->Set(v8::String::NewSymbol("leveldown")
, v8::FunctionTemplate::New(LevelDOWN)->GetFunction());
diff --git a/test/cbatch-test.js b/test/cbatch-test.js
new file mode 100644
index 0000000..1dd05f1
--- /dev/null
+++ b/test/cbatch-test.js
@@ -0,0 +1,27 @@
+const test = require('tap').test
+ , testCommon = require('./common')
+ , leveldown = require('../')
+
+var db
+
+test('setUp', function (t) {
+ db = leveldown(testCommon.location())
+ db.open(testCommon.setUp.bind(null, t))
+})
+
+test('writes', function (t) {
+ var batch = new leveldown.CBatch()
+ batch.put('foo', 'bar')
+ db.write(batch, function (err) {
+ t.notOk(err, 'no error')
+ db.get('foo', function (err, value) {
+ t.notOk(err, 'no error')
+ t.equal(value.toString(), 'bar')
+ t.end()
+ })
+ })
+})
+
+test('tearDown', function (t) {
+ db.close(testCommon.tearDown.bind(null, t))
+})
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-leveldown.git
More information about the Pkg-javascript-commits
mailing list