Skip to content

Commit

Permalink
Cleanup, remove history management code
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Dec 25, 2023
1 parent 72623b0 commit 287a7ca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 70 deletions.
56 changes: 5 additions & 51 deletions Sources/HummingbirdFluent/Fluent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,6 @@ import ServiceLifecycle
/// You can either create this separate from `HBApplication` or add it to your application
/// using `HBApplication.addFluent`.
public struct HBFluent: @unchecked Sendable, Service {
/// Fluent history management
public class History {
/// Is history recording enabled
public private(set) var enabled: Bool
// History of queries to Fluent
public private(set) var history: QueryHistory?

init() {
self.enabled = false
self.history = nil
}

/// Start recording history
public func start() {
self.enabled = true
self.history = .init()
}

/// Stop recording history
public func stop() {
self.enabled = false
}

/// Clear history
public func clear() {
self.history = .init()
}
}

/// Databases attached
public let databases: Databases
/// List of migrations
Expand All @@ -58,8 +29,6 @@ public struct HBFluent: @unchecked Sendable, Service {
public let eventLoopGroup: EventLoopGroup
/// Logger
public let logger: Logger
/// Fluent history setup
public let history: History

/// Initialize HBFluent
/// - Parameters:
Expand All @@ -76,7 +45,6 @@ public struct HBFluent: @unchecked Sendable, Service {
self.migrations = .init()
self.eventLoopGroup = eventLoopGroup
self.logger = logger
self.history = .init()
}

public func run() async throws {
Expand Down Expand Up @@ -110,31 +78,17 @@ public struct HBFluent: @unchecked Sendable, Service {
///
/// - Parameters:
/// - id: ID of database
/// - eventLoop: Eventloop database connection is running on
/// - history: Query history storage
/// - pageSizeLimit: Set page size limit to avoid server overload
/// - Returns: Database connection
public func db(_ id: DatabaseID? = nil) -> Database {
public func db(_ id: DatabaseID? = nil, history: QueryHistory? = nil, pageSizeLimit: Int? = nil) -> Database {
self.databases
.database(
id,
logger: self.logger,
on: self.eventLoopGroup.any(),
history: self.history.enabled ? self.history.history : nil
)!
}

/// Return Database connection
///
/// - Parameters:
/// - id: ID of database
/// - eventLoop: Eventloop database connection is running on
/// - Returns: Database connection
public func db(_ id: DatabaseID? = nil, on eventLoop: EventLoop) -> Database {
self.databases
.database(
id,
logger: self.logger,
on: eventLoop,
history: self.history.enabled ? self.history.history : nil
history: history,
pageSizeLimit: pageSizeLimit
)!
}
}
25 changes: 6 additions & 19 deletions Sources/HummingbirdFluent/Persist+fluent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public final class HBFluentPersistDriver<C: Clock>: HBPersistDriver {
let fluent: HBFluent
let databaseID: DatabaseID?
let clock: C
var tidyTask: RepeatedTask?

/// Initialize HBFluentPersistDriver
/// - Parameters:
Expand All @@ -33,19 +32,12 @@ public final class HBFluentPersistDriver<C: Clock>: HBPersistDriver {
self.databaseID = databaseID
self.clock = clock
self.fluent.migrations.add(CreatePersistModel())
self.tidyTask = fluent.eventLoopGroup.next().scheduleRepeatedTask(initialDelay: .hours(1), delay: .hours(1)) { _ in
self.tidy()
}
}

/// shutdown driver, cancel tidy task
public func shutdown() {
self.tidyTask?.cancel()
self.tidy()
}

/// Create new key. This doesn't check for the existence of this key already so may fail if the key already exists
public func create<Object: Codable>(key: String, value: Object, expires: Duration?) async throws {
let db = self.database(on: self.fluent.eventLoopGroup.any())
let db = self.fluent.db(self.databaseID)
let data = try JSONEncoder().encode(value)
let date = expires.map { Date.now + Double($0.components.seconds) } ?? Date.distantFuture
let model = PersistModel(id: key, data: data, expires: date)
Expand All @@ -60,7 +52,7 @@ public final class HBFluentPersistDriver<C: Clock>: HBPersistDriver {

/// Set value for key.
public func set<Object: Codable>(key: String, value: Object, expires: Duration?) async throws {
let db = self.database(on: self.fluent.eventLoopGroup.any())
let db = self.fluent.db(self.databaseID)
let data = try JSONEncoder().encode(value)
let date = expires.map { Date.now + Double($0.components.seconds) } ?? Date.distantFuture
let model = PersistModel(id: key, data: data, expires: date)
Expand All @@ -86,7 +78,7 @@ public final class HBFluentPersistDriver<C: Clock>: HBPersistDriver {

/// Get value for key
public func get<Object: Codable>(key: String, as object: Object.Type) async throws -> Object? {
let db = self.database(on: self.fluent.eventLoopGroup.any())
let db = self.fluent.db(self.databaseID)
do {
let query = try await PersistModel.query(on: db)
.filter(\._$id == key)
Expand All @@ -99,23 +91,18 @@ public final class HBFluentPersistDriver<C: Clock>: HBPersistDriver {

/// Remove key
public func remove(key: String) async throws {
let db = self.database(on: self.fluent.eventLoopGroup.any())
let db = self.fluent.db(self.databaseID)
let model = try await PersistModel.find(key, on: db)
guard let model = model else { return }
return try await model.delete(force: true, on: db)
}

/// tidy up database by cleaning out expired keys
func tidy() {
_ = PersistModel.query(on: self.database(on: self.fluent.eventLoopGroup.next()))
_ = PersistModel.query(on: self.fluent.db(self.databaseID))
.filter(\.$expires < Date())
.delete()
}

/// Get database connection on event loop
func database(on eventLoop: EventLoop) -> Database {
self.fluent.db(self.databaseID, on: eventLoop)
}
}

/// Fluent model used to store persist data
Expand Down

0 comments on commit 287a7ca

Please sign in to comment.