Skip to content

Commit

Permalink
DB#initialize(readonly)
Browse files Browse the repository at this point in the history
  • Loading branch information
maiha committed Dec 4, 2016
1 parent 0ab9d61 commit e1e809d
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 6 deletions.
6 changes: 3 additions & 3 deletions API.md
@@ -1,12 +1,12 @@
# Supported API
## Implemented 8% (22/259)
### Database (2 / 24)
## Implemented 8% (23/259)
### Database (3 / 24)

|Command |impl|test|note|
|-----------------------------------------------------|:--:|:--:|----|
|`rocksdb_open` ||| |
|`rocksdb_close` ||| |
|`rocksdb_open_for_read_only` | | | |
|`rocksdb_open_for_read_only` | | | |
|`rocksdb_backup_engine_open` | | | |
|`rocksdb_backup_engine_create_new_backup` | | | |
|`rocksdb_restore_options_create` | | | |
Expand Down
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -39,6 +39,14 @@ db.get!("foo") # raise RocksDB::NotFound.new("foo")
db.close
```

### readonly mode

```crystal
db = RocksDB::DB.new("tmp/db1", readonly: true)
# or RocksDB::DB.read("tmp/db1")
db.put("foo", "1") # raise RocksDB::Error("Not supported operation in read only mode.")
```

### Iterations

```shell
Expand Down Expand Up @@ -94,6 +102,7 @@ db.get("\u{0}") # => "\t"
#### 0.4.0
- [x] readonly mode
- [ ] Iterations for Binary
## Testing
Expand Down
1 change: 1 addition & 0 deletions doc/api/impl
Expand Up @@ -12,6 +12,7 @@ rocksdb_iter_seek_to_last
rocksdb_iter_valid
rocksdb_iter_value
rocksdb_open
rocksdb_open_for_read_only
rocksdb_options_create
rocksdb_options_destroy
rocksdb_put
Expand Down
1 change: 1 addition & 0 deletions doc/api/test
Expand Up @@ -15,4 +15,5 @@ rocksdb_iter_seek_to_last
rocksdb_iter_valid
rocksdb_iter_value
rocksdb_open
rocksdb_open_for_read_only
rocksdb_put
60 changes: 60 additions & 0 deletions spec/readonly_spec.cr
@@ -0,0 +1,60 @@
require "./spec_helper"

describe RocksDB::DB do
path = "tmp/db_readonly"
invalid_path = "tmp/xxx"

it "(setup)" do
db = RocksDB::DB.new(path)
db.put("key", "1")
db.close
end

describe ".new(readonly: true)" do
it "can read data" do
db = RocksDB::DB.new(path, readonly: true)
db.get("key").should eq("1")
db.keys.should eq(["key"])
db.close
end

it "can't write data" do
db = RocksDB::DB.new(path, readonly: true)
expect_raises(RocksDB::Error, /Not supported operation in read only mode/) do
db.put("key", "10")
end
db.close
end

it "can't open when file not found" do
expect_raises(RocksDB::Error, /No such file or directory/) do
RocksDB::DB.new(invalid_path, readonly: true)
end
end
end

describe "read" do
it "can read data" do
db = RocksDB::DB.read(path)
db.get("key").should eq("1")
db.keys.should eq(["key"])
db.close
end

it "can't write data" do
db = RocksDB::DB.read(path)
expect_raises(RocksDB::Error, /Not supported operation in read only mode/) do
db.put("key", "10")
end
db.close
end

it "can't open when file not found" do
expect_raises(RocksDB::Error, /No such file or directory/) do
RocksDB::DB.read(invalid_path)
end
end
end

it "#rocksdb_open_for_read_only" do; end
end
3 changes: 2 additions & 1 deletion src/rocksdb/api.cr
Expand Up @@ -13,7 +13,7 @@ end
private macro try(name)
def {{name.id}}(*args)
LibRocksDB.{{name.id}}(*args, @err).tap {
raise "ERR: {{name}} #{String.new(@err.value)}" if !@err.value.null?
raise RocksDB::Error.new("ERR: {{name}} #{String.new(@err.value)}") if !@err.value.null?
}
end
end
Expand All @@ -22,6 +22,7 @@ module RocksDB::Api
### Basic

try rocksdb_open
try rocksdb_open_for_read_only
api rocksdb_close
try rocksdb_get
try rocksdb_put
Expand Down
13 changes: 11 additions & 2 deletions src/rocksdb/db.cr
Expand Up @@ -11,7 +11,11 @@ class RocksDB::DB
getter! read_options
getter! write_options

def initialize(@path : String, options : Options? = nil, read_options : ReadOptions? = nil, write_options : WriteOptions? = nil)
def self.read(path : String, options : Options? = nil, read_options : ReadOptions? = nil, error_if_log_file_exist : Bool = false)
new(path, options: options, read_options: read_options, readonly: true, error_if_log_file_exist: error_if_log_file_exist)
end

def initialize(@path : String, options : Options? = nil, read_options : ReadOptions? = nil, write_options : WriteOptions? = nil, readonly : Bool = false, error_if_log_file_exist : Bool = false)
@closables = [] of Closable

@options = options || mark_closable(Options.new.set_create_if_missing(1))
Expand All @@ -20,7 +24,12 @@ class RocksDB::DB

@len = Pointer(UInt64).malloc(1_u64)
@err = Pointer(Pointer(UInt8)).malloc(1_u64)
@raw = rocksdb_open(@options.raw, @path)
if readonly
flag = error_if_log_file_exist ? 1_u8 : 0_u8
@raw = rocksdb_open_for_read_only(@options.raw, @path, flag)
else
@raw = rocksdb_open(@options.raw, @path)
end
@opened = true
end

Expand Down

0 comments on commit e1e809d

Please sign in to comment.