Skip to content

Commit c0d88ec

Browse files
committed
fix collection scan due to failure to use partial index
1 parent ec634fe commit c0d88ec

File tree

10 files changed

+194
-187
lines changed

10 files changed

+194
-187
lines changed

Sources/UnidocDatabase/Database/Database.Groups.swift

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,35 @@ extension Database
1717
}
1818
extension Database.Groups:DatabaseCollection
1919
{
20-
typealias ElementID = Unidoc.Scalar
21-
2220
@inlinable public static
2321
var name:Mongo.Collection { "groups" }
24-
}
25-
extension Database.Groups
26-
{
27-
func setup(with session:Mongo.Session) async throws
28-
{
29-
let response:Mongo.CreateIndexesResponse = try await session.run(
30-
command: Mongo.CreateIndexes.init(Self.name,
31-
writeConcern: .majority,
32-
indexes:
33-
[
34-
.init
35-
{
36-
$0[.unique] = true
37-
$0[.name] = "id"
38-
$0[.key] = .init
39-
{
40-
$0[Volume.Group[.id]] = (+)
41-
$0[Volume.Group[.latest]] = (-)
42-
}
43-
},
44-
.init
45-
{
46-
$0[.unique] = false
47-
$0[.name] = "scope"
48-
$0[.key] = .init
49-
{
50-
$0[Volume.Group[.scope]] = (+)
51-
$0[Volume.Group[.latest]] = (-)
52-
}
53-
},
54-
]),
55-
against: self.database)
5622

57-
assert(response.indexesAfter == 3)
58-
}
23+
typealias ElementID = Unidoc.Scalar
24+
25+
static
26+
let indexes:[Mongo.CreateIndexStatement] =
27+
[
28+
// .init
29+
// {
30+
// $0[.unique] = true
31+
// $0[.name] = "id,latest"
32+
// $0[.key] = .init
33+
// {
34+
// $0[Volume.Group[.id]] = (+)
35+
// $0[Volume.Group[.latest]] = (-)
36+
// }
37+
// },
38+
.init
39+
{
40+
$0[.unique] = false
41+
$0[.name] = "scope"
42+
$0[.key] = .init
43+
{
44+
$0[Volume.Group[.scope]] = (+)
45+
$0[Volume.Group[.latest]] = (-)
46+
}
47+
},
48+
]
5949
}
6050
extension Database.Groups
6151
{

Sources/UnidocDatabase/Database/Database.Masters.swift

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,59 +18,49 @@ extension Database
1818
}
1919
extension Database.Masters:DatabaseCollection
2020
{
21-
typealias ElementID = Unidoc.Scalar
22-
2321
@inlinable public static
2422
var name:Mongo.Collection { "masters" }
25-
}
26-
extension Database.Masters
27-
{
28-
func setup(with session:Mongo.Session) async throws
29-
{
30-
let response:Mongo.CreateIndexesResponse = try await session.run(
31-
command: Mongo.CreateIndexes.init(Self.name,
32-
writeConcern: .majority,
33-
indexes:
34-
[
35-
// If a snapshot contains a hash collision, insertion will fail.
36-
// Because the index is prefixed with the stem, we expect this to be
37-
// extraordinarily rare.
38-
// See:
39-
// forums.swift.org/t/how-does-docc-mitigate-fnv-1-hash-collisions/65673
40-
.init
41-
{
42-
$0[.unique] = true
43-
$0[.name] = "zone,stem,hash"
4423

45-
$0[.collation] = Database.collation
46-
$0[.key] = .init
47-
{
48-
$0[Volume.Master[.zone]] = (+)
49-
$0[Volume.Master[.stem]] = (+)
50-
$0[Volume.Master[.hash]] = (+)
51-
}
52-
// This limits the index to masters with a stem. This is all of them,
53-
// except for ``Volume.Master.File``.
54-
$0[.partialFilterExpression] = .init
55-
{
56-
$0[Volume.Master[.stem]] = .init { $0[.exists] = true }
57-
}
58-
},
59-
.init
60-
{
61-
$0[.unique] = true
62-
$0[.name] = "hash,id"
24+
typealias ElementID = Unidoc.Scalar
6325

64-
$0[.collation] = Database.collation
65-
$0[.key] = .init
66-
{
67-
$0[Volume.Master[.hash]] = (+)
68-
$0[Volume.Master[.id]] = (+)
69-
}
70-
},
71-
]),
72-
against: self.database)
26+
static
27+
let indexes:[Mongo.CreateIndexStatement] =
28+
[
29+
// If a snapshot contains a hash collision, insertion will fail.
30+
// Because the index is prefixed with the stem, we expect this to be
31+
// extraordinarily rare.
32+
// See:
33+
// forums.swift.org/t/how-does-docc-mitigate-fnv-1-hash-collisions/65673
34+
.init
35+
{
36+
$0[.unique] = true
37+
$0[.name] = "zone,stem,hash"
7338

74-
assert(response.indexesAfter == 3)
75-
}
39+
$0[.collation] = Database.collation
40+
$0[.key] = .init
41+
{
42+
$0[Volume.Master[.zone]] = (+)
43+
$0[Volume.Master[.stem]] = (+)
44+
$0[Volume.Master[.hash]] = (+)
45+
}
46+
// This limits the index to masters with a stem. This is all of them,
47+
// except for ``Volume.Master.File``.
48+
$0[.partialFilterExpression] = .init
49+
{
50+
$0[Volume.Master[.stem]] = .init { $0[.exists] = true }
51+
}
52+
},
53+
.init
54+
{
55+
$0[.unique] = true
56+
$0[.name] = "hash,id"
57+
58+
$0[.collation] = Database.collation
59+
$0[.key] = .init
60+
{
61+
$0[Volume.Master[.hash]] = (+)
62+
$0[Volume.Master[.id]] = (+)
63+
}
64+
},
65+
]
7666
}

Sources/UnidocDatabase/Database/Database.Names.swift

Lines changed: 56 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -18,79 +18,69 @@ extension Database
1818
}
1919
extension Database.Names:DatabaseCollection
2020
{
21-
typealias ElementID = Unidoc.Zone
22-
2321
@inlinable public static
2422
var name:Mongo.Collection { "names" }
25-
}
26-
extension Database.Names
27-
{
28-
func setup(with session:Mongo.Session) async throws
29-
{
30-
let response:Mongo.CreateIndexesResponse = try await session.run(
31-
command: Mongo.CreateIndexes.init(Self.name,
32-
writeConcern: .majority,
33-
indexes:
34-
[
35-
.init
36-
{
37-
$0[.unique] = true
38-
$0[.name] = "id,latest"
3923

40-
$0[.key] = .init
41-
{
42-
$0[Volume.Names[.id]] = (+)
43-
$0[Volume.Names[.latest]] = (-)
44-
}
45-
},
46-
.init
47-
{
48-
$0[.unique] = true
49-
$0[.name] = "id,patch"
24+
typealias ElementID = Unidoc.Zone
5025

51-
$0[.key] = .init
52-
{
53-
$0[Volume.Names[.id]] = (+)
54-
$0[Volume.Names[.patch]] = (-)
55-
}
56-
$0[.partialFilterExpression] = .init
57-
{
58-
$0[Volume.Names[.patch]] = .init { $0[.exists] = true }
59-
}
60-
},
61-
.init
62-
{
63-
$0[.unique] = true
64-
$0[.name] = "package,patch"
26+
static
27+
let indexes:[Mongo.CreateIndexStatement] =
28+
[
29+
.init
30+
{
31+
$0[.unique] = true
32+
$0[.name] = "id,latest"
6533

66-
$0[.collation] = Database.collation
67-
$0[.key] = .init
68-
{
69-
$0[Volume.Names[.package]] = (+)
70-
$0[Volume.Names[.patch]] = (-)
71-
}
72-
$0[.partialFilterExpression] = .init
73-
{
74-
$0[Volume.Names[.patch]] = .init { $0[.exists] = true }
75-
}
76-
},
77-
.init
78-
{
79-
$0[.unique] = true
80-
$0[.name] = "package,version"
34+
$0[.key] = .init
35+
{
36+
$0[Volume.Names[.id]] = (+)
37+
$0[Volume.Names[.latest]] = (-)
38+
}
39+
},
40+
.init
41+
{
42+
$0[.unique] = true
43+
$0[.name] = "id,patch"
8144

82-
$0[.collation] = Database.collation
83-
$0[.key] = .init
84-
{
85-
$0[Volume.Names[.package]] = (+)
86-
$0[Volume.Names[.version]] = (+)
87-
}
88-
},
89-
]),
90-
against: self.database)
45+
$0[.key] = .init
46+
{
47+
$0[Volume.Names[.id]] = (+)
48+
$0[Volume.Names[.patch]] = (-)
49+
}
50+
$0[.partialFilterExpression] = .init
51+
{
52+
$0[Volume.Names[.patch]] = .init { $0[.exists] = true }
53+
}
54+
},
55+
.init
56+
{
57+
$0[.unique] = true
58+
$0[.name] = "package,patch"
9159

92-
assert(response.indexesAfter == 5)
93-
}
60+
$0[.collation] = Database.collation
61+
$0[.key] = .init
62+
{
63+
$0[Volume.Names[.package]] = (+)
64+
$0[Volume.Names[.patch]] = (-)
65+
}
66+
$0[.partialFilterExpression] = .init
67+
{
68+
$0[Volume.Names[.patch]] = .init { $0[.exists] = true }
69+
}
70+
},
71+
.init
72+
{
73+
$0[.unique] = true
74+
$0[.name] = "package,version"
75+
76+
$0[.collation] = Database.collation
77+
$0[.key] = .init
78+
{
79+
$0[Volume.Names[.package]] = (+)
80+
$0[Volume.Names[.version]] = (+)
81+
}
82+
},
83+
]
9484
}
9585
extension Database.Names
9686
{

Sources/UnidocDatabase/Database/Database.Packages.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@ extension Database
2020
}
2121
extension Database.Packages:DatabaseCollection
2222
{
23-
typealias ElementID = Never?
24-
2523
@inlinable public static
2624
var name:Mongo.Collection { "packages" }
27-
}
28-
extension Database.Packages
29-
{
30-
func setup(with session:Mongo.Session) async throws
31-
{
32-
}
25+
26+
typealias ElementID = Never?
27+
28+
static
29+
var indexes:[Mongo.CreateIndexStatement] { [] }
3330
}

Sources/UnidocDatabase/Database/Database.Search.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@ extension Database
1717
}
1818
extension Database.Search:DatabaseCollection
1919
{
20-
typealias ElementID = VolumeIdentifier
21-
2220
@inlinable public static
2321
var name:Mongo.Collection { "search" }
24-
}
25-
extension Database.Search
26-
{
27-
func setup(with session:Mongo.Session) async throws
28-
{
29-
}
22+
23+
typealias ElementID = VolumeIdentifier
24+
25+
static
26+
var indexes:[Mongo.CreateIndexStatement] { [] }
3027
}

Sources/UnidocDatabase/Database/Database.Trees.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@ extension Database
1717
}
1818
extension Database.Trees:DatabaseCollection
1919
{
20-
typealias ElementID = Unidoc.Scalar
21-
2220
@inlinable public static
2321
var name:Mongo.Collection { "trees" }
24-
}
25-
extension Database.Trees
26-
{
27-
func setup(with session:Mongo.Session) async throws
28-
{
29-
}
22+
23+
typealias ElementID = Unidoc.Scalar
24+
25+
static
26+
var indexes:[Mongo.CreateIndexStatement] { [] }
3027
}

0 commit comments

Comments
 (0)