Class: Relix::IndexSet

Inherits:
Object
  • Object
show all
Defined in:
lib/relix/index_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, redis) ⇒ IndexSet

Returns a new instance of IndexSet.



5
6
7
8
9
10
# File 'lib/relix/index_set.rb', line 5

def initialize(klass, redis)
  @klass = klass
  @redis = redis
  @indexes = Hash.new
  @keyer = Keyer.default_for(@klass) unless parent
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/relix/index_set.rb', line 36

def method_missing(m, *args)
  if Relix.index_types.keys.include?(m.to_sym)
    add_index(m, *args)
  else
    super
  end
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



4
5
6
# File 'lib/relix/index_set.rb', line 4

def klass
  @klass
end

#redisObject

Returns the value of attribute redis.



3
4
5
# File 'lib/relix/index_set.rb', line 3

def redis
  @redis
end

Instance Method Details

#add_index(index_type, name, options = {}) ⇒ Object



44
45
46
47
# File 'lib/relix/index_set.rb', line 44

def add_index(index_type, name, options={})
  accessor = (options.delete(:on) || name)
  @indexes[name.to_s] = Relix.index_types[index_type].new(self, name, accessor, options)
end

#current_values_name(pk) ⇒ Object



133
134
135
# File 'lib/relix/index_set.rb', line 133

def current_values_name(pk)
  keyer.values(pk, @klass)
end

#deindex!(object) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/relix/index_set.rb', line 100

def deindex!(object)
  pk = primary_key_for(object)

  handle_concurrent_modifications(pk) do
    current_values_name = current_values_name(pk)
    @redis.watch current_values_name
    current_values = @redis.hgetall(current_values_name)

    indexes.map do |name, index|
      old_value = if index.attribute_immutable?
        index.read_normalized(object)
      else
        current_values[name]
      end

      ((watch = index.watch(old_value)) && @redis.watch(*watch))
      proc { index.deindex(@redis, pk, object, old_value) }
    end.tap { |ops| ops << proc { @redis.del current_values_name } }
  end
end

#index!(object) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/relix/index_set.rb', line 92

def index!(object)
  pk = primary_key_for(object)

  handle_concurrent_modifications(pk) do
    index_ops(object, pk)
  end
end

#index_ops(object, pk) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/relix/index_set.rb', line 63

def index_ops(object, pk)
  current_values_name = current_values_name(pk)
  @redis.watch current_values_name
  current_values = @redis.hgetall(current_values_name)

  ops = indexes.collect do |name,index|
    value = index.read_normalized(object)
    old_value = current_values[name]

    ((watch = index.watch(value, old_value)) && @redis.watch(*watch))

    next if value == old_value
    current_values[name] = value unless index.attribute_immutable?

    next unless index.filter(@redis, object, value)

    query_value = index.query(@redis, value)
    proc do
      index.index(@redis, pk, object, value, old_value, *query_value)
    end
  end.compact

  ops << proc do
    @redis.hmset(current_values_name, *current_values.flatten)
  end if current_values.any?

  ops
end

#indexesObject



49
50
51
# File 'lib/relix/index_set.rb', line 49

def indexes
  (parent ? parent.indexes.merge(@indexes) : @indexes)
end

#key_prefix(name) ⇒ Object



121
122
123
# File 'lib/relix/index_set.rb', line 121

def key_prefix(name)
  "#{@klass.name}:#{name}"
end

#keyer(value = nil, options = {}) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/relix/index_set.rb', line 28

def keyer(value=nil, options={})
  if value
    @keyer = value.new(@klass, options)
  else
    (@keyer || parent.keyer)
  end
end

#lookup(&block) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/relix/index_set.rb', line 53

def lookup(&block)
  if block
    query = Query.new(self)
    yield(query)
    query.run
  else
    primary_key_index.all(@redis)
  end
end

#parentObject



125
126
127
128
129
130
131
# File 'lib/relix/index_set.rb', line 125

def parent
  unless @parent || @parent == false
    parent = @klass.superclass
    @parent = (parent.respond_to?(:relix) ? parent.relix : false)
  end
  @parent
end

#primary_key(accessor) ⇒ Object Also known as: pk



12
13
14
# File 'lib/relix/index_set.rb', line 12

def primary_key(accessor)
  @primary_key_index = add_index(:primary_key, accessor)
end

#primary_key_indexObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/relix/index_set.rb', line 17

def primary_key_index
  unless @primary_key_index
    if parent
      @primary_key_index = parent.primary_key_index
    else
      raise MissingPrimaryKeyError.new("You must declare a primary key for #{@klass.name}")
    end
  end
  @primary_key_index
end