Class: Rod::Index::HashIndex

Inherits:
Base
  • Object
show all
Defined in:
lib/rod/index/hash_index.rb

Overview

This implementation of index is based on the Berkeley DB Hash access method.

Defined Under Namespace

Classes: Handle

Instance Attribute Summary collapse

Attributes inherited from Base

#path

Instance Method Summary collapse

Methods inherited from Base

#[], create, #key_persisted, #to_s

Methods included from Utils

#remove_file, #remove_files, #remove_files_but, #report_progress

Constructor Details

#initialize(path, klass, options = {}) ⇒ HashIndex

Initializes the index with path and class. Options are not (yet) used.



18
19
20
21
22
23
# File 'lib/rod/index/hash_index.rb', line 18

def initialize(path,klass,options={})
  @path = path + ".db"
  @klass = klass
  @options = options
  @property_type = options[:property_type]
end

Instance Attribute Details

#klassObject (readonly)

The class given index is associated with.



14
15
16
# File 'lib/rod/index/hash_index.rb', line 14

def klass
  @klass
end

Instance Method Details

#copy(index) ⇒ Object

Copies the index from the given index. The index have to cleared before being copied.



57
58
59
60
# File 'lib/rod/index/hash_index.rb', line 57

def copy(index)
  self.destroy
  super(index)
end

#delete(key, value = nil) ⇒ Object

Delets given value for the given key. If the value is nil, then the key is removed with all the values.



65
66
67
# File 'lib/rod/index/hash_index.rb', line 65

def delete(key,value=nil)
  _delete(key,value)
end

#destroyObject

Clears the contents of the index.



31
32
33
34
# File 'lib/rod/index/hash_index.rb', line 31

def destroy
  close if opened?
  remove_file(@path)
end

#eachObject

Iterates over the keys and corresponding values present in the index.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rod/index/hash_index.rb', line 38

def each
  if block_given?
    open(@path, :create => true) unless opened?
    _each_key do |key|
      next if key.empty?
      if @property_type == :string
        key.force_encoding("UTF-8")
      else
        key = Marshal.load(key)
      end
      yield key,self[key]
    end
  else
    enum_for(:each)
  end
end

#each_for(key) ⇒ Object

Iterates over all values for a given key. Raises KeyMissing if the key is not present.



76
77
78
79
80
# File 'lib/rod/index/hash_index.rb', line 76

def each_for(key)
  _get(key) do |value|
    yield value
  end
end

#get_first(key) ⇒ Object

Returns the first value for a given key or raises keyMissing exception if it is not present.



84
85
86
# File 'lib/rod/index/hash_index.rb', line 84

def get_first(key)
  _get_first(key)
end

#put(key, rod_id) ⇒ Object

Registers given rod_id for the given key.



70
71
72
# File 'lib/rod/index/hash_index.rb', line 70

def put(key,rod_id)
  _put(key,rod_id)
end

#saveObject

Stores the index on disk.



26
27
28
# File 'lib/rod/index/hash_index.rb', line 26

def save
  close
end