Class: Rufus::Tokyo::Map

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

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

Constructor Summary

public initialize(pointer = nil)

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)

[View source]


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 Instance Methods Included from Rufus::Tokyo::HashMethods

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

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

clib, outlen_op, pointer, pointer_or_raise

Public Class Method Details

[]

public [](*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 !)

[View source]


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

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

[View source]


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

public to_h(map_pointer, free = true)

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.

[View source]


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

[]=

public []=(k, v)

Inserts key/value pair

[View source]


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

public clear

Empties the map

[View source]


118
119
120
121
# File 'lib/rufus/tokyo/cabinet/util.rb', line 118

def clear

  clib.tcmapclear(pointer_or_raise)
end

delete

public delete(k)

Deletes an entry

[View source]


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

public free

Also known as: destroy close

Frees the map (nukes it from memory)

[View source]


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

public keys

Returns an array of all the keys in the map

[View source]


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

public size

Also known as: length

Returns the count of entries in the map

[View source]


155
156
157
158
# File 'lib/rufus/tokyo/cabinet/util.rb', line 155

def size

  clib.tcmaprnum(pointer_or_raise)
end
Generated on Friday, September 18 2009 at 10:35:42 AM by YARD 0.2.3.5 (ruby-1.8.7).