Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Side by Side Diff: third_party/gsutil/boto/boto/dynamodb/batch.py

Issue 12042069: Scripts to download files from google storage based on sha1 sums (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Review fixes, updated gsutil Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright (c) 2012 Mitch Garnaat http://garnaat.org/
2 # Copyright (c) 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining a
5 # copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish, dis-
8 # tribute, sublicense, and/or sell copies of the Software, and to permit
9 # persons to whom the Software is furnished to do so, subject to the fol-
10 # lowing conditions:
11 #
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 # IN THE SOFTWARE.
22 #
23
24
25 class Batch(object):
26 """
27 Used to construct a BatchGet request.
28
29 :ivar table: The Table object from which the item is retrieved.
30
31 :ivar keys: A list of scalar or tuple values. Each element in the
32 list represents one Item to retrieve. If the schema for the
33 table has both a HashKey and a RangeKey, each element in the
34 list should be a tuple consisting of (hash_key, range_key). If
35 the schema for the table contains only a HashKey, each element
36 in the list should be a scalar value of the appropriate type
37 for the table schema. NOTE: The maximum number of items that
38 can be retrieved for a single operation is 100. Also, the
39 number of items retrieved is constrained by a 1 MB size limit.
40
41 :ivar attributes_to_get: A list of attribute names.
42 If supplied, only the specified attribute names will
43 be returned. Otherwise, all attributes will be returned.
44
45 :ivar consistent_read: Specify whether or not to use a
46 consistent read. Defaults to False.
47
48 """
49
50 def __init__(self, table, keys, attributes_to_get=None,
51 consistent_read=False):
52 self.table = table
53 self.keys = keys
54 self.attributes_to_get = attributes_to_get
55 self.consistent_read = consistent_read
56
57 def to_dict(self):
58 """
59 Convert the Batch object into the format required for Layer1.
60 """
61 batch_dict = {}
62 key_list = []
63 for key in self.keys:
64 if isinstance(key, tuple):
65 hash_key, range_key = key
66 else:
67 hash_key = key
68 range_key = None
69 k = self.table.layer2.build_key_from_values(self.table.schema,
70 hash_key, range_key)
71 key_list.append(k)
72 batch_dict['Keys'] = key_list
73 if self.attributes_to_get:
74 batch_dict['AttributesToGet'] = self.attributes_to_get
75 if self.consistent_read:
76 batch_dict['ConsistentRead'] = True
77 else:
78 batch_dict['ConsistentRead'] = False
79 return batch_dict
80
81
82 class BatchWrite(object):
83 """
84 Used to construct a BatchWrite request. Each BatchWrite object
85 represents a collection of PutItem and DeleteItem requests for
86 a single Table.
87
88 :ivar table: The Table object from which the item is retrieved.
89
90 :ivar puts: A list of :class:`boto.dynamodb.item.Item` objects
91 that you want to write to DynamoDB.
92
93 :ivar deletes: A list of scalar or tuple values. Each element in the
94 list represents one Item to delete. If the schema for the
95 table has both a HashKey and a RangeKey, each element in the
96 list should be a tuple consisting of (hash_key, range_key). If
97 the schema for the table contains only a HashKey, each element
98 in the list should be a scalar value of the appropriate type
99 for the table schema.
100 """
101
102 def __init__(self, table, puts=None, deletes=None):
103 self.table = table
104 self.puts = puts or []
105 self.deletes = deletes or []
106
107 def to_dict(self):
108 """
109 Convert the Batch object into the format required for Layer1.
110 """
111 op_list = []
112 for item in self.puts:
113 d = {'Item': self.table.layer2.dynamize_item(item)}
114 d = {'PutRequest': d}
115 op_list.append(d)
116 for key in self.deletes:
117 if isinstance(key, tuple):
118 hash_key, range_key = key
119 else:
120 hash_key = key
121 range_key = None
122 k = self.table.layer2.build_key_from_values(self.table.schema,
123 hash_key, range_key)
124 d = {'Key': k}
125 op_list.append({'DeleteRequest': d})
126 return (self.table.name, op_list)
127
128
129 class BatchList(list):
130 """
131 A subclass of a list object that contains a collection of
132 :class:`boto.dynamodb.batch.Batch` objects.
133 """
134
135 def __init__(self, layer2):
136 list.__init__(self)
137 self.unprocessed = None
138 self.layer2 = layer2
139
140 def add_batch(self, table, keys, attributes_to_get=None,
141 consistent_read=False):
142 """
143 Add a Batch to this BatchList.
144
145 :type table: :class:`boto.dynamodb.table.Table`
146 :param table: The Table object in which the items are contained.
147
148 :type keys: list
149 :param keys: A list of scalar or tuple values. Each element in the
150 list represents one Item to retrieve. If the schema for the
151 table has both a HashKey and a RangeKey, each element in the
152 list should be a tuple consisting of (hash_key, range_key). If
153 the schema for the table contains only a HashKey, each element
154 in the list should be a scalar value of the appropriate type
155 for the table schema. NOTE: The maximum number of items that
156 can be retrieved for a single operation is 100. Also, the
157 number of items retrieved is constrained by a 1 MB size limit.
158
159 :type attributes_to_get: list
160 :param attributes_to_get: A list of attribute names.
161 If supplied, only the specified attribute names will
162 be returned. Otherwise, all attributes will be returned.
163 """
164 self.append(Batch(table, keys, attributes_to_get, consistent_read))
165
166 def resubmit(self):
167 """
168 Resubmit the batch to get the next result set. The request object is
169 rebuild from scratch meaning that all batch added between ``submit``
170 and ``resubmit`` will be lost.
171
172 Note: This method is experimental and subject to changes in future relea ses
173 """
174 del self[:]
175
176 if not self.unprocessed:
177 return None
178
179 for table_name, table_req in self.unprocessed.iteritems():
180 table_keys = table_req['Keys']
181 table = self.layer2.get_table(table_name)
182
183 keys = []
184 for key in table_keys:
185 h = key['HashKeyElement']
186 r = None
187 if 'RangeKeyElement' in key:
188 r = key['RangeKeyElement']
189 keys.append((h, r))
190
191 attributes_to_get = None
192 if 'AttributesToGet' in table_req:
193 attributes_to_get = table_req['AttributesToGet']
194
195 self.add_batch(table, keys, attributes_to_get=attributes_to_get)
196
197 return self.submit()
198
199
200 def submit(self):
201 res = self.layer2.batch_get_item(self)
202 if 'UnprocessedKeys' in res:
203 self.unprocessed = res['UnprocessedKeys']
204 return res
205
206 def to_dict(self):
207 """
208 Convert a BatchList object into format required for Layer1.
209 """
210 d = {}
211 for batch in self:
212 b = batch.to_dict()
213 if b['Keys']:
214 d[batch.table.name] = b
215 return d
216
217
218 class BatchWriteList(list):
219 """
220 A subclass of a list object that contains a collection of
221 :class:`boto.dynamodb.batch.BatchWrite` objects.
222 """
223
224 def __init__(self, layer2):
225 list.__init__(self)
226 self.layer2 = layer2
227
228 def add_batch(self, table, puts=None, deletes=None):
229 """
230 Add a BatchWrite to this BatchWriteList.
231
232 :type table: :class:`boto.dynamodb.table.Table`
233 :param table: The Table object in which the items are contained.
234
235 :type puts: list of :class:`boto.dynamodb.item.Item` objects
236 :param puts: A list of items that you want to write to DynamoDB.
237
238 :type deletes: A list
239 :param deletes: A list of scalar or tuple values. Each element
240 in the list represents one Item to delete. If the schema
241 for the table has both a HashKey and a RangeKey, each
242 element in the list should be a tuple consisting of
243 (hash_key, range_key). If the schema for the table
244 contains only a HashKey, each element in the list should
245 be a scalar value of the appropriate type for the table
246 schema.
247 """
248 self.append(BatchWrite(table, puts, deletes))
249
250 def submit(self):
251 return self.layer2.batch_write_item(self)
252
253 def to_dict(self):
254 """
255 Convert a BatchWriteList object into format required for Layer1.
256 """
257 d = {}
258 for batch in self:
259 table_name, batch_dict = batch.to_dict()
260 d[table_name] = batch_dict
261 return d
262
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698