Skip to content

Commit

Permalink
refactor linker actions into async functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tayloraswift committed Sep 16, 2024
1 parent d581634 commit 15dd5d0
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 120 deletions.
43 changes: 0 additions & 43 deletions Sources/UnidocDB/Snapshots/Unidoc.DB.Snapshots.ClearAction.swift

This file was deleted.

69 changes: 0 additions & 69 deletions Sources/UnidocDB/Snapshots/Unidoc.DB.Snapshots.QueueAction.swift

This file was deleted.

101 changes: 101 additions & 0 deletions Sources/UnidocDB/Snapshots/Unidoc.DB.Snapshots.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,107 @@ extension Unidoc.DB.Snapshots
}
}

extension Unidoc.DB.Snapshots
{
@discardableResult
public
func queueAll(for action:Unidoc.LinkerAction) async throws -> Int
{
try await self.updateMany
{
$0
{
$0[.multi] = true
$0[.q]
{
$0[Unidoc.Snapshot[.action]] { $0[.exists] = false }
}
$0[.u]
{
$0[.set]
{
$0[Unidoc.Snapshot[.action]] = action
}
}
}
}
}

@discardableResult
public
func queue(id:Unidoc.Edition, for action:Unidoc.LinkerAction) async throws -> Bool?
{
try await self.update
{
$0
{
$0[.q]
{
$0[Unidoc.Snapshot[.action]] { $0[.exists] = false }
$0[Unidoc.Snapshot[.id]] = id
}
$0[.u]
{
$0[.set]
{
$0[Unidoc.Snapshot[.action]] = action
}
}
}
}
}

/// Clears the **queued action** for a single snapshot. Does not delete the snapshot!
@discardableResult
public
func clear(id:Unidoc.Edition) async throws -> Bool?
{
try await self.update
{
$0
{
$0[.q] { $0[Unidoc.Snapshot[.id]] = id }
$0[.u]
{
$0[.unset]
{
$0[Unidoc.Snapshot[.action]] = ()
}
}
}
}
}

@discardableResult
public
func mark(id:Unidoc.Edition, vintage:Bool) async throws -> Bool?
{
try await self.update
{
$0
{
$0[.q] { $0[Unidoc.Snapshot[.id]] = id }
$0[.u]
{
if vintage
{
$0[.set]
{
$0[Unidoc.Snapshot[.vintage]] = true
}
}
else
{
$0[.unset]
{
$0[Unidoc.Snapshot[.vintage]] = ()
}
}
}
}
}
}
}
extension Unidoc.DB.Snapshots
{
/// Returns a single batch of symbol graphs that are queued for linking.
Expand Down
3 changes: 1 addition & 2 deletions Sources/UnidocLinkerPlugin/Unidoc.GraphLinker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ extension Unidoc.GraphLinker
}
}

try await context.db.update(
with: Unidoc.DB.Snapshots.ClearAction.one(operation.edition))
try await context.db.snapshots.clear(id: operation.edition)

if let event:Event
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ extension Unidoc
/// Queues one or more editions for uplinking. The uplinking process itself is asynchronous.
struct LinkerOperation:Sendable
{
let queue:DB.Snapshots.QueueAction
let action:LinkerAction
let scope:Edition?
let back:String?

init(queue:DB.Snapshots.QueueAction, back:String? = nil)
init(action:Unidoc.LinkerAction, scope:Unidoc.Edition?, back:String? = nil)
{
self.queue = queue
self.action = action
self.scope = scope
self.back = back
}
}
Expand All @@ -24,7 +26,7 @@ extension Unidoc.LinkerOperation
{
init(action:Unidoc.LinkerAction, form:Unidoc.LinkerForm)
{
self.init(queue: .one(form.edition, action: action), back: form.back)
self.init(action: action, scope: form.edition, back: form.back)
}
}
extension Unidoc.LinkerOperation:Unidoc.AdministrativeOperation
Expand All @@ -33,7 +35,15 @@ extension Unidoc.LinkerOperation:Unidoc.AdministrativeOperation
db:Unidoc.DB,
as _:Unidoc.RenderFormat) async throws -> HTTP.ServerResponse?
{
try await db.update(with: self.queue)
if let scope:Unidoc.Edition = self.scope
{
try await db.snapshots.queue(id: scope, for: self.action)
}
else
{
try await db.snapshots.queueAll(for: self.action)
}

return .redirect(.seeOther(self.back ?? "/admin"))
}
}
2 changes: 1 addition & 1 deletion Sources/UnidocServer/Requests/Unidoc.Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ extension Unidoc.Router
}

case .uplinkAll:
return .unordered(Unidoc.LinkerOperation.init(queue: .all))
return .unordered(Unidoc.LinkerOperation.init(action: .uplinkRefresh, scope: nil))

case .userConfig:
if let account:Unidoc.Account = self.authorization.account,
Expand Down

0 comments on commit 15dd5d0

Please sign in to comment.