on master help sign in
Sources/App/Models/SafeDict.swift+11−3
since splitsnapshots
unifiedsplit
@@ -7,6 +7,14 @@
7
7
8import Foundation
8import Foundation
9
9
10+func getRandomNum(_ min: Int, _ max: Int) -> Int {
11+ #if os(Linux)
12+ return Int(random() % max) + min
13+ #else
14+ return Int(arc4random_uniform(UInt32(max)) + UInt32(min))
15+ #endif
16+}
17+
10struct SafeDict<Key: Hashable, Value> {
18struct SafeDict<Key: Hashable, Value> {
11 private var queue = DispatchQueue(label: "soy.iko.safeDict.queue", attributes: .concurrent)
19 private var queue = DispatchQueue(label: "soy.iko.safeDict.queue", attributes: .concurrent)
12
20
@@ -33,9 +41,9 @@ struct SafeDict<Key: Hashable, Value> {
33 mutating func removeRandom() -> Key? {
41 mutating func removeRandom() -> Key? {
34 var key: Key?
42 var key: Key?
35 queue.sync(flags: .barrier) {
43 queue.sync(flags: .barrier) {
36− key = dict.keys.randomElement()
44+ if dict.count > 0 {
37− if let key = key {
45+ key = Array(dict.keys)[getRandomNum(0, dict.count)]
38− dict[key] = nil
46+ dict[key!] = nil
39 }
47 }
40 }
48 }
41 return key
49 return key
Sources/App/routes.swift+4
@@ -33,6 +33,10 @@ public func routes(_ router: Router) throws {
33 }
33 }
34 return post
34 return post
35 }
35 }
36+
37+ router.get("posts") { (req) -> [Post] in
38+ return Array(Post.storage.values)
39+ }
36}
40}
37
41
38enum DatabaseError: Error, AbortError {
42enum DatabaseError: Error, AbortError {
Sources/Run/main.swift+6
@@ -1,3 +1,9 @@
1import App
1import App
2
2
3+#if os(Linux)
4+import SwiftGlibc
5+srandom(UInt32(time(nil)))
6+#endif
7+
8+
3try app(.detect()).run()
9try app(.detect()).run()