Skip to content

Commit 4fe69f0

Browse files
authored
Merge pull request #4 from Hexaville/[email protected]
Bump AWSSDKSwift version to 2.0.2
2 parents 7d65c46 + c2b50c9 commit 4fe69f0

File tree

5 files changed

+175
-64
lines changed

5 files changed

+175
-64
lines changed

Package.resolved

Lines changed: 56 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ let package = Package(
1010
.executable(name: "dynamodb-session-store-example", targets: ["DynamodbSessionStoreExample"]),
1111
],
1212
dependencies: [
13-
.package(url: "https://github.com/swift-aws/dynamodb.git", .upToNextMajor(from: "1.0.0")),
14-
.package(url: "https://github.com/noppoMan/HexavilleFramework.git", .upToNextMajor(from: "0.1.15"))
13+
.package(url: "https://github.com/swift-aws/aws-sdk-swift.git", .upToNextMajor(from: "2.0.2")),
14+
.package(url: "https://github.com/noppoMan/HexavilleFramework.git", .upToNextMajor(from: "1.0.0-rc.1"))
1515
],
1616
targets: [
17-
.target(name: "DynamodbSessionStore", dependencies: ["SwiftAWSDynamodb", "HexavilleFramework"]),
17+
.target(name: "DynamodbSessionStore", dependencies: ["DynamoDB", "HexavilleFramework"]),
1818
.target(name: "DynamodbSessionStoreTableManager", dependencies: ["DynamodbSessionStore"]),
1919
.target(name: "DynamodbSessionStoreExample", dependencies: ["DynamodbSessionStore"])
2020
]

Sources/DynamodbSessionStore/DynamodbSessionStore.swift

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import Foundation
22
import HexavilleFramework
3-
import SwiftAWSDynamodb
3+
import DynamoDB
44

55
enum DynamodbSessionStoreError: Error {
66
case couldNotFindItem
77
}
88

99
public struct DynamodbSessionStore: SessionStoreProvider {
1010

11-
let dynamodb: Dynamodb
11+
let dynamodb: DynamoDB
1212

1313
let tableName: String
1414

15-
public init(tableName: String, dynamodb: Dynamodb) {
15+
public init(tableName: String, dynamodb: DynamoDB) {
1616
self.dynamodb = dynamodb
1717
self.tableName = tableName
1818
}
@@ -23,9 +23,9 @@ public struct DynamodbSessionStore: SessionStoreProvider {
2323
}
2424

2525
public func read(forKey: String) throws -> [String : Any]? {
26-
let input = Dynamodb.GetItemInput(
26+
let input = DynamoDB.GetItemInput(
27+
key: ["session_id": DynamoDB.AttributeValue(s: forKey)],
2728
consistentRead: true,
28-
key: ["session_id": Dynamodb.AttributeValue(s: forKey)],
2929
tableName: tableName
3030
)
3131
let result = try dynamodb.getItem(input)
@@ -39,19 +39,20 @@ public struct DynamodbSessionStore: SessionStoreProvider {
3939

4040
public func write(value: [String : Any], forKey: String, ttl: Int?) throws {
4141
let data = try JSONSerialization.data(withJSONObject: value, options: [])
42-
let stringValue = String(bytes: Base64Encoder.shared.encode(data.bytes), encoding: .utf8) ?? ""
43-
var item: [String: Dynamodb.AttributeValue] = [
44-
"session_id" : Dynamodb.AttributeValue(s: forKey),
45-
"value": Dynamodb.AttributeValue(s: stringValue)
42+
let bytes = data.withUnsafeBytes({ [UInt8](UnsafeBufferPointer(start: $0, count: data.count)) })
43+
let stringValue = String(bytes: Base64Encoder.shared.encode(bytes), encoding: .utf8) ?? ""
44+
var item: [String: DynamoDB.AttributeValue] = [
45+
"session_id" : DynamoDB.AttributeValue(s: forKey),
46+
"value": DynamoDB.AttributeValue(s: stringValue)
4647
]
4748

4849
if let ttl = ttl {
4950
var date = Date()
5051
date.addTimeInterval(TimeInterval(ttl))
51-
item["expires_at"] = Dynamodb.AttributeValue(n: "\(Int(date.timeIntervalSince1970))")
52+
item["expires_at"] = DynamoDB.AttributeValue(n: "\(Int(date.timeIntervalSince1970))")
5253
}
5354

54-
let input = Dynamodb.PutItemInput(
55+
let input = DynamoDB.PutItemInput(
5556
item: item,
5657
tableName: tableName
5758
)
@@ -60,8 +61,8 @@ public struct DynamodbSessionStore: SessionStoreProvider {
6061
}
6162

6263
public func delete(forKey: String) throws {
63-
let input = Dynamodb.DeleteItemInput(
64-
key: ["session_id" : Dynamodb.AttributeValue(s: forKey)],
64+
let input = DynamoDB.DeleteItemInput(
65+
key: ["session_id" : DynamoDB.AttributeValue(s: forKey)],
6566
tableName: tableName
6667
)
6768
_ = try dynamodb.deleteItem(input)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//
2+
// Pthread.swift
3+
// AWSSDKSwiftCore
4+
//
5+
// Created by Yuki Takei on 2018/12/12.
6+
//
7+
8+
import Foundation
9+
10+
#if os(Linux)
11+
import Glibc
12+
#else
13+
import Darwin.C
14+
#endif
15+
16+
import Foundation
17+
18+
public class Mutex {
19+
fileprivate var mutex: pthread_mutex_t
20+
21+
public init(){
22+
mutex = pthread_mutex_t()
23+
pthread_mutex_init(&mutex, nil)
24+
}
25+
26+
public func lock(){
27+
pthread_mutex_lock(&mutex)
28+
}
29+
30+
public func unlock(){
31+
pthread_mutex_unlock(&mutex)
32+
}
33+
34+
deinit{
35+
pthread_mutex_destroy(&mutex)
36+
}
37+
}
38+
39+
public class Cond {
40+
41+
public let mutex = Mutex()
42+
43+
fileprivate var cond: pthread_cond_t
44+
45+
public convenience init(){
46+
self.init(mutext: Mutex())
47+
}
48+
49+
public init(mutext: Mutex){
50+
cond = pthread_cond_t()
51+
pthread_cond_init(&cond, nil)
52+
}
53+
54+
public func broadcast() {
55+
pthread_cond_broadcast(&cond)
56+
}
57+
58+
public func wait(){
59+
pthread_cond_wait(&cond, &mutex.mutex)
60+
}
61+
62+
@discardableResult
63+
public func wait(timeout: TimeInterval) -> Bool {
64+
let ms = Int(timeout*1000)
65+
var tv = timeval()
66+
var ts = timespec()
67+
gettimeofday(&tv, nil)
68+
ts.tv_sec = time(nil) + ms / 1000
69+
let tmp = 1000 * 1000 * (ms % 1000)
70+
ts.tv_nsec = Int(Int(tv.tv_usec) * 1000 + tmp)
71+
ts.tv_sec += ts.tv_nsec / 1000000000
72+
ts.tv_nsec %= 1000000000
73+
74+
return pthread_cond_timedwait(&cond, &mutex.mutex, &ts) == 0
75+
}
76+
77+
public func signal(){
78+
pthread_cond_signal(&cond)
79+
}
80+
81+
deinit{
82+
pthread_cond_destroy(&cond)
83+
}
84+
}

Sources/DynamodbSessionStoreTableManager/main.swift

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import Foundation
2-
import SwiftAWSDynamodb
3-
import Prorsum
2+
import DynamoDB
43
import SwiftCLI
54

65
class CreateCommand: Command {
@@ -16,24 +15,24 @@ class CreateCommand: Command {
1615
let endpoint = Key<String>("--endpoint", description: "The endpoint string. ex: http://localhost:8000")
1716

1817
func execute() throws {
19-
let dynamodb = Dynamodb(endpoint: endpoint.value)
20-
let input = Dynamodb.CreateTableInput(
18+
let dynamodb = DynamoDB(endpoint: endpoint.value)
19+
let input = DynamoDB.CreateTableInput(
20+
provisionedThroughput: DynamoDB.ProvisionedThroughput(
21+
readCapacityUnits: Int64(readCapacityUnits.value ?? 10),
22+
writeCapacityUnits: Int64(writeCapacityUnits.value ?? 10)
23+
),
24+
tableName: tableName.value,
2125
attributeDefinitions: [
22-
Dynamodb.AttributeDefinition(attributeType: .s, attributeName: "session_id"),
26+
DynamoDB.AttributeDefinition(attributeName: "session_id", attributeType: .s),
2327
],
2428
keySchema: [
25-
Dynamodb.KeySchemaElement(attributeName: "session_id", keyType: .hash)
26-
],
27-
provisionedThroughput: Dynamodb.ProvisionedThroughput(
28-
writeCapacityUnits: Int64(writeCapacityUnits.value ?? 10),
29-
readCapacityUnits: Int64(readCapacityUnits.value ?? 10)
30-
),
31-
tableName: tableName.value
29+
DynamoDB.KeySchemaElement(keyType: .hash, attributeName: "session_id")
30+
]
3231
)
3332

3433
do {
3534
_ = try dynamodb.createTable(input)
36-
let timeToLiveSpecificationInput = Dynamodb.TimeToLiveSpecification(
35+
let timeToLiveSpecificationInput = DynamoDB.TimeToLiveSpecification(
3736
attributeName: "expires_at",
3837
enabled: true
3938
)
@@ -57,7 +56,7 @@ class CreateCommand: Command {
5756
throw CreateCommandError.timeout
5857
}
5958

60-
let describeTableOutput = try dynamodb.describeTable(Dynamodb.DescribeTableInput(tableName: tableName.value))
59+
let describeTableOutput = try dynamodb.describeTable(DynamoDB.DescribeTableInput(tableName: tableName.value))
6160
guard let tableStatus = describeTableOutput.table?.tableStatus else {
6261
fatalError("TableStatus must not empty")
6362
}
@@ -76,9 +75,9 @@ class CreateCommand: Command {
7675

7776
print("Applying updateTimeToLive configuration to \(tableName.value)....")
7877

79-
let updateTimeToLiveInput = Dynamodb.UpdateTimeToLiveInput(
80-
tableName: tableName.value,
81-
timeToLiveSpecification: timeToLiveSpecificationInput
78+
let updateTimeToLiveInput = DynamoDB.UpdateTimeToLiveInput(
79+
timeToLiveSpecification: timeToLiveSpecificationInput,
80+
tableName: tableName.value
8281
)
8382

8483
_ = try dynamodb.updateTimeToLive(updateTimeToLiveInput)
@@ -98,8 +97,8 @@ class DeleteCommand: Command {
9897

9998
func execute() throws {
10099
do {
101-
let dynamodb = Dynamodb(endpoint: endpoint.value)
102-
_ = try dynamodb.deleteTable(Dynamodb.DeleteTableInput(tableName: tableName.value))
100+
let dynamodb = DynamoDB(endpoint: endpoint.value)
101+
_ = try dynamodb.deleteTable(DynamoDB.DeleteTableInput(tableName: tableName.value))
103102
} catch {
104103
print(error)
105104
throw error

0 commit comments

Comments
 (0)