-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathMovieTable.swift
More file actions
571 lines (498 loc) · 19.6 KB
/
MovieTable.swift
File metadata and controls
571 lines (498 loc) · 19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
137
138
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
169
170
171
172
173
174
175
176
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
224
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
261
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
287
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
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
363
364
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
396
397
398
399
400
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// An example demonstrating one way to represent an Amazon DynamoDB data table
// using a Swift class.
// snippet-start:[ddb.swift.basics.movietable]
import AWSDynamoDB
import Foundation
/// An enumeration of error codes representing issues that can arise when using
/// the `MovieTable` class.
enum MoviesError: Error {
/// The specified table wasn't found or couldn't be created.
case TableNotFound
/// The specified item wasn't found or couldn't be created.
case ItemNotFound
/// The Amazon DynamoDB client is not properly initialized.
case UninitializedClient
/// The table status reported by Amazon DynamoDB is not recognized.
case StatusUnknown
/// One or more specified attribute values are invalid or missing.
case InvalidAttributes
}
/// A class representing an Amazon DynamoDB table containing movie
/// information.
public class MovieTable {
var ddbClient: DynamoDBClient?
let tableName: String
/// Create an object representing a movie table in an Amazon DynamoDB
/// database.
///
/// - Parameters:
/// - region: The optional Amazon Region to create the database in.
/// - tableName: The name to assign to the table. If not specified, a
/// random table name is generated automatically.
///
/// > Note: The table is not necessarily available when this function
/// returns. Use `tableExists()` to check for its availability, or
/// `awaitTableActive()` to wait until the table's status is reported as
/// ready to use by Amazon DynamoDB.
///
init(region: String? = nil, tableName: String) async throws {
do {
let config = try await DynamoDBClient.DynamoDBClientConfiguration()
if let region = region {
config.region = region
}
self.ddbClient = DynamoDBClient(config: config)
self.tableName = tableName
try await self.createTable()
} catch {
print("ERROR: ", dump(error, name: "Initializing Amazon DynamoDBClient client"))
throw error
}
}
// snippet-start:[ddb.swift.basics.createtable]
///
/// Create a movie table in the Amazon DynamoDB data store.
///
private func createTable() async throws {
do {
guard let client = self.ddbClient else {
throw MoviesError.UninitializedClient
}
let input = CreateTableInput(
attributeDefinitions: [
DynamoDBClientTypes.AttributeDefinition(attributeName: "year", attributeType: .n),
DynamoDBClientTypes.AttributeDefinition(attributeName: "title", attributeType: .s)
],
billingMode: DynamoDBClientTypes.BillingMode.payPerRequest,
keySchema: [
DynamoDBClientTypes.KeySchemaElement(attributeName: "year", keyType: .hash),
DynamoDBClientTypes.KeySchemaElement(attributeName: "title", keyType: .range)
],
tableName: self.tableName
)
let output = try await client.createTable(input: input)
if output.tableDescription == nil {
throw MoviesError.TableNotFound
}
} catch {
print("ERROR: createTable:", dump(error))
throw error
}
}
// snippet-end:[ddb.swift.basics.createtable]
// snippet-start:[ddb.swift.basics.tableexists]
/// Check to see if the table exists online yet.
///
/// - Returns: `true` if the table exists, or `false` if not.
///
func tableExists() async throws -> Bool {
do {
guard let client = self.ddbClient else {
throw MoviesError.UninitializedClient
}
let input = DescribeTableInput(
tableName: tableName
)
let output = try await client.describeTable(input: input)
guard let description = output.table else {
throw MoviesError.TableNotFound
}
return description.tableName == self.tableName
} catch {
print("ERROR: tableExists:", dump(error))
throw error
}
}
// snippet-end:[ddb.swift.basics.tableexists]
// snippet-start:[ddb.swift.basics.awaittableactive]
///
/// Waits for the table to exist and for its status to be active.
///
func awaitTableActive() async throws {
while try (await self.tableExists() == false) {
do {
let duration = UInt64(0.25 * 1_000_000_000) // Convert .25 seconds to nanoseconds.
try await Task.sleep(nanoseconds: duration)
} catch {
print("Sleep error:", dump(error))
}
}
while try (await self.getTableStatus() != .active) {
do {
let duration = UInt64(0.25 * 1_000_000_000) // Convert .25 seconds to nanoseconds.
try await Task.sleep(nanoseconds: duration)
} catch {
print("Sleep error:", dump(error))
}
}
}
// snippet-end:[ddb.swift.basics.awaittableactive]
// snippet-start:[ddb.swift.basics.deletetable]
///
/// Deletes the table from Amazon DynamoDB.
///
func deleteTable() async throws {
do {
guard let client = self.ddbClient else {
throw MoviesError.UninitializedClient
}
let input = DeleteTableInput(
tableName: self.tableName
)
_ = try await client.deleteTable(input: input)
} catch {
print("ERROR: deleteTable:", dump(error))
throw error
}
}
// snippet-end:[ddb.swift.basics.deletetable]
// snippet-start:[ddb.swift.basics.gettablestatus]
/// Get the table's status.
///
/// - Returns: The table status, as defined by the
/// `DynamoDBClientTypes.TableStatus` enum.
///
func getTableStatus() async throws -> DynamoDBClientTypes.TableStatus {
do {
guard let client = self.ddbClient else {
throw MoviesError.UninitializedClient
}
let input = DescribeTableInput(
tableName: self.tableName
)
let output = try await client.describeTable(input: input)
guard let description = output.table else {
throw MoviesError.TableNotFound
}
guard let status = description.tableStatus else {
throw MoviesError.StatusUnknown
}
return status
} catch {
print("ERROR: getTableStatus:", dump(error))
throw error
}
}
// snippet-end:[ddb.swift.basics.gettablestatus]
// snippet-start:[ddb.swift.basics.populate]
/// Populate the movie database from the specified JSON file.
///
/// - Parameter jsonPath: Path to a JSON file containing movie data.
///
func populate(jsonPath: String) async throws {
do {
guard let client = self.ddbClient else {
throw MoviesError.UninitializedClient
}
// Create a Swift `URL` and use it to load the file into a `Data`
// object. Then decode the JSON into an array of `Movie` objects.
let fileUrl = URL(fileURLWithPath: jsonPath)
let jsonData = try Data(contentsOf: fileUrl)
var movieList = try JSONDecoder().decode([Movie].self, from: jsonData)
// Truncate the list to the first 200 entries or so for this example.
if movieList.count > 200 {
movieList = Array(movieList[...199])
}
// Before sending records to the database, break the movie list into
// 25-entry chunks, which is the maximum size of a batch item request.
let count = movieList.count
let chunks = stride(from: 0, to: count, by: 25).map {
Array(movieList[$0 ..< Swift.min($0 + 25, count)])
}
// For each chunk, create a list of write request records and populate
// them with `PutRequest` requests, each specifying one movie from the
// chunk. Once the chunk's items are all in the `PutRequest` list,
// send them to Amazon DynamoDB using the
// `DynamoDBClient.batchWriteItem()` function.
for chunk in chunks {
var requestList: [DynamoDBClientTypes.WriteRequest] = []
for movie in chunk {
let item = try await movie.getAsItem()
let request = DynamoDBClientTypes.WriteRequest(
putRequest: .init(
item: item
)
)
requestList.append(request)
}
let input = BatchWriteItemInput(requestItems: [tableName: requestList])
_ = try await client.batchWriteItem(input: input)
}
} catch {
print("ERROR: populate:", dump(error))
throw error
}
}
// snippet-end:[ddb.swift.basics.populate]
// snippet-start:[ddb.swift.basics.add-movie]
/// Add a movie specified as a `Movie` structure to the Amazon DynamoDB
/// table.
///
/// - Parameter movie: The `Movie` to add to the table.
///
func add(movie: Movie) async throws {
do {
guard let client = self.ddbClient else {
throw MoviesError.UninitializedClient
}
// Get a DynamoDB item containing the movie data.
let item = try await movie.getAsItem()
// Send the `PutItem` request to Amazon DynamoDB.
let input = PutItemInput(
item: item,
tableName: self.tableName
)
_ = try await client.putItem(input: input)
} catch {
print("ERROR: add movie:", dump(error))
throw error
}
}
// snippet-end:[ddb.swift.basics.add-movie]
// snippet-start:[ddb.swift.basics.add-args]
/// Given a movie's details, add a movie to the Amazon DynamoDB table.
///
/// - Parameters:
/// - title: The movie's title as a `String`.
/// - year: The release year of the movie (`Int`).
/// - rating: The movie's rating if available (`Double`; default is
/// `nil`).
/// - plot: A summary of the movie's plot (`String`; default is `nil`,
/// indicating no plot summary is available).
///
func add(title: String, year: Int, rating: Double? = nil,
plot: String? = nil) async throws
{
do {
let movie = Movie(title: title, year: year, rating: rating, plot: plot)
try await self.add(movie: movie)
} catch {
print("ERROR: add with fields:", dump(error))
throw error
}
}
// snippet-end:[ddb.swift.basics.add-args]
// snippet-start:[ddb.swift.basics.get]
/// Return a `Movie` record describing the specified movie from the Amazon
/// DynamoDB table.
///
/// - Parameters:
/// - title: The movie's title (`String`).
/// - year: The movie's release year (`Int`).
///
/// - Throws: `MoviesError.ItemNotFound` if the movie isn't in the table.
///
/// - Returns: A `Movie` record with the movie's details.
func get(title: String, year: Int) async throws -> Movie {
do {
guard let client = self.ddbClient else {
throw MoviesError.UninitializedClient
}
let input = GetItemInput(
key: [
"year": .n(String(year)),
"title": .s(title)
],
tableName: self.tableName
)
let output = try await client.getItem(input: input)
guard let item = output.item else {
throw MoviesError.ItemNotFound
}
let movie = try Movie(withItem: item)
return movie
} catch {
print("ERROR: get:", dump(error))
throw error
}
}
// snippet-end:[ddb.swift.basics.get]
// snippet-start:[ddb.swift.basics.getMovies-year]
/// Get all the movies released in the specified year.
///
/// - Parameter year: The release year of the movies to return.
///
/// - Returns: An array of `Movie` objects describing each matching movie.
///
func getMovies(fromYear year: Int) async throws -> [Movie] {
do {
guard let client = self.ddbClient else {
throw MoviesError.UninitializedClient
}
let input = QueryInput(
expressionAttributeNames: [
"#y": "year"
],
expressionAttributeValues: [
":y": .n(String(year))
],
keyConditionExpression: "#y = :y",
tableName: self.tableName
)
// Use "Paginated" to get all the movies.
// This lets the SDK handle the 'lastEvaluatedKey' property in "QueryOutput".
let pages = client.queryPaginated(input: input)
var movieList: [Movie] = []
for try await page in pages {
guard let items = page.items else {
print("Error: no items returned.")
continue
}
// Convert the found movies into `Movie` objects and return an array
// of them.
for item in items {
let movie = try Movie(withItem: item)
movieList.append(movie)
}
}
return movieList
} catch {
print("ERROR: getMovies:", dump(error))
throw error
}
}
// snippet-end:[ddb.swift.basics.getMovies-year]
// snippet-start:[ddb.swift.basics.getmovies-range]
/// Return an array of `Movie` objects released in the specified range of
/// years.
///
/// - Parameters:
/// - firstYear: The first year of movies to return.
/// - lastYear: The last year of movies to return.
/// - startKey: A starting point to resume processing; always use `nil`.
///
/// - Returns: An array of `Movie` objects describing the matching movies.
///
/// > Note: The `startKey` parameter is used by this function when
/// recursively calling itself, and should always be `nil` when calling
/// directly.
///
func getMovies(firstYear: Int, lastYear: Int,
startKey: [Swift.String: DynamoDBClientTypes.AttributeValue]? = nil)
async throws -> [Movie]
{
do {
var movieList: [Movie] = []
guard let client = self.ddbClient else {
throw MoviesError.UninitializedClient
}
let input = ScanInput(
consistentRead: true,
exclusiveStartKey: startKey,
expressionAttributeNames: [
"#y": "year" // `year` is a reserved word, so use `#y` instead.
],
expressionAttributeValues: [
":y1": .n(String(firstYear)),
":y2": .n(String(lastYear))
],
filterExpression: "#y BETWEEN :y1 AND :y2",
tableName: self.tableName
)
let pages = client.scanPaginated(input: input)
for try await page in pages {
guard let items = page.items else {
print("Error: no items returned.")
continue
}
// Build an array of `Movie` objects for the returned items.
for item in items {
let movie = try Movie(withItem: item)
movieList.append(movie)
}
}
return movieList
} catch {
print("ERROR: getMovies with scan:", dump(error))
throw error
}
}
// snippet-end:[ddb.swift.basics.getmovies-range]
// snippet-start:[ddb.swift.basics.update]
/// Update the specified movie with new `rating` and `plot` information.
///
/// - Parameters:
/// - title: The title of the movie to update.
/// - year: The release year of the movie to update.
/// - rating: The new rating for the movie.
/// - plot: The new plot summary string for the movie.
///
/// - Returns: An array of mappings of attribute names to their new
/// listing each item actually changed. Items that didn't need to change
/// aren't included in this list. `nil` if no changes were made.
///
func update(title: String, year: Int, rating: Double? = nil, plot: String? = nil) async throws
-> [Swift.String: DynamoDBClientTypes.AttributeValue]?
{
do {
guard let client = self.ddbClient else {
throw MoviesError.UninitializedClient
}
// Build the update expression and the list of expression attribute
// values. Include only the information that's changed.
var expressionParts: [String] = []
var attrValues: [Swift.String: DynamoDBClientTypes.AttributeValue] = [:]
if rating != nil {
expressionParts.append("info.rating=:r")
attrValues[":r"] = .n(String(rating!))
}
if plot != nil {
expressionParts.append("info.plot=:p")
attrValues[":p"] = .s(plot!)
}
let expression = "set \(expressionParts.joined(separator: ", "))"
let input = UpdateItemInput(
// Create substitution tokens for the attribute values, to ensure
// no conflicts in expression syntax.
expressionAttributeValues: attrValues,
// The key identifying the movie to update consists of the release
// year and title.
key: [
"year": .n(String(year)),
"title": .s(title)
],
returnValues: .updatedNew,
tableName: self.tableName,
updateExpression: expression
)
let output = try await client.updateItem(input: input)
guard let attributes: [Swift.String: DynamoDBClientTypes.AttributeValue] = output.attributes else {
throw MoviesError.InvalidAttributes
}
return attributes
} catch {
print("ERROR: update:", dump(error))
throw error
}
}
// snippet-end:[ddb.swift.basics.update]
// snippet-start:[ddb.swift.basics.delete]
/// Delete a movie, given its title and release year.
///
/// - Parameters:
/// - title: The movie's title.
/// - year: The movie's release year.
///
func delete(title: String, year: Int) async throws {
do {
guard let client = self.ddbClient else {
throw MoviesError.UninitializedClient
}
let input = DeleteItemInput(
key: [
"year": .n(String(year)),
"title": .s(title)
],
tableName: self.tableName
)
_ = try await client.deleteItem(input: input)
} catch {
print("ERROR: delete:", dump(error))
throw error
}
}
// snippet-end:[ddb.swift.basics.delete]
}
// snippet-end:[ddb.swift.basics.movietable]