Class: Eternity::Blob

Inherits:
Object
  • Object
show all
Defined in:
lib/eternity/blob.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, sha1) ⇒ Blob

Returns a new instance of Blob.



6
7
8
9
# File 'lib/eternity/blob.rb', line 6

def initialize(type, sha1)
  @type = type
  @sha1 = sha1
end

Instance Attribute Details

#sha1Object (readonly)

Returns the value of attribute sha1.



4
5
6
# File 'lib/eternity/blob.rb', line 4

def sha1
  @sha1
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/eternity/blob.rb', line 4

def type
  @type
end

Class Method Details

.clear_cacheObject



55
56
57
58
59
# File 'lib/eternity/blob.rb', line 55

def clear_cache
  Eternity.connection.call('KEYS', Eternity.keyspace[:blob]['*']).each_slice(1000) do |keys|
    Eternity.connection.call 'DEL', *keys
  end
end

.countObject



61
62
63
# File 'lib/eternity/blob.rb', line 61

def count
  Eternity.connection.call('KEYS', Eternity.keyspace[:blob]['*']).count
end

.deserialize(string) ⇒ Object



39
40
41
# File 'lib/eternity/blob.rb', line 39

def deserialize(string)
  MessagePack.unpack string
end

.digest(string) ⇒ Object



31
32
33
# File 'lib/eternity/blob.rb', line 31

def digest(string)
  Digest::SHA1.hexdigest string
end

.normalize(data) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/eternity/blob.rb', line 43

def normalize(data)
  case data
    when Hash
      sorted_data = Hash[data.sort_by { |k,v| k.to_s }]
      sorted_data.each { |k,v| sorted_data[k] = v.utc.strftime TIME_FORMAT if v.respond_to? :utc }
    when Array
      data.map { |d| normalize d }
    else
      data
  end
end

.read(type, sha1) ⇒ Object



27
28
29
# File 'lib/eternity/blob.rb', line 27

def read(type, sha1)
  deserialize read_redis(type, sha1) || read_file(type, sha1)
end

.serialize(data) ⇒ Object



35
36
37
# File 'lib/eternity/blob.rb', line 35

def serialize(data)
  MessagePack.pack normalize(data)
end

.write(type, data) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/eternity/blob.rb', line 17

def write(type, data)
  serialization = serialize data
  sha1 = digest serialization

  write_redis type, sha1, serialization
  write_file type, sha1, serialization

  sha1
end

Instance Method Details

#dataObject



11
12
13
# File 'lib/eternity/blob.rb', line 11

def data
  sha1 ? Blob.read(type, sha1) : {}
end