Browse files

Initial commit of working version.

  • Loading branch information...
0 parents commit ef435f5fd0261f9f04efc9a8675a208cf201c5ed @jnunemaker committed Nov 8, 2012
Showing with 348 additions and 0 deletions.
  1. +17 −0 .gitignore
  2. +1 −0 .rspec
  3. +11 −0 Gemfile
  4. +11 −0 Guardfile
  5. +22 −0 LICENSE.txt
  6. +69 −0 README.md
  7. +6 −0 Rakefile
  8. +22 −0 adapter-cassanity.gemspec
  9. +44 −0 examples/cassanity.rb
  10. +1 −0 lib/adapter-cassanity.rb
  11. +43 −0 lib/adapter/cassanity.rb
  12. +5 −0 lib/adapter/cassanity/version.rb
  13. +40 −0 spec/cassanity_spec.rb
  14. +56 −0 spec/helper.rb
17 .gitignore
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
1 .rspec
@@ -0,0 +1 @@
+--color
11 Gemfile
@@ -0,0 +1,11 @@
+source 'https://rubygems.org'
+gemspec
+
+gem 'rake'
+gem 'rspec', '~> 2.8'
+
+group(:guard) do
+ gem 'guard', '~> 1.0.0'
+ gem 'guard-rspec', '~> 0.6.0'
+ gem 'guard-bundler', '~> 0.1.0'
+end
11 Guardfile
@@ -0,0 +1,11 @@
+guard 'bundler' do
+ watch('Gemfile')
+ watch(/^.+\.gemspec/)
+end
+
+guard 'rspec', :version => 2 do
+ watch(%r{^spec/.+_spec\.rb$}) { "spec" }
+ watch(%r{^lib/(.+)\.rb$}) { "spec" }
+ watch('spec/helper.rb') { "spec" }
+end
+
22 LICENSE.txt
@@ -0,0 +1,22 @@
+Copyright (c) 2012 John Nunemaker
+
+MIT License
+
+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.
69 README.md
@@ -0,0 +1,69 @@
+# Adapter::Cassanity
+
+Adapter for Cassanity.
+
+## Usage
+
+```ruby
+require 'adapter/cassanity'
+
+client = CassandraCQL::Database.new('127.0.0.1:9160')
+executor = Cassanity::Executors::CassandraCql.new(client: client)
+connection = Cassanity::Connection.new(executor: executor)
+keyspace = connection.keyspace('adapter_cassanity')
+keyspace.recreate
+
+apps = keyspace.column_family(:apps, {
+ schema: Cassanity::Schema.new({
+ primary_key: :id,
+ columns: {
+ id: :timeuuid,
+ name: :text,
+ }
+ }),
+})
+
+apps.create
+
+client = apps
+adapter = Adapter[:cassanity].new(client)
+adapter.clear
+
+id = CassandraCQL::UUID.new
+
+adapter.read(id) # => nil
+
+adapter.write(id, name: 'GitHub')
+adapter.read(id) # => {'id' => ..., 'name' => 'GitHub'}
+
+adapter.delete(id)
+adapter.read(id) # => nil
+
+adapter.write(id, name: 'GitHub')
+adapter.read(id) # => {'id' => ..., 'name' => 'GitHub'}
+
+adapter.clear
+adapter.read(id) # => nil
+```
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'adapter-cassanity'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install adapter-cassanity
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Add some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
6 Rakefile
@@ -0,0 +1,6 @@
+require 'bundler/gem_tasks'
+
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new
+
+task :default => :spec
22 adapter-cassanity.gemspec
@@ -0,0 +1,22 @@
+# -*- encoding: utf-8 -*-
+lib = File.expand_path('../lib', __FILE__)
+$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
+require 'adapter/cassanity/version'
+
+Gem::Specification.new do |gem|
+ gem.name = "adapter-cassanity"
+ gem.version = Adapter::Cassanity::VERSION
+ gem.authors = ["John Nunemaker"]
+ gem.email = ["nunemaker@gmail.com"]
+ gem.description = %q{Adapter for Cassanity}
+ gem.summary = %q{Adapter for Cassanity}
+ gem.homepage = ""
+
+ gem.files = `git ls-files`.split($/)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.require_paths = ["lib"]
+
+ gem.add_dependency 'adapter', '~> 0.6.3'
+ gem.add_dependency 'cassanity', '~> 0.2.0'
+end
44 examples/cassanity.rb
@@ -0,0 +1,44 @@
+require 'rubygems'
+require 'pathname'
+
+root_path = Pathname(__FILE__).dirname.join('..').expand_path
+lib_path = root_path.join('lib')
+$:.unshift(lib_path)
+
+require 'adapter/cassanity'
+
+client = CassandraCQL::Database.new('127.0.0.1:9160')
+executor = Cassanity::Executors::CassandraCql.new(client: client)
+connection = Cassanity::Connection.new(executor: executor)
+keyspace = connection.keyspace('adapter_cassanity')
+keyspace.recreate
+
+apps = keyspace.column_family(:apps, {
+ schema: Cassanity::Schema.new({
+ primary_key: :id,
+ columns: {
+ id: :timeuuid,
+ name: :text,
+ }
+ }),
+})
+
+apps.create
+
+client = apps
+adapter = Adapter[:cassanity].new(client)
+adapter.clear
+
+id = CassandraCQL::UUID.new
+
+adapter.write(id, name: 'GitHub')
+puts 'Should be {id: ..., name: "GitHub"}: ' + adapter.read(id).inspect
+
+adapter.delete(id)
+puts 'Should be nil: ' + adapter.read(id).inspect
+
+adapter.write(id, name: 'GitHub')
+adapter.clear
+puts 'Should be nil: ' + adapter.read(id).inspect
+
+puts 'Should be {foo: "bar"}: ' + adapter.fetch(id, {foo: 'bar'}).inspect
1 lib/adapter-cassanity.rb
@@ -0,0 +1 @@
+require 'adapter/cassanity'
43 lib/adapter/cassanity.rb
@@ -0,0 +1,43 @@
+require 'adapter'
+require 'forwardable'
+require 'cassanity'
+
+module Adapter
+ module Cassanity
+ extend Forwardable
+
+ def_delegator :@client, :schema
+
+ def read(key)
+ rows = client.select(where: where(key))
+ rows.empty? ? nil : decode(rows.first)
+ end
+
+ def write(key, attributes)
+ client.update({
+ set: encode(attributes),
+ where: where(key),
+ })
+ end
+
+ def delete(key)
+ client.delete(where: where(key))
+ end
+
+ def clear
+ client.truncate
+ end
+
+ # Private
+ def where(criteria)
+ if schema.composite_primary_key?
+ criteria
+ else
+ primary_key = schema.primary_keys.first
+ {primary_key => criteria}
+ end
+ end
+ end
+end
+
+Adapter.define(:cassanity, Adapter::Cassanity)
5 lib/adapter/cassanity/version.rb
@@ -0,0 +1,5 @@
+module Adapter
+ module Cassanity
+ VERSION = "0.0.1"
+ end
+end
40 spec/cassanity_spec.rb
@@ -0,0 +1,40 @@
+require 'helper'
+
+describe "Cassanity adapter" do
+
+ context "single primary key" do
+ before do
+ @client = COLUMN_FAMILIES[:single]
+ @adapter = Adapter[adapter_name].new(@client)
+ @adapter.clear
+ end
+
+ let(:adapter_name) { :cassanity }
+
+ let(:adapter) { @adapter }
+ let(:client) { @client }
+
+ it_behaves_like 'an adapter'
+ end
+
+ context "composite primary key" do
+ before do
+ @client = COLUMN_FAMILIES[:composite]
+ @adapter = Adapter[adapter_name].new(@client)
+ @adapter.clear
+ end
+
+ let(:adapter_name) { :cassanity }
+
+ let(:adapter) { @adapter }
+ let(:client) { @client }
+
+ it_behaves_like 'an adapter' do
+ let(:key) { {:bucket => '1', :id => CassandraCQL::UUID.new} }
+ let(:key2) { {:bucket => '2', :id => CassandraCQL::UUID.new} }
+
+ let(:unavailable_key) { {:bucket => '3', :id => CassandraCQL::UUID.new} }
+ let(:unavailable_key2) { {:bucket => '4', :id => CassandraCQL::UUID.new} }
+ end
+ end
+end
56 spec/helper.rb
@@ -0,0 +1,56 @@
+$:.unshift(File.expand_path('../../lib', __FILE__))
+$:.unshift(File.expand_path('../', __FILE__))
+
+require 'rubygems'
+require 'bundler'
+
+Bundler.require(:default, :test)
+
+require 'adapter/spec/an_adapter'
+require 'adapter-cassanity'
+
+COLUMN_FAMILIES = {}
+
+cassandra_setup = lambda { |args|
+ host = ENV.fetch('CASSANDRA_HOST', '127.0.0.1:9160')
+ keyspace_name = ENV.fetch('CASSANDRA_KEYSPACE_NAME', 'adapter_cassanity')
+ client = CassandraCQL::Database.new(host)
+ executor = Cassanity::Executors::CassandraCql.new(client: client)
+ connection = Cassanity::Connection.new(executor: executor)
+ keyspace = connection.keyspace(keyspace_name)
+ keyspace.recreate
+
+ COLUMN_FAMILIES[:single] = keyspace.column_family(:single, {
+ schema: Cassanity::Schema.new({
+ primary_key: :some_key,
+ columns: {
+ some_key: :text,
+ one: :text,
+ two: :text,
+ three: :text,
+ four: :text,
+ }
+ }),
+ })
+
+ COLUMN_FAMILIES[:composite] = keyspace.column_family(:composite, {
+ schema: Cassanity::Schema.new({
+ primary_key: [:bucket, :id],
+ columns: {
+ bucket: :text,
+ id: :timeuuid,
+ one: :text,
+ two: :text,
+ three: :text,
+ four: :text,
+ }
+ }),
+ })
+
+ COLUMN_FAMILIES[:single].create
+ COLUMN_FAMILIES[:composite].create
+}
+
+RSpec.configure do |config|
+ config.before :suite, &cassandra_setup
+end

0 comments on commit ef435f5

Please sign in to comment.