Permalink
Browse files

make compression options classes instead of enums

  • Loading branch information...
1 parent 8174fca commit 672259f9eba29484016b5342b7eb45a5024c6993 @wmorgan committed Jun 25, 2012
Showing with 35 additions and 51 deletions.
  1. +31 −44 ext/leveldb/leveldb.cc
  2. +1 −4 lib/leveldb.rb
  3. +3 −3 test/db_options_test.rb
View
@@ -19,6 +19,8 @@ static VALUE c_db;
static VALUE c_iter;
static VALUE c_batch;
static VALUE c_error;
+static VALUE c_no_compression;
+static VALUE c_snappy_compression;
static VALUE k_fill;
static VALUE k_verify;
static VALUE k_sync;
@@ -94,52 +96,32 @@ static void sync_vals(VALUE opts, VALUE key, VALUE db_options, int* pOptionVal)
}
static void set_db_option(VALUE o_options, VALUE opts, leveldb::Options* options) {
- if(!NIL_P(o_options)) {
- Check_Type(opts, T_HASH);
-
- sync_vals(opts, k_create_if_missing, o_options, &(options->create_if_missing));
- sync_vals(opts, k_error_if_exists, o_options, &(options->error_if_exists));
- sync_vals(opts, k_paranoid_checks, o_options, &(options->paranoid_checks));
- sync_vals(opts, k_write_buffer_size, o_options, &(options->write_buffer_size));
- sync_vals(opts, k_max_open_files, o_options, &(options->max_open_files));
- sync_vals(opts, k_block_size, o_options, &(options->block_size));
- sync_vals(opts, k_block_restart_interval, o_options, &(options->block_restart_interval));
-
- VALUE v;
- v = rb_hash_aref(opts, k_block_cache_size);
- if(!NIL_P(v)) {
- if(FIXNUM_P(v)) {
- options->block_cache = leveldb::NewLRUCache(NUM2INT(v));
- rb_iv_set(o_options, "@block_cache_size", v);
- } else {
- rb_raise(rb_eTypeError, "invalid type for %s", rb_id2name(SYM2ID(k_block_cache_size)));
- }
- }
+ if(NIL_P(o_options)) return;
+ Check_Type(opts, T_HASH);
+
+ sync_vals(opts, k_create_if_missing, o_options, &(options->create_if_missing));
+ sync_vals(opts, k_error_if_exists, o_options, &(options->error_if_exists));
+ sync_vals(opts, k_paranoid_checks, o_options, &(options->paranoid_checks));
+ sync_vals(opts, k_write_buffer_size, o_options, &(options->write_buffer_size));
+ sync_vals(opts, k_max_open_files, o_options, &(options->max_open_files));
+ sync_vals(opts, k_block_size, o_options, &(options->block_size));
+ sync_vals(opts, k_block_restart_interval, o_options, &(options->block_restart_interval));
+
+ VALUE v = rb_hash_aref(opts, k_block_cache_size);
+ if(!NIL_P(v)) {
+ options->block_cache = leveldb::NewLRUCache(NUM2INT(v));
+ rb_iv_set(o_options, "@block_cache_size", v);
+ }
- v = rb_hash_aref(opts, k_compression);
- rb_iv_set(o_options, "@compression", UINT2NUM(options->compression));
- if(!NIL_P(v)) {
- if(FIXNUM_P(v)) {
- switch(NUM2INT(v)) {
- case leveldb::kNoCompression:
- options->compression = leveldb::kNoCompression;
- rb_iv_set(o_options, "@compression", v);
- break;
-
- case leveldb::kSnappyCompression:
- options->compression = leveldb::kSnappyCompression;
- rb_iv_set(o_options, "@compression", v);
- break;
-
- default:
- rb_raise(rb_eTypeError, "invalid type for %s", rb_id2name(SYM2ID(k_compression)));
- break;
- }
- } else {
- rb_raise(rb_eTypeError, "invalid type for %s", rb_id2name(SYM2ID(k_compression)));
- }
- }
+ v = rb_hash_aref(opts, k_compression);
+ if(!NIL_P(v)) {
+ if(v == c_no_compression) options->compression = leveldb::kNoCompression;
+ else if(v == c_snappy_compression) options->compression = leveldb::kSnappyCompression;
+ else rb_raise(rb_eTypeError, "invalid type for %s", rb_id2name(SYM2ID(k_compression)));
}
+
+ if(options->compression == leveldb::kNoCompression) rb_iv_set(o_options, "@compression", c_no_compression);
+ else if(options->compression == leveldb::kSnappyCompression) rb_iv_set(o_options, "@compression", c_snappy_compression);
}
/*
@@ -713,6 +695,11 @@ void Init_leveldb() {
c_db_options = rb_define_class_under(m_leveldb, "Options", rb_cObject);
+ VALUE m_ctype = rb_define_module_under(m_leveldb, "CompressionType");
+ VALUE c_base = rb_define_class_under(m_ctype, "Base", rb_cObject);
+ c_no_compression = rb_define_class_under(m_ctype, "NoCompression", c_base);
+ c_snappy_compression = rb_define_class_under(m_ctype, "SnappyCompression", c_base);
+
c_error = rb_define_class_under(m_leveldb, "Error", rb_eStandardError);
}
}
View
@@ -90,6 +90,7 @@ class Options
DEFAULT_WRITE_BUFFER_SIZE = 4 * 1024 * 1024
DEFAULT_BLOCK_SIZE = 4 * 1024
DEFAULT_BLOCK_RESTART_INTERVAL = 16
+ DEFAULT_COMPRESSION = LevelDB::CompressionType::SnappyCompression
attr_reader :create_if_missing, :error_if_exists,
:block_cache_size, :paranoid_checks,
@@ -98,8 +99,4 @@ class Options
:compression
end
-module CompressionType
- NoCompression = 0x0
- SnappyCompression = 0x1
-end
end # module LevelDB
View
@@ -112,7 +112,7 @@ def test_block_restart_interval_default
def test_block_restart_interval
db = LevelDB::DB.new @path, :block_restart_interval => 32
- assert_equal db.options.block_restart_interval, 32
+ assert_equal 32, db.options.block_restart_interval
end
def test_block_restart_interval_invalid
@@ -121,12 +121,12 @@ def test_block_restart_interval_invalid
def test_compression_default
db = LevelDB::DB.new @path
- assert_equal db.options.compression, LevelDB::CompressionType::SnappyCompression
+ assert_equal LevelDB::Options::DEFAULT_COMPRESSION, db.options.compression
end
def test_compression
db = LevelDB::DB.new @path, :compression => LevelDB::CompressionType::NoCompression
- assert_equal db.options.compression, LevelDB::CompressionType::NoCompression
+ assert_equal LevelDB::CompressionType::NoCompression, db.options.compression
end
def test_compression_invalid_type

0 comments on commit 672259f

Please sign in to comment.