Skip to content

Instantly share code, notes, and snippets.

@darashi
Created March 6, 2012 04:47
Show Gist options
  • Save darashi/1983626 to your computer and use it in GitHub Desktop.
Save darashi/1983626 to your computer and use it in GitHub Desktop.
toy example of git-backended KVS
#!/usr/bin/env ruby
require 'grit'
class Database
def initialize(path)
@repository = Grit::Repo.new(path)
end
def find(key)
blob = @repository.tree / key
return nil unless blob
blob.data
end
def update(key, value, commit_message)
file_path = File.join(@repository.working_dir, key)
File.write(file_path, value)
Dir.chdir(@repository.working_dir) do
@repository.add key
end
@repository.commit_index commit_message
end
def delete(key, commit_message)
@repository.remove key
@repository.commit_index commit_message
end
end
if $0 == __FILE__
db = Database.new('myrepos')
p db.find('time')
db.update('time', Time.now.to_s, 'update time')
p db.find('time')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment