Skip to content

Commit d70b276

Browse files
committed
add a “latest” flag to article vertices
1 parent c542f97 commit d70b276

File tree

10 files changed

+189
-106
lines changed

10 files changed

+189
-106
lines changed

Package.resolved

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

Sources/UnidocDB/Unidoc.DB.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,8 @@ extension Unidoc.DB
599599
try await self.trees.insert(mesh.trees)
600600

601601
try await self.redirects.insert(mesh.redirects)
602-
try await self.vertices.insert(mesh.vertices)
602+
try await self.vertices.insert(mesh.vertices,
603+
latest: mesh.metadata.latest)
603604
try await self.groups.insert(mesh.groups,
604605
realm: mesh.metadata.latest ? mesh.metadata.realm : nil)
605606

@@ -654,7 +655,8 @@ extension Unidoc.DB
654655
alignment:
655656
if let latest:Unidoc.Edition = mesh.latestRelease
656657
{
657-
try await self.update(with: Volumes.AlignLatest.init(to: latest))
658+
try await self.update(with: Vertices.VacuumLatest.init(around: latest))
659+
try await self.update(with: Volumes.VacuumLatest.init(around: latest))
658660

659661
guard
660662
let realm:Unidoc.Realm = mesh.metadata.realm
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import MongoDB
2+
import MongoQL
3+
import Unidoc
4+
import UnidocRecords
5+
6+
extension Unidoc.DB.Vertices
7+
{
8+
struct VacuumLatest
9+
{
10+
let latest:Unidoc.Edition
11+
12+
init(around latest:Unidoc.Edition)
13+
{
14+
self.latest = latest
15+
}
16+
}
17+
}
18+
extension Unidoc.DB.Vertices.VacuumLatest:Mongo.UpdateQuery
19+
{
20+
typealias Target = Unidoc.DB.Vertices
21+
typealias Effect = Mongo.Many
22+
23+
var ordered:Bool { true }
24+
25+
func build(updates:inout Mongo.UpdateListEncoder<Mongo.Many>)
26+
{
27+
let latest:ClosedRange<Unidoc.Scalar> = .edition(self.latest)
28+
let all:ClosedRange<Unidoc.Scalar> = .package(self.latest.package)
29+
30+
updates
31+
{
32+
$0[.multi] = true
33+
$0[.hint] = Unidoc.DB.Vertices.indexLinkableFlag.id
34+
$0[.q]
35+
{
36+
$0[.and]
37+
{
38+
$0 { $0[Unidoc.AnyVertex[.linkable]] = true }
39+
$0 { $0[Unidoc.AnyVertex[.id]] { $0[.gte] = all.lowerBound } }
40+
$0 { $0[Unidoc.AnyVertex[.id]] { $0[.lte] = all.upperBound } }
41+
$0
42+
{
43+
$0[.or]
44+
{
45+
$0 { $0[Unidoc.AnyVertex[.id]] { $0[.lt] = latest.lowerBound } }
46+
$0 { $0[Unidoc.AnyVertex[.id]] { $0[.gt] = latest.upperBound } }
47+
}
48+
}
49+
}
50+
}
51+
$0[.u]
52+
{
53+
$0[.unset] { $0[Unidoc.AnyVertex[.linkable]] = true }
54+
}
55+
}
56+
}
57+
}

Sources/UnidocDB/Vertices/Unidoc.DB.Vertices.swift

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ extension Unidoc.DB
2323
}
2424
extension Unidoc.DB.Vertices
2525
{
26-
public static
27-
let indexStem:Mongo.CollectionIndex = .init("Stem",
26+
public
27+
static let indexStem:Mongo.CollectionIndex = .init("Stem",
2828
collation: .casefolding,
2929
unique: true)
3030
{
@@ -44,14 +44,33 @@ extension Unidoc.DB.Vertices
4444
$0[Unidoc.AnyVertex[.stem]] { $0[.exists] = true }
4545
}
4646

47-
public static
48-
let indexHash:Mongo.CollectionIndex = .init("Hash",
47+
public
48+
static let indexHash:Mongo.CollectionIndex = .init("Hash",
4949
collation: .casefolding,
5050
unique: true)
5151
{
5252
$0[Unidoc.AnyVertex[.hash]] = (+)
5353
$0[Unidoc.AnyVertex[.id]] = (+)
5454
}
55+
56+
public
57+
static let indexLinkableFlag:Mongo.CollectionIndex = .init("LinkableFlag",
58+
unique: true)
59+
{
60+
$0[Unidoc.AnyVertex[.linkable]] = (+)
61+
$0[Unidoc.AnyVertex[.id]] = (+)
62+
}
63+
64+
public
65+
static let indexLinkableStem:Mongo.CollectionIndex = .init("LinkableStem",
66+
collation: .casefolding)
67+
{
68+
$0[Unidoc.AnyVertex[.stem]] = (+)
69+
}
70+
where:
71+
{
72+
$0[Unidoc.AnyVertex[.linkable]] = true
73+
}
5574
}
5675
extension Unidoc.DB.Vertices:Mongo.CollectionModel
5776
{
@@ -64,13 +83,17 @@ extension Unidoc.DB.Vertices:Mongo.CollectionModel
6483
@inlinable public static
6584
var indexes:[Mongo.CollectionIndex] { [ Self.indexStem, Self.indexHash ] }
6685
}
86+
@available(*, unavailable, message: """
87+
Vertices contain flags set by the database, which would be lost if they were decoded and \
88+
re-encoded.
89+
""")
6790
extension Unidoc.DB.Vertices:Mongo.RecodableModel
6891
{
6992
}
7093
extension Unidoc.DB.Vertices
7194
{
7295
@discardableResult
73-
func insert(_ vertices:Unidoc.Mesh.Vertices) async throws -> Mongo.Insertions
96+
func insert(_ vertices:Unidoc.Mesh.Vertices, latest:Bool) async throws -> Mongo.Insertions
7497
{
7598
let response:Mongo.InsertResponse = try await session.run(
7699
command: Mongo.Insert.init(Self.name,
@@ -80,7 +103,16 @@ extension Unidoc.DB.Vertices
80103
}
81104
documents:
82105
{
83-
$0 += vertices.articles.lazy.map(Unidoc.AnyVertex.article(_:))
106+
for article:Unidoc.ArticleVertex in vertices.articles
107+
{
108+
$0[Unidoc.AnyVertex.CodingKey.self]
109+
{
110+
Unidoc.AnyVertex.article(article).encode(to: &$0)
111+
112+
$0[.linkable] = latest ? latest : nil
113+
}
114+
}
115+
84116
$0 += vertices.cultures.lazy.map(Unidoc.AnyVertex.culture(_:))
85117
$0 += vertices.decls.lazy.map(Unidoc.AnyVertex.decl(_:))
86118
$0 += vertices.files.lazy.map(Unidoc.AnyVertex.file(_:))

Sources/UnidocDB/Volumes/Unidoc.DB.Volumes.AlignLatest.swift

Lines changed: 0 additions & 77 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import MongoDB
2+
import MongoQL
3+
import Unidoc
4+
import UnidocRecords
5+
6+
extension Unidoc.DB.Volumes
7+
{
8+
struct VacuumLatest
9+
{
10+
let latest:Unidoc.Edition
11+
12+
init(around latest:Unidoc.Edition)
13+
{
14+
self.latest = latest
15+
}
16+
}
17+
}
18+
extension Unidoc.DB.Volumes.VacuumLatest:Mongo.UpdateQuery
19+
{
20+
typealias Target = Unidoc.DB.Volumes
21+
typealias Effect = Mongo.Many
22+
23+
var ordered:Bool { false }
24+
25+
func build(updates:inout Mongo.UpdateListEncoder<Mongo.Many>)
26+
{
27+
updates
28+
{
29+
$0[.multi] = true
30+
$0[.hint] = Unidoc.DB.Volumes.indexLatestFlag.id
31+
$0[.q]
32+
{
33+
$0[.and]
34+
{
35+
let cell:ClosedRange<Unidoc.Edition> = .package(self.latest.package)
36+
37+
$0 { $0[Unidoc.VolumeMetadata[.latest]] = true }
38+
$0 { $0[Unidoc.VolumeMetadata[.id]] { $0[.gte] = cell.lowerBound } }
39+
$0 { $0[Unidoc.VolumeMetadata[.id]] { $0[.lte] = cell.upperBound } }
40+
$0 { $0[Unidoc.VolumeMetadata[.id]] { $0[.ne] = self.latest } }
41+
}
42+
}
43+
$0[.u]
44+
{
45+
$0[.unset] { $0[Unidoc.VolumeMetadata[.latest]] = true }
46+
}
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)