You are here: Home » NewsFeeds » Gkv – Git as a key-value store

Gkv – Git as a key-value store


README.md

Join the chat at https://gitter.im/ybur-yug/gkv

Gem Version
Build Status

Gkv is a simple git wrapper that allows you to use it as a kv store

proof in our pudding

asciicast

The documentation says thats what it does. So why not yo?

This is the product of a tutorial I wrote to explore git.

Installation

Add this line to your application’s Gemfile:

And then execute:

$ bundle

Or install it yourself as:

$ gem install gkv

API

Types are implicitly understood, and are automatically set/loaded. Only symbols are excluded.
There are 4 main functions:

Set

db[key] = value

db = Gkv::Database.new
db['Pants'] = 'red leather'
# => 'red leather'

This allows a shorthand notation using operator overloading to set without invoking set directly.

set(key, value)

db = Gkv::Database.new
db.set('key', '12')
# => 'key'
db.set('test', 12)
# => 'test'

Get

db[key]

db = Gkv::Database.new
db['Pants']
# => 'red leather'

This allows a shorthand notation using operator overloading to get without invoking get directly.

get(key)

db = Gkv::Database.new
db.set('apples', '10')
# => 'apples'
db.get('apples')
# => '10'

The type is inferred from when you initially set the value. Note that saving the string '1' will
return the integer 1 due to the naive nature of the implementation. Hashes, arrays and booleans
behave as expected when saved.

Get Version

get_version(version, key)

db = Gkv::Database.new
db.set('apples', '20')
# => 'apples'
db.set('apples', '50')
# => 'apples'
db.get_version(1, 'apples')
# => '20'
db.get_version(2, 'apples')
# => '50'

All

all

db.set('apples', 20.0)
db.set('ants',   'pants')
db.set('things', {})
db.all
# =>[{ 'apples': 20.0 }, { 'ants': 'pants'}, { 'things': {} }]

Usage

db = Gkv::Database.new

db.set('Apples', '10')
# => 'Apples'
db.get('Apples')
# => '10'

# update some values
db.set('Apples', '12')
# => 'Apples'
db.get('Apples')
# => '12'
db.get_version(1, 'Apples')
#=> '10'

# keys that do not exist return KeyError
db.get('magic')
# => KeyError

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the
tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version,
update the version number in version.rb, and then run bundle exec rake release, which will create
a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Feel free to check out the gitter room and ask whats on the agenda.

Bug reports and pull requests are welcome on GitHub at https://github.com/ybur-yug/gkv. This project is
intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
the Contributor Covenant code of conduct.

License

See this.


 

Original article