Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 06e1ea5939aed6b8f0ce77bd5ed7dca96a940359 @jugyo committed Sep 15, 2009
Showing with 257 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +3 −0 ChangeLog
  3. +53 −0 README.markdown
  4. +56 −0 Rakefile
  5. +28 −0 kvs.gemspec
  6. +41 −0 lib/kvs.rb
  7. +75 −0 spec/kvs_spec.rb
@@ -0,0 +1 @@
+pkg
@@ -0,0 +1,3 @@
+== 0.1.0 2009-09-15
+
+* first release!
@@ -0,0 +1,53 @@
+KVS
+========
+
+[http://github.com/jugyo/kvs](http://github.com/jugyo/kvs)
+
+Description
+--------
+
+KVS is a simple key value store.
+
+Install
+--------
+
+ sudo gem install jugyo-kvs --source http://gems.github.com
+
+Usage
+--------
+
+ KVS.dir = '/tmp/kvs'
+
+ KVS['foo'] = 'bar'
+ puts KVS['foo'] #=> bar
+
+ key = KVS << 'jugyo'
+ puts KVS[key] #=> jugyo
+
+ KVS.delete('foo')
+
+License
+--------
+
+(The MIT License)
+
+Copyright (c) 2008-2009 jugyo
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,56 @@
+$:.unshift File.dirname(__FILE__) + '/lib'
+require 'rubygems'
+require 'kvs'
+require 'spec/rake/spectask'
+require 'rake/clean'
+require 'rake/gempackagetask'
+require 'rake/rdoctask'
+
+name = 'kvs'
+version = KVS::VERSION
+
+spec = Gem::Specification.new do |s|
+ s.name = name
+ s.version = version
+ s.summary = "simple key value store."
+ s.description = "KVS is a simple key value store."
+ s.files = %w(Rakefile README.markdown ChangeLog) + Dir.glob("{lib,spec}/**/*.rb")
+ s.authors = %w(jugyo)
+ s.email = 'jugyo.org@gmail.com'
+ s.homepage = 'http://github.com/jugyo/kvs'
+ s.rubyforge_project = 'kvs'
+ s.has_rdoc = false
+end
+
+Rake::GemPackageTask.new(spec) do |p|
+ p.need_tar = true
+end
+
+task :gemspec do
+ filename = "#{name}.gemspec"
+ open(filename, 'w') do |f|
+ f.write spec.to_ruby
+ end
+ puts <<-EOS
+ Successfully generated gemspec
+ Name: #{name}
+ Version: #{version}
+ File: #{filename}
+ EOS
+end
+
+task :install => [ :package ] do
+ sh %{sudo gem install pkg/#{name}-#{version}.gem}
+end
+
+task :uninstall => [ :clean ] do
+ sh %{sudo gem uninstall #{name}}
+end
+
+desc 'run all specs'
+Spec::Rake::SpecTask.new do |t|
+ t.spec_files = FileList['spec/**/*_spec.rb']
+ t.spec_opts = ['-c']
+end
+
+CLEAN.include ['pkg']
@@ -0,0 +1,28 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{kvs}
+ s.version = "0.1.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["jugyo"]
+ s.date = %q{2009-09-15}
+ s.description = %q{KVS is a simple key value store.}
+ s.email = %q{jugyo.org@gmail.com}
+ s.files = ["Rakefile", "README.markdown", "ChangeLog", "lib/kvs.rb", "spec/kvs_spec.rb"]
+ s.homepage = %q{http://github.com/jugyo/kvs}
+ s.require_paths = ["lib"]
+ s.rubyforge_project = %q{kvs}
+ s.rubygems_version = %q{1.3.4}
+ s.summary = %q{simple key value store.}
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ else
+ end
+ else
+ end
+end
@@ -0,0 +1,41 @@
+require 'digest/sha1'
+require 'tmpdir'
+
+module KVS
+ VERSION = '0.1.0'
+
+ class <<self
+ attr_accessor :dir
+
+ def <<(value)
+ key = key_gen(value)
+ self[id] = value
+ key
+ end
+
+ def [](key)
+ path = file_of(key)
+ return nil unless File.exists?(path)
+ File.read(path)
+ end
+
+ def []=(key, value)
+ File.open(file_of(key), 'wb') { |f| f << value.to_s }
+ end
+
+ def delete(key)
+ File.delete(file_of(key))
+ end
+
+ def file_of(key)
+ key = key.to_s
+ raise ArgumentError, 'invalid key' unless key =~ /^[\w]+$/
+ raise "dir is not specified" unless dir
+ File.join(dir, key)
+ end
+
+ def key_gen(value)
+ Digest::SHA1.hexdigest(value.to_s)
+ end
+ end
+end
@@ -0,0 +1,75 @@
+$:.unshift File.dirname(__FILE__) + '/../lib'
+require 'kvs'
+
+describe KVS do
+ before do
+ @tmpdir = Dir.tmpdir
+ KVS.dir = @tmpdir
+ end
+
+ it 'should generate file path' do
+ KVS.file_of('foo').should == @tmpdir + '/foo'
+ end
+
+ it 'should store data' do
+ KVS['foo'] = 'bar'
+ KVS['foo'].should == 'bar'
+ File.exists?(KVS.file_of('foo')).should be_true
+ end
+
+ it 'should store data with "<<"' do
+ key = KVS << 'test'
+ KVS[key].should == 'test'
+ File.exists?(KVS.file_of(key)).should be_true
+ end
+
+ it 'can also use Symbol as key' do
+ KVS[:foo] = 'test'
+ KVS['foo'].should == 'test'
+ KVS['bar'] = 'test test'
+ KVS[:bar].should == 'test test'
+ end
+
+ describe 'no value exists' do
+ it 'should return nil' do
+ KVS['x'].should be_nil
+ end
+ end
+
+ describe 'delete value' do
+ it 'should delete value' do
+ KVS['foo'] = 'bar'
+ KVS['foo'].should == 'bar'
+ KVS.delete('foo')
+ KVS['foo'].should == nil
+ File.exists?(KVS.file_of('foo')).should be_false
+ end
+ end
+
+ describe 'use valid key' do
+ it 'should not raise ArgumentError' do
+ lambda { KVS['foo'] }.should_not raise_error(ArgumentError)
+ lambda { KVS['_'] }.should_not raise_error(ArgumentError)
+ lambda { KVS['1'] }.should_not raise_error(ArgumentError)
+ end
+ end
+
+ describe 'use invalid key' do
+ it 'should raise ArgumentError' do
+ lambda { KVS['.'] }.should raise_error(ArgumentError)
+ lambda { KVS['../'] }.should raise_error(ArgumentError)
+ lambda { KVS['/'] }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe 'dir is nil' do
+ before do
+ KVS.dir = nil
+ end
+
+ it 'should raise ArgumentError' do
+ lambda { KVS['foo'] = 'bar' }.should raise_error(RuntimeError)
+ lambda { KVS['foo'] }.should raise_error(RuntimeError)
+ end
+ end
+end

0 comments on commit 06e1ea5

Please sign in to comment.