Module: GitModel

Defined in:
lib/gitmodel.rb,
lib/gitmodel/index.rb,
lib/gitmodel/errors.rb,
lib/gitmodel/persistable.rb,
lib/gitmodel/transaction.rb

Defined Under Namespace

Modules: Persistable Classes: AttributeNotIndexed, GitModelError, Index, IndexRequired, NullId, RecordDoesntExist, RecordExists, RecordNotFound, RecordNotSaved, Transaction

Class Method Summary collapse

Class Method Details

.cache(branch, key, &block) ⇒ Object

If we’re using memcached (i.e. the memcache_servers setting is not nil) and the key exists in memcached, it’s value will be returned and the block will not be run. If key does not exist in memcached, block will be executed, it’s value stored in memcached under key, and value will be returned.

There’s no need to sweep the cache because the SHA of the latest Git commit is appended to the key, so any database change invalidates all cached objects.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gitmodel.rb', line 98

def self.cache(branch, key, &block)
  key = "#{key}-#{head_sha(branch)}"
  value = nil
  if memcache_servers
    @@memcache ||= Dalli::Client.new memcache_servers, :namespace => "#{File.basename(db_root)}#{memcache_namespace.blank? ? '' : '-'}#{memcache_namespace}"
    value = @@memcache.get(key)
    if value.nil?
      logger.info("✗ memcache MISS for key #{key}")
      value = yield
      @@memcache.set(key, value)
    else
      logger.info("✔ memcache HIT for key #{key}")
    end
  else
    logger.debug("No memcache servers defined, not checking cache for key #{key}")
    value = yield
  end
  value
end

.create_db!Object

Create the database defined in db_root. Raises an exception if it exists.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gitmodel.rb', line 46

def self.create_db!
  raise "Database #{db_root} already exists!" if File.exist? db_root
  if db_root =~ /.+\.git/
    #logger.info "Creating database (bare): #{db_root}"
    #Grit::Repo.init_bare db_root
    logger.error "Bare repositories aren't supported yet"
  else
    logger.info "Creating database: #{db_root}"
    Grit::Repo.init db_root
  end
end

.current_tree(branch) ⇒ Object



78
79
80
81
# File 'lib/gitmodel.rb', line 78

def self.current_tree(branch)
  c = last_commit(branch)
  c ? c.tree : nil
end

.index!(branch) ⇒ Object



83
84
85
86
87
88
# File 'lib/gitmodel.rb', line 83

def self.index!(branch)
  dirs = (GitModel.current_tree(branch)).trees
  dirs.each do |dir|
    dir.name.classify.constantize.index!(branch)
  end
end

.last_commit(branch) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/gitmodel.rb', line 65

def self.last_commit(branch)
  cache(branch, 'last-commit') do
    unless repo.commits(branch).any?
      nil
    else
      # We should be able to use just repo.commits(branch).first here but
      # this is a workaround for this bug: 
      # http://github.com/mojombo/grit/issues/issue/38
      GitModel.repo.commits("#{branch}^..#{branch}").first || GitModel.repo.commits(branch).first
    end
  end
end

.recreate_db!Object

Delete and re-create the database defined in db_root. Dangerous!



59
60
61
62
63
# File 'lib/gitmodel.rb', line 59

def self.recreate_db!
  logger.info "Deleting database #{db_root}!!"
  FileUtils.rm_rf db_root
  create_db!
end

.repoObject



41
42
43
# File 'lib/gitmodel.rb', line 41

def self.repo
  @@repo = Grit::Repo.new(GitModel.db_root)
end