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
93
94
# File 'lib/rufus/tokyo/cabinet/util.rb', line 89

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



199
200
201
202
203
204
205
206
# File 'lib/rufus/tokyo/cabinet/util.rb', line 199

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



191
192
193
194
# File 'lib/rufus/tokyo/cabinet/util.rb', line 191

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.



179
180
181
182
183
184
185
186
# File 'lib/rufus/tokyo/cabinet/util.rb', line 179

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



98
99
100
101
102
103
# File 'lib/rufus/tokyo/cabinet/util.rb', line 98

def []= (k, v)

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

  v
end

#clearObject

Empties the map



120
121
122
123
# File 'lib/rufus/tokyo/cabinet/util.rb', line 120

def clear

  clib.tcmapclear(pointer_or_raise)
end

#delete(k) ⇒ Object

Deletes an entry



107
108
109
110
111
112
113
114
115
116
# File 'lib/rufus/tokyo/cabinet/util.rb', line 107

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)



166
167
168
169
170
# File 'lib/rufus/tokyo/cabinet/util.rb', line 166

def free

  clib.tcmapdel(pointer_or_raise)
  @pointer = nil
end

#keysObject

Returns an array of all the keys in the map



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

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



157
158
159
160
# File 'lib/rufus/tokyo/cabinet/util.rb', line 157

def size

  clib.tcmaprnum(pointer_or_raise)
end