adapter-cassanity 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ # Changelog
2
+
3
+ ## 0.2.0
4
+
5
+ * Added adapter options: :read, :write, :delete. These can be used to tune consistency and all other operation arguments.
6
+ * Added method options (ie: read('key', using: {consistency: :quorum})). Method operations override any similar adapter options.
data/README.md CHANGED
@@ -29,6 +29,14 @@ client = apps
29
29
  adapter = Adapter[:cassanity].new(client)
30
30
  adapter.clear
31
31
 
32
+ # you can also set options for the adapter
33
+ using = {using: {consistency: :quorum}}
34
+ adapter = Adapter[:cassanity].new(client, {
35
+ read: using, # read using quorum consistency
36
+ write: using, # write using quorum consistency
37
+ delete: using, # delete using quorum consistency
38
+ })
39
+
32
40
  id = CassandraCQL::UUID.new
33
41
 
34
42
  adapter.read(id) # => nil
@@ -44,6 +52,10 @@ adapter.read(id) # => {'id' => ..., 'name' => 'GitHub'}
44
52
 
45
53
  adapter.clear
46
54
  adapter.read(id) # => nil
55
+
56
+ # You can also override adapter options per method:
57
+ # This will perform read with consistency set to ONE.
58
+ adapter.read(id, using: {consistency: :one})
47
59
  ```
48
60
 
49
61
  ## Installation
@@ -18,5 +18,5 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  gem.add_dependency 'adapter', '~> 0.6.3'
21
- gem.add_dependency 'cassanity', '~> 0.2.0'
21
+ gem.add_dependency 'cassanity', '~> 0.2.1'
22
22
  end
@@ -0,0 +1,16 @@
1
+ require_relative 'shared_setup'
2
+
3
+ client = AppsCF
4
+ using = {using: {consistency: :quorum}}
5
+ adapter = Adapter[:cassanity].new(client, {
6
+ read: using,
7
+ write: using,
8
+ delete: using,
9
+ })
10
+ adapter.clear
11
+
12
+ id = CassandraCQL::UUID.new
13
+
14
+ adapter.write(id, {name: 'GitHub'})
15
+ pp adapter.read(id)
16
+ adapter.delete(id)
@@ -1,31 +1,6 @@
1
- require 'rubygems'
2
- require 'pathname'
1
+ require_relative 'shared_setup'
3
2
 
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
3
+ client = AppsCF
29
4
  adapter = Adapter[:cassanity].new(client)
30
5
  adapter.clear
31
6
 
@@ -0,0 +1,11 @@
1
+ require_relative 'shared_setup'
2
+
3
+ client = AppsCF
4
+ adapter = Adapter[:cassanity].new(client)
5
+ adapter.clear
6
+
7
+ id = CassandraCQL::UUID.new
8
+
9
+ adapter.write(id, {name: 'GitHub'}, using: {consistency: :quorum})
10
+ pp adapter.read(id, using: {consistency: :quorum})
11
+ adapter.delete(id)
@@ -0,0 +1,27 @@
1
+ require 'pp'
2
+ require 'pathname'
3
+ require 'rubygems'
4
+
5
+ root_path = Pathname(__FILE__).dirname.join('..').expand_path
6
+ lib_path = root_path.join('lib')
7
+ $:.unshift(lib_path)
8
+
9
+ require 'adapter/cassanity'
10
+
11
+ client = CassandraCQL::Database.new('127.0.0.1:9160')
12
+ executor = Cassanity::Executors::CassandraCql.new(client: client)
13
+ connection = Cassanity::Connection.new(executor: executor)
14
+ keyspace = connection.keyspace('adapter_cassanity')
15
+ keyspace.recreate
16
+
17
+ AppsCF = keyspace.column_family(:apps, {
18
+ schema: Cassanity::Schema.new({
19
+ primary_key: :id,
20
+ columns: {
21
+ id: :timeuuid,
22
+ name: :text,
23
+ }
24
+ }),
25
+ })
26
+
27
+ AppsCF.create
@@ -8,24 +8,37 @@ module Adapter
8
8
 
9
9
  def_delegator :@client, :schema
10
10
 
11
- def read(key)
12
- rows = client.select(where: where(key))
11
+ # Public
12
+ def read(key, options = nil)
13
+ operation_options = {where: where(key)}
14
+ adapter_options = @options[:read]
15
+ arguments = update_arguments(operation_options, adapter_options, options)
16
+
17
+ rows = @client.select(arguments)
13
18
  rows.empty? ? nil : decode(rows.first)
14
19
  end
15
20
 
16
- def write(key, attributes)
17
- client.update({
18
- set: encode(attributes),
19
- where: where(key),
20
- })
21
+ # Public
22
+ def write(key, attributes, options = nil)
23
+ operation_options = {set: encode(attributes), where: where(key)}
24
+ adapter_options = @options[:write]
25
+ arguments = update_arguments(operation_options, adapter_options, options)
26
+
27
+ @client.update(arguments)
21
28
  end
22
29
 
23
- def delete(key)
24
- client.delete(where: where(key))
30
+ # Public
31
+ def delete(key, options = nil)
32
+ operation_options = {where: where(key)}
33
+ adapter_options = @options[:delete]
34
+ arguments = update_arguments(operation_options, adapter_options, options)
35
+
36
+ @client.delete(arguments)
25
37
  end
26
38
 
39
+ # Public
27
40
  def clear
28
- client.truncate
41
+ @client.truncate
29
42
  end
30
43
 
31
44
  # Private
@@ -37,6 +50,23 @@ module Adapter
37
50
  {primary_key => criteria}
38
51
  end
39
52
  end
53
+
54
+ # Private
55
+ def update_arguments(operation_options, adapter_options, method_options)
56
+ keys = operation_options.keys
57
+
58
+ if !adapter_options.nil? && !adapter_options.empty?
59
+ filtered_options = adapter_options.reject { |key| keys.include?(key) }
60
+ operation_options.update(filtered_options)
61
+ end
62
+
63
+ if !method_options.nil? && !method_options.empty?
64
+ filtered_options = method_options.reject { |key| keys.include?(key) }
65
+ operation_options.update(filtered_options)
66
+ end
67
+
68
+ operation_options
69
+ end
40
70
  end
41
71
  end
42
72
 
@@ -1,5 +1,5 @@
1
1
  module Adapter
2
2
  module Cassanity
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -1,6 +1,7 @@
1
1
  require 'helper'
2
2
 
3
3
  describe "Cassanity adapter" do
4
+ let(:adapter_name) { :cassanity }
4
5
 
5
6
  context "single primary key" do
6
7
  before do
@@ -9,14 +10,160 @@ describe "Cassanity adapter" do
9
10
  @adapter.clear
10
11
  end
11
12
 
12
- let(:adapter_name) { :cassanity }
13
-
14
13
  let(:adapter) { @adapter }
15
14
  let(:client) { @client }
16
15
 
17
16
  it_behaves_like 'an adapter'
18
17
  end
19
18
 
19
+ context "with adapter read options" do
20
+ it "uses read options for read method" do
21
+ client = COLUMN_FAMILIES[:single]
22
+ options = {read: {
23
+ using: {consistency: :quorum},
24
+ }}
25
+ adapter = Adapter[adapter_name].new(client, options)
26
+
27
+ client.should_receive(:select).with({
28
+ where: {:some_key => 'foo'},
29
+ using: {consistency: :quorum},
30
+ }).and_return([])
31
+
32
+ adapter.read('foo')
33
+ end
34
+
35
+ it "does not override where" do
36
+ client = COLUMN_FAMILIES[:single]
37
+ options = {read: {
38
+ where: {:some_key => 'bar'},
39
+ using: {consistency: :quorum},
40
+ }}
41
+ adapter = Adapter[adapter_name].new(client, options)
42
+
43
+ client.should_receive(:select).with({
44
+ where: {:some_key => 'foo'},
45
+ using: {consistency: :quorum},
46
+ }).and_return([])
47
+
48
+ adapter.read('foo')
49
+ end
50
+
51
+ it "can be overriden by read method options" do
52
+ client = COLUMN_FAMILIES[:single]
53
+ options = {read: {
54
+ using: {consistency: :quorum},
55
+ }}
56
+ adapter = Adapter[adapter_name].new(client, options)
57
+
58
+ client.should_receive(:select).with({
59
+ where: {:some_key => 'foo'},
60
+ using: {consistency: :one},
61
+ }).and_return([])
62
+
63
+ adapter.read('foo', using: {consistency: :one})
64
+ end
65
+ end
66
+
67
+ context "with adapter write options" do
68
+ it "uses write options for write method" do
69
+ client = COLUMN_FAMILIES[:single]
70
+ options = {write: {
71
+ using: {consistency: :quorum},
72
+ }}
73
+ adapter = Adapter[adapter_name].new(client, options)
74
+
75
+ client.should_receive(:update).with({
76
+ set: {'name' => 'New Name'},
77
+ where: {:some_key => 'foo'},
78
+ using: {consistency: :quorum},
79
+ })
80
+
81
+ adapter.write('foo', {'name' => 'New Name'})
82
+ end
83
+
84
+ it "does not override where or set" do
85
+ client = COLUMN_FAMILIES[:single]
86
+ options = {write: {
87
+ where: {:some_key => 'should not use this'},
88
+ set: {'name' => 'should not use this'},
89
+ using: {consistency: :quorum,
90
+ }}}
91
+ adapter = Adapter[adapter_name].new(client, options)
92
+
93
+ client.should_receive(:update).with({
94
+ set: {'name' => 'New Name'},
95
+ where: {:some_key => 'foo'},
96
+ using: {consistency: :quorum},
97
+ })
98
+
99
+ adapter.write('foo', {'name' => 'New Name'})
100
+ end
101
+
102
+ it "can be overriden by write method options" do
103
+ client = COLUMN_FAMILIES[:single]
104
+ options = {write: {
105
+ using: {consistency: :quorum},
106
+ }}
107
+ adapter = Adapter[adapter_name].new(client, options)
108
+
109
+ client.should_receive(:update).with({
110
+ set: {'name' => 'New Name'},
111
+ where: {:some_key => 'foo'},
112
+ using: {consistency: :one},
113
+ })
114
+
115
+ adapter.write('foo', {'name' => 'New Name'}, using: {consistency: :one})
116
+ end
117
+ end
118
+
119
+ context "with adapter delete options" do
120
+ it "uses delete options for delete method" do
121
+ client = COLUMN_FAMILIES[:single]
122
+ options = {delete: {
123
+ using: {consistency: :quorum},
124
+ }}
125
+ adapter = Adapter[adapter_name].new(client, options)
126
+
127
+ client.should_receive(:delete).with({
128
+ where: {:some_key => 'foo'},
129
+ using: {consistency: :quorum},
130
+ })
131
+
132
+ adapter.delete('foo')
133
+ end
134
+
135
+ it "does not override where" do
136
+ client = COLUMN_FAMILIES[:single]
137
+ options = {delete: {
138
+ where: {:some_key => 'bar'},
139
+ using: {consistency: :quorum},
140
+ }}
141
+ adapter = Adapter[adapter_name].new(client, options)
142
+
143
+ client.should_receive(:delete).with({
144
+ where: {:some_key => 'foo'},
145
+ using: {consistency: :quorum},
146
+ })
147
+
148
+ adapter.delete('foo')
149
+ end
150
+
151
+ it "can be overriden by delete method options" do
152
+ client = COLUMN_FAMILIES[:single]
153
+ options = {delete: {
154
+ using: {consistency: :quorum},
155
+ }}
156
+ adapter = Adapter[adapter_name].new(client, options)
157
+
158
+ client.should_receive(:delete).with({
159
+ where: {:some_key => 'foo'},
160
+ using: {consistency: :one},
161
+ })
162
+
163
+ adapter.delete('foo', using: {consistency: :one})
164
+ end
165
+ end
166
+
20
167
  context "composite primary key" do
21
168
  before do
22
169
  @client = COLUMN_FAMILIES[:composite]
@@ -24,12 +171,10 @@ describe "Cassanity adapter" do
24
171
  @adapter.clear
25
172
  end
26
173
 
27
- let(:adapter_name) { :cassanity }
28
-
29
- let(:adapter) { @adapter }
30
- let(:client) { @client }
31
-
32
174
  it_behaves_like 'an adapter' do
175
+ let(:adapter) { @adapter }
176
+ let(:client) { @client }
177
+
33
178
  let(:key) { {:bucket => '1', :id => CassandraCQL::UUID.new} }
34
179
  let(:key2) { {:bucket => '2', :id => CassandraCQL::UUID.new} }
35
180
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adapter-cassanity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-09 00:00:00.000000000 Z
12
+ date: 2012-11-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: adapter
@@ -34,7 +34,7 @@ dependencies:
34
34
  requirements:
35
35
  - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: 0.2.0
37
+ version: 0.2.1
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +42,7 @@ dependencies:
42
42
  requirements:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: 0.2.0
45
+ version: 0.2.1
46
46
  description: Adapter for Cassanity
47
47
  email:
48
48
  - nunemaker@gmail.com
@@ -53,13 +53,17 @@ files:
53
53
  - .gitignore
54
54
  - .rspec
55
55
  - .travis.yml
56
+ - Changelog.md
56
57
  - Gemfile
57
58
  - Guardfile
58
59
  - LICENSE.txt
59
60
  - README.md
60
61
  - Rakefile
61
62
  - adapter-cassanity.gemspec
63
+ - examples/adapter_options.rb
62
64
  - examples/cassanity.rb
65
+ - examples/method_options.rb
66
+ - examples/shared_setup.rb
63
67
  - lib/adapter-cassanity.rb
64
68
  - lib/adapter/cassanity.rb
65
69
  - lib/adapter/cassanity/version.rb
@@ -79,7 +83,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
79
83
  version: '0'
80
84
  segments:
81
85
  - 0
82
- hash: -267742197424905858
86
+ hash: 135504894243960004
83
87
  required_rubygems_version: !ruby/object:Gem::Requirement
84
88
  none: false
85
89
  requirements:
@@ -88,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
92
  version: '0'
89
93
  segments:
90
94
  - 0
91
- hash: -267742197424905858
95
+ hash: 135504894243960004
92
96
  requirements: []
93
97
  rubyforge_project:
94
98
  rubygems_version: 1.8.23