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)



92
93
94
95
96
97
# File 'lib/rufus/tokyo/cabinet/util.rb', line 92

def initialize (pointer=nil)

  @pointer = pointer || clib.tcmapnew

  @default_proc = nil
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 !)



202
203
204
205
206
207
208
209
# File 'lib/rufus/tokyo/cabinet/util.rb', line 202

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



194
195
196
197
# File 'lib/rufus/tokyo/cabinet/util.rb', line 194

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.



182
183
184
185
186
187
188
189
# File 'lib/rufus/tokyo/cabinet/util.rb', line 182

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



101
102
103
104
105
106
# File 'lib/rufus/tokyo/cabinet/util.rb', line 101

def []= (k, v)

  clib.tcmapput(pointer, k, Rufus::Tokyo::blen(k), v, Rufus::Tokyo::blen(v))

  v
end

#clearObject

Empties the map



123
124
125
126
# File 'lib/rufus/tokyo/cabinet/util.rb', line 123

def clear

  clib.tcmapclear(pointer_or_raise)
end

#delete(k) ⇒ Object

Deletes an entry



110
111
112
113
114
115
116
117
118
119
# File 'lib/rufus/tokyo/cabinet/util.rb', line 110

def delete (k)

  v = self[k]
  return nil unless v

  clib.tcmapout(pointer_or_raise, k, Rufus::Tokyo::blen(k)) ||
    raise("failed to remove key '#{k}'")

  v
end

#freeObject Also known as: destroy, close

Frees the map (nukes it from memory)



169
170
171
172
173
# File 'lib/rufus/tokyo/cabinet/util.rb', line 169

def free

  clib.tcmapdel(pointer_or_raise)
  @pointer = nil
end

#keysObject

Returns an array of all the keys in the map



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rufus/tokyo/cabinet/util.rb', line 138

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



160
161
162
163
# File 'lib/rufus/tokyo/cabinet/util.rb', line 160

def size

  clib.tcmaprnum(pointer_or_raise)
end