Skip to content

Commit

Permalink
implement to check write_buffer_size type
Browse files Browse the repository at this point in the history
  • Loading branch information
byplayer committed Mar 17, 2012
1 parent ffdec51 commit 40d37f9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
12 changes: 11 additions & 1 deletion ext/leveldb/leveldb.cc
Expand Up @@ -60,6 +60,16 @@ static void db_free(bound_db* db) {
delete db;
}

static bool check_uint_val(VALUE v, const char* const type_name_p) {
if(v == Qnil) {
return false;
} else if(FIXNUM_P(v)) {
return true;
} else {
rb_raise(rb_eTypeError, "invalid type for %s", type_name_p);
}
}

static void set_db_option(VALUE o_options, VALUE opts, leveldb::Options* options) {
if(!NIL_P(o_options)) {
Check_Type(opts, T_HASH);
Expand Down Expand Up @@ -91,7 +101,7 @@ static void set_db_option(VALUE o_options, VALUE opts, leveldb::Options* options
}

v = rb_hash_aref(opts, k_write_buffer_size);
if(FIXNUM_P(v)) {
if(check_uint_val(v, "write_buffer_size")) {
options->write_buffer_size = NUM2UINT(v);
rb_iv_set(o_options, "@write_buffer_size", v);
} else {
Expand Down
3 changes: 2 additions & 1 deletion lib/leveldb.rb
Expand Up @@ -85,7 +85,8 @@ class << self
end

class Options
attr_reader :block_cache_size, :paranoid_checks,
attr_reader :create_if_missing, :error_if_exists,
:block_cache_size, :paranoid_checks,
:write_buffer_size, :max_open_files,
:block_size, :block_restart_interval,
:compression
Expand Down
6 changes: 6 additions & 0 deletions test/db_options_test.rb
Expand Up @@ -53,6 +53,12 @@ def test_write_buffer_size
assert_equal db.options.write_buffer_size, (10 * 1042)
end

def test_write_buffer_size_raise
assert_raise TypeError, "invalid type for write_buffer_size" do
db = LevelDB::DB.new(@path, :write_buffer_size => "1234")
end
end

def test_max_open_files_default
db = LevelDB::DB.new(@path)
assert_equal db.options.max_open_files, 1000
Expand Down

0 comments on commit 40d37f9

Please sign in to comment.