sdb_dal 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sdb_dal/domain_object.rb +475 -422
- data/lib/sdb_dal/lazy_loading_text.rb +2 -0
- data/lib/sdb_dal/memory_repository.rb +54 -23
- data/lib/sdb_dal/repository.rb +356 -305
- data/lib/sdb_dal/repository_factory.rb +4 -3
- data/lib/sdb_dal/s3.rb +69 -57
- data/lib/sdb_dal/sdb_monkey_patch.rb +47 -0
- data/lib/sdb_dal/storage.rb +8 -4
- metadata +4 -3
data/lib/sdb_dal/repository.rb
CHANGED
@@ -1,368 +1,419 @@
|
|
1
1
|
module SdbDal
|
2
|
-
$KCODE = 'u'
|
3
|
-
require "aws_sdb"
|
4
|
-
require "sdb_dal/storage.rb"
|
5
|
-
require 'ya2yaml'
|
6
|
-
require File.dirname(__FILE__) +"/memory_repository.rb"
|
7
|
-
require File.dirname(__FILE__) +"/domain_object_cache_item.rb"
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
2
|
+
$KCODE = 'u'
|
3
|
+
require "aws_sdb"
|
4
|
+
require "sdb_dal/storage.rb"
|
5
|
+
require 'ya2yaml'
|
6
|
+
require File.dirname(__FILE__) +"/memory_repository.rb"
|
7
|
+
require File.dirname(__FILE__) +"/domain_object_cache_item.rb"
|
8
|
+
|
9
|
+
require File.dirname(__FILE__) +"/sdb_monkey_patch.rb"
|
10
|
+
class Repository
|
11
|
+
attr_accessor :use_cache
|
12
|
+
attr_accessor :storage
|
13
|
+
def initialize(
|
14
|
+
sdb_domain_prefix,#MyDevPayApp
|
15
|
+
clob_bucket,
|
16
|
+
aws_key_id,
|
17
|
+
aws_secret_key,
|
18
|
+
memcache_servers = nil ,
|
19
|
+
a_storage=nil,
|
20
|
+
append_table_to_domain=true,
|
21
|
+
options={}
|
20
22
|
|
21
|
-
|
23
|
+
)
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
25
|
+
@use_cache=true
|
26
|
+
@storage=a_storage
|
27
|
+
@storage||=Storage.new(aws_key_id,aws_secret_key,memcache_servers)
|
28
|
+
@sdb_domain_prefix = sdb_domain_prefix
|
29
|
+
@clob_bucket = clob_bucket
|
30
|
+
@memcache_servers = memcache_servers
|
31
|
+
@aws_key_id = aws_key_id
|
32
|
+
@aws_secret_key = aws_secret_key
|
33
|
+
@append_table_to_domain=append_table_to_domain
|
34
|
+
@logger = options[:logger]
|
35
|
+
if !@logger
|
36
|
+
@logger = Logger.new(STDOUT)
|
37
|
+
@logger.level = Logger::ERROR
|
38
|
+
end
|
37
39
|
|
38
|
-
|
39
|
-
|
40
|
+
@sdb=AwsSdb::Service.new(:access_key_id=>aws_key_id,:secret_access_key=>aws_secret_key,:url=>"http://sdb.amazonaws.com",:logger=>@logger)
|
41
|
+
@session_cache=MemoryRepository.new
|
40
42
|
|
41
|
-
|
42
|
-
|
43
|
+
# #create_domain()
|
44
|
+
end
|
43
45
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
end
|
53
|
-
def get_clob(table_name,primary_key,clob_name)
|
54
|
-
return @storage.get(@clob_bucket,make_clob_key(table_name,primary_key,clob_name))
|
55
|
-
|
56
|
-
end
|
57
|
-
def save(table_name, primary_key, attributes)
|
58
|
-
@session_cache.save(table_name,primary_key,attributes)
|
59
|
-
formatted_attributes={}
|
60
|
-
attributes.each do |description,value|
|
61
|
-
if value || description.value_type==:boolean
|
62
|
-
if description.is_clob
|
63
|
-
@storage.put(
|
64
|
-
@clob_bucket,
|
65
|
-
make_clob_key(table_name,primary_key,description.name),
|
66
|
-
value.to_s,
|
67
|
-
{})
|
68
|
-
else
|
69
|
-
formatted_attributes[description.name]=description.format_for_sdb(value)
|
46
|
+
def clear_session_cache
|
47
|
+
@session_cache.clear
|
48
|
+
end
|
49
|
+
def flatten_key(key)
|
50
|
+
if key.is_a?( Array)
|
51
|
+
flattened_key=""
|
52
|
+
key.each do |key_part|
|
53
|
+
flattened_key<<CGI.escape(key_part.to_s)+"/"
|
70
54
|
end
|
55
|
+
return flattened_key[0..-2]
|
56
|
+
else
|
57
|
+
return CGI.escape(key.to_s)
|
71
58
|
end
|
72
59
|
end
|
60
|
+
def make_cache_key(table_name,primary_key)
|
61
|
+
|
62
|
+
primary_key=flatten_key(primary_key)
|
63
|
+
primary_key="#{table_name}/#{primary_key}" unless @append_table_to_domain
|
64
|
+
return primary_key
|
65
|
+
end
|
66
|
+
def make_clob_key(table_name,primary_key,clob_name)
|
67
|
+
return "clobs/#{table_name}/#{flatten_key(primary_key)}/#{clob_name}"
|
68
|
+
end
|
69
|
+
def get_clob(table_name,primary_key,clob_name)
|
70
|
+
return @storage.get(@clob_bucket,make_clob_key(table_name,primary_key,clob_name))
|
73
71
|
|
74
|
-
|
75
|
-
|
72
|
+
end
|
73
|
+
def save(table_name, primary_key, attributes,index_descriptions)
|
74
|
+
@session_cache.save(table_name,primary_key,attributes,index_descriptions)
|
75
|
+
formatted_attributes={}
|
76
|
+
attributes.each do |description,value|
|
77
|
+
if value || description.value_type==:boolean
|
78
|
+
if description.is_clob
|
79
|
+
@storage.put(
|
80
|
+
@clob_bucket,
|
81
|
+
make_clob_key(table_name,primary_key,description.name),
|
82
|
+
value.to_s,
|
83
|
+
{})
|
84
|
+
else
|
85
|
+
formatted_attributes[description.name]=description.format_for_sdb(value)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
if !@append_table_to_domain
|
90
|
+
formatted_attributes['metadata%%table_name'] = table_name
|
91
|
+
|
92
|
+
end
|
93
|
+
put_attributes(table_name,primary_key, formatted_attributes )
|
94
|
+
# put_into_cache(table_name, primary_key, formatted_attributes)
|
76
95
|
|
77
|
-
|
78
|
-
# def put_into_cache(table_name, primary_key, formatted_attributes)
|
79
|
-
# if @use_cache
|
80
|
-
# cacheItem=DomainObjectCacheItem.new(table_name, primary_key, formatted_attributes)
|
81
|
-
#
|
82
|
-
# yaml=cacheItem.ya2yaml(:syck_compatible => true)
|
83
|
-
#
|
84
|
-
# @storage.put(
|
85
|
-
# @clob_bucket,
|
86
|
-
# make_cache_key(table_name,primary_key),
|
87
|
-
# yaml ,
|
88
|
-
# {})
|
89
|
-
# end
|
90
|
-
# end
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
+
end
|
97
|
+
# def put_into_cache(table_name, primary_key, formatted_attributes)
|
98
|
+
# if @use_cache
|
99
|
+
# cacheItem=DomainObjectCacheItem.new(table_name, primary_key, formatted_attributes)
|
100
|
+
#
|
101
|
+
# yaml=cacheItem.ya2yaml(:syck_compatible => true)
|
102
|
+
#
|
103
|
+
# @storage.put(
|
104
|
+
# @clob_bucket,
|
105
|
+
# make_cache_key(table_name,primary_key),
|
106
|
+
# yaml ,
|
107
|
+
# {})
|
108
|
+
# end
|
109
|
+
# end
|
110
|
+
def create_domain
|
111
|
+
20.times do |i|
|
112
|
+
begin
|
113
|
+
@sdb.create_domain(@sdb_domain_prefix)
|
114
|
+
return
|
96
115
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
116
|
+
rescue => e
|
117
|
+
s= "#{e.message}\n#{e.backtrace}"
|
118
|
+
@logger.warn(s) if @logger
|
119
|
+
sleep(i*i)
|
101
120
|
|
121
|
+
end
|
102
122
|
end
|
103
|
-
end
|
104
123
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
124
|
+
end
|
125
|
+
def put_attributes(table_name,primary_key, formatted_attributes,options={})
|
126
|
+
20.times do |i|
|
127
|
+
begin
|
128
|
+
@sdb.put_attributes(make_domain_name(table_name),make_cache_key(table_name,primary_key) , formatted_attributes, true )
|
129
|
+
return
|
111
130
|
|
112
|
-
|
113
|
-
|
114
|
-
|
131
|
+
rescue Exception => e
|
132
|
+
s= "#{e.message}\n#{e.backtrace}"
|
133
|
+
@logger.warn(s) if @logger
|
115
134
|
|
116
|
-
|
135
|
+
sleep(i*i)
|
136
|
+
end
|
117
137
|
end
|
118
|
-
end
|
119
138
|
|
120
|
-
end
|
121
|
-
def extend_query(query,new_clause)
|
122
|
-
if query.length>0
|
123
|
-
query << " intersection "
|
124
139
|
end
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
def escape_quotes(value)
|
129
|
-
value.gsub( "'","\\'")
|
130
|
-
end
|
131
|
-
def query_ids(table_name,attribute_descriptions,options={})
|
132
|
-
|
133
|
-
if options.has_key?(:limit) and !options.has_key?(:order_by)
|
134
|
-
session_cache_result=@session_cache.query_ids(table_name,attribute_descriptions,options)
|
135
|
-
if options[:limit]==session_cache_result.length
|
136
|
-
return session_cache_result
|
140
|
+
def extend_query(query,new_clause)
|
141
|
+
if query.length>0
|
142
|
+
query << " intersection "
|
137
143
|
end
|
144
|
+
|
145
|
+
query << new_clause
|
138
146
|
end
|
139
|
-
|
147
|
+
def escape_quotes(value)
|
148
|
+
return nil unless value
|
149
|
+
value.gsub( "'","\\'")
|
150
|
+
end
|
151
|
+
def build_query(table_name,attribute_descriptions,options={})
|
152
|
+
|
140
153
|
|
141
|
-
|
142
|
-
|
143
|
-
|
154
|
+
query=""
|
155
|
+
params=nil
|
156
|
+
params=options[:params] if options.has_key?(:params)
|
157
|
+
if options.has_key?(:map)
|
158
|
+
params||={}
|
159
|
+
keys=options[:map][:keys]
|
160
|
+
values=options[:map][:values]
|
161
|
+
(0..keys.length-1).each do |i|
|
162
|
+
key=keys[i]
|
163
|
+
value=values[i]
|
164
|
+
params[key]=value
|
165
|
+
end
|
166
|
+
|
144
167
|
end
|
145
|
-
if
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
168
|
+
if !@append_table_to_domain
|
169
|
+
extend_query(query," ['metadata%%table_name' = '#{table_name}']")
|
170
|
+
|
171
|
+
end
|
172
|
+
if options
|
173
|
+
if options.has_key?(:query)
|
174
|
+
extend_query(query,"["+options[:query]+"]")
|
175
|
+
end
|
176
|
+
if params
|
177
|
+
params.each do |key,value|
|
178
|
+
got_something=false
|
179
|
+
if attribute_descriptions.has_key?(key)
|
180
|
+
if value==:NOT_NULL
|
181
|
+
got_something=true
|
182
|
+
extend_query(query," ['#{key}' starts-with '']")
|
183
|
+
end
|
184
|
+
if value==:NULL
|
185
|
+
got_something=true
|
186
|
+
extend_query(query," not ['#{key}' starts-with '']")
|
187
|
+
end
|
188
|
+
if value.respond_to?(:less_than) && value.less_than
|
189
|
+
got_something=true
|
190
|
+
extend_query(query," ['#{key}' < '#{escape_quotes(attribute_descriptions[key].format_for_sdb_single( value.less_than))}']")
|
191
|
+
end
|
192
|
+
if value.respond_to?(:greater_than) && value.greater_than
|
193
|
+
got_something=true
|
194
|
+
extend_query(query," ['#{key}' > '#{escape_quotes( attribute_descriptions[key].format_for_sdb_single( value.greater_than))}']")
|
195
|
+
end
|
196
|
+
if value.respond_to?(:less_than_or_equal_to) && value.less_than_or_equal_to
|
197
|
+
got_something=true
|
198
|
+
extend_query(query,"['#{key}' <= '#{escape_quotes( attribute_descriptions[key].format_for_sdb_single( value.less_than_or_equal_to))}']")
|
199
|
+
end
|
200
|
+
if value.respond_to?(:greater_than_or_equal_to) && value.greater_than_or_equal_to
|
201
|
+
got_something=true
|
202
|
+
extend_query(query," ['#{key}' >= '#{escape_quotes(attribute_descriptions[key].format_for_sdb_single( value.greater_than_or_equal_to))}']")
|
203
|
+
end
|
204
|
+
if value==false
|
205
|
+
got_something=true
|
206
|
+
extend_query(query," ['#{key}' != 'true' ]")
|
207
|
+
end
|
208
|
+
if !got_something
|
209
|
+
extend_query(query," ['#{key}' = '#{escape_quotes( attribute_descriptions[key].format_for_sdb_single(value))}']")
|
210
|
+
end
|
211
|
+
else
|
212
|
+
# #it must be formatted already. likely an index
|
213
|
+
extend_query(query,"['#{key}' = '#{escape_quotes value}']")
|
179
214
|
end
|
180
|
-
else
|
181
|
-
#it must be formatted already. likely an index
|
182
|
-
extend_query(query,"['#{key}' = '#{escape_quotes value}']")
|
183
215
|
end
|
184
216
|
end
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
options[:conditions].each do |condition|
|
217
|
+
if options.has_key?(:order_by) && options[:order_by]
|
218
|
+
clause=" ['#{options[:order_by]}' starts-with ''] sort '#{options[:order_by]}' "
|
219
|
+
if options.has_key?(:order) and options[:order]==:descending
|
220
|
+
clause<<" desc "
|
221
|
+
end
|
222
|
+
extend_query(query,clause)
|
223
|
+
end
|
224
|
+
if options.has_key?(:conditions)
|
225
|
+
options[:conditions].each do |condition|
|
195
226
|
|
196
|
-
|
227
|
+
extend_query(query," [ "+condition.to_sdb_query()+"]")
|
228
|
+
end
|
197
229
|
end
|
198
|
-
end
|
199
230
|
|
231
|
+
end
|
232
|
+
return query
|
233
|
+
|
200
234
|
end
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
page_size=max>250?250:max
|
208
|
-
sdb_results,token=sdb_query(table_name,query,page_size)
|
209
|
-
|
210
|
-
while !(token.nil? || token.empty? || sdb_results.length>=max)
|
211
|
-
page_size=max- sdb_results.length
|
212
|
-
page_size=page_size>250?250:page_size
|
213
|
-
partial_results,token=sdb_query(table_name,query,page_size,token)
|
214
|
-
sdb_results.concat( partial_results)
|
215
|
-
end
|
216
|
-
sdb_results
|
217
|
-
|
218
|
-
|
219
|
-
return sdb_results
|
220
|
-
end
|
221
|
-
def query(table_name,attribute_descriptions,options)
|
222
|
-
ids=query_ids(table_name,attribute_descriptions,options)
|
223
|
-
result=[]
|
224
|
-
ids.each do |primary_key|
|
225
|
-
if primary_key
|
226
|
-
(0..4).each do |try_count|
|
227
|
-
this_one=find_one(table_name,primary_key,attribute_descriptions)
|
228
|
-
if this_one
|
229
|
-
result<<this_one
|
230
|
-
break
|
231
|
-
else
|
232
|
-
#sdb query just told us it was there so keep trying
|
233
|
-
sleep(try_count*try_count)
|
234
|
-
end
|
235
|
+
# #result will be an array of hashes. each hash is a set of attributes
|
236
|
+
def query(table_name,attribute_descriptions,options)
|
237
|
+
if options.has_key?(:limit) and !options.has_key?(:order_by)
|
238
|
+
session_cache_result=@session_cache.query(table_name,attribute_descriptions,options)
|
239
|
+
if options[:limit]==session_cache_result.length
|
240
|
+
return session_cache_result
|
235
241
|
end
|
236
|
-
else
|
237
|
-
@logger.error("blank primary key")
|
238
242
|
end
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
243
|
+
|
244
|
+
the_query=build_query(table_name, attribute_descriptions, options)
|
245
|
+
max=250
|
246
|
+
if options[:limit]
|
247
|
+
max=options[:limit].to_i
|
248
|
+
|
249
|
+
end
|
250
|
+
page_size=max>250?250:max
|
251
|
+
sdb_result,token=sdb_query_with_attributes(table_name,the_query,page_size)
|
252
|
+
|
253
|
+
while !(token.nil? || token.empty? || sdb_result.length>=max)
|
254
|
+
page_size=max- sdb_result.length
|
255
|
+
page_size=page_size>250?250:page_size
|
256
|
+
partial_results,token=sdb_query_with_attributes(table_name,the_query,page_size,token)
|
257
|
+
|
258
|
+
sdb_result.merge!( partial_results)
|
259
|
+
end
|
260
|
+
result=[]
|
261
|
+
sdb_result.each{|primary_key,sdb_row|
|
262
|
+
attributes =parse_attributes(attribute_descriptions,sdb_row)
|
263
|
+
if attributes
|
264
|
+
# #attributes[:primary_key]=primary_key
|
265
|
+
|
266
|
+
result<<attributes
|
267
|
+
end
|
268
|
+
}
|
269
|
+
if options and options[:order_by]
|
270
|
+
result.sort! do |a,b|
|
271
|
+
a_value=a[options[:order_by]]
|
272
|
+
b_value=b[options[:order_by]]
|
273
|
+
if options[:order] && options[:order]!=:ascending
|
274
|
+
if !a_value
|
275
|
+
1
|
250
276
|
else
|
251
|
-
|
277
|
+
if b_value
|
278
|
+
b_value <=> a_value
|
279
|
+
else
|
280
|
+
-1
|
281
|
+
end
|
252
282
|
end
|
253
|
-
end
|
254
|
-
else
|
255
|
-
if !b_value
|
256
|
-
1
|
257
283
|
else
|
258
|
-
if
|
259
|
-
|
284
|
+
if !b_value
|
285
|
+
1
|
260
286
|
else
|
261
|
-
|
287
|
+
if a_value
|
288
|
+
a_value <=> b_value
|
289
|
+
else
|
290
|
+
-1
|
291
|
+
end
|
262
292
|
end
|
263
293
|
end
|
264
294
|
end
|
265
295
|
end
|
296
|
+
if options[:limit] && result.length>options[:limit]
|
297
|
+
result=result[0..(options[:limit]-1)]
|
298
|
+
end
|
299
|
+
return result
|
266
300
|
end
|
267
|
-
|
268
|
-
|
301
|
+
def find_one(table_name, primary_key,attribute_descriptions)#, non_clob_attribute_names, clob_attribute_names)
|
302
|
+
session_cache_result=@session_cache.find_one(table_name, make_cache_key(table_name,primary_key),attribute_descriptions)
|
303
|
+
return session_cache_result if session_cache_result
|
304
|
+
# if @use_cache
|
305
|
+
# yaml=@storage.get(@clob_bucket,make_cache_key(table_name,primary_key))
|
306
|
+
# if yaml
|
307
|
+
# result=YAML::load( yaml)
|
308
|
+
# if result.respond_to?(:non_clob_attributes) && result.non_clob_attributes!=nil
|
309
|
+
# return parse_attributes(attribute_descriptions, result.non_clob_attributes)
|
310
|
+
# end
|
311
|
+
#
|
312
|
+
# end
|
313
|
+
# end
|
314
|
+
attributes=parse_attributes(attribute_descriptions,sdb_get_attributes(table_name,primary_key))
|
315
|
+
if attributes
|
316
|
+
# #attributes[:primary_key]=primary_key #put_into_cache(table_name,
|
317
|
+
# primary_key, attributes)
|
318
|
+
end
|
319
|
+
|
320
|
+
attributes
|
269
321
|
end
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
# yaml=@storage.get(@clob_bucket,make_cache_key(table_name,primary_key))
|
277
|
-
# if yaml
|
278
|
-
# result=YAML::load( yaml)
|
279
|
-
# if result.respond_to?(:non_clob_attributes) && result.non_clob_attributes!=nil
|
280
|
-
# return parse_attributes(attribute_descriptions, result.non_clob_attributes)
|
281
|
-
# end
|
282
|
-
#
|
283
|
-
# end
|
284
|
-
# end
|
285
|
-
attributes=parse_attributes(attribute_descriptions,sdb_get_attributes(table_name,primary_key))
|
286
|
-
if attributes
|
287
|
-
attributes[:primary_key]=primary_key
|
288
|
-
#put_into_cache(table_name, primary_key, attributes)
|
322
|
+
def make_domain_name(table_name)
|
323
|
+
if @append_table_to_domain
|
324
|
+
@sdb_domain_prefix+"_"+table_name
|
325
|
+
else
|
326
|
+
@sdb_domain_prefix
|
327
|
+
end
|
289
328
|
end
|
290
|
-
|
291
|
-
attributes
|
292
|
-
end
|
293
|
-
def make_domain_name(table_name)
|
294
|
-
if @append_table_to_domain
|
295
|
-
@sdb_domain_prefix+"_"+table_name
|
296
|
-
else
|
297
|
-
@sdb_domain_prefix
|
298
|
-
end
|
299
|
-
end
|
300
|
-
def sdb_get_attributes(table_name,primary_key)
|
329
|
+
def sdb_get_attributes(table_name,primary_key)
|
301
330
|
|
302
|
-
|
331
|
+
@logger.debug( "SDB get_attributes #{table_name} : #{primary_key}") if @logger
|
303
332
|
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
333
|
+
20.times do |i|
|
334
|
+
begin
|
335
|
+
return @sdb.get_attributes(make_domain_name(table_name), make_cache_key(table_name,primary_key))
|
336
|
+
rescue Exception => e
|
337
|
+
s= "#{e.message}\n#{e.backtrace}"
|
338
|
+
@logger.warn(s) if @logger
|
310
339
|
|
311
|
-
|
312
|
-
|
340
|
+
sleep(i*i)
|
341
|
+
ensure
|
313
342
|
|
343
|
+
end
|
314
344
|
end
|
315
|
-
end
|
316
345
|
|
317
|
-
|
318
|
-
|
346
|
+
end
|
347
|
+
def sdb_query(table_name,query,max,token=nil)
|
319
348
|
|
320
349
|
@logger.debug( "SDB query:#{table_name}(#{max}) : #{query} #{token}" ) if @logger
|
321
|
-
puts "#{table_name} #{query} (#{max}) #{token}"
|
322
|
-
|
323
|
-
|
324
|
-
|
350
|
+
# puts "#{table_name} #{query} (#{max}) #{token}"
|
351
|
+
20.times do |i|
|
352
|
+
begin
|
353
|
+
return @sdb.query(make_domain_name(table_name),query,max,token)
|
325
354
|
|
326
|
-
|
327
|
-
|
328
|
-
|
355
|
+
rescue Exception => e
|
356
|
+
s= "#{e.message}\n#{e.backtrace}"
|
357
|
+
@logger.error(s) if @logger
|
329
358
|
|
330
|
-
|
331
|
-
|
359
|
+
sleep(i*i)
|
360
|
+
ensure
|
332
361
|
|
362
|
+
end
|
333
363
|
end
|
334
|
-
end
|
335
|
-
|
336
|
-
end
|
337
364
|
|
338
|
-
def parse_attributes(attribute_descriptions,attributes)
|
339
|
-
if !attributes || attributes.length==0
|
340
|
-
return nil
|
341
365
|
end
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
366
|
+
def sdb_query_with_attributes(table_name,query,max,token=nil)
|
367
|
+
|
368
|
+
@logger.debug( "SDB query:#{table_name}(#{max}) : #{query} #{token}" ) if @logger
|
369
|
+
puts "#{table_name} #{query} (#{max}) #{token}"
|
370
|
+
20.times do |i|
|
371
|
+
begin
|
372
|
+
return @sdb.query_with_attributes(make_domain_name(table_name),query,max,token)
|
373
|
+
|
374
|
+
rescue Exception => e
|
375
|
+
s= "#{e.message}\n#{e.backtrace}"
|
376
|
+
@logger.error(s) if @logger
|
377
|
+
|
378
|
+
sleep(i*i)
|
379
|
+
ensure
|
380
|
+
|
381
|
+
end
|
347
382
|
end
|
348
|
-
|
349
|
-
if !attribute_description.is_collection && value.respond_to?(:flatten) && value.length==1
|
350
|
-
value=value[0]
|
351
|
-
end
|
352
|
-
parsed_attributes[attribute_name.to_sym]=attribute_description.parse_from_sdb(value)
|
383
|
+
|
353
384
|
end
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
385
|
+
def parse_attributes(attribute_descriptions,attributes)
|
386
|
+
if !attributes || attributes.length==0
|
387
|
+
return nil
|
388
|
+
end
|
389
|
+
parsed_attributes={}
|
390
|
+
attribute_descriptions.each do |attribute_name,attribute_description|
|
391
|
+
value=attributes[attribute_name.to_sym]
|
392
|
+
if !value
|
393
|
+
value=attributes[attribute_name]
|
394
|
+
end
|
395
|
+
#sdb attributes are often array of one
|
396
|
+
if !attribute_description.is_collection && value.respond_to?(:flatten) && value.length==1
|
397
|
+
value=value[0]
|
398
|
+
end
|
399
|
+
parsed_attributes[attribute_name.to_sym]=attribute_description.parse_from_sdb(value)
|
400
|
+
end
|
401
|
+
parsed_attributes
|
360
402
|
end
|
361
|
-
|
362
|
-
|
403
|
+
def destroy(table_name, primary_key)
|
404
|
+
@sdb.delete_attributes(make_domain_name(table_name),make_cache_key(table_name, primary_key) )
|
405
|
+
# if @use_cache
|
406
|
+
# @storage.delete(@clob_bucket,make_cache_key(table_name,primary_key))
|
407
|
+
# end
|
408
|
+
end
|
409
|
+
#parse date in yyyy-mm-dd format
|
363
410
|
|
364
|
-
def pause
|
365
|
-
|
366
|
-
end
|
367
|
-
|
411
|
+
def pause
|
412
|
+
sleep(2)
|
413
|
+
end
|
414
|
+
|
415
|
+
def clear
|
416
|
+
@session_cache.clear
|
417
|
+
end
|
418
|
+
end
|
368
419
|
end
|