Class: Rufus::Tokyo::Map
Included Modules
A Tokyo Cabinet in-memory (tcutil.h) map
http://tokyocabinet.sourceforge.net/spex-en.html#tcutilapi
Constructor Summary
Creates an empty instance of a Tokyo Cabinet in-memory map
(It’s OK to pass the pointer of a C map directly, this is in fact used in rufus/tokyo/table when retrieving entries)
89 90 91 92 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 89 def initialize (pointer=nil) @pointer = pointer || clib.tcmapnew end |
Public Visibility
Public Class Method Summary
| [](*h_or_a) |
Behaves much like Hash#[] but outputs a Rufus::Tokyo::Map (don’t forget to free the map when you’re done with it !). |
|---|---|
| from_h(h) |
Turns a Ruby hash into a Tokyo Cabinet Map and returns it (don’t forget to free the map when you’re done with it !). |
| to_h(map_pointer, free = true) |
Turns a given Tokyo map structure into a Ruby Hash. |
Public Instance Method Summary
| #[]=(k, v) |
Inserts key/value pair. |
|---|---|
| #clear |
Empties the map. |
| #delete(k) |
Deletes an entry. |
| #free #destroy #close |
Frees the map (nukes it from memory). |
| #keys |
Returns an array of all the keys in the map. |
| #size #length |
Returns the count of entries in the map. |
Public Class Method Details
[]
Behaves much like Hash#[] but outputs a Rufus::Tokyo::Map (don’t forget to free the map when you’re done with it !)
197 198 199 200 201 202 203 204 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 197 def self.[] (*h_or_a) if h_or_a.is_a?(Array) && h_or_a.size == 1 && h_or_a.first.is_a?(Array) h_or_a = h_or_a.first end from_h(::Hash[*h_or_a]) end |
from_h
Turns a Ruby hash into a Tokyo Cabinet Map and returns it (don’t forget to free the map when you’re done with it !)
189 190 191 192 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 189 def self.from_h (h) h.inject(Map.new) { |m, (k, v)| m[k] = v; m } end |
to_h
Turns a given Tokyo map structure into a Ruby Hash. By default (free = true) will dispose of the map before replying with the Ruby Hash.
177 178 179 180 181 182 183 184 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 177 def self.to_h (map_pointer, free=true) m = self.new(map_pointer) h = m.to_h m.free if free h end |
Public Instance Method Details
[]=
Inserts key/value pair
96 97 98 99 100 101 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 96 def []= (k, v) clib.tcmapput(pointer, k, Rufus::Tokyo::blen(k), v, Rufus::Tokyo::blen(v)) v end |
clear
Empties the map
118 119 120 121 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 118 def clear clib.tcmapclear(pointer_or_raise) end |
delete
Deletes an entry
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 105 def delete (k) v = self[k] return nil unless v (clib.tcmapout(pointer_or_raise, k, Rufus::Tokyo::blen(k)) == 1) || raise("failed to remove key '#{k}'") v end |
free
Also known as: destroy close
Frees the map (nukes it from memory)
164 165 166 167 168 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 164 def free clib.tcmapdel(pointer_or_raise) @pointer = nil end |
keys
Returns an array of all the keys in the map
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 133 def keys clib.tcmapiterinit(pointer_or_raise) a = [] klen = FFI::MemoryPointer.new(:int) loop do k = clib.tcmapiternext(@pointer, klen) break if k.address == 0 a << k.get_bytes(0, klen.get_int(0)) end return a ensure klen.free end |
size
Also known as: length
Returns the count of entries in the map
155 156 157 158 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 155 def size clib.tcmaprnum(pointer_or_raise) end |