Code Monkey home page Code Monkey logo

git_store's Introduction

Git Store - using Git as versioned data store in Ruby

GitStore implements a versioned data store based on the revision management system Git. You can store object hierarchies as nested hashes, which will be mapped on the directory structure of a git repository. Basically GitStore checks out the repository into a in-memory representation, which can be modified and finally committed.

GitStore supports transactions, so that updates to the store either fail or succeed completely.

Installation

GitStore can be installed as gem easily:

$ gem sources -a http://gems.github.com
$ sudo gem install georgi-git_store

Usage Example

First thing you should do, is to initialize a new git repository.

$ mkdir test
$ cd test
$ git init

Now you can instantiate a GitStore instance and store some data. The data will be serialized depending on the file extension. So for YAML storage you can use the 'yml' extension:

store = GitStore.new('/path/to/repo')

store['users/matthias.yml'] = User.new('Matthias')
store['pages/home.yml'] = Page.new('matthias', 'Home')

store.commit 'Added user and page'

Transactions

GitStore manages concurrent access by a file locking scheme. So only one process can start a transaction at one time. This is implemented by locking the refs/head/<branch>.lock file, which is also respected by the git binary.

If you access the repository from different processes or threads, you should write to the store using transactions. If something goes wrong inside a transaction, all changes will be rolled back to the original state.

store = GitStore.new('/path/to/repo')

store.transaction do
  # If an exception happens here, the transaction will be aborted.
  store['pages/home.yml'] = Page.new('matthias', 'Home')
end

A transaction without a block looks like this:

store.start_transaction

store['pages/home.yml'] = Page.new('matthias', 'Home')

store.rollback # This will restore the original state

Data Storage

When you call the commit method, your data is written back straight into the git repository. No intermediate file representation. So if you want to have a look at your data, you can use a git browser like git-gui or checkout the files:

$ git checkout

Iteration

Iterating over the data objects is quite easy. Furthermore you can iterate over trees and subtrees, so you can partition your data in a meaningful way. For example you may separate the config files and the pages of a wiki:

store['pages/home.yml'] = Page.new('matthias', 'Home')
store['pages/about.yml'] = Page.new('matthias', 'About')
store['config/wiki.yml'] = { 'name' => 'My Personal Wiki' }

# Enumerate all objects
store.each { |obj| ... } 

# Enumerate only pages
store['pages'].each { |page| ... }

Serialization

Serialization is dependent on the filename extension. You can add more handlers if you like, the interface is like this:

class YAMLHandler
  def read(data)
    YAML.load(data)
  end

  def write(data)
    data.to_yaml
  end    
end

Shinmun uses its own handler for files with md extension:

class PostHandler
  def read(data)
    Post.new(:src => data)
  end

  def write(post)
    post.dump
  end    
end

store = GitStore.new('.')
store.handler['md'] = PostHandler.new

GitStore on GitHub

Download or fork the project on its Github page

git_store's People

Contributors

georgi avatar manveru avatar thinkerbot avatar zapnap avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

git_store's Issues

Ruby 1.9.1 compatibility problem

$ /opt/ruby-1.9.1-p129/bin/ruby -Ilib test/*
                          user system total real
store 1000 objects 3.470000 0.720000 4.190000 ( 4.920075)
commit one object 0.190000 0.030000 0.220000 ( 0.225264)
load 1000 objects /opt/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/georgi-git_store-0.3/lib            /git_store.rb:294:in `get_object': not a loose object:     8717b89b4e102a5eedc57ee74582f320b6e7feb4 (RuntimeError)
        from /opt/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/georgi-git_store-0.3/lib/git_store.rb:270:in `get'
        from /opt/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/georgi-git_store-0.3/lib/git_store.rb:149:in `load'
        from /opt/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/georgi-git_store-0.3/lib/git_store.rb:69:in `initialize'
        from test/benchmark.rb:24:in `new'
        from test/benchmark.rb:24:in `block (2 levels) in <main>'
        from /opt/ruby-1.9.1-p129/lib/ruby/1.9.1/benchmark.rb:294:in `measure'
        from /opt/ruby-1.9.1-p129/lib/ruby/1.9.1/benchmark.rb:381:in `item'
        from test/benchmark.rb:23:in `block in <main>'
        from /opt/ruby-1.9.1-p129/lib/ruby/1.9.1/benchmark.rb:178:in `benchmark'
        from /opt/ruby-1.9.1-p129/lib/ruby/1.9.1/benchmark.rb:208:in `bm'
        from test/benchmark.rb:16:in `<main>'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.