Skip to content

Commit

Permalink
DB#each (String only)
Browse files Browse the repository at this point in the history
  • Loading branch information
maiha committed Dec 4, 2016
1 parent 4cda6c4 commit a6e097a
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 60 deletions.
24 changes: 12 additions & 12 deletions API.md
@@ -1,5 +1,5 @@
# Supported API
## Implemented 7% (20/259)
## Implemented 8% (22/259)
### Database (2 / 24)

|Command |impl|test|note|
Expand Down Expand Up @@ -47,21 +47,21 @@
|`rocksdb_multi_get` | | | |
|`rocksdb_multi_get_cf`| | | |

### Iteration (8 / 12)
### Iteration (10 / 12)

|Command |impl|test|note|
|----------------------------|:--:|:--:|----|
|`rocksdb_create_iterator` || | |
|`rocksdb_create_iterator` || | |
|`rocksdb_create_iterator_cf`| | | |
|`rocksdb_iter_destroy` || | |
|`rocksdb_iter_valid` || | |
|`rocksdb_iter_seek_to_first`|| | |
|`rocksdb_iter_seek_to_last` || | |
|`rocksdb_iter_seek` | | | |
|`rocksdb_iter_next` || | |
|`rocksdb_iter_prev` || | |
|`rocksdb_iter_key` || | |
|`rocksdb_iter_value` | | | |
|`rocksdb_iter_destroy` || | |
|`rocksdb_iter_valid` || | |
|`rocksdb_iter_seek_to_first`|| | |
|`rocksdb_iter_seek_to_last` || | |
|`rocksdb_iter_seek` | | | |
|`rocksdb_iter_next` || | |
|`rocksdb_iter_prev` || | |
|`rocksdb_iter_key` || | |
|`rocksdb_iter_value` | | | |
|`rocksdb_iter_get_error` | | | |

### Snapshots (0 / 15)
Expand Down
2 changes: 1 addition & 1 deletion Makefile
@@ -1,6 +1,6 @@
API_IMPLS := src/rocksdb/api.cr
OPT_IMPLS := src/rocksdb/options.cr
CMD_TESTS := $(shell find spec/commands -name '*.cr')
CMD_TESTS := $(shell find spec -name '*_spec.cr')
API_FILES := $(shell find doc/api -name '*.*')

.PHONY : all
Expand Down
46 changes: 41 additions & 5 deletions README.md
Expand Up @@ -36,12 +36,44 @@ db.get("foo") # => ""
db.get?("foo") # => nil
db.get!("foo") # raise RocksDB::NotFound.new("foo")
db.keys # => ["k1","k2","k3",...]
db.keys(2) # => ["k1","k2"]
db.close
```

### Iterations

```shell
3.times{|i| db.put("k#{i}", i) }
db.keys # => ["k0","k1","k2"]
db.keys(2) # => ["k0","k1"]

db.each do |(k,v)|
```

### Iterator

```shell
iter = db.new_iterator
iter.key # => "k0"
iter.next
iter.value # => "1"
iter.first
iter.key # => "k0"
iter.seek("k2")
iter.next
iter.valid? # => false
```

- same as `each`

```shell
iter = db.new_iterator
iter.first
while (iter.valid?)
# yield {iter.key, iter.value}
iter.next
end
```
### binary data
Although all data are stored as Binary in RocksDB,
Expand All @@ -57,8 +89,12 @@ db.get("\u{0}") # => "\t"
#### 0.3.0
- [x] `keys`
- [ ] `each`
- [x] `keys` (String)
- [x] `each` (String)
#### 0.4.0
- [ ] Iterations for Binary
## Testing
Expand Down
2 changes: 2 additions & 0 deletions doc/api/impl
Expand Up @@ -6,9 +6,11 @@ rocksdb_iter_destroy
rocksdb_iter_key
rocksdb_iter_next
rocksdb_iter_prev
rocksdb_iter_seek
rocksdb_iter_seek_to_first
rocksdb_iter_seek_to_last
rocksdb_iter_valid
rocksdb_iter_value
rocksdb_open
rocksdb_options_create
rocksdb_options_destroy
Expand Down
11 changes: 11 additions & 0 deletions doc/api/test
@@ -1,7 +1,18 @@
each
keys
new_iterator
rocksdb_close
rocksdb_create_iterator
rocksdb_delete
rocksdb_get
rocksdb_iter_destroy
rocksdb_iter_key
rocksdb_iter_next
rocksdb_iter_prev
rocksdb_iter_seek
rocksdb_iter_seek_to_first
rocksdb_iter_seek_to_last
rocksdb_iter_valid
rocksdb_iter_value
rocksdb_open
rocksdb_put
23 changes: 15 additions & 8 deletions spec/commands/iteration_spec.cr
@@ -1,17 +1,17 @@
require "../spec_helper"

describe "Iterations" do
path = "tmp/db_iter"
path = "tmp/db_iteration"

it "(setup)" do
db = RocksDB::DB.new(path)
5.times{|i| db.put("k#{i}", i) }
3.times{|i| db.put("k#{i}", i) }
db.close
end

it "#keys" do
db = RocksDB::DB.new(path)
db.keys.should eq(%w( k0 k1 k2 k3 k4 ))
db.keys.should eq(%w( k0 k1 k2 ))
db.close
end

Expand All @@ -23,12 +23,19 @@ describe "Iterations" do

it "#each" do
db = RocksDB::DB.new(path)
items = [] of Tuple(String, String)
db.each do |k,v|
items << {k, v}
end
items.should eq([{"k0", "0"}, {"k1", "1"}, {"k2", "2"}])
db.close
end

# items = [] of Tuple(String, String)
# db.each do |k,v|
# items << {k, v}
# end
it "#new_iterator" do
db = RocksDB::DB.new(path)
iter = db.new_iterator
iter.should be_a(RocksDB::Iterator)
iter.close
db.close
end
end
61 changes: 61 additions & 0 deletions spec/iterator_spec.cr
@@ -0,0 +1,61 @@
require "./spec_helper"

describe "Iterator" do
path = "tmp/db_iteration"

it "work" do
db = RocksDB::DB.new(path)
3.times{|i| db.put("k#{i}", i) }
iter = RocksDB::Iterator.new(db)

iter.valid?.should be_false

iter.first
iter.valid?.should be_true
iter.key.should eq("k0")
iter.value.should eq("0")

iter.next
iter.key.should eq("k1")
iter.value.should eq("1")

iter.next
iter.key.should eq("k2")
iter.value.should eq("2")

iter.prev
iter.key.should eq("k1")
iter.value.should eq("1")

iter.first
iter.key.should eq("k0")
iter.value.should eq("0")

iter.last
iter.key.should eq("k2")
iter.value.should eq("2")

iter.next
iter.valid?.should be_false

iter.seek("k1")
iter.valid?.should be_true
iter.key.should eq("k1")

iter.seek("xx")
iter.valid?.should be_false

db.close
end

it "#rocksdb_create_iterator" do ; end
it "#rocksdb_iter_destroy" do ; end
it "#rocksdb_iter_seek_to_first" do ; end
it "#rocksdb_iter_seek_to_last" do ; end
it "#rocksdb_iter_seek" do; end
it "#rocksdb_iter_prev" do; end
it "#rocksdb_iter_next" do; end
it "#rocksdb_iter_key" do; end
it "#rocksdb_iter_value" do ; end
it "#rocksdb_iter_valid" do ; end
end
3 changes: 2 additions & 1 deletion src/rocksdb/api.cr
Expand Up @@ -35,11 +35,12 @@ module RocksDB::Api
api rocksdb_iter_seek_to_last
api rocksdb_iter_prev
api rocksdb_iter_next
api rocksdb_iter_seek
api rocksdb_iter_key
api rocksdb_iter_value
api rocksdb_iter_valid

# fun rocksdb_iter_seek(x0 : RocksdbIteratorT, k : LibC::Char*, klen : LibC::SizeT)
# fun rocksdb_iter_value(x0 : RocksdbIteratorT, vlen : LibC::SizeT*) : LibC::Char*
# fun rocksdb_iter_get_error(x0 : RocksdbIteratorT, errptr : LibC::Char**)


Expand Down
20 changes: 16 additions & 4 deletions src/rocksdb/commands/iteration.cr
@@ -1,20 +1,32 @@
module RocksDB::Commands
include Api

def new_iterator
Iterator.new(self)
end

def each(&block)
iter = Iterator.new(self)
# iter = StringIterator.new(self)
iter = new_iterator
iter.first
while iter.valid?
yield({iter.key, iter.value})
iter.next
end
ensure
iter.try(&.close)
end

def keys(limit = Int32::MAX)
iter = Iterator.new(self)
iter.first!
iter = new_iterator
iter.first
array = [] of String
i = 0
while (key = iter.key?)
i += 1
break if i > limit
array << key
iter.next!
iter.next
end
return array
ensure
Expand Down
20 changes: 10 additions & 10 deletions src/rocksdb/db.cr
@@ -1,8 +1,8 @@
class RocksDB::DB
include Value(LibRocksDB::RocksdbT)
include RawMemory(LibRocksDB::RocksdbT)
include Commands

alias RawValue = Options | ReadOptions | WriteOptions
alias Closable = Options | ReadOptions | WriteOptions

@options : Options
@read_options : ReadOptions
Expand All @@ -12,11 +12,11 @@ class RocksDB::DB
getter! write_options

def initialize(@path : String, options : Options? = nil, read_options : ReadOptions? = nil, write_options : WriteOptions? = nil)
@raw_values = [] of RawValue
@closables = [] of Closable

@options = options || raw_value(Options.new.set_create_if_missing(1))
@read_options = read_options || raw_value(ReadOptions.new)
@write_options = write_options || raw_value(WriteOptions.new)
@options = options || mark_closable(Options.new.set_create_if_missing(1))
@read_options = read_options || mark_closable(ReadOptions.new)
@write_options = write_options || mark_closable(WriteOptions.new)

@len = Pointer(UInt64).malloc(1_u64)
@err = Pointer(Pointer(UInt8)).malloc(1_u64)
Expand All @@ -25,17 +25,17 @@ class RocksDB::DB
end

protected def free
@raw_values.each(&.close)
@raw_values.clear
@closables.each(&.close)
@closables.clear
rocksdb_close(@raw.not_nil!)
end

protected def clue
@path
end

protected def raw_value(value)
@raw_values << value
protected def mark_closable(value)
@closables << value
value
end
end
2 changes: 1 addition & 1 deletion src/rocksdb/dsl.cr
Expand Up @@ -3,7 +3,7 @@ macro option_class(klass)
{% method = klass.stringify.downcase %}
class RocksDB::{{ klass.id }}
include Api
include Value({{ underly.id }})
include RawMemory({{ underly.id }})

def initialize
@raw = rocksdb_{{ method.id }}_create
Expand Down

0 comments on commit a6e097a

Please sign in to comment.