Module: Cache

Extended by:
Cache
Included in:
Cache, ProjectCache
Defined in:
lib/cache.rb

Constant Summary collapse

CACHE_BASE_PATH =
".gitolemy"
OBJECT_CACHE_BASE =
"objects"
BRANCH_CACHE_BASE =
"branches"
REMOTES_REGEX =
/^remotes\/origin\//

Instance Method Summary collapse

Instance Method Details

#branch_path(branch) ⇒ Object



84
85
86
87
# File 'lib/cache.rb', line 84

def branch_path(branch)
  branch_name = branch.gsub(REMOTES_REGEX, "")
  File.join(".gitolemy", BRANCH_CACHE_BASE, "#{branch_name}.lock")
end

#cache_path(key) ⇒ Object



14
15
16
# File 'lib/cache.rb', line 14

def cache_path(key)
  File.join(CACHE_BASE_PATH, "#{key}.json.gz")
end

#ensure_directory(dirname) ⇒ Object



67
68
69
70
# File 'lib/cache.rb', line 67

def ensure_directory(dirname)
  return if File.directory?(dirname)
  FileUtils.makedirs(dirname)
end

#index_commit(branch, commit_id) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/cache.rb', line 37

def index_commit(branch, commit_id)
  return if not persist?
  branch_path = branch_path(branch)
  ensure_directory(File.dirname(branch_path))
  FileUtils.touch(branch_path) if not File.exist?(branch_path)
  File.open(branch_path, "a") do |file|
    file << "#{commit_id.to_s}\n"
  end
end

#index_commits(branch, commits) ⇒ Object



47
48
49
50
# File 'lib/cache.rb', line 47

def index_commits(branch,  commits)
  return if not persist?
  File.write(branch_path(branch), commits.join("\n") + "\n")
end

#last_indexed_commit(branch, commit_ids) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cache.rb', line 52

def last_indexed_commit(branch, commit_ids)
  cached_commit_ids = File
    .read(branch_path(branch))
    .lines
    .map(&:chomp)
    .reduce({}) do |acc, commit_id|
      acc[commit_id.to_sym] = true
      acc
    end

  commit_ids.detect { |commit_id| cached_commit_ids[commit_id] }
rescue Errno::ENOENT
  nil
end

#object_rel_path(object_id) ⇒ Object



80
81
82
# File 'lib/cache.rb', line 80

def object_rel_path(object_id)
  File.join(OBJECT_CACHE_BASE, object_id)
end

#persist?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/cache.rb', line 89

def persist?
  ENV["GITOLEMY_PERSIST"] != "false"
end

#read(key, default_val = nil) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/cache.rb', line 29

def read(key, default_val=nil)
  filename = cache_path(key)
  return default_val if not File.exist?(filename)
  JSON.parse(Zlib::GzipReader.open(filename) { |gz| gz.read })
rescue JSON::ParserError, Zlib::GzipFile::Error
  default_val
end

#read_object(key, default_val = nil) ⇒ Object



72
73
74
# File 'lib/cache.rb', line 72

def read_object(key, default_val=nil)
  read(object_rel_path(key.to_s), default_val)
end

#write(key, data, existing_data = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/cache.rb', line 18

def write(key, data, existing_data=nil)
  return data if not persist?
  filename = cache_path(key)
  dirname = File.dirname(filename)
  ensure_directory(dirname)

  store_data = existing_data.nil? ? data : existing_data.merge(data)
  Zlib::GzipWriter.open(filename) { |gz| gz.write(store_data.to_json) }
  data
end

#write_object(key, data) ⇒ Object



76
77
78
# File 'lib/cache.rb', line 76

def write_object(key, data)
  write(object_rel_path(key.to_s), data)
end