Class: SimpleDB
Constant Summary collapse
- ENDPOINT_URI =
URI.parse("https://sdb.amazonaws.com/")
- API_VERSION =
'2007-11-07'- SIGNATURE_VERSION =
'1'- HTTP_METHOD =
‘GET’
'POST'
Instance Attribute Summary collapse
-
#prior_box_usage ⇒ Object
readonly
Returns the value of attribute prior_box_usage.
-
#total_box_usage ⇒ Object
readonly
Returns the value of attribute total_box_usage.
Instance Method Summary collapse
- #build_attribute_params(attributes = {}, replace = false) ⇒ Object
- #create_domain(domain_name) ⇒ Object
- #decode_attribute_value(value_str) ⇒ Object
- #decode_boolean(value_str) ⇒ Object
- #decode_date(value_str) ⇒ Object
- #decode_float(value_str) ⇒ Object
- #decode_integer(value_str) ⇒ Object
- #delete_attributes(domain_name, item_name, attributes = {}) ⇒ Object
- #delete_domain(domain_name) ⇒ Object
- #do_sdb_query(parameters) ⇒ Object
- #encode_attribute_value(value) ⇒ Object
- #encode_boolean(value) ⇒ Object
- #encode_date(value) ⇒ Object
- #encode_float(value, max_exp_digits = 2, max_precision_digits = 15) ⇒ Object
- #encode_integer(value, max_digits = 18) ⇒ Object
- #get_attributes(domain_name, item_name, attribute_name = nil) ⇒ Object
- #list_domains(max_domains = 100) ⇒ Object
- #put_attributes(domain_name, item_name, attributes, replace = false) ⇒ Object
- #query(domain_name, query_expression = nil, options = {:fetch_all=>true}) ⇒ Object
-
#query_with_attributes(domain_name, query_expression = nil, attribute_names = [], options = {:fetch_all=>true}) ⇒ Object
The query with attributes feature was added to the S3 API after the release of “Programming Amazon Web Services” so it is not discussed in the book’s text.
Instance Attribute Details
#prior_box_usage ⇒ Object (readonly)
Returns the value of attribute prior_box_usage.
21 22 23 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 21 def prior_box_usage @prior_box_usage end |
#total_box_usage ⇒ Object (readonly)
Returns the value of attribute total_box_usage.
22 23 24 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 22 def total_box_usage @total_box_usage end |
Instance Method Details
#build_attribute_params(attributes = {}, replace = false) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 262 def build_attribute_params(attributes={}, replace=false) attribute_params = {} index = 0 attributes.each do |attrib_name, attrib_value| attrib_value = [attrib_value] if not attrib_value.is_a? Array attrib_value.each do |value| attribute_params["Attribute.#{index}.Name"] = attrib_name if not value.nil? if respond_to? :encode_attribute_value # Automatically encode attribute values if the method # encode_attribute_value is available in this class value = encode_attribute_value(value) end attribute_params["Attribute.#{index}.Value"] = value end # Add a Replace parameter for the attribute if the replace flag is set attribute_params["Attribute.#{index}.Replace"] = 'true' if replace index += 1 end if attrib_value end return attribute_params end |
#create_domain(domain_name) ⇒ Object
238 239 240 241 242 243 244 245 246 247 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 238 def create_domain(domain_name) parameters = build_query_params(API_VERSION, SIGNATURE_VERSION, { 'Action' => 'CreateDomain', 'DomainName' => domain_name }) do_sdb_query(parameters) return true end |
#decode_attribute_value(value_str) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 188 def decode_attribute_value(value_str) return '' if value_str.nil? # Check whether the '!' flag is present to indicate an encoded value return value_str if value_str[0..0] != '!' prefix = value_str[0..1].downcase if prefix == '!b' return decode_boolean(value_str) elsif prefix == '!d' return decode_date(value_str) elsif prefix == '!i' return decode_integer(value_str) elsif prefix == '!f' return decode_float(value_str) else return value_str end end |
#decode_boolean(value_str) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 47 def decode_boolean(value_str) if value_str == '!B' return false elsif value_str == '!b' return true else raise "Cannot decode boolean from string: #{value_str}" end end |
#decode_date(value_str) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 63 def decode_date(value_str) if value_str[0..1] == '!d' return Time.parse(value_str[2..-1]) else raise "Cannot decode date from string: #{value_str}" end end |
#decode_float(value_str) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 139 def decode_float(value_str) prefix = value_str[0..1] if prefix != '!f' and prefix != '!F' raise "Cannot decode float from string: #{value_str}" end value_str =~ /!([0-9]+)/ exp_str = $1 fraction_str = $2 max_exp_digits = exp_str.size exp_midpoint = (10 ** max_exp_digits) / 2 max_precision_digits = fraction_str.size if prefix == '!F' sign = -1 exp = exp_midpoint - exp_str.to_i fraction_upper_bound = (10 ** max_precision_digits) fraction = fraction_upper_bound - BigDecimal(fraction_str) else sign = 1 exp = exp_str.to_i - exp_midpoint fraction = BigDecimal(fraction_str) end return sign * "0.#{fraction.to_i}".to_f * (10 ** exp) end |
#decode_integer(value_str) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 88 def decode_integer(value_str) if value_str[0..1] == '!I' # Encoded value is a negative integer max_digits = value_str.size - 2 upper_bound = (10 ** max_digits) return value_str[2..-1].to_i - upper_bound elsif value_str[0..1] == '!i' # Encoded value is a positive integer return value_str[2..-1].to_i else raise "Cannot decode integer from string: #{value_str}" end end |
#delete_attributes(domain_name, item_name, attributes = {}) ⇒ Object
304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 304 def delete_attributes(domain_name, item_name, attributes={}) parameters = build_query_params(API_VERSION, SIGNATURE_VERSION, { 'Action' => 'DeleteAttributes', 'DomainName' => domain_name, 'ItemName' => item_name }) parameters.merge!(build_attribute_params(attributes)) do_sdb_query(parameters) return true end |
#delete_domain(domain_name) ⇒ Object
250 251 252 253 254 255 256 257 258 259 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 250 def delete_domain(domain_name) parameters = build_query_params(API_VERSION, SIGNATURE_VERSION, { 'Action' => 'DeleteDomain', 'DomainName' => domain_name }) do_sdb_query(parameters) return true end |
#do_sdb_query(parameters) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 25 def do_sdb_query(parameters) response = do_query(HTTP_METHOD, ENDPOINT_URI, parameters) xml_doc = REXML::Document.new(response.body) @total_box_usage = 0 if @total_box_usage.nil? @prior_box_usage = xml_doc.elements['//BoxUsage'].text.to_f @total_box_usage += @prior_box_usage return xml_doc end |
#encode_attribute_value(value) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 171 def encode_attribute_value(value) if value == true or value == false return encode_boolean(value) elsif value.is_a? Time return encode_date(value) elsif value.is_a? Integer return encode_integer(value) elsif value.is_a? Numeric return encode_float(value) else # No type-specific encoding is available, so we simply convert # the value to a string. return value.to_s end end |
#encode_boolean(value) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 38 def encode_boolean(value) if value return '!b' else return '!B' end end |
#encode_date(value) ⇒ Object
58 59 60 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 58 def encode_date(value) return "!d" + value.getutc.iso8601 end |
#encode_float(value, max_exp_digits = 2, max_precision_digits = 15) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 104 def encode_float(value, max_exp_digits=2, max_precision_digits=15) exp_midpoint = (10 ** max_exp_digits) / 2 sign, fraction, base, exponent = BigDecimal(value.to_s).split if exponent >= exp_midpoint or exponent < -exp_midpoint raise "Exponent #{exponent} is outside encoding range " + "(-#{exp_midpoint} " + "to #{exp_midpoint - 1})" end if fraction.size > max_precision_digits # Round fraction value if it exceeds allowed precision. fraction_str = fraction[0...max_precision_digits] + '.' + fraction[max_precision_digits..-1] fraction = BigDecimal(fraction_str).round(0).split[1] elsif fraction.size < max_precision_digits # Right-pad fraction with zeros if it is too short. fraction = fraction + ('0' * (max_precision_digits - fraction.size)) end # The zero value is a special case, for which the exponent must be 0 exponent = -exp_midpoint if value == 0 if sign == 1 return format("!f%0#{max_exp_digits}d", exp_midpoint + exponent) + format("!%0#{max_precision_digits}d", fraction.to_i) else fraction_upper_bound = (10 ** max_precision_digits) diff_fraction = fraction_upper_bound - BigDecimal(fraction) return format("!F%0#{max_exp_digits}d", exp_midpoint - exponent) + format("!%0#{max_precision_digits}d", diff_fraction) end end |
#encode_integer(value, max_digits = 18) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 72 def encode_integer(value, max_digits=18) upper_bound = (10 ** max_digits) if value >= upper_bound or value < -upper_bound raise "Integer #{value} is outside encoding range (-#{upper_bound} " + "to #{upper_bound - 1})" end if value < 0 return "!I" + format("%0#{max_digits}d", upper_bound + value) else return "!i" + format("%0#{max_digits}d", value) end end |
#get_attributes(domain_name, item_name, attribute_name = nil) ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 319 def get_attributes(domain_name, item_name, attribute_name=nil) parameters = build_query_params(API_VERSION, SIGNATURE_VERSION, { 'Action' => 'GetAttributes', 'DomainName' => domain_name, 'ItemName' => item_name, 'AttributeName' => attribute_name }) xml_doc = do_sdb_query(parameters) attributes = {} xml_doc.elements.each('//Attribute') do |attribute_node| attr_name = attribute_node.elements['Name'].text value = attribute_node.elements['Value'].text if respond_to? :decode_attribute_value # Automatically decode attribute values if the method # decode_attribute_value is available in this class value = decode_attribute_value(value) end # An empty attribute value is an empty string, not nil. value = '' if value.nil? if attributes.has_key?(attr_name) attributes[attr_name] << value else attributes[attr_name] = [value] end end if not attribute_name.nil? # If a specific attribute was requested, return only the values array # for this attribute. if not attributes[attribute_name] return [] else return attributes[attribute_name] end else return attributes end end |
#list_domains(max_domains = 100) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 209 def list_domains(max_domains=100) more_domains = true next_token = nil domain_names = [] while more_domains parameters = build_query_params(API_VERSION, SIGNATURE_VERSION, { 'Action' => 'ListDomains', 'MaxNumberOfDomains' => max_domains, 'NextToken' => next_token }) xml_doc = do_sdb_query(parameters) xml_doc.elements.each('//DomainName') do |name| domain_names << name.text end # If we receive a NextToken element, perform a follow-up operation # to retrieve the next set of domain names. next_token = xml_doc.elements['//NextToken/text()'] more_domains = !next_token.nil? end return domain_names end |
#put_attributes(domain_name, item_name, attributes, replace = false) ⇒ Object
289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 289 def put_attributes(domain_name, item_name, attributes, replace=false) parameters = build_query_params(API_VERSION, SIGNATURE_VERSION, { 'Action' => 'PutAttributes', 'DomainName' => domain_name, 'ItemName' => item_name }) parameters.merge!(build_attribute_params(attributes, replace)) do_sdb_query(parameters) return true end |
#query(domain_name, query_expression = nil, options = {:fetch_all=>true}) ⇒ Object
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 365 def query(domain_name, query_expression=nil, ={:fetch_all=>true}) more_items = true next_token = nil item_names = [] while more_items parameters = build_query_params(API_VERSION, SIGNATURE_VERSION, { 'Action' => 'Query', 'DomainName' => domain_name, 'QueryExpression' => query_expression, 'MaxNumberOfItems' => [:max_items], 'NextToken' => next_token }) xml_doc = do_sdb_query(parameters) xml_doc.elements.each('//ItemName') do |item_name| item_names << item_name.text end if xml_doc.elements['//NextToken'] next_token = xml_doc.elements['//NextToken'].text.gsub("\n","") more_items = [:fetch_all] else more_items = false end end return item_names end |
#query_with_attributes(domain_name, query_expression = nil, attribute_names = [], options = {:fetch_all=>true}) ⇒ Object
The query with attributes feature was added to the S3 API after the release of “Programming Amazon Web Services” so it is not discussed in the book’s text. For more details, see: www.jamesmurty.com/2008/09/07/samples-for-simpledb-querywithattributes/
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/OriginalAWS/SimpleDB.rb', line 401 def query_with_attributes(domain_name, query_expression=nil, attribute_names=[], ={:fetch_all=>true}) more_items = true next_token = nil items = [] while more_items parameters = build_query_params(API_VERSION, SIGNATURE_VERSION, { 'Action' => 'QueryWithAttributes', 'DomainName' => domain_name, 'QueryExpression' => query_expression, 'MaxNumberOfItems' => [:max_items], 'NextToken' => next_token },{ 'AttributeName' => attribute_names, }) xml_doc = do_sdb_query(parameters) xml_doc.elements.each('//Item') do |item_node| item = {'name' => item_node.elements['Name'].text} attributes = {} item_node.elements.each('Attribute') do |attribute_node| attr_name = attribute_node.elements['Name'].text value = attribute_node.elements['Value'].text if respond_to? :decode_attribute_value # Automatically decode attribute values if the method # decode_attribute_value is available in this class value = decode_attribute_value(value) end # An empty attribute value is an empty string, not nil. value = '' if value.nil? if attributes.has_key?(attr_name) attributes[attr_name] << value else attributes[attr_name] = [value] end end item['attributes'] = attributes items << item end if xml_doc.elements['//NextToken'] next_token = xml_doc.elements['//NextToken'].text.gsub("\n","") more_items = [:fetch_all] else more_items = false end end return items end |