Class: Tx::Map

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Util
Includes:
Enumerable, Util
Defined in:
lib/tx.rb

Overview

Wrapper of UnsafeMap. Boundary checking of pos/len and some methods are added.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

add_encoding, bytesize, def_wrapper_methods, default_encoding, to_binary

Constructor Details

#initialize(file_pefix, encoding = nil) ⇒ Map

Returns a new instance of Map.



158
159
160
161
162
163
164
165
166
# File 'lib/tx.rb', line 158

def initialize(file_pefix, encoding = nil)
  @unsafe = UnsafeMap.new()
  if !@unsafe.open(file_pefix)
    raise(IOError, "failed to open #{file_pefix}.key, #{file_pefix}.val or #{file_pefix}.map")
  end
  @encoding = encoding || default_encoding()
  @key_index = Index.new(@unsafe.key_index, @encoding)
  @value_index = Index.new(@unsafe.value_index, @encoding)
end

Instance Attribute Details

#encodingObject (readonly)

Returns the value of attribute encoding.



168
169
170
# File 'lib/tx.rb', line 168

def encoding
  @encoding
end

#key_indexObject (readonly)

Returns the value of attribute key_index.



168
169
170
# File 'lib/tx.rb', line 168

def key_index
  @key_index
end

#value_indexObject (readonly)

Returns the value of attribute value_index.



168
169
170
# File 'lib/tx.rb', line 168

def value_index
  @value_index
end

Instance Method Details

#[](str, pos = 0, len = -1)) ⇒ Object



176
177
178
# File 'lib/tx.rb', line 176

def [](str, pos = 0, len = -1)
  return has_key(str, pos, len) ? lookup(str, pos, len) : nil
end

#each(&block) ⇒ Object



200
201
202
# File 'lib/tx.rb', line 200

def each(&block)
  each_key(){ |k| yield([k, lookup(k)]) }
end

#each_key(&block) ⇒ Object



192
193
194
# File 'lib/tx.rb', line 192

def each_key(&block)
  return self.keys.each(&block)
end

#each_pair(&block) ⇒ Object



204
205
206
# File 'lib/tx.rb', line 204

def each_pair(&block)
  each_key(){ |k| yield(k, lookup(k)) }
end

#each_value(&block) ⇒ Object



196
197
198
# File 'lib/tx.rb', line 196

def each_value(&block)
  return self.values.each(&block)
end

#inspectObject



172
173
174
# File 'lib/tx.rb', line 172

def inspect()
  return "\#<%p:0x%x>" % [self.class, self.object_id]
end

#keysObject



184
185
186
# File 'lib/tx.rb', line 184

def keys
  return @key_index.search_expansions("")
end

#scan(str, &block) ⇒ Object



208
209
210
211
212
213
214
215
# File 'lib/tx.rb', line 208

def scan(str, &block)
  result = []
  @key_index.scan(str) do |key, pos|
    args = [key, pos, lookup(key)]
    block ? yield(*args) : result.push(args)
  end
  return block ? str : result
end

#sizeObject



180
181
182
# File 'lib/tx.rb', line 180

def size
  return self.keys.sizse
end

#valuesObject



188
189
190
# File 'lib/tx.rb', line 188

def values
  return self.keys.map(){ |k| lookup(k) }
end