Module: FlagpoleSitta::ExistenceHash::ClassMethods

Defined in:
lib/flagpole_sitta/existence_hash.rb

Instance Method Summary collapse

Instance Method Details

#each_existence_hash(&block) ⇒ Object

Goes through each entry in the hash returning a key and value



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/flagpole_sitta/existence_hash.rb', line 108

def each_existence_hash &block

  clazz = self

  superclazz = get_super_with_existence_hash

  flag_key = eh_key_gen "Flag"
  flag = FlagpoleSitta::CommonFs.flagpole_cache_read(flag_key)

  if flag.nil?
    flag = initialize_existence_hash
  end

  unless flag[:count] == 0
    for i in 0..flag[:space] do

      cur_array_key = eh_key_gen i
      value = FlagpoleSitta::CommonFs.flagpole_cache_read(cur_array_key)

      if value.present? && value[:type].to_s.eql?(clazz.to_s)
        cur_main_key = eh_key_gen value[:key], :class => value[:type]
        hash = FlagpoleSitta::CommonFs.flagpole_cache_read(cur_main_key)
        #This if statement is to make it fail gracefully if the cache has degraded.
        if hash.present?
          yield value[:key], hash
        end
      end

    end
  end

  nil

end

#eh_key_gen(key, options = {}) ⇒ Object

Options :emptystack will make it generate a key for the emptystack instead of the general cache array.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/flagpole_sitta/existence_hash.rb', line 16

def eh_key_gen key, options={}

  superclazz = get_super_with_existence_hash

  end_key = end_key_gen key, options[:class]


  if options[:emptystack]
    "#{superclazz}/ExistenceHash/EmptyStack/#{end_key}"
  else
    "#{superclazz}/ExistenceHash/#{end_key}"
  end

end

#end_key_gen(key, clazz) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/flagpole_sitta/existence_hash.rb', line 31

def end_key_gen key, clazz
  if clazz
    "#{clazz}/#{key}"
  else
    "#{key}"
  end
end

#get_existence_hash(key) ⇒ Object

Gets a value from the ‘hash’ in the cache given a key.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/flagpole_sitta/existence_hash.rb', line 68

def get_existence_hash key

  clazz = self

  superclazz = get_super_with_existence_hash
  #Try to find the hash
  flag_key = eh_key_gen "Flag"
  flag = FlagpoleSitta::CommonFs.flagpole_cache_read(flag_key)
  #If it doesn't exist start the process of creating it
  if flag.nil?
    initialize_existence_hash
  end

  main_key = eh_key_gen key, :class => clazz
  FlagpoleSitta::CommonFs.flagpole_cache_read(main_key)

end

#get_super_with_existence_hashObject

Gets its original super class.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/flagpole_sitta/existence_hash.rb', line 144

def get_super_with_existence_hash

  if @_existence_hash_main_class.nil?

    c = self
    #Get the original super class that declares the existence hash

    while(c.superclass.respond_to? :get_existence_hash)
      c = c.superclass
    end

    @_existence_hash_main_class = c

  end

  @_existence_hash_main_class

end

#increment_existence_hash(key) ⇒ Object

Increments a value from the ‘hash’ in the cache given a key.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/flagpole_sitta/existence_hash.rb', line 87

def increment_existence_hash key

  clazz = self
    
  superclazz = get_super_with_existence_hash
  #Try to find the hash
  hash = get_existence_hash key

  #Update the hash key if it exists
  if hash
    hash[:num] = hash[:num] + 1
    main_key = eh_key_gen key, :class => clazz
    FlagpoleSitta::CommonFs.flagpole_cache_write(main_key, hash)
  end

  #Return the value
  hash

end

#initialize_existence_hashObject

Creates the ‘hash’ in the cache.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/flagpole_sitta/existence_hash.rb', line 41

def initialize_existence_hash

  superclazz = get_super_with_existence_hash

  #Used to emulate an array, keeps the stored count and space. The space is not actually a count of existing entries, but rather how long
  #the 'container' goes, it starts at 0, so thats why 1 is subtracted. The count is well the count. They should start out the same. 
  count = superclazz.count

  flag = {:space => (count - 1), :count => count, :empty => -1}

  flag_key = eh_key_gen "Flag"
  FlagpoleSitta::CommonFs.flagpole_cache_write(flag_key, flag)
  i = 0
  superclazz.find_each do |m|
    #Route ID is the key. The POS is used to emulate an array, along with the length stored in the flag.
    main_key = eh_key_gen m.send(m.class.route_id), :class => m.class
    FlagpoleSitta::CommonFs.flagpole_cache_write(main_key, {:type => m.class.to_s, :pos => i, :num => m.has_attribute?('num') ? (m.num || 0) : 0})
    array_key = eh_key_gen i
    FlagpoleSitta::CommonFs.flagpole_cache_write(array_key, {:key => m.send(m.class.route_id).to_s, :type => m.class.to_s})
    i = i + 1
  end

  flag

end