Skip to content

Commit

Permalink
batch() returns err on any null/undef key/value
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Mar 9, 2013
1 parent 932f3c3 commit cffda8e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
20 changes: 11 additions & 9 deletions src/database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ v8::Handle<v8::Value> Database::Put (const v8::Arguments& args) {

LD_METHOD_SETUP_COMMON(put, 2, 3)

LD_CB_ERR_IF_NULL_OR_UNDEFINED(0, Key)
LD_CB_ERR_IF_NULL_OR_UNDEFINED(1, Value)
LD_CB_ERR_IF_OPTION_NULL_OR_UNDEFINED(0, Key)
LD_CB_ERR_IF_OPTION_NULL_OR_UNDEFINED(1, Value)

v8::Local<v8::Value> keyBufferV = args[0];
v8::Local<v8::Value> valueBufferV = args[1];
Expand Down Expand Up @@ -248,7 +248,7 @@ v8::Handle<v8::Value> Database::Get (const v8::Arguments& args) {

LD_METHOD_SETUP_COMMON(put, 1, 2)

LD_CB_ERR_IF_NULL_OR_UNDEFINED(0, Key)
LD_CB_ERR_IF_OPTION_NULL_OR_UNDEFINED(0, Key)

v8::Local<v8::Value> keyBufferV = args[0];
LD_STRING_OR_BUFFER_TO_SLICE(key, keyBufferV, Key)
Expand Down Expand Up @@ -276,7 +276,7 @@ v8::Handle<v8::Value> Database::Delete (const v8::Arguments& args) {

LD_METHOD_SETUP_COMMON(put, 1, 2)

LD_CB_ERR_IF_NULL_OR_UNDEFINED(0, Key)
LD_CB_ERR_IF_OPTION_NULL_OR_UNDEFINED(0, Key)

v8::Local<v8::Value> keyBufferV = args[0];
LD_STRING_OR_BUFFER_TO_SLICE(key, keyBufferV, Key)
Expand Down Expand Up @@ -320,10 +320,11 @@ v8::Handle<v8::Value> Database::Batch (const v8::Arguments& args) {
continue;

v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(array->Get(i));
if (!obj->Has(str_type) || !obj->Has(str_key))
continue;

LD_CB_ERR_IF_NULL_OR_UNDEFINED(obj->Get(str_type), type)

v8::Local<v8::Value> keyBuffer = obj->Get(str_key);
LD_CB_ERR_IF_NULL_OR_UNDEFINED(keyBuffer, key)

if (obj->Get(str_type)->StrictEquals(str_del)) {
LD_STRING_OR_BUFFER_TO_SLICE(key, keyBuffer, Key)
Expand All @@ -332,8 +333,9 @@ v8::Handle<v8::Value> Database::Batch (const v8::Arguments& args) {
key
, v8::Persistent<v8::Value>::New(keyBuffer)
));
} else if (obj->Get(str_type)->StrictEquals(str_put) && obj->Has(str_value)) {
} else if (obj->Get(str_type)->StrictEquals(str_put)) {
v8::Local<v8::Value> valueBuffer = obj->Get(str_value);
LD_CB_ERR_IF_NULL_OR_UNDEFINED(valueBuffer, value)

LD_STRING_OR_BUFFER_TO_SLICE(key, keyBuffer, Key)
LD_STRING_OR_BUFFER_TO_SLICE(value, valueBuffer, Value)
Expand Down Expand Up @@ -377,8 +379,8 @@ v8::Handle<v8::Value> Database::ApproximateSize (const v8::Arguments& args) {

LD_METHOD_SETUP_COMMON(approximateSize, -1, 2)

LD_CB_ERR_IF_NULL_OR_UNDEFINED(0, Start)
LD_CB_ERR_IF_NULL_OR_UNDEFINED(1, End)
LD_CB_ERR_IF_OPTION_NULL_OR_UNDEFINED(0, Start)
LD_CB_ERR_IF_OPTION_NULL_OR_UNDEFINED(1, End)

LD_STRING_OR_BUFFER_TO_SLICE(start, startBufferV, Start)
LD_STRING_OR_BUFFER_TO_SLICE(end, endBufferV, End)
Expand Down
9 changes: 6 additions & 3 deletions src/leveldown.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@
#define LD_V8_METHOD(name) \
static v8::Handle<v8::Value> name (const v8::Arguments& args);

#define LD_CB_ERR_IF_NULL_OR_UNDEFINED(index, name) \
if (args[index]->IsNull() || args[index]->IsUndefined()) { \
#define LD_CB_ERR_IF_NULL_OR_UNDEFINED(thing, name) \
if (thing->IsNull() || thing->IsUndefined()) { \
v8::Local<v8::Value> argv[] = { \
v8::Local<v8::Value>::New(v8::Exception::Error( \
v8::String::New(#name " argument cannot be `null` or `undefined`")) \
v8::String::New(#name " cannot be `null` or `undefined`")) \
) \
}; \
LD_RUN_CALLBACK(callback, argv, 1); \
return v8::Undefined(); \
}

#define LD_CB_ERR_IF_OPTION_NULL_OR_UNDEFINED(index, name) \
LD_CB_ERR_IF_NULL_OR_UNDEFINED(args[index], name argument)

#define LD_FROM_V8_STRING(to, from) \
size_t to ## Sz_; \
char* to; \
Expand Down
26 changes: 17 additions & 9 deletions test/batch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,35 @@ test('test simple batch()', function (t) {

test('test batch() with missing `value`', function (t) {
db.batch([{ type: 'put', key: 'foo1' }], function (err) {
t.notOk(err, 'no error')
t.like(err.message, /value cannot be `null` or `undefined`/, 'correct error message')
t.end()
})
})

db.get('foo1', function (err, value) {
t.ok(err, 'entry not found')
t.notOk(value, 'value not returned')
t.like(err.message, /NotFound/)
t.end()
})
test('test batch() with null `value`', function (t) {
db.batch([{ type: 'put', key: 'foo1', value: null }], function (err) {
t.like(err.message, /value cannot be `null` or `undefined`/, 'correct error message')
t.end()
})
})

test('test batch() with missing `key`', function (t) {
db.batch([{ type: 'put', value: 'foo1' }], function (err) {
t.notOk(err, 'no error')
t.like(err.message, /key cannot be `null` or `undefined`/, 'correct error message')
t.end()
})
})

test('test batch() with null `key`', function (t) {
db.batch([{ type: 'put', key: null, value: 'foo1' }], function (err) {
t.like(err.message, /key cannot be `null` or `undefined`/, 'correct error message')
t.end()
})
})

test('test batch() with missing `key` and `value`', function (t) {
db.batch([{ type: 'put' }], function (err) {
t.notOk(err, 'no error')
t.like(err.message, /key cannot be `null` or `undefined`/, 'correct error message')
t.end()
})
})
Expand Down

0 comments on commit cffda8e

Please sign in to comment.