Module: Rufus::Edo::CabinetCore
- Includes:
- Tokyo::HashMethods, Tokyo::Transactions
- Defined in:
- lib/rufus/edo/cabcore.rb
Overview
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.
Instance Attribute Summary
Attributes included from Tokyo::HashMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#[]=(k, v) ⇒ Object
No comment.
-
#clear ⇒ Object
Removes all the records in the cabinet (use with care).
-
#close ⇒ Object
Closes the cabinet (and frees the datastructure allocated for it), returns true in case of success.
-
#compact_copy(target_path) ⇒ Object
Copies the current cabinet to a new file.
-
#copy(target_path) ⇒ Object
Copies the current cabinet to a new file.
-
#defrag ⇒ Object
Triggers a defrag (TC >= 1.4.21 only).
-
#delete(k) ⇒ Object
Removes a record from the cabinet, returns the value if successful else nil.
-
#delete_keys_with_prefix(prefix) ⇒ Object
Deletes all the entries whose keys begin with the given prefix.
-
#incr(key, val = 1) ⇒ Object
(also: #adddouble, #addint, #add_double, #add_int)
Increments the value stored under the given key with the given increment (defaults to 1 (integer)).
-
#keys(options = {}) ⇒ Object
Returns an array of all the primary keys in the db.
-
#ldelete(keys) ⇒ Object
Given a list of keys, deletes all the matching entries (in one sweep).
-
#lget(keys) ⇒ Object
(also: #mget)
Given a list of keys, returns a Hash { key => value } of the matching entries (in one sweep).
-
#merge!(hash) ⇒ Object
(also: #lput)
Default impl provided by HashMethods.
-
#original ⇒ Object
Returns the underlying ‘native’ Ruby object (of the class devised by Hirabayashi-san).
-
#path ⇒ Object
Returns the path to this database.
-
#putcat(k, v) ⇒ Object
Appends the given string at the end of the current string value for key k.
-
#putkeep(k, v) ⇒ Object
Like #put but doesn’t overwrite the value if already set.
-
#size ⇒ Object
Returns the number of records in the ‘cabinet’.
-
#sync ⇒ Object
“synchronize updated contents of an abstract database object with the file and the device”.
-
#tranabort ⇒ Object
This is rather low-level use #transaction and a block for a higher-level technique.
-
#tranbegin ⇒ Object
This is rather low-level, you’d better use #transaction like in.
-
#trancommit ⇒ Object
This is rather low-level use #transaction and a block for a higher-level technique.
-
#weight ⇒ Object
Returns the ‘weight’ of the db (in bytes).
Methods included from Tokyo::Transactions
Methods included from Tokyo::HashMethods
#[], #default, #default=, #each, #merge, #to_a, #to_h, #values
Class Method Details
.included(target_module) ⇒ Object
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 |
Instance Method Details
#[]=(k, v) ⇒ Object
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 ⇒ Object
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 ⇒ Object
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(target_path) ⇒ Object
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(target_path) ⇒ Object
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 ⇒ Object
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(k) ⇒ Object
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(prefix) ⇒ Object
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(key, val = 1) ⇒ Object 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(options = {}) ⇒ Object
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(keys) ⇒ Object
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(keys) ⇒ Object 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!(hash) ⇒ Object 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 ⇒ Object
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 ⇒ Object
Returns the path to this database.
68 69 70 71 |
# File 'lib/rufus/edo/cabcore.rb', line 68 def path @path end |
#putcat(k, v) ⇒ Object
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(k, v) ⇒ Object
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 ⇒ Object
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 ⇒ Object
“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 ⇒ Object
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 ⇒ Object
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 ⇒ Object
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 ⇒ Object
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 |