Class: SimpleDB::Driver
Defined Under Namespace
Classes: Iterator
Constant Summary collapse
- MAX_NUMBER_SUBMITTED_ITEMS =
25
Instance Attribute Summary collapse
-
#iteratable ⇒ Object
Returns the value of attribute iteratable.
Instance Method Summary collapse
-
#create_domain(domain_name) ⇒ Object
domain action.
- #current_list(consistent = false) ⇒ Object
- #current_page ⇒ Object
- #delete(domain_name, items = {}) ⇒ Object
- #describe(domain_name) ⇒ Object
- #drop_domain(domain_name) ⇒ Object
- #endpoint ⇒ Object
- #endpoint=(v) ⇒ Object
- #get(domain_name, item_name, attr_names = [], consistent = false) ⇒ Object
-
#initialize(accessKeyId, secretAccessKey, endpoint = 'sdb.amazonaws.com') ⇒ Driver
constructor
A new instance of Driver.
-
#insert(domain_name, items = {}) ⇒ Object
attr action.
- #next_list(consistent = false) ⇒ Object
- #page_num ⇒ Object
- #page_to(page, consistent = false) ⇒ Object
- #prev_list(consistent = false) ⇒ Object
- #select(expr, consistent = false, persist = false) ⇒ Object
- #show_domains ⇒ Object
- #tail(domain_name, consistent = false) ⇒ Object
- #timeout ⇒ Object
- #timeout=(v) ⇒ Object
- #update(domain_name, items = {}) ⇒ Object
Constructor Details
#initialize(accessKeyId, secretAccessKey, endpoint = 'sdb.amazonaws.com') ⇒ Driver
Returns a new instance of Driver.
13 14 15 16 17 18 19 |
# File 'lib/sdbcli/sdb-driver.rb', line 13 def initialize(accessKeyId, secretAccessKey, endpoint = 'sdb.amazonaws.com') @client = Client.new(accessKeyId, secretAccessKey, endpoint) @select_expr = nil @next_token = nil @current_token = nil @current_page = 1 end |
Instance Attribute Details
#iteratable ⇒ Object
Returns the value of attribute iteratable.
11 12 13 |
# File 'lib/sdbcli/sdb-driver.rb', line 11 def iteratable @iteratable end |
Instance Method Details
#create_domain(domain_name) ⇒ Object
domain action
39 40 41 |
# File 'lib/sdbcli/sdb-driver.rb', line 39 def create_domain(domain_name) @client.create_domain(domain_name) end |
#current_list(consistent = false) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/sdbcli/sdb-driver.rb', line 155 def current_list(consistent = false) unless @select_expr return [] end params = {:SelectExpression => @select_expr, :ConsistentRead => consistent} items = [] iterate(:select, params, @current_token || :first) do |doc| doc.css('Item').map do |i| items << [i.at_css('Name').content, attrs_to_hash(i)] end end return items end |
#current_page ⇒ Object
262 263 264 |
# File 'lib/sdbcli/sdb-driver.rb', line 262 def current_page @current_page end |
#delete(domain_name, items = {}) ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/sdbcli/sdb-driver.rb', line 266 def delete(domain_name, items = {}) until (chunk = items.slice!(0, MAX_NUMBER_SUBMITTED_ITEMS)).empty? params = {} i = j = 0 chunk.each do |item_name, attrs| i += 1 params["Item.#{i}.ItemName"] = item_name (attrs || []).each do |attr_name, values| [values].flatten.each do |v| j += 1 params["Item.#{i}.Attribute.#{j}.Name"] = attr_name params["Item.#{i}.Attribute.#{j}.Value"] = v if v end end end @client.batch_delete_attributes(domain_name, params) end end |
#describe(domain_name) ⇒ Object
329 330 331 332 333 334 335 336 337 338 |
# File 'lib/sdbcli/sdb-driver.rb', line 329 def describe(domain_name) doc = @client.(domain_name) h = {} doc.at_css('DomainMetadataResult').children.each do |child| h[child.name] = child.content end return h end |
#drop_domain(domain_name) ⇒ Object
55 56 57 |
# File 'lib/sdbcli/sdb-driver.rb', line 55 def drop_domain(domain_name) @client.delete_domain(domain_name) end |
#endpoint ⇒ Object
21 22 23 |
# File 'lib/sdbcli/sdb-driver.rb', line 21 def endpoint @client.endpoint end |
#endpoint=(v) ⇒ Object
25 26 27 |
# File 'lib/sdbcli/sdb-driver.rb', line 25 def endpoint=(v) @client.endpoint = v end |
#get(domain_name, item_name, attr_names = [], consistent = false) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/sdbcli/sdb-driver.rb', line 107 def get(domain_name, item_name, attr_names = [], consistent = false) params = {:ConsistentRead => consistent} attr_names.each_with_index {|name, i| params["AttributeName.#{i}"] = name } doc = @client.get_attributes(domain_name, item_name, params) attrs_to_hash(doc) end |
#insert(domain_name, items = {}) ⇒ Object
attr action
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/sdbcli/sdb-driver.rb', line 61 def insert(domain_name, items = {}) until (chunk = items.slice!(0, MAX_NUMBER_SUBMITTED_ITEMS)).empty? params = {} i = j = 0 chunk.each do |item_name, attrs| i += 1 params["Item.#{i}.ItemName"] = item_name (attrs || {}).each do |attr_name, values| [values].flatten.each do |v| j += 1 params["Item.#{i}.Attribute.#{j}.Name"] = attr_name params["Item.#{i}.Attribute.#{j}.Value"] = v params["Item.#{i}.Attribute.#{j}.Replace"] = false end end end @client.batch_put_attributes(domain_name, params) end end |
#next_list(consistent = false) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/sdbcli/sdb-driver.rb', line 134 def next_list(consistent = false) unless @select_expr and @next_token return [] end params = {:SelectExpression => @select_expr, :ConsistentRead => consistent} items = [] token = iterate(:select, params, @next_token) do |doc| doc.css('Item').map do |i| items << [i.at_css('Name').content, attrs_to_hash(i)] end end @current_token = @next_token @next_token = token @current_page += 1 return items end |
#page_num ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/sdbcli/sdb-driver.rb', line 288 def page_num return nil unless @select_expr ss = StringScanner.new(@select_expr) domain_name = nil until ss.eos? if ss.scan(/[^F`'"|!]+/i) #' elsif ss.scan(/`(?:[^`]|``)*`/) elsif ss.scan(/'(?:[^']|'')*'/) #' elsif ss.scan(/"(?:[^"]|"")*"/) #" elsif (tok = ss.scan /FROM\b\s*[^\s]+/i) domain_name = tok.split(/\b/, 2).last break elsif ss.scan(/./) end end return nil unless domain_name domain_name.strip!.gsub!(/`([^`]+)`/, '\1') item_count = describe(domain_name)['ItemCount'].to_f ss = StringScanner.new(@select_expr) limit = 100 until ss.eos? if ss.scan(/[^`'"L]+/i) #' elsif ss.scan( /`(?:[^`]|``)*`/) elsif ss.scan(/'(?:[^']|'')*'/) #' elsif ss.scan(/"(?:[^"]|"")*"/) #" elsif (tok = ss.scan /LIMIT\s+\d\b/i) limit = tok.split(/\s+/).last.to_i break elsif ss.scan(/./) end end return (item_count / limit).ceil end |
#page_to(page, consistent = false) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/sdbcli/sdb-driver.rb', line 177 def page_to(page, consistent = false) if page.zero? raise SimpleDB::Error, "Invalid page number: #{page}" end unless @select_expr return [] end if page < 0 n = page_num return [] unless n page = n + 1 + page end ss = StringScanner.new(@select_expr.dup) limit = 100 until ss.eos? if ss.scan(/[^`'"L]+/i) #' elsif ss.scan( /`(?:[^`]|``)*`/) elsif ss.scan(/'(?:[^']|'')*'/) #' elsif ss.scan(/"(?:[^"]|"")*"/) #" elsif (tok = ss.scan /LIMIT\s+\d\b/i) limit = tok.split(/\s+/).last.to_i break elsif ss.scan(/./) end end params = {:SelectExpression => @select_expr, :ConsistentRead => consistent} items = [] token = (page > 1) ? SimpleDB::TokenGenerator.next_token(limit, page) : :first new_token = iterate(:select, params, token) do |doc| doc.css('Item').map do |i| items << [i.at_css('Name').content, attrs_to_hash(i)] end end @current_token = token @next_token = new_token @current_page = page return items end |
#prev_list(consistent = false) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/sdbcli/sdb-driver.rb', line 225 def prev_list(consistent = false) unless @select_expr and @current_page > 1 return [] end ss = StringScanner.new(@select_expr.dup) limit = 100 until ss.eos? if ss.scan(/[^`'"L]+/i) #' elsif ss.scan( /`(?:[^`]|``)*`/) elsif ss.scan(/'(?:[^']|'')*'/) #' elsif ss.scan(/"(?:[^"]|"")*"/) #" elsif (tok = ss.scan /LIMIT\s+\d\b/i) limit = tok.split(/\s+/).last.to_i elsif ss.scan(/./) end end params = {:SelectExpression => @select_expr, :ConsistentRead => consistent} items = [] token = (@current_page > 2) ? SimpleDB::TokenGenerator.next_token(limit, @current_page - 1) : :first new_token = iterate(:select, params, token) do |doc| doc.css('Item').map do |i| items << [i.at_css('Name').content, attrs_to_hash(i)] end end @current_token = token @next_token = new_token @current_page -= 1 return items end |
#select(expr, consistent = false, persist = false) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/sdbcli/sdb-driver.rb', line 114 def select(expr, consistent = false, persist = false) params = {:SelectExpression => expr, :ConsistentRead => consistent} items = [] token = iterate(:select, params) do |doc| doc.css('Item').map do |i| items << [i.at_css('Name').content, attrs_to_hash(i)] end end if persist @select_expr = expr @current_token = nil @next_token = token @current_page = 1 end return items end |
#show_domains ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/sdbcli/sdb-driver.rb', line 43 def show_domains domains = [] iterate(:list_domains) do |doc| doc.css('DomainName').each do |i| domains << i.content end end return domains end |
#tail(domain_name, consistent = false) ⇒ Object
172 173 174 175 |
# File 'lib/sdbcli/sdb-driver.rb', line 172 def tail(domain_name, consistent = false) @select_expr = "SELECT * FROM #{domain_name}" page_to(-1, consistent) end |
#timeout ⇒ Object
29 30 31 |
# File 'lib/sdbcli/sdb-driver.rb', line 29 def timeout @client.timeout end |
#timeout=(v) ⇒ Object
33 34 35 |
# File 'lib/sdbcli/sdb-driver.rb', line 33 def timeout=(v) @client.timeout = v end |
#update(domain_name, items = {}) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/sdbcli/sdb-driver.rb', line 84 def update(domain_name, items = {}) until (chunk = items.slice!(0, MAX_NUMBER_SUBMITTED_ITEMS)).empty? params = {} i = j = 0 chunk.each do |item_name, attrs| i += 1 params["Item.#{i}.ItemName"] = item_name (attrs || {}).each do |attr_name, values| [values].flatten.each do |v| j += 1 params["Item.#{i}.Attribute.#{j}.Name"] = attr_name params["Item.#{i}.Attribute.#{j}.Value"] = v params["Item.#{i}.Attribute.#{j}.Replace"] = true end end end @client.batch_put_attributes(domain_name, params) end end |