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

.cache_sizeObject



68
69
70
# File 'lib/eternity/blob.rb', line 68

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

.clear_cacheObject



62
63
64
65
66
# File 'lib/eternity/blob.rb', line 62

def clear_cache
  Eternity.connection.call('KEYS', Eternity.keyspace[:blob]['*']).each_slice(1000) do |keys|
    Eternity.connection.call 'DEL', *keys
  end
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
54
55
56
57
58
59
60
# File 'lib/eternity/blob.rb', line 43

def normalize(data)
  if data.kind_of? Hash
    sorted_data = Hash[data.sort_by { |k,v| k.to_s }]
    sorted_data.each { |k,v| sorted_data[k] = normalize v }

  elsif data.kind_of? Array
    data.map { |v| normalize v }

  elsif data.kind_of? String
    data.encode 'UTF-8'

  elsif data.respond_to? :utc
    data.utc.strftime TIME_FORMAT

  else
    data
  end
end

.orphan_filesObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/eternity/blob.rb', line 72

def orphan_files
  repositories = Repository.all
  
  repo_commits = repositories.map { |r| r.current_commit } + 
                 repositories.flat_map { |r| r.branches.values.map { |c| Commit.new c } }
  
  branch_commits = Branch.names.map { |b| Branch[b] }

  used_by_type = {
    commit: (repo_commits.flat_map { |c| [c.id] + c.history_ids } + branch_commits.flat_map { |c| [c.id] + c.history_ids }).uniq
  }

  commit_blobs = used_by_type[:commit].map { |id| Blob.read :commit, id }

  [:index, :delta, :history].each do |type|
    used_by_type[type] = commit_blobs.map { |b| b[type.to_s] }.compact
  end

  used_by_type.each_with_object({}) do |(type, used), hash|
    hash[type] = files_of(type) - used.map { |id| file_for type, id }
  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