Module: Rufus::Edo::CabinetCore
Included Modules
The base cabinet methods are gathered here. They focus on wrapping Hirabayashi-san’s methods.
This module is included by Edo::Cabinet and Edo::NTyrant. Placing the methods in this module avoids having to load ‘TokyoCabinet’ when using only rufus/edo/ntyrant.
Public Visibility
Public Class Method Summary
| included(target_module) |
|---|
Public Instance Method Summary
| #[]=(k, v) |
No comment. |
|---|---|
| #clear |
Removes all the records in the cabinet (use with care). |
| #close |
Closes the cabinet (and frees the datastructure allocated for it), returns true in case of success. |
| #compact_copy(target_path) |
Copies the current cabinet to a new file. |
| #copy(target_path) |
Copies the current cabinet to a new file. |
| #defrag |
Triggers a defrag (TC >= 1. |
| #delete(k) |
Removes a record from the cabinet, returns the value if successful else nil. |
| #delete_keys_with_prefix(prefix) |
Deletes all the entries whose keys begin with the given prefix. |
| #incr(key, val = 1) #adddouble #addint #add_double #add_int |
Increments the value stored under the given key with the given increment (defaults to 1 (integer)). |
| #keys(options = {}) |
Returns an array of all the primary keys in the db. |
| #ldelete(keys) |
Given a list of keys, deletes all the matching entries (in one sweep). |
| #lget(keys) #mget |
Given a list of keys, returns a Hash { key => value } of the matching entries (in one sweep). |
| #merge!(hash) #lput |
Default impl provided by HashMethods. |
| #original |
Returns the underlying ‘native’ Ruby object (of the class devised by Hirabayashi-san). |
| #path |
Returns the path to this database. |
| #putcat(k, v) |
Appends the given string at the end of the current string value for key k. |
| #putkeep(k, v) |
Like #put but doesn’t overwrite the value if already set. |
| #size |
Returns the number of records in the ‘cabinet’. |
| #sync |
"synchronize updated contents of an abstract database object with the file and the device". |
| #tranabort |
This is rather low-level use #transaction and a block for a higher-level technique. |
| #tranbegin |
This is rather low-level, you’d better use #transaction like in. |
| #trancommit |
This is rather low-level use #transaction and a block for a higher-level technique. |
| #weight |
Returns the ‘weight’ of the db (in bytes). |
Public Instance Methods Included from Rufus::Tokyo::HashMethods
Public Instance Methods Included from Rufus::Tokyo::Transactions
Public Class Method Details
included
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rufus/edo/cabcore.rb', line 45 def self.included (target_module) target_module.module_eval do # # Same args as initialize, but can take a block form that will # close the db when done. Similar to File.open (via Zev) # def self.open (name, params={}) db = self.new(name, params) if block_given? yield db nil else db end ensure db.close if block_given? && db end end end |
Public Instance Method Details
[]=
No comment
75 76 77 78 79 80 |
# File 'lib/rufus/edo/cabcore.rb', line 75 def []= (k, v) k = k.to_s; v = v.to_s @db.put(k, v) || raise_error end |
clear
Removes all the records in the cabinet (use with care)
Returns self (like Ruby’s Hash does).
134 135 136 137 138 139 |
# File 'lib/rufus/edo/cabcore.rb', line 134 def clear @db.vanish || raise_error self end |
close
Closes the cabinet (and frees the datastructure allocated for it), returns true in case of success.
151 152 153 154 |
# File 'lib/rufus/edo/cabcore.rb', line 151 def close @db.close || raise_error end |
compact_copy
Copies the current cabinet to a new file.
Does it by copying each entry afresh to the target file. Spares some space, hence the ‘compact’ label…
170 171 172 173 174 175 |
# File 'lib/rufus/edo/cabcore.rb', line 170 def compact_copy (target_path) @other_db = self.class.new(target_path) self.each { |k, v| @other_db[k] = v } @other_db.close end |
copy
Copies the current cabinet to a new file.
Returns true if it was successful.
160 161 162 163 |
# File 'lib/rufus/edo/cabcore.rb', line 160 def copy (target_path) @db.copy(target_path) end |
defrag
Triggers a defrag (TC >= 1.4.21 only)
310 311 312 313 314 315 316 317 |
# File 'lib/rufus/edo/cabcore.rb', line 310 def defrag #raise(NotImplementedError.new( # "defrag (misc) only available when opening db with :type => :abstract" #)) unless @db.respond_to?(:misc) @db.misc('defrag', []) end |
delete
Removes a record from the cabinet, returns the value if successful else nil.
115 116 117 118 119 120 121 |
# File 'lib/rufus/edo/cabcore.rb', line 115 def delete (k) k = k.to_s v = self[k] @db.out(k) ? v : nil end |
delete_keys_with_prefix
Deletes all the entries whose keys begin with the given prefix
220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/rufus/edo/cabcore.rb', line 220 def delete_keys_with_prefix (prefix) # only ADB has the #misc method... if @db.respond_to?(:misc) @db.misc('outlist', @db.fwmkeys(prefix, -1)) else @db.fwmkeys(prefix, -1).each { |k| self.delete(k) } end nil end |
incr
Also known as: adddouble addint add_double add_int
Increments the value stored under the given key with the given increment (defaults to 1 (integer)).
290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/rufus/edo/cabcore.rb', line 290 def incr (key, val=1) key = key.to_s v = val.is_a?(Fixnum) ? @db.addint(key, val) : @db.adddouble(key, val) raise(EdoError.new( "incr failed, there is probably already a string value set " + "for the key '#{key}'" )) unless v v end |
keys
Returns an array of all the primary keys in the db.
With no options given, this method will return all the keys (strings) in a Ruby array.
:prefix --> returns only the keys who match a given string prefix :limit --> returns a limited number of keys
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/rufus/edo/cabcore.rb', line 194 def keys (={}) if pref = [:prefix] @db.fwmkeys(pref, [:limit] || -1) else limit = [:limit] || -1 limit = nil if limit < 1 l = [] @db.iterinit while (k = @db.iternext) break if limit and l.size >= limit l << k end l end end |
ldelete
Given a list of keys, deletes all the matching entries (in one sweep).
Warning : this is a naive (slow) implementation.
272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/rufus/edo/cabcore.rb', line 272 def ldelete (keys) keys = keys.collect { |k| k.to_s } # only ADB has the #misc method... if @db.respond_to?(:misc) @db.misc('outlist', keys) else keys.each { |k| self.delete(k) } end nil end |
lget
Also known as: mget
Given a list of keys, returns a Hash { key => value } of the matching entries (in one sweep).
Warning : this is a naive (slow) implementation.
238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/rufus/edo/cabcore.rb', line 238 def lget (keys) keys = keys.collect { |k| k.to_s } # only ADB has the #misc method... if @db.respond_to?(:misc) Hash[*@db.misc('getlist', keys)] else keys.inject({}) { |h, k| v = self[k]; h[k] = v if v; h } end end |
merge!
Also known as: lput
Default impl provided by HashMethods
255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/rufus/edo/cabcore.rb', line 255 def merge! (hash) # only ADB has the #misc method... if @db.respond_to?(:misc) @db.misc('putlist', hash.to_a.flatten) else super(hash) end self end |
original
Returns the underlying ‘native’ Ruby object (of the class devised by Hirabayashi-san)
322 323 324 325 |
# File 'lib/rufus/edo/cabcore.rb', line 322 def original @db end |
path
Returns the path to this database.
68 69 70 71 |
# File 'lib/rufus/edo/cabcore.rb', line 68 def path @path end |
putcat
Appends the given string at the end of the current string value for key k. If there is no record for key k, a new record will be created.
Returns true if successful.
97 98 99 100 101 102 |
# File 'lib/rufus/edo/cabcore.rb', line 97 def putcat (k, v) k = k.to_s; v = v.to_s @db.putcat(k, v) end |
putkeep
Like #put but doesn’t overwrite the value if already set. Returns true only if there no previous entry for k.
85 86 87 88 89 90 |
# File 'lib/rufus/edo/cabcore.rb', line 85 def putkeep (k, v) k = k.to_s; v = v.to_s @db.putkeep(k, v) end |
size
Returns the number of records in the ‘cabinet’
125 126 127 128 |
# File 'lib/rufus/edo/cabcore.rb', line 125 def size @db.rnum end |
sync
"synchronize updated contents of an abstract database object with the file and the device"
180 181 182 183 |
# File 'lib/rufus/edo/cabcore.rb', line 180 def sync @db.sync || raise_error end |
tranabort
This is rather low-level use #transaction and a block for a higher-level technique.
Note that fixed-length dbs do not support transactions. It will result in a NoMethodError.
358 359 360 |
# File 'lib/rufus/edo/cabcore.rb', line 358 def tranabort @db.tranabort end |
tranbegin
This is rather low-level, you’d better use #transaction like in
db.transaction do db['a'] = 'alpha' db['b'] = 'bravo' db.abort if something_went_wrong? end
Note that fixed-length dbs do not support transactions. It will result in a NoMethodError.
338 339 340 |
# File 'lib/rufus/edo/cabcore.rb', line 338 def tranbegin @db.tranbegin end |
trancommit
This is rather low-level use #transaction and a block for a higher-level technique.
Note that fixed-length dbs do not support transactions. It will result in a NoMethodError.
348 349 350 |
# File 'lib/rufus/edo/cabcore.rb', line 348 def trancommit @db.trancommit end |
weight
Returns the ‘weight’ of the db (in bytes)
143 144 145 146 |
# File 'lib/rufus/edo/cabcore.rb', line 143 def weight @db.fsiz end |