Class: Rufus::Tokyo::Map

Inherits:
Object
  • Object
show all
Includes:
HashMethods, ListMapMixin
Defined in:
lib/rufus/tokyo/cabinet/util.rb

Overview

A Tokyo Cabinet in-memory (tcutil.h) map

tokyocabinet.sourceforge.net/spex-en.html#tcutilapi

Instance Attribute Summary

Attributes included from HashMethods

#default_proc

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ListMapMixin

#clib, #outlen_op, #pointer, #pointer_or_raise

Methods included from HashMethods

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

Constructor Details

#initialize(pointer = nil) ⇒ Map

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

Class Method Details

.[](*h_or_a) ⇒ Object

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(h) ⇒ Object

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(map_pointer, free = true) ⇒ Object

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

Instance Method Details

#[]=(k, v) ⇒ Object

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

#clearObject

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(k) ⇒ Object

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

#freeObject 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

#keysObject

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

#sizeObject 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