leveldb 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 123601bbef23ab13db9da5dd25db49eb0a477e09
4
- data.tar.gz: 81d8c02992688dc995c3a8836dc41f18c56e233e
3
+ metadata.gz: 45ece05a2078c65923a11be951e9bd4ed6065d52
4
+ data.tar.gz: 1e0067e76973f4562a84a8831cfb79e77289292a
5
5
  SHA512:
6
- metadata.gz: 33f3b1a0dc7a8882ba275f33eaa407e3a9dad106d0eec444199ed1c93a7da0aa36db4acc58cb35b2506b66c56ab19a10a597976652189687d23e6accd83ea152
7
- data.tar.gz: 25a569065c2f94b56a83171a4f10252de107fa323e2902e31d76f0255232aefdd9835c6e3ed131ecd93c22163c1a5a5369a5f7931039256c4c98141f03e2cb7c
6
+ metadata.gz: d82cbd5f00b07320b1f02d01964b0f46c4dcf494e65dac1156e18aff8079c8ebddef747cabe1266111f3c5f88d2bd58c5142149a24cc2f07201e337688b54180
7
+ data.tar.gz: 00d47b0cf131dcf9edd6d2ddbf2fc4aaa64eab36179f0a5994fd0a3792067070b54e7fe79bfbdb06f3d9133f0e203c94b27320f269060ce978ef7bf9676eeabf
data/README.md CHANGED
@@ -59,6 +59,11 @@ db['hello'] = 'world'
59
59
  # Reading
60
60
  db.get('hello') # => world
61
61
  db['hello'] # => world
62
+ db.exists?('hello') # => true
63
+
64
+ # Reading/Writing
65
+ db.fetch('hello', 'hello world') # => will write 'hello world' if there is no key 'hello'
66
+ db.fetch('hello'){ |key| 'hello world' } # => same as above
62
67
 
63
68
  # Deleting
64
69
  db.delete('hello')
data/lib/leveldb/batch.rb CHANGED
@@ -56,5 +56,10 @@ module LevelDB
56
56
  end
57
57
  end
58
58
  alias clear_errors! error_message
59
+
60
+ def inspect
61
+ "#<LevelDB::Batch:#{'0x%x' % object_id}>"
62
+ end
63
+ alias to_s inspect
59
64
  end
60
65
  end
data/lib/leveldb/db.rb CHANGED
@@ -8,6 +8,7 @@ module LevelDB
8
8
  include Enumerable
9
9
 
10
10
  class Error < StandardError; end
11
+ class KeyError < StandardError; end
11
12
  class ClosedError < StandardError; end
12
13
 
13
14
  attr_reader :path
@@ -85,7 +86,7 @@ module LevelDB
85
86
 
86
87
  raise Error, error_message if errors?
87
88
 
88
- val.null? ? nil : val.to_s(@_read_len.value)
89
+ @_read_len.value == 0 ? nil : val.to_s(@_read_len.value)
89
90
  end
90
91
  alias get []
91
92
 
@@ -101,6 +102,24 @@ module LevelDB
101
102
  val
102
103
  end
103
104
 
105
+ def exists?(key)
106
+ get(key) != nil
107
+ end
108
+ alias includes? exists?
109
+ alias contains? exists?
110
+ alias member? exists?
111
+ alias has_key? exists?
112
+
113
+ def fetch(key, default=nil, &block)
114
+ val = get(key)
115
+
116
+ return val if val
117
+ raise KeyError if default.nil? && !block_given?
118
+
119
+ val = block_given? ? block[key] : default
120
+ put(key, val)
121
+ end
122
+
104
123
  def snapshot
105
124
  Snapshot.new(@_db, @_read_opts)
106
125
  end
@@ -195,5 +214,10 @@ module LevelDB
195
214
  end
196
215
  end
197
216
  alias clear_errors! error_message
217
+
218
+ def inspect
219
+ "#<LevelDB::DB:#{'0x%x' % object_id}>"
220
+ end
221
+ alias to_s inspect
198
222
  end
199
223
  end
@@ -72,6 +72,11 @@ module LevelDB
72
72
  !!@_range
73
73
  end
74
74
 
75
+ def inspect
76
+ "#<LevelDB::Iterator:#{'0x%x' % object_id}>"
77
+ end
78
+ alias to_s inspect
79
+
75
80
  private
76
81
  def current
77
82
  return unless valid?
@@ -15,5 +15,10 @@ module LevelDB
15
15
  def reset!
16
16
  C.readoptions_set_snapshot(@_read_opts, nil)
17
17
  end
18
+
19
+ def inspect
20
+ "#<LevelDB::Snapshot:#{'0x%x' % object_id}>"
21
+ end
22
+ alias to_s inspect
18
23
  end
19
24
  end
@@ -1,3 +1,3 @@
1
1
  module LevelDB
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
data/test/test_db.rb CHANGED
@@ -1,9 +1,15 @@
1
1
  require_relative './helper'
2
2
 
3
3
  class TestBasic < Minitest::Test
4
+ attr_reader :db
4
5
 
5
- def db
6
- Thread.current[:__db__] ||= LevelDB::DB.new './tmp/test'
6
+ def setup
7
+ @db ||= LevelDB::DB.new './tmp/test-db'
8
+ end
9
+
10
+ def teardown
11
+ db.close
12
+ db.destroy
7
13
  end
8
14
 
9
15
  def test_open
@@ -29,6 +35,28 @@ class TestBasic < Minitest::Test
29
35
  assert_equal 'bax', db[:foo]
30
36
  end
31
37
 
38
+ def test_exists?
39
+ db[:foo] = :bar
40
+
41
+ assert db.exists?(:foo)
42
+ assert db.includes?(:foo)
43
+ assert db.contains?(:foo)
44
+ assert db.member?(:foo)
45
+ assert db.has_key?(:foo)
46
+ refute db.exists?(:foxy)
47
+ end
48
+
49
+ def test_fetch
50
+ db[:foo] = :bar
51
+
52
+ assert_equal 'bar', db.fetch(:foo)
53
+ assert_raises LevelDB::DB::KeyError do
54
+ db.fetch(:sten)
55
+ end
56
+ assert_equal 'smith', db.fetch(:sten, :smith)
57
+ assert_equal 'francine', db.fetch(:francine){ |key| key }
58
+ end
59
+
32
60
  def test_delete
33
61
  db[:foo] = 'bar'
34
62
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leveldb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - DAddYE
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-23 00:00:00.000000000 Z
11
+ date: 2013-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fiddler-rb