Module: Rufus::Edo::CabinetCore

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

[], default, default=, each, merge, to_a, to_h, values

Public Instance Methods Included from Rufus::Tokyo::Transactions

abort, transaction

Public Class Method Details

included

public included(target_module)
[View source]


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

[]=

public []=(k, v)

No comment

[View source]


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

public clear

Removes all the records in the cabinet (use with care)

Returns self (like Ruby’s Hash does).

[View source]


134
135
136
137
138
139
# File 'lib/rufus/edo/cabcore.rb', line 134

def clear

  @db.vanish || raise_error

  self
end

close

public close

Closes the cabinet (and frees the datastructure allocated for it), returns true in case of success.

[View source]


151
152
153
154
# File 'lib/rufus/edo/cabcore.rb', line 151

def close

  @db.close || raise_error
end

compact_copy

public compact_copy(target_path)

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…

[View source]


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

public copy(target_path)

Copies the current cabinet to a new file.

Returns true if it was successful.

[View source]


160
161
162
163
# File 'lib/rufus/edo/cabcore.rb', line 160

def copy (target_path)

  @db.copy(target_path)
end

defrag

public defrag

Triggers a defrag (TC >= 1.4.21 only)

[View source]


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

public delete(k)

Removes a record from the cabinet, returns the value if successful else nil.

[View source]


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

public delete_keys_with_prefix(prefix)

Deletes all the entries whose keys begin with the given prefix

[View source]


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

public incr(key, val = 1)

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)).

Meta Tags

Raises:

[EdoError]
[View source]


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

public keys(options = {})

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
[View source]


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 (options={})

  if pref = options[:prefix]

    @db.fwmkeys(pref, options[:limit] || -1)

  else

    limit = options[: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

public ldelete(keys)

Given a list of keys, deletes all the matching entries (in one sweep).

Warning : this is a naive (slow) implementation.

[View source]


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

public lget(keys)

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.

[View source]


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!

public merge!(hash)

Also known as: lput

Default impl provided by HashMethods

[View source]


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

public original

Returns the underlying ‘native’ Ruby object (of the class devised by Hirabayashi-san)

[View source]


322
323
324
325
# File 'lib/rufus/edo/cabcore.rb', line 322

def original

  @db
end

path

public path

Returns the path to this database.

[View source]


68
69
70
71
# File 'lib/rufus/edo/cabcore.rb', line 68

def path

  @path
end

putcat

public putcat(k, v)

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.

[View source]


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

public putkeep(k, v)

Like #put but doesn’t overwrite the value if already set. Returns true only if there no previous entry for k.

[View source]


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

public size

Returns the number of records in the ‘cabinet’

[View source]


125
126
127
128
# File 'lib/rufus/edo/cabcore.rb', line 125

def size

  @db.rnum
end

sync

public sync

"synchronize updated contents of an abstract database object with the file and the device"

[View source]


180
181
182
183
# File 'lib/rufus/edo/cabcore.rb', line 180

def sync

  @db.sync || raise_error
end

tranabort

public 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.

[View source]


358
359
360
# File 'lib/rufus/edo/cabcore.rb', line 358

def tranabort
  @db.tranabort
end

tranbegin

public 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.

[View source]


338
339
340
# File 'lib/rufus/edo/cabcore.rb', line 338

def tranbegin
  @db.tranbegin
end

trancommit

public 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.

[View source]


348
349
350
# File 'lib/rufus/edo/cabcore.rb', line 348

def trancommit
  @db.trancommit
end

weight

public weight

Returns the ‘weight’ of the db (in bytes)

[View source]


143
144
145
146
# File 'lib/rufus/edo/cabcore.rb', line 143

def weight

  @db.fsiz
end
Generated on Friday, September 18 2009 at 10:35:39 AM by YARD 0.2.3.5 (ruby-1.8.7).