Class: Hyperion

Inherits:
Object
  • Object
show all
Extended by:
Indices, Keys, Logger, Version
Defined in:
lib/hyperion.rb,
lib/hyperion/base.rb,
lib/hyperion/keys.rb,
lib/hyperion/logger.rb,
lib/hyperion/finders.rb,
lib/hyperion/indices.rb,
lib/hyperion/version.rb

Defined Under Namespace

Modules: Finders, Indices, Keys, Logger, Version Classes: HyperionException, NoKey

Constant Summary collapse

DEBUG =
false

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Indices

hyperion_index, reindex!

Methods included from Keys

hyperion_key

Methods included from Version

branch, major, minor, patch, version

Methods included from Logger

logger

Constructor Details

#initialize(opts = {}) ⇒ Hyperion

TODO: ActiveModel lint it mayhaps TODO: atomic operations TODO: default key called #class_id if there isn’t any @@redis_key



23
24
25
26
27
28
29
# File 'lib/hyperion.rb', line 23

def initialize(opts = {})
  defaults = (self.class.class_variable_defined?('@@redis_defaults') ? self.class.class_variable_get('@@redis_defaults') : {})

  defaults.merge(opts).each {|k,v|
    self.send(k.to_s+'=',v)
  }
end

Class Method Details

.deserialize(what) ⇒ Object



72
# File 'lib/hyperion.rb', line 72

def self.deserialize(what); YAML.load(what); end

.dump(output = STDOUT, lock = false) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/hyperion.rb', line 108

def self.dump(output = STDOUT, lock = false)
  # TODO: lockability and progress
  output.write(<<-eos)
# Hyperion Dump
# Generated by @adrianpike's Hyperion gem.
  eos
  output.write('# Generated on ' + Time.current.to_s + "\n")
  output.write('# DB size is ' + redis.dbsize.to_s + "\n")
  
  redis.keys.each{|k|
    case redis.type(k)
    when "string"
      output.write({ k => redis.get(k)}.to_yaml)
    when "set"
      output.write({ k => redis.smembers(k) }.to_yaml)
    end
  }
end

.find(conds) ⇒ Object



36
37
38
39
40
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
66
67
68
69
70
# File 'lib/hyperion.rb', line 36

def self.find(conds)
  Hyperion.logger.debug("[RS] Searching for #{conds.inspect}") if Hyperion::DEBUG
  
  if conds.is_a? Hash then
    Hyperion.logger.debug("[RS] Its a Hash, digging through indexes!") if Hyperion::DEBUG
    ids = []
    index_keys = []
    index_values = []
    
    if conds.keys.size > 1 then
      conds.sort.each {|k,v|
        index_values << v
        index_keys << k.to_s
      }
      index_key = self.to_s.downcase + '_' + index_keys.join('.') + '_' + index_values.join('.')
      ids << Hyperion.redis.smembers(index_key)
    else
      conds.each{|k,v|
        index_key = self.to_s.downcase + '_' + k.to_s + '_' + v.to_s
        ids << Hyperion.redis.smembers(index_key)
      }
    end
    ids.flatten.uniq.collect{|i|
      self.find(i)
    }
  else
    Hyperion.logger.debug("[RS] Fetching #{self.to_s.downcase + '_' + conds.to_s}") if Hyperion::DEBUG
    v = redis[self.to_s.downcase + '_' + conds.to_s].to_s
    if v and not v.empty? then
      self.deserialize(v)
    else
      nil
    end
  end
end

.first(conds) ⇒ Object



31
32
33
34
# File 'lib/hyperion.rb', line 31

def self.first(conds)
	# FIXME: gotta be a faster way ;)
	self.find(conds).first
end

.hyperion_defaults(defaults) ⇒ Object



6
7
8
# File 'lib/hyperion/base.rb', line 6

def self.hyperion_defaults(defaults)
  class_variable_set(:@@redis_defaults, defaults)
end

.load(file = STDIN, truncate = true, lock = false) ⇒ Object

THIS IS MAD DANGEROUS AND UNTESTED, BEWARE DATA INTEGRITY



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/hyperion.rb', line 129

def self.load(file = STDIN, truncate = true, lock = false)
  # TODO: lockability and progress    
  
  YAML.each_document( file ) do |ydoc|
    ydoc.each {|k,v|
      redis.del(k) if truncate

      case v.class.to_s
        when 'String'
          redis[k] = v
        when 'Array'
          v.each{|val|
            redis.sadd(k,val)
          }
        else
          p v.class
      end
    }
  end
  
end

.redisObject



10
11
12
# File 'lib/hyperion/base.rb', line 10

def self.redis
  @@redis ||= Redis.new
end

.truncate!Object

THIS IS TOTALLY IRREVERSIBLE YO



152
153
154
# File 'lib/hyperion.rb', line 152

def self.truncate!
  redis.flushdb
end

Instance Method Details

#saveObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/hyperion.rb', line 75

def save
  Hyperion.logger.debug("[RS] Saving a #{self.class.to_s}:") if Hyperion::DEBUG

unless (self.class.class_variable_defined?('@@redis_key')) then
	self.class.send('attr_accessor', 'id')
   self.class.class_variable_set('@@redis_key', 'id')
end

unless (self.send(self.class.class_variable_get('@@redis_key'))) then
    Hyperion.logger.debug("[RS] Generating new key!") if Hyperion::DEBUG
    self.send(self.class.class_variable_get('@@redis_key').to_s + '=', new_key)
  end    

  Hyperion.logger.debug("[RS] Saving into #{full_key}: #{self.inspect}") if Hyperion::DEBUG
  Hyperion.redis[full_key] = self.serialize
  
  # Now lets update any indexes
  # BUG: need to clear out any old indexes of us
  self.class.class_variable_get('@@redis_indexes').each{|idx|
    Hyperion.logger.debug("[RS] Updating index for #{idx}") if Hyperion::DEBUG
  
    if idx.is_a?(Array) then
      index_values = idx.sort.collect {|i| self.send(i) }.join('.')
      index_key = self.class.to_s.downcase + '_' + idx.sort.join('.').to_s + '_' + index_values
    else
		value = self.send(idx)
      index_key = self.class.to_s.downcase + '_' + idx.to_s + '_' + value.to_s if value
    end
   Hyperion.logger.debug("[RS] Saving index #{index_key}: #{self.send(self.class.class_variable_get('@@redis_key'))}") if Hyperion::DEBUG
    Hyperion.redis.sadd(index_key, self.send(self.class.class_variable_get('@@redis_key')))
  } if self.class.class_variable_defined?('@@redis_indexes')
end

#serializeObject



73
# File 'lib/hyperion.rb', line 73

def serialize; YAML::dump(self); end