adapter-cassanity 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ gemfile:
2
+ - Gemfile
3
+ language: ruby
4
+ rvm:
5
+ - 1.9.3
6
+ notifications:
7
+ email: false
8
+ bundler_args: --without guard
9
+ services:
10
+ - cassandra
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ gem 'rake'
5
+ gem 'rspec', '~> 2.8'
6
+
7
+ group(:guard) do
8
+ gem 'guard', '~> 1.0.0'
9
+ gem 'guard-rspec', '~> 0.6.0'
10
+ gem 'guard-bundler', '~> 0.1.0'
11
+ end
data/Guardfile ADDED
@@ -0,0 +1,11 @@
1
+ guard 'bundler' do
2
+ watch('Gemfile')
3
+ watch(/^.+\.gemspec/)
4
+ end
5
+
6
+ guard 'rspec', :version => 2 do
7
+ watch(%r{^spec/.+_spec\.rb$}) { "spec" }
8
+ watch(%r{^lib/(.+)\.rb$}) { "spec" }
9
+ watch('spec/helper.rb') { "spec" }
10
+ end
11
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 John Nunemaker
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Adapter::Cassanity
2
+
3
+ Adapter for Cassanity.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ require 'adapter/cassanity'
9
+
10
+ client = CassandraCQL::Database.new('127.0.0.1:9160')
11
+ executor = Cassanity::Executors::CassandraCql.new(client: client)
12
+ connection = Cassanity::Connection.new(executor: executor)
13
+ keyspace = connection.keyspace('adapter_cassanity')
14
+ keyspace.recreate
15
+
16
+ apps = keyspace.column_family(:apps, {
17
+ schema: Cassanity::Schema.new({
18
+ primary_key: :id,
19
+ columns: {
20
+ id: :timeuuid,
21
+ name: :text,
22
+ }
23
+ }),
24
+ })
25
+
26
+ apps.create
27
+
28
+ client = apps
29
+ adapter = Adapter[:cassanity].new(client)
30
+ adapter.clear
31
+
32
+ id = CassandraCQL::UUID.new
33
+
34
+ adapter.read(id) # => nil
35
+
36
+ adapter.write(id, name: 'GitHub')
37
+ adapter.read(id) # => {'id' => ..., 'name' => 'GitHub'}
38
+
39
+ adapter.delete(id)
40
+ adapter.read(id) # => nil
41
+
42
+ adapter.write(id, name: 'GitHub')
43
+ adapter.read(id) # => {'id' => ..., 'name' => 'GitHub'}
44
+
45
+ adapter.clear
46
+ adapter.read(id) # => nil
47
+ ```
48
+
49
+ ## Installation
50
+
51
+ Add this line to your application's Gemfile:
52
+
53
+ gem 'adapter-cassanity'
54
+
55
+ And then execute:
56
+
57
+ $ bundle
58
+
59
+ Or install it yourself as:
60
+
61
+ $ gem install adapter-cassanity
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'adapter/cassanity/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "adapter-cassanity"
8
+ gem.version = Adapter::Cassanity::VERSION
9
+ gem.authors = ["John Nunemaker"]
10
+ gem.email = ["nunemaker@gmail.com"]
11
+ gem.description = %q{Adapter for Cassanity}
12
+ gem.summary = %q{Adapter for Cassanity}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'adapter', '~> 0.6.3'
21
+ gem.add_dependency 'cassanity', '~> 0.2.0'
22
+ end
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'pathname'
3
+
4
+ root_path = Pathname(__FILE__).dirname.join('..').expand_path
5
+ lib_path = root_path.join('lib')
6
+ $:.unshift(lib_path)
7
+
8
+ require 'adapter/cassanity'
9
+
10
+ client = CassandraCQL::Database.new('127.0.0.1:9160')
11
+ executor = Cassanity::Executors::CassandraCql.new(client: client)
12
+ connection = Cassanity::Connection.new(executor: executor)
13
+ keyspace = connection.keyspace('adapter_cassanity')
14
+ keyspace.recreate
15
+
16
+ apps = keyspace.column_family(:apps, {
17
+ schema: Cassanity::Schema.new({
18
+ primary_key: :id,
19
+ columns: {
20
+ id: :timeuuid,
21
+ name: :text,
22
+ }
23
+ }),
24
+ })
25
+
26
+ apps.create
27
+
28
+ client = apps
29
+ adapter = Adapter[:cassanity].new(client)
30
+ adapter.clear
31
+
32
+ id = CassandraCQL::UUID.new
33
+
34
+ adapter.write(id, name: 'GitHub')
35
+ puts 'Should be {id: ..., name: "GitHub"}: ' + adapter.read(id).inspect
36
+
37
+ adapter.delete(id)
38
+ puts 'Should be nil: ' + adapter.read(id).inspect
39
+
40
+ adapter.write(id, name: 'GitHub')
41
+ adapter.clear
42
+ puts 'Should be nil: ' + adapter.read(id).inspect
43
+
44
+ puts 'Should be {foo: "bar"}: ' + adapter.fetch(id, {foo: 'bar'}).inspect
@@ -0,0 +1,5 @@
1
+ module Adapter
2
+ module Cassanity
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,43 @@
1
+ require 'adapter'
2
+ require 'forwardable'
3
+ require 'cassanity'
4
+
5
+ module Adapter
6
+ module Cassanity
7
+ extend Forwardable
8
+
9
+ def_delegator :@client, :schema
10
+
11
+ def read(key)
12
+ rows = client.select(where: where(key))
13
+ rows.empty? ? nil : decode(rows.first)
14
+ end
15
+
16
+ def write(key, attributes)
17
+ client.update({
18
+ set: encode(attributes),
19
+ where: where(key),
20
+ })
21
+ end
22
+
23
+ def delete(key)
24
+ client.delete(where: where(key))
25
+ end
26
+
27
+ def clear
28
+ client.truncate
29
+ end
30
+
31
+ # Private
32
+ def where(criteria)
33
+ if schema.composite_primary_key?
34
+ criteria
35
+ else
36
+ primary_key = schema.primary_keys.first
37
+ {primary_key => criteria}
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ Adapter.define(:cassanity, Adapter::Cassanity)
@@ -0,0 +1 @@
1
+ require 'adapter/cassanity'
@@ -0,0 +1,40 @@
1
+ require 'helper'
2
+
3
+ describe "Cassanity adapter" do
4
+
5
+ context "single primary key" do
6
+ before do
7
+ @client = COLUMN_FAMILIES[:single]
8
+ @adapter = Adapter[adapter_name].new(@client)
9
+ @adapter.clear
10
+ end
11
+
12
+ let(:adapter_name) { :cassanity }
13
+
14
+ let(:adapter) { @adapter }
15
+ let(:client) { @client }
16
+
17
+ it_behaves_like 'an adapter'
18
+ end
19
+
20
+ context "composite primary key" do
21
+ before do
22
+ @client = COLUMN_FAMILIES[:composite]
23
+ @adapter = Adapter[adapter_name].new(@client)
24
+ @adapter.clear
25
+ end
26
+
27
+ let(:adapter_name) { :cassanity }
28
+
29
+ let(:adapter) { @adapter }
30
+ let(:client) { @client }
31
+
32
+ it_behaves_like 'an adapter' do
33
+ let(:key) { {:bucket => '1', :id => CassandraCQL::UUID.new} }
34
+ let(:key2) { {:bucket => '2', :id => CassandraCQL::UUID.new} }
35
+
36
+ let(:unavailable_key) { {:bucket => '3', :id => CassandraCQL::UUID.new} }
37
+ let(:unavailable_key2) { {:bucket => '4', :id => CassandraCQL::UUID.new} }
38
+ end
39
+ end
40
+ end
data/spec/helper.rb ADDED
@@ -0,0 +1,66 @@
1
+ $:.unshift(File.expand_path('../../lib', __FILE__))
2
+ $:.unshift(File.expand_path('../', __FILE__))
3
+
4
+ require 'rubygems'
5
+ require 'bundler'
6
+ require 'logger'
7
+ require 'pathname'
8
+
9
+ Bundler.require(:default, :test)
10
+
11
+ require 'adapter/spec/an_adapter'
12
+ require 'adapter-cassanity'
13
+
14
+ log_path = Pathname(__FILE__).join('..', '..', 'log').expand_path
15
+ log_path.mkpath
16
+
17
+ logger = Logger.new(log_path.join('test.log'))
18
+
19
+ COLUMN_FAMILIES = {}
20
+
21
+ cassandra_setup = lambda { |args|
22
+ host = ENV.fetch('CASSANDRA_HOST', '127.0.0.1:9160')
23
+ keyspace_name = ENV.fetch('CASSANDRA_KEYSPACE_NAME', 'adapter_cassanity')
24
+ client = CassandraCQL::Database.new(host, cql_version: '3.0.0')
25
+ executor = Cassanity::Executors::CassandraCql.new({
26
+ client: client,
27
+ logger: logger,
28
+ })
29
+ connection = Cassanity::Connection.new(executor: executor)
30
+ keyspace = connection.keyspace(keyspace_name)
31
+ keyspace.recreate
32
+
33
+ COLUMN_FAMILIES[:single] = keyspace.column_family(:single, {
34
+ schema: Cassanity::Schema.new({
35
+ primary_key: :some_key,
36
+ columns: {
37
+ some_key: :text,
38
+ one: :text,
39
+ two: :text,
40
+ three: :text,
41
+ four: :text,
42
+ }
43
+ }),
44
+ })
45
+
46
+ COLUMN_FAMILIES[:composite] = keyspace.column_family(:composite, {
47
+ schema: Cassanity::Schema.new({
48
+ primary_key: [:bucket, :id],
49
+ columns: {
50
+ bucket: :text,
51
+ id: :timeuuid,
52
+ one: :text,
53
+ two: :text,
54
+ three: :text,
55
+ four: :text,
56
+ }
57
+ }),
58
+ })
59
+
60
+ COLUMN_FAMILIES[:single].create
61
+ COLUMN_FAMILIES[:composite].create
62
+ }
63
+
64
+ RSpec.configure do |config|
65
+ config.before :suite, &cassandra_setup
66
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adapter-cassanity
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - John Nunemaker
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: adapter
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.6.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.6.3
30
+ - !ruby/object:Gem::Dependency
31
+ name: cassanity
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.2.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.2.0
46
+ description: Adapter for Cassanity
47
+ email:
48
+ - nunemaker@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - .travis.yml
56
+ - Gemfile
57
+ - Guardfile
58
+ - LICENSE.txt
59
+ - README.md
60
+ - Rakefile
61
+ - adapter-cassanity.gemspec
62
+ - examples/cassanity.rb
63
+ - lib/adapter-cassanity.rb
64
+ - lib/adapter/cassanity.rb
65
+ - lib/adapter/cassanity/version.rb
66
+ - spec/cassanity_spec.rb
67
+ - spec/helper.rb
68
+ homepage: ''
69
+ licenses: []
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ segments:
81
+ - 0
82
+ hash: -267742197424905858
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ segments:
90
+ - 0
91
+ hash: -267742197424905858
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 1.8.23
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Adapter for Cassanity
98
+ test_files:
99
+ - spec/cassanity_spec.rb
100
+ - spec/helper.rb