riaktor 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -68,11 +68,12 @@ module Riaktor
68
68
  op['params']["dw"] ||= 0
69
69
  op['params']["returnbody"] ||= true
70
70
  op['headers']["Content-Type"] ||= "text/plain"
71
+ op['headers']["Accept"] ||= "text/plain"
71
72
  op['headers']["X-Riak-ClientId"] ||= self.client_id
72
73
  request = Typhoeus::Request.new(self.build_url(op['bucket'],op['key']),
73
74
  :method => :put, :headers => op['headers'], :params => op['params'], :body => op['value'].to_s)
74
75
  request.on_complete do |response|
75
- results[[op["bucket"],op["key"]]] = { "headers" => response.headers_hash,
76
+ results[[op["bucket"],op["key"]]] = { "raw_headers" => response.headers, "headers" => response.headers_hash,
76
77
  "body" => response.body, "code" => response.code }
77
78
  end
78
79
  hydra.queue request
@@ -96,11 +97,12 @@ module Riaktor
96
97
  operations.each do |op|
97
98
  op['params']["r"] ||= 2
98
99
  op['headers']["Content-Type"] ||= "text/plain"
100
+ op['headers']["Accept"] ||= "text/plain"
99
101
  op['headers']["X-Riak-ClientId"] ||= self.client_id
100
102
  request = Typhoeus::Request.new(self.build_url(op['bucket'],op['key']),
101
103
  :method => :get, :headers => op['headers'], :params => op['params'])
102
104
  request.on_complete do |response|
103
- results[[op["bucket"], op["key"]]] = { "headers" => response.headers_hash,
105
+ results[[op["bucket"], op["key"]]] = { "raw_headers" => response.headers, "headers" => response.headers_hash,
104
106
  "body" => response.body, "code" => response.code }
105
107
  end
106
108
  hydra.queue request
@@ -123,10 +125,11 @@ module Riaktor
123
125
  hydra = Typhoeus::Hydra.new(:max_concurrency => self.max_concurrency)
124
126
  operations.each do |op|
125
127
  op['headers']["X-Riak-ClientId"] ||= self.client_id
128
+ op['headers']["Accept"] ||= "text/plain"
126
129
  request = Typhoeus::Request.new(self.build_url(op['bucket'],op['key']),
127
130
  :method => :delete, :headers => op['headers'], :params => op['params'])
128
131
  request.on_complete do |response|
129
- results[[op["bucket"], op["key"]]] = { "headers" => response.headers_hash,
132
+ results[[op["bucket"], op["key"]]] = { "raw_headers" => response.headers, "headers" => response.headers_hash,
130
133
  "body" => response.body, "code" => response.code }
131
134
  end
132
135
  hydra.queue request
@@ -40,16 +40,18 @@ module Riaktor
40
40
  :value => decode_value(resp["body"]),
41
41
  :vector_clock => resp["headers"]["X-Riak-Vclock"],
42
42
  :links => Link.from_header(resp["headers"]["Link"]),
43
- :new => false)
43
+ :new => false,
44
+ :raw_response => resp)
44
45
  when 300 then
45
46
  found[key] = new(:key => key,
46
47
  :bucket => bucket,
47
48
  :value => nil,
48
49
  :vector_clock => resp["headers"]["X-Riak-Vclock"],
49
50
  :links => Link.from_header(resp["headers"]["Link"]),
50
- :new => false)
51
+ :new => false,
52
+ :raw_response => resp)
51
53
  vtags = resp["body"].split("\n")[1..-1]
52
- found[key].load_siblings(vtags,{"r" => opts["r"] || 2})
54
+ found[key].load_siblings_from_vtags(vtags,{"r" => opts["r"] || 2})
53
55
  else
54
56
  found[key] = nil
55
57
  end
@@ -78,7 +80,13 @@ module Riaktor
78
80
  end # ClassMethods
79
81
 
80
82
  module InstanceMethods
81
- def load_siblings(vtags,params={})
83
+ def load_siblings_from_multipart_response(resp)
84
+ #puts resp.inspect + "\n\n"
85
+ Riaktor::Utils.parse_multipart(resp)
86
+ @siblings = {}
87
+ end
88
+
89
+ def load_siblings_from_vtags(vtags,params={})
82
90
  params["r"] ||= 2
83
91
  @siblings = {}
84
92
 
@@ -103,7 +111,7 @@ module Riaktor
103
111
 
104
112
  def save(opts={})
105
113
  raise(SaveError, "cannot save with siblings present") if self.in_conflict?
106
- if changed? or opts[:force] == true
114
+ if new_record? || changed? or opts[:force] == true
107
115
  op = {}
108
116
  op["bucket"] = self.bucket
109
117
  op["key"] = self.key
@@ -115,6 +123,7 @@ module Riaktor
115
123
  if [204,404].include?(resp["code"])
116
124
  resp = Riaktor::Client.get(op)
117
125
  end
126
+ @raw_response = resp
118
127
  case resp["code"]
119
128
  when 200 then
120
129
  @links = Link.from_header(resp["headers"]["Link"])
@@ -125,7 +134,7 @@ module Riaktor
125
134
  @vector_clock = resp["headers"]["X-Riak-Vclock"]
126
135
  @new = false
127
136
  vtags = resp["body"].split("\n")[1..-1]
128
- load_siblings(vtags,{"r" => op["params"]["r"]})
137
+ load_siblings_from_vtags(vtags,{"r" => op["params"]["r"]})
129
138
  else
130
139
  raise(SaveError, resp["code"])
131
140
  end # case
@@ -142,6 +151,7 @@ module Riaktor
142
151
  op["params"] = { "dw" => opts[:dw] || 0, "w" => opts[:w] || 2, "r" => opts[:r] || 2 }
143
152
  op["headers"] = { "X-Riak-Vclock" => self.vector_clock } if self.vector_clock
144
153
  resp = Riaktor::Client.delete(op)
154
+ @raw_response = resp
145
155
  case resp["code"]
146
156
  when 204 then
147
157
  @deleted = true
@@ -4,7 +4,8 @@ module Riaktor
4
4
  class RObject
5
5
  include Persistence
6
6
 
7
- attr_accessor :key, :bucket, :value, :orig_value, :links, :siblings, :vector_clock, :deleted
7
+ attr_accessor :key, :bucket, :value, :orig_value, :links, :siblings, :vector_clock, :deleted,
8
+ :raw_response
8
9
 
9
10
  def self.encoder
10
11
  @encoder || :yaml
@@ -23,6 +24,7 @@ module Riaktor
23
24
  @new = opts[:new].nil? ? true : opts[:new]
24
25
  @orig_value = @new == true ? nil : @value.dup rescue nil
25
26
  @vector_clock = opts[:vector_clock]
27
+ @raw_response = opts[:raw_response]
26
28
  end
27
29
 
28
30
  alias :id :key
data/lib/riaktor.rb CHANGED
@@ -17,7 +17,7 @@ gem "activemodel", '= 3.0.0.beta'
17
17
  require "active_model"
18
18
 
19
19
  module Riaktor
20
- VERSION = '0.0.2'
20
+ VERSION = '0.0.3'
21
21
  end
22
22
 
23
23
  require "#{File.dirname(__FILE__)}/riaktor/client"
data/test/test_riaktor.rb CHANGED
@@ -161,63 +161,6 @@ class TestRiaktor < Test::Unit::TestCase
161
161
  assert p.updated_at.value > t1
162
162
  end
163
163
 
164
- def test_siblings
165
- props = Riaktor::Client.bucket_props("person")
166
- props["props"]["allow_mult"] = true
167
- assert Riaktor::Client.set_bucket_props("person", props)
168
- props = Riaktor::Client.bucket_props("person")
169
- assert props["props"]["allow_mult"] == true
170
-
171
- if p = Person.find("test_siblings")
172
- assert p.resolved!
173
- assert p.age.resolved!
174
- assert p.updated_at.resolved!
175
- assert p.destroy
176
- sleep(0.1)
177
- assert_nil Person.find("test_siblings")
178
- end
179
-
180
- Riaktor::Client.client_id = "client1"
181
- unless client1_p = Person.find("test_siblings")
182
- client1_p = Person.new
183
- client1_p.key = "test_siblings"
184
- client1_p.value = "client1 value"
185
- client1_p.age.value = 20
186
- end
187
- assert client1_p.save
188
-
189
- client2_p = Person.find("test_siblings")
190
- assert_equal 20, client2_p.age.value
191
- assert_equal "client1 value", client2_p.value
192
-
193
- Riaktor::Client.client_id = "client1"
194
- client1_p.value = "client1 value II"
195
- client1_p.age.value = 21
196
- assert client1_p.save
197
-
198
- Riaktor::Client.client_id = "client2"
199
- client2_p.value = "client2 value"
200
- client2_p.age.value = 22
201
- assert client2_p.save
202
-
203
- assert_equal 2, client2_p.siblings.keys.size
204
- assert_equal 2, client2_p.age.siblings.keys.size
205
- assert_equal 2, client2_p.updated_at.siblings.keys.size
206
- assert client2_p.in_conflict?
207
- client2_p.value = "client2 value"
208
- client2_p.resolved!
209
- assert client2_p.in_conflict?
210
- client2_p.age.value = 23
211
- client2_p.age.resolved!
212
- client2_p.updated_at.resolved!
213
- assert !client2_p.in_conflict?
214
- assert client2_p.save
215
-
216
- p = Person.find("test_siblings")
217
- assert_equal "client2 value", p.value
218
- assert_equal 23, p.age.value
219
- end
220
-
221
164
  def test_indexes
222
165
  if p = Person.find("test_indexes")
223
166
  assert p.destroy
@@ -0,0 +1,68 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestRiaktor < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_siblings
9
+ props = Riaktor::Client.bucket_props("person")
10
+ props["props"]["allow_mult"] = true
11
+ assert Riaktor::Client.set_bucket_props("person", props)
12
+ props = Riaktor::Client.bucket_props("person")
13
+ assert props["props"]["allow_mult"] == true
14
+
15
+ if p = Person.find("test_siblings")
16
+ assert p.resolved!
17
+ assert p.age.resolved!
18
+ assert p.updated_at.resolved!
19
+ assert p.destroy
20
+ sleep(0.1)
21
+ assert_nil Person.find("test_siblings")
22
+ end
23
+
24
+ Riaktor::Client.client_id = "client1"
25
+ unless client1_p = Person.find("test_siblings")
26
+ client1_p = Person.new
27
+ client1_p.key = "test_siblings"
28
+ client1_p.value = "client1 value"
29
+ client1_p.age.value = 20
30
+ end
31
+ assert client1_p.save
32
+
33
+ client2_p = Person.find("test_siblings")
34
+ assert_equal 20, client2_p.age.value
35
+ assert_equal "client1 value", client2_p.value
36
+
37
+ Riaktor::Client.client_id = "client1"
38
+ client1_p.value = "client1 value II"
39
+ client1_p.age.value = 21
40
+ pre_vclock = client1_p.vector_clock.dup
41
+ assert client1_p.save
42
+ post_vclock = client1_p.vector_clock.dup
43
+ assert pre_vclock != post_vclock
44
+
45
+ Riaktor::Client.client_id = "client2"
46
+ client2_p.value = "client2 value"
47
+ client2_p.age.value = 22
48
+ assert_equal pre_vclock, client2_p.vector_clock
49
+ assert client2_p.save
50
+ assert_equal 2, client2_p.siblings.keys.size
51
+ assert_equal 2, client2_p.age.siblings.keys.size
52
+ assert_equal 2, client2_p.updated_at.siblings.keys.size
53
+ assert client2_p.in_conflict?
54
+ client2_p.value = "client2 value"
55
+ client2_p.resolved!
56
+ assert client2_p.in_conflict?
57
+ client2_p.age.value = 23
58
+ client2_p.age.resolved!
59
+ client2_p.updated_at.resolved!
60
+ assert !client2_p.in_conflict?
61
+ assert client2_p.save
62
+
63
+ p = Person.find("test_siblings")
64
+ assert_equal "client2 value", p.value
65
+ assert_equal 23, p.age.value
66
+ end
67
+
68
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ben Myles
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-18 00:00:00 -07:00
17
+ date: 2010-03-19 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -202,3 +202,4 @@ summary: Riaktor is a Ruby client and object mapper for Riak (http://riak.basho.
202
202
  test_files:
203
203
  - test/test_helper.rb
204
204
  - test/test_riaktor.rb
205
+ - test/test_siblings.rb