Class: LevelDB::DB
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/leveldb/db.rb
Defined Under Namespace
Classes: ClosedError, Error, KeyError
Constant Summary
collapse
- DEFAULT =
{
create_if_missing: true,
error_if_exists: false,
paranoid_checks: false,
write_buffer_size: 4 << 20,
block_size: 4096,
max_open_files: 200,
block_cache_size: 8 * (2 << 20),
block_restart_interval: 16,
compression: false,
verify_checksums: false,
fill_cache: true
}
- @@mutex =
Mutex.new
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#[](key) ⇒ Object
(also: #get)
-
#[]=(key, val) ⇒ Object
(also: #put)
-
#batch(&block) ⇒ Object
-
#close ⇒ Object
-
#closed? ⇒ Boolean
-
#delete(key) ⇒ Object
-
#destroy ⇒ Object
-
#destroy! ⇒ Object
(also: #clear!)
-
#each(&block) ⇒ Object
-
#error_message ⇒ Object
(also: #clear_errors!)
-
#errors? ⇒ Boolean
-
#exists?(key) ⇒ Boolean
(also: #includes?, #contains?, #member?, #has_key?)
-
#fetch(key, default = nil, &block) ⇒ Object
-
#initialize(path, options = {}) ⇒ DB
constructor
-
#inspect ⇒ Object
(also: #to_s)
-
#keys ⇒ Object
-
#range(from, to, &block) ⇒ Object
-
#read_property(name) ⇒ Object
-
#reopen ⇒ Object
(also: #reopen!)
-
#reverse_each(&block) ⇒ Object
-
#reverse_range(from, to, &block) ⇒ Object
-
#snapshot ⇒ Object
-
#stats ⇒ Object
-
#values ⇒ Object
Constructor Details
#initialize(path, options = {}) ⇒ DB
Returns a new instance of DB.
31
32
33
|
# File 'lib/leveldb/db.rb', line 31
def initialize(path, options={})
new!(path, options)
end
|
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
14
15
16
|
# File 'lib/leveldb/db.rb', line 14
def options
@options
end
|
#path ⇒ Object
Returns the value of attribute path.
14
15
16
|
# File 'lib/leveldb/db.rb', line 14
def path
@path
end
|
Instance Method Details
#[](key) ⇒ Object
Also known as:
get
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/leveldb/db.rb', line 97
def [](key)
raise ClosedError if closed?
key = key.to_s
val = C.get(@_db, @_read_opts, key, key.size, @_read_len, @_err)
val.free = C[:free]
raise Error, error_message if errors?
@_read_len.value == 0 ? nil : val.to_s(@_read_len.value).clone
end
|
#[]=(key, val) ⇒ Object
Also known as:
put
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/leveldb/db.rb', line 83
def []=(key, val)
raise ClosedError if closed?
key = key.to_s
val = val.to_s
C.put(@_db, @_write_opts, key, key.size, val, val.size, @_err)
raise Error, error_message if errors?
val
end
|
#batch(&block) ⇒ Object
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/leveldb/db.rb', line 144
def batch(&block)
raise ClosedError if closed?
batch = Batch.new(@_db, @_write_opts)
if block_given?
block[batch]
batch.write!
else
batch
end
end
|
#close ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/leveldb/db.rb', line 157
def close
raise ClosedError if closed?
@_db.free = nil
C.close(@_db)
@@mutex.synchronize { @_closed = true }
raise Error, error_message if errors?
true
end
|
#closed? ⇒ Boolean
198
199
200
|
# File 'lib/leveldb/db.rb', line 198
def closed?
@@mutex.synchronize { @_closed }
end
|
#delete(key) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/leveldb/db.rb', line 110
def delete(key)
raise ClosedError if closed?
key = key.to_s
val = get(key)
C.delete(@_db, @_write_opts, key, key.size, @_err)
raise Error, error_message if errors?
val
end
|
#destroy ⇒ Object
202
203
204
205
206
207
|
# File 'lib/leveldb/db.rb', line 202
def destroy
C.destroy_db(@_db_opts, @path, @_err)
raise Error, error_message if errors?
true
end
|
#destroy! ⇒ Object
Also known as:
clear!
209
210
211
|
# File 'lib/leveldb/db.rb', line 209
def destroy!
close && destroy && reopen
end
|
#each(&block) ⇒ Object
170
171
172
173
174
175
176
|
# File 'lib/leveldb/db.rb', line 170
def each(&block)
raise ClosedError if closed?
iterator = Iterator.new(@_db, @_read_opts, @_read_len)
iterator.each(&block)
iterator
end
|
#error_message ⇒ Object
Also known as:
clear_errors!
229
230
231
232
233
234
235
236
237
|
# File 'lib/leveldb/db.rb', line 229
def error_message
return unless errors?
@_err.ptr.to_s
ensure
if errors?
@_err = C::Pointer.malloc(C::SIZEOF_VOIDP)
@_err.free = C[:free]
end
end
|
#errors? ⇒ Boolean
224
225
226
227
|
# File 'lib/leveldb/db.rb', line 224
def errors?
return unless @_err
!@_err.ptr.null?
end
|
#exists?(key) ⇒ Boolean
Also known as:
includes?, contains?, member?, has_key?
122
123
124
|
# File 'lib/leveldb/db.rb', line 122
def exists?(key)
get(key) != nil
end
|
#fetch(key, default = nil, &block) ⇒ Object
130
131
132
133
134
135
136
137
138
|
# File 'lib/leveldb/db.rb', line 130
def fetch(key, default=nil, &block)
val = get(key)
return val if val
raise KeyError if default.nil? && !block_given?
val = block_given? ? block[key] : default
put(key, val)
end
|
#inspect ⇒ Object
Also known as:
to_s
240
241
242
|
# File 'lib/leveldb/db.rb', line 240
def inspect
"#<LevelDB::DB:#{'0x%x' % object_id}>"
end
|
#keys ⇒ Object
190
191
192
|
# File 'lib/leveldb/db.rb', line 190
def keys
map { |k, v| k }
end
|
#range(from, to, &block) ⇒ Object
182
183
184
|
# File 'lib/leveldb/db.rb', line 182
def range(from, to, &block)
each.range(from, to, &block)
end
|
#read_property(name) ⇒ Object
214
215
216
217
218
|
# File 'lib/leveldb/db.rb', line 214
def read_property(name)
raise ClosedError if closed?
C.property_value(@_db, name).to_s
end
|
#reopen ⇒ Object
Also known as:
reopen!
76
77
78
79
80
|
# File 'lib/leveldb/db.rb', line 76
def reopen
close unless closed?
@@mutex.synchronize { @_closed = false }
new!(@path, @options)
end
|
#reverse_each(&block) ⇒ Object
178
179
180
|
# File 'lib/leveldb/db.rb', line 178
def reverse_each(&block)
each.reverse_each(&block)
end
|
#reverse_range(from, to, &block) ⇒ Object
186
187
188
|
# File 'lib/leveldb/db.rb', line 186
def reverse_range(from, to, &block)
reverse_each.range(from, to, &block)
end
|
#snapshot ⇒ Object
140
141
142
|
# File 'lib/leveldb/db.rb', line 140
def snapshot
Snapshot.new(@_db, @_read_opts)
end
|
#stats ⇒ Object
220
221
222
|
# File 'lib/leveldb/db.rb', line 220
def stats
read_property('leveldb.stats')
end
|
#values ⇒ Object
194
195
196
|
# File 'lib/leveldb/db.rb', line 194
def values
map { |k, v| v }
end
|