Class: Rod::Index::Base

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/rod/index/base.rb

Overview

Base class for index implementations. It provides only a method for accessing the index by keys, but doesn’t allow to set values for keys, since the kind of a value is a collection (proxy) of objects, that are indexed via given key. The returned collection allows for adding and removing the indexed objects.

The implementing classes have to provide get and set methods, which are used to retrive and assign the values respectively.

Direct Known Subclasses

FlatIndex, HashIndex, SegmentedIndex

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#remove_file, #remove_files, #remove_files_but, #report_progress

Constructor Details

#initialize(klass) ⇒ Base

Sets the class this index belongs to.



21
22
23
24
# File 'lib/rod/index/base.rb', line 21

def initialize(klass)
  @klass = klass
  @unstored_map = {}
end

Instance Attribute Details

#pathObject (readonly)

The path of the index.



18
19
20
# File 'lib/rod/index/base.rb', line 18

def path
  @path
end

Class Method Details

.create(path, klass, options) ⇒ Object

Creats the proper instance of Index or one of its sublcasses. The path is the path were the index is stored, while index is the previous index instance. The klass is the class given index belongs to. Options might include class-specific options.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rod/index/base.rb', line 102

def create(path,klass,options)
  options = options.dup
  type = options.delete(:index)
  case type
  when :flat
    FlatIndex.new(path,klass,options)
  when :segmented
    SegmentedIndex.new(path,klass,options)
  when :hash
    HashIndex.new(path,klass,options)
  when true
    ActiveSupport::Deprecation.
      warn("Index type 'true' is deprecated. It will be removed in ROD 0.8.0")
    FlatIndex.new(path,klass,options)
  else
    raise RodException.new("Invalid index type '#{type}'")
  end
end

Instance Method Details

#[](key) ⇒ Object

Returns the collection of objects indexed by given key. The key might be a direct value (such as String) or a Rod object.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rod/index/base.rb', line 28

def [](key)
  unstored_object = false
  if key.is_a?(Model)
    if key.new?
      proxy = @unstored_map[key]
      unstored_object = true
    else
      # TODO #155, the problem is how to determine the name_hash,
      # when the class is generated in different module
      # key = [key.rod_id,key.class.name_hash]
      key = key.rod_id
      proxy = get(key)
    end
  else
    proxy = get(key)
  end
  if proxy.nil?
    proxy = empty_collection_proxy(key)
  else
    if Array === proxy
      offset, count = proxy
      proxy = CollectionProxy.new(count,@klass.database,offset,@klass)
    end
  end
  if unstored_object
    key.reference_updaters << ReferenceUpdater.for_index(self)
    @unstored_map[key] = proxy
  else
    set(key,proxy)
  end
  proxy
end

#copy(index) ⇒ Object

Copies the values from index to this index.



62
63
64
65
66
67
68
69
# File 'lib/rod/index/base.rb', line 62

def copy(index)
  index.each.with_index do |key_value,position|
    # TODO #206 this doesn't work for hash
    self.set(key_value[0],key_value[1])
    # TODO #182 implement size for index
    # report_progress(position,index.size) if $ROD_DEBUG
  end
end

#key_persisted(object) ⇒ Object

Moves the association between an ustored object from memory to the index.



73
74
75
76
77
78
79
80
81
82
# File 'lib/rod/index/base.rb', line 73

def key_persisted(object)
  proxy = @unstored_map.delete(object)
  # the update for that object has been done
  return if proxy.nil?
  # TODO #155, the problem is how to determine the name_hash,
  # when the class is generated in different module
  # key = [key.rod_id,key.class.name_hash]
  key = object.rod_id
  set(key,proxy)
end

#to_sObject

The default representation shows the index class and path.



85
86
87
# File 'lib/rod/index/base.rb', line 85

def to_s
  "#{self.class}@#{@path}"
end