Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit dd80520dd0dbd411b39dd466bf9355512ebb2a56 @jnunemaker committed Jan 26, 2011
Showing with 814 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +13 −0 Gemfile
  3. +46 −0 Gemfile.lock
  4. +7 −0 Rakefile
  5. +23 −0 adapter-riak.gemspec
  6. +25 −0 examples/riak.rb
  7. +53 −0 lib/adapter/riak.rb
  8. +12 −0 lib/adapter/riak/conflict.rb
  9. +5 −0 lib/adapter/riak/version.rb
  10. +544 −0 log/test.log
  11. +31 −0 spec/helper.rb
  12. +40 −0 spec/riak_spec.rb
  13. +12 −0 spec/support/test_server.rb
@@ -0,0 +1,3 @@
+pkg/*
+*.gem
+.bundle
13 Gemfile
@@ -0,0 +1,13 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in adapter-redis.gemspec
+gemspec
+
+group(:development) do
+ gem 'curb'
+ gem 'yajl-ruby'
+ gem 'rspec', '~> 2.3'
+ gem 'log_buddy', '~> 0.5.0'
+ gem 'timecop', '~> 0.3.5'
+ gem 'i18n', '0.5.0'
+end
@@ -0,0 +1,46 @@
+PATH
+ remote: .
+ specs:
+ adapter-riak (0.5)
+ activesupport (~> 3.0.3)
+ adapter (~> 0.5.1)
+ riak-client (~> 0.8.3)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activesupport (3.0.3)
+ adapter (0.5.1)
+ builder (2.1.2)
+ curb (0.7.9)
+ diff-lcs (1.1.2)
+ i18n (0.5.0)
+ log_buddy (0.5.0)
+ riak-client (0.8.3)
+ builder (~> 2.1.2)
+ i18n (>= 0.4.0)
+ rspec (2.4.0)
+ rspec-core (~> 2.4.0)
+ rspec-expectations (~> 2.4.0)
+ rspec-mocks (~> 2.4.0)
+ rspec-core (2.4.0)
+ rspec-expectations (2.4.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.4.0)
+ timecop (0.3.5)
+ yajl-ruby (0.7.9)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ activesupport (~> 3.0.3)
+ adapter (~> 0.5.1)
+ adapter-riak!
+ curb
+ i18n (= 0.5.0)
+ log_buddy (~> 0.5.0)
+ riak-client (~> 0.8.3)
+ rspec (~> 2.3)
+ timecop (~> 0.3.5)
+ yajl-ruby
@@ -0,0 +1,7 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new
+
+task :default => :spec
@@ -0,0 +1,23 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "adapter/riak/version"
+
+Gem::Specification.new do |s|
+ s.name = "adapter-riak"
+ s.version = Adapter::Riak::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["John Nunemaker"]
+ s.email = ["nunemaker@gmail.com"]
+ s.homepage = ""
+ s.summary = %q{Adapter for riak}
+ s.description = %q{Adapter for riak}
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_dependency 'adapter', '~> 0.5.1'
+ s.add_dependency 'riak-client', '~> 0.8.3'
+ s.add_dependency 'activesupport', '~> 3.0.3'
+end
@@ -0,0 +1,25 @@
+require 'rubygems'
+require 'pathname'
+
+root_path = Pathname(__FILE__).dirname.join('..').expand_path
+lib_path = root_path.join('lib')
+$:.unshift(lib_path)
+
+require 'adapter/riak'
+
+client = Riak::Client.new['adapter_example']
+adapter = Adapter[:riak].new(client)
+adapter.clear
+
+adapter.write('foo', 'bar')
+puts 'Should be bar: ' + adapter.read('foo').inspect
+
+adapter.delete('foo')
+puts 'Should be nil: ' + adapter.read('foo').inspect
+
+adapter.write('foo', 'bar')
+adapter.clear
+puts 'Should be nil: ' + adapter.read('foo').inspect
+
+puts 'Should be bar: ' + adapter.fetch('foo', 'bar')
+puts 'Should be bar: ' + adapter.read('foo')
@@ -0,0 +1,53 @@
+require 'adapter'
+require 'adapter/riak/conflict'
+require 'riak'
+# present? that comes with riak-client is always
+# returning true and thus always raising read conflicts
+# need this until I can talk to maintainer or fix riak-client
+require 'active_support/core_ext/object/blank'
@jnunemaker

jnunemaker Jan 26, 2011

Owner

Sweet. I was going to make a failing test case for you. Thanks for being one step ahead. ;)

+
+module Adapter
+ module Riak
+ # Optimize key? to do head request in riak instead of full key read and nil check
+ def key?(key)
+ client.exists?(key_for(key))
+ end
+
+ def read(key)
+ robject = client.get(key_for(key))
+ raise Conflict.new(robject) if robject.conflict?
+ robject.data
+ rescue ::Riak::FailedRequest => e
+ e.code.to_i == 404 ? nil : raise(e)
+ end
+
+ def write(key, value)
+ key = key_for(key)
+ obj = client.get_or_new(key)
+ obj.content_type = 'application/json'
+ obj.data = value
+ obj.store
+ value
+ end
+
+ def delete(key)
+ read(key).tap { client.delete(key_for(key)) }
+ end
+
+ def clear
+ client.keys do |keys|
+ keys.each { |key| client.delete(key) }
+ end
+ end
+
+ def encode(value)
+ value
+ end
+
+ def decode(value)
+ value
+ end
+ end
+end
+
+Adapter.define(:riak, Adapter::Riak)
@@ -0,0 +1,12 @@
+module Adapter
+ module Riak
+ class Conflict < StandardError
+ attr_reader :robject
+
+ def initialize(robject)
+ @robject = robject
+ super('Read conflict present')
+ end
+ end
+ end
+end
@@ -0,0 +1,5 @@
+module Adapter
+ module Riak
+ VERSION = "0.5"
+ end
+end
Oops, something went wrong.

0 comments on commit dd80520

Please sign in to comment.