Skip to content

Commit

Permalink
add methods.
Browse files Browse the repository at this point in the history
is_readonly?
is_open?
  • Loading branch information
isamu committed Jun 14, 2018
1 parent 2309fbd commit 8ae4194
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
20 changes: 18 additions & 2 deletions ext/rocksdb/rocksdb_db_rb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extern "C" {
rocksdb_pointer* db_pointer;
Data_Get_Struct(self, rocksdb_pointer, db_pointer);
db_pointer->db = NULL;

db_pointer->readonly = true;
Data_Get_Struct(self, rocksdb_pointer, db_pointer);

return Qtrue;
Expand Down Expand Up @@ -317,7 +317,23 @@ extern "C" {
VALUE rocksdb_db_debug(VALUE self){
return Qnil;
}

VALUE rocksdb_db_is_readonly(VALUE self){
rocksdb_pointer* db_pointer;
Data_Get_Struct(self, rocksdb_pointer, db_pointer);
if (db_pointer->readonly) {
return Qtrue;
}
if (!db_pointer->readonly) {
return Qfalse;
}
return Qnil;
}
VALUE rocksdb_db_is_open(VALUE self){
rocksdb_pointer* db_pointer;
Data_Get_Struct(self, rocksdb_pointer, db_pointer);
return (db_pointer->db == NULL) ? Qfalse : Qtrue;
}

VALUE rocksdb_db_compact(int argc, VALUE* argv, VALUE self) {
VALUE v_from, v_to;
rocksdb::Slice from, to;
Expand Down
2 changes: 2 additions & 0 deletions ext/rocksdb/rocksdb_db_rb.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ extern "C" {
VALUE rocksdb_db_exists(VALUE self, VALUE v_key);
VALUE rocksdb_db_close(VALUE self);
VALUE rocksdb_db_debug(VALUE self);
VALUE rocksdb_db_is_readonly(VALUE self);
VALUE rocksdb_db_is_open(VALUE self);
VALUE rocksdb_db_new_iterator(VALUE self);
VALUE rocksdb_db_each(VALUE self);
VALUE rocksdb_db_each_index(VALUE self);
Expand Down
2 changes: 2 additions & 0 deletions ext/rocksdb/rocksdb_rb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ extern "C" {
rb_define_method(cRocksdb_db, "debug", (METHOD)rocksdb_db_debug, 0);
rb_define_method(cRocksdb_db, "new_iterator", (METHOD)rocksdb_db_new_iterator, 0);
rb_define_method(cRocksdb_db, "compact", (METHOD)rocksdb_db_compact, -1);
rb_define_method(cRocksdb_db, "is_readonly?", (METHOD)rocksdb_db_is_readonly, 0);
rb_define_method(cRocksdb_db, "is_open?", (METHOD)rocksdb_db_is_open, 0);

rb_define_method(cRocksdb_db, "iterator", (METHOD)rocksdb_db_each, 0);
rb_define_method(cRocksdb_db, "each_index", (METHOD)rocksdb_db_each_index, 0);
Expand Down
2 changes: 0 additions & 2 deletions lib/rocksdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ def get_instance *args
def initialize *args
readonly = !!(args[1] && args[1][:readonly])
@key = args[0]

if !readonly and @@cache[@key]
__initialize2(*args)
raise DBError.new("error #{@key.to_s} alread open")
end

__initialize(*args)
unless readonly
@@cache[@key] = self
Expand Down
7 changes: 4 additions & 3 deletions spec/db_readonly_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
@rocksdb = RocksDB::DB.new "/tmp/file2"
@rocksdb.put("test:multi_db", "1")
@rocksdb.close

@rocksdb2 = RocksDB::DB.new "/tmp/file2", {:readonly => true}
@rocksdb2 = RocksDB::DB.new("/tmp/file2", {:readonly => true})
end

it 'should get data' do
expect{@rocksdb2.put("test:multi_db", "10")}.to raise_error(RuntimeError)
expect{@rocksdb2.delete("test:multi_db")}.to raise_error(RuntimeError)
expect(@rocksdb2.get("test:multi_db")).to eq "1"

expect(@rocksdb2.is_readonly?).to eq true

batch = RocksDB::Batch.new
batch.delete("test:batch1")
batch.put("test:batch2", "b")
Expand Down
37 changes: 29 additions & 8 deletions spec/db_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

describe RocksDB do
before do
@rocksdb = RocksDB::DB.new "/tmp/file"
@rocksdb = RocksDB::DB.new("/tmp/file")
end

it 'should get data' do
@rocksdb.put("test:read", "1")
expect(@rocksdb.is_readonly?).to eq false
expect(@rocksdb.get("test:read")).to eq "1"
end

Expand Down Expand Up @@ -50,8 +51,7 @@
end

it 'should use multiple db' do
@rocksdb2 = RocksDB::DB.new "/tmp/file2"

@rocksdb2 = RocksDB::DB.new("/tmp/file2")
@rocksdb.put("test:multi_db", "1")
@rocksdb2.put("test:multi_db", "2")

Expand Down Expand Up @@ -120,34 +120,55 @@
key = "test"
value = "1"

expect{RocksDB::DB.new "/tmp/file"}.to raise_error(RocksDB::DBError)
expect{RocksDB::DB.new("/tmp/file")}.to raise_error(RocksDB::DBError)

expect(@rocksdb.put("test:put", "1")).to be true

@rocksdb2 = RocksDB::DB.new "/tmp/file", {:readonly => true}
@rocksdb2 = RocksDB::DB.new("/tmp/file", {:readonly => true})
expect(@rocksdb2.is_readonly?).to eq true
expect(@rocksdb2.get("test:put")).to eq "1"

@rocksdb.close
@rocksdb = RocksDB::DB.new "/tmp/file"

expect(@rocksdb.is_open?).to eq false

@rocksdb = RocksDB::DB.new("/tmp/file")
expect(@rocksdb.is_readonly?).to eq false
expect(@rocksdb.is_open?).to eq true
expect(@rocksdb.put("test:put", "2")).to be true

@rocksdb3 = RocksDB::DB.new "/tmp/file", {:readonly => true}
@rocksdb3 = RocksDB::DB.new("/tmp/file", {:readonly => true})
expect(@rocksdb3.is_readonly?).to eq true
expect(@rocksdb3.is_open?).to eq true
expect(@rocksdb3.get("test:put")).to eq "2"

@rocksdb2.close
@rocksdb3.close

end

it 'singleton' do
@rocksdb2 = RocksDB::DB.get_instance("/tmp/file")
expect(@rocksdb2.is_readonly?).to eq false
expect(@rocksdb2.is_open?).to eq true

@rocksdb3 = RocksDB::DB.get_instance("/tmp/file")
expect(@rocksdb3.is_readonly?).to eq false
expect(@rocksdb).to eq (@rocksdb3)
expect(@rocksdb2).to eq (@rocksdb3)

@rocksdb4 = RocksDB::DB.get_instance("/tmp/file", {:readonly => true})
expect(@rocksdb2).not_to eq (@rocksdb4)

expect(@rocksdb4.is_readonly?).to eq true
expect(@rocksdb4.is_open?).to eq true

@rocksdb2.close
expect{@rocksdb2.get("test:put")}.to raise_error(RuntimeError)
expect(@rocksdb2.is_open?).to eq false
expect{@rocksdb3.get("test:put")}.to raise_error(RuntimeError)
expect(@rocksdb3.is_open?).to eq false

@rocksdb4.close
end

context 'compact' do
Expand Down

0 comments on commit 8ae4194

Please sign in to comment.