Skip to content

Commit 67cee1b

Browse files
committed
adopt more consise swift-mongodb api
1 parent e409edb commit 67cee1b

39 files changed

+205
-332
lines changed

Package.resolved

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

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ let package:Package = .init(
100100
from: "0.5.0")),
101101
//.package(url: "https://github.com/tayloraswift/swift-mongodb", .upToNextMinor(
102102
// from: "0.11.0")),
103-
.package(url: "https://github.com/tayloraswift/swift-mongodb", branch: "swap-type-names"),
103+
.package(path: "../swift-mongodb"),
104+
104105
.package(url: "https://github.com/tayloraswift/swift-png", .upToNextMinor(
105106
from: "4.2.0")),
106107

Sources/SwiftinitServer/Configurations/Swiftinit.ServerOptions.Development.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ extension Swiftinit.ServerOptions
1010
/// Whether to enable IP whitelisting.
1111
var whitelists:Bool
1212

13+
/// The name of the replica set to use for development.
14+
var replicaSet:String
15+
1316
/// A test bucket for development. For this to work, you should probably make the bucket
1417
/// publically writable. It goes without saying that you should delete such a bucket
1518
/// as soon as you are done testing it.
@@ -21,9 +24,8 @@ extension Swiftinit.ServerOptions
2124
{
2225
self.cloudfront = false
2326
self.whitelists = false
24-
27+
self.replicaSet = "unidoc-rs"
2528
self.bucket = nil
26-
2729
self.port = 8443
2830
}
2931
}

Sources/SwiftinitServer/Configurations/Swiftinit.ServerOptions.swift

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,30 @@ extension Swiftinit
2626
}
2727
extension Swiftinit.ServerOptions
2828
{
29-
subscript(dynamicMember keyPath:KeyPath<Development, Bool>) -> Bool
29+
private
30+
var development:Development?
3031
{
3132
switch self.mode
3233
{
33-
case .development(_, let options): options[keyPath: keyPath]
34-
case .production: true
34+
case .development(_, let options): options
35+
case .production: nil
3536
}
3637
}
38+
}
39+
extension Swiftinit.ServerOptions
40+
{
41+
subscript(dynamicMember keyPath:KeyPath<Development, Bool>) -> Bool
42+
{
43+
self.development?[keyPath: keyPath] ?? true
44+
}
45+
46+
var replicaSet:String
47+
{
48+
self.development?.replicaSet ?? "swiftinit-rs"
49+
}
3750

3851
var port:Int
3952
{
40-
switch self.mode
41-
{
42-
case .development(_, let options): options.port
43-
case .production: 443
44-
}
53+
self.development?.port ?? 443
4554
}
4655
}

Sources/SwiftinitServer/Main.OptionsError.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extension Main
88
case invalidBucketName(String?)
99
case invalidCertificateDirectory
1010
case invalidPort(String?)
11+
case invalidReplicaSet
1112
case unrecognized(String)
1213
}
1314
}

Sources/SwiftinitServer/Main.swift

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ extension Main
8787
case "-r", "--redirect":
8888
self.redirect = true
8989

90+
case "-s", "--replica-set":
91+
guard let replicaSet:String = arguments.next()
92+
else
93+
{
94+
throw Main.OptionsError.invalidReplicaSet
95+
}
96+
97+
self.development.replicaSet = replicaSet
98+
9099
case "-m", "--mongo":
91100
switch arguments.next()
92101
{
@@ -183,24 +192,26 @@ extension Main
183192
}
184193
else
185194
{
186-
let mongodb:Mongo.DriverBootstrap = MongoDB / [self.mongo] /?
195+
let mongod:Mongo.Host = self.mongo
196+
197+
var configuration:TLSConfiguration = .makeClientConfiguration()
198+
configuration.applicationProtocols = ["h2"]
199+
200+
let context:Swiftinit.ServerPluginContext = .init(threads: threads,
201+
niossl: try .init(configuration: configuration))
202+
let options:Swiftinit.ServerOptions = try self.options()
203+
204+
let mongodb:Mongo.DriverBootstrap = MongoDB / [mongod] /?
187205
{
188206
$0.executors = .shared(threads)
189207
$0.appname = "Unidoc Server"
190208

191209
$0.connectionTimeout = .seconds(5)
192210
$0.monitorInterval = .seconds(3)
193211

194-
$0.topology = .replicated(set: "swiftinit-rs")
212+
$0.topology = .replicated(set: options.replicaSet)
195213
}
196214

197-
var configuration:TLSConfiguration = .makeClientConfiguration()
198-
configuration.applicationProtocols = ["h2"]
199-
200-
let context:Swiftinit.ServerPluginContext = .init(threads: threads,
201-
niossl: try .init(configuration: configuration))
202-
let options:Swiftinit.ServerOptions = try self.options()
203-
204215
await mongodb.withSessionPool(logger: .init(level: .error))
205216
{
206217
@Sendable (pool:Mongo.SessionPool) in

Sources/UnidocDB/Crawling/Unidoc.DB.CrawlingWindows.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ extension Unidoc.DB.CrawlingWindows
5959
$0
6060
{
6161
$0[.upsert] = true
62-
$0[.q] = .init { $0[Unidoc.CrawlingWindow[.id]] = id }
62+
$0[.q] { $0[Unidoc.CrawlingWindow[.id]] = id }
6363
$0[.u]
6464
{
6565
$0[.set]

Sources/UnidocDB/Editions/Unidoc.DB.Editions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extension Unidoc.DB.Editions
5050
}
5151
where:
5252
{
53-
$0[Unidoc.EditionMetadata[.release]] = .init { $0[.eq] = false }
53+
$0[Unidoc.EditionMetadata[.release]] { $0[.eq] = false }
5454
}
5555

5656
public static
@@ -63,7 +63,7 @@ extension Unidoc.DB.Editions
6363
}
6464
where:
6565
{
66-
$0[Unidoc.EditionMetadata[.release]] = .init { $0[.eq] = true }
66+
$0[Unidoc.EditionMetadata[.release]] { $0[.eq] = true }
6767
}
6868
}
6969
extension Unidoc.DB.Editions:Mongo.CollectionModel

Sources/UnidocDB/Mongo.CollectionIndex.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension Mongo.CollectionIndex
4040
collation:Mongo.Collation? = nil,
4141
unique:Bool = false,
4242
fields:(inout Mongo.SortDocument) -> (),
43-
where filter:((inout Mongo.PredicateDocument) -> ())? = nil)
43+
where filter:((inout Mongo.PredicateEncoder) -> ())? = nil)
4444
{
4545
self.init(id: id,
4646
collation: collation,

Sources/UnidocDB/Mongo.CollectionModel.swift

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -473,15 +473,15 @@ extension Mongo.CollectionModel
473473

474474
@inlinable internal
475475
func delete(with session:Mongo.Session,
476-
matching predicate:(inout Mongo.PredicateDocument) throws -> ()) async throws -> Bool
476+
matching predicate:(inout Mongo.PredicateEncoder) -> ()) async throws -> Bool
477477
{
478478
let response:Mongo.DeleteResponse = try await session.run(
479479
command: Mongo.Delete<Mongo.One>.init(Self.name)
480480
{
481-
try $0
481+
$0
482482
{
483483
$0[.limit] = .one
484-
$0[.q] = try .init(with: predicate)
484+
$0[.q, predicate]
485485
}
486486
},
487487
against: self.database)
@@ -491,15 +491,15 @@ extension Mongo.CollectionModel
491491
}
492492

493493
func deleteAll(with session:Mongo.Session,
494-
matching predicate:(inout Mongo.PredicateDocument) throws -> ()) async throws -> Int
494+
matching predicate:(inout Mongo.PredicateEncoder) -> ()) async throws -> Int
495495
{
496496
let response:Mongo.DeleteResponse = try await session.run(
497497
command: Mongo.Delete<Mongo.Many>.init(Self.name)
498498
{
499-
try $0
499+
$0
500500
{
501501
$0[.limit] = .unlimited
502-
$0[.q] = try .init(with: predicate)
502+
$0[.q, predicate]
503503
}
504504
},
505505
against: self.database)
@@ -521,24 +521,12 @@ extension Mongo.CollectionModel // where Element.ID == Unidoc.Scalar
521521
$0
522522
{
523523
$0[.limit] = .unlimited
524-
$0[.q] = .init
524+
$0[.q]
525525
{
526-
$0[.and] = .init
526+
$0[.and]
527527
{
528-
$0.append
529-
{
530-
$0["_id"] = .init
531-
{
532-
$0[.gte] = range.min
533-
}
534-
}
535-
$0.append
536-
{
537-
$0["_id"] = .init
538-
{
539-
$0[.lte] = range.max
540-
}
541-
}
528+
$0 { $0["_id"] { $0[.gte] = range.min } }
529+
$0 { $0["_id"] { $0[.lte] = range.max } }
542530
}
543531
}
544532
}

Sources/UnidocDB/Mongo.UpdateListEncoder (ext).swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ extension Mongo.UpdateListEncoder
1212
self
1313
{
1414
$0[.hint] = .init { $0[index] = (+) }
15-
$0[.q] = .init { $0[index] = key }
16-
$0[.u]
17-
{
18-
$0[.set] { $0[field] = value }
19-
}
15+
$0[.q] { $0[index] = key }
16+
$0[.u] { $0[.set] { $0[field] = value } }
2017
}
2118
}
2219
}

Sources/UnidocDB/Packages/Unidoc.DB.Packages.AlignRealm.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extension Unidoc.DB.Packages.AlignRealm:Mongo.UpdateQuery
3737
switch self
3838
{
3939
case .aligning(let package):
40-
$0[.q] = .init { $0[Unidoc.PackageMetadata[.id]] = package }
40+
$0[.q] { $0[Unidoc.PackageMetadata[.id]] = package }
4141
$0[.u]
4242
{
4343
$0[.set]
@@ -47,7 +47,7 @@ extension Unidoc.DB.Packages.AlignRealm:Mongo.UpdateQuery
4747
}
4848

4949
case .aligned(let package, let realm):
50-
$0[.q] = .init { $0[Unidoc.PackageMetadata[.id]] = package }
50+
$0[.q] { $0[Unidoc.PackageMetadata[.id]] = package }
5151
$0[.u]
5252
{
5353
$0[.unset]

Sources/UnidocDB/Packages/Unidoc.DB.Packages.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extension Unidoc.DB.Packages
2828
}
2929
where:
3030
{
31-
$0[Unidoc.PackageMetadata[.repo] / Unidoc.PackageRepo[.account]] = .init
31+
$0[Unidoc.PackageMetadata[.repo] / Unidoc.PackageRepo[.account]]
3232
{
3333
$0[.exists] = true
3434
}
@@ -41,7 +41,7 @@ extension Unidoc.DB.Packages
4141
}
4242
where:
4343
{
44-
$0[Unidoc.PackageMetadata[.repo] / Unidoc.PackageRepo[.expires]] = .init
44+
$0[Unidoc.PackageMetadata[.repo] / Unidoc.PackageRepo[.expires]]
4545
{
4646
$0[.exists] = true
4747
}
@@ -54,7 +54,7 @@ extension Unidoc.DB.Packages
5454
}
5555
where:
5656
{
57-
$0[Unidoc.PackageMetadata[.repo]] = .init { $0[.exists] = true }
57+
$0[Unidoc.PackageMetadata[.repo]] { $0[.exists] = true }
5858
}
5959

6060
public static
@@ -66,8 +66,10 @@ extension Unidoc.DB.Packages
6666
}
6767
where:
6868
{
69-
$0[ Unidoc.PackageMetadata[.repo] /
70-
Unidoc.PackageRepo[.github]] = .init { $0[.exists] = true }
69+
$0[ Unidoc.PackageMetadata[.repo] / Unidoc.PackageRepo[.github]]
70+
{
71+
$0[.exists] = true
72+
}
7173
}
7274

7375
public static
@@ -77,7 +79,7 @@ extension Unidoc.DB.Packages
7779
}
7880
where:
7981
{
80-
$0[Unidoc.PackageMetadata[.realm]] = .init { $0[.exists] = true }
82+
$0[Unidoc.PackageMetadata[.realm]] { $0[.exists] = true }
8183
}
8284
}
8385
extension Unidoc.DB.Packages:Mongo.CollectionModel
@@ -122,7 +124,7 @@ extension Unidoc.DB.Packages
122124
{
123125
// We need this to use the partial index, for some reason.
124126
$0[ Unidoc.PackageMetadata[.repo] /
125-
Unidoc.PackageRepo[.github]] = .init { $0[.exists] = true }
127+
Unidoc.PackageRepo[.github]] { $0[.exists] = true }
126128

127129
$0[ Unidoc.PackageMetadata[.repo] /
128130
Unidoc.PackageRepo[.github] /
@@ -204,7 +206,7 @@ extension Unidoc.DB.Packages
204206
{
205207
$0[.filter] = .init
206208
{
207-
$0[Unidoc.PackageMetadata[.repo] / Unidoc.PackageRepo[.expires]] = .init
209+
$0[Unidoc.PackageMetadata[.repo] / Unidoc.PackageRepo[.expires]]
208210
{
209211
$0[.exists] = true
210212
}
@@ -235,7 +237,7 @@ extension Unidoc.DB.Packages
235237
{
236238
$0[.filter] = .init
237239
{
238-
$0[Unidoc.PackageMetadata[.hidden]] = .init { $0[.exists] = false }
240+
$0[Unidoc.PackageMetadata[.hidden]] { $0[.exists] = false }
239241
}
240242
},
241243
against: self.database)

Sources/UnidocDB/Snapshots/Unidoc.DB.Snapshots.ClearUplink.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extension Unidoc.DB.Snapshots.ClearUplink:Mongo.UpdateQuery
2828
switch self
2929
{
3030
case .one(let edition):
31-
$0[.q] = .init { $0[Unidoc.Snapshot[.id]] = edition }
31+
$0[.q] { $0[Unidoc.Snapshot[.id]] = edition }
3232
}
3333

3434
$0[.u]

Sources/UnidocDB/Snapshots/Unidoc.DB.Snapshots.QueueUplink.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extension Unidoc.DB.Snapshots.QueueUplink:Mongo.UpdateQuery
3131
$0[.multi] = true
3232
}
3333

34-
$0[.q] = .init
34+
$0[.q]
3535
{
3636
switch self
3737
{

0 commit comments

Comments
 (0)