Skip to content

Commit

Permalink
Merge pull request #74 from kirilltitov/async-await
Browse files Browse the repository at this point in the history
Adopt native async/await 🎉
  • Loading branch information
kirilltitov authored Sep 30, 2021
2 parents 66661b2 + 1a47f26 commit d13cda2
Show file tree
Hide file tree
Showing 29 changed files with 889 additions and 2,349 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.1
5.5
17 changes: 13 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
// swift-tools-version:5.0
// swift-tools-version:5.5

import PackageDescription

let package = Package(
name: "FDBSwift",
platforms: [.macOS(.v10_15)],
products: [
.library(name: "FDB", targets: ["FDB"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.0.0")),
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.0.0")),
],
targets: [
.systemLibrary(name: "CFDB", pkgConfig: "libfdb"),
.target(name: "FDB", dependencies: ["CFDB", "NIO", "Logging"]),
.testTarget(name: "FDBTests", dependencies: ["FDB"]),
.target(
name: "FDB",
dependencies: [
"CFDB",
.product(name: "Logging", package: "swift-log"),
]
),
.testTarget(
name: "FDBTests",
dependencies: ["FDB"]
),
]
)
380 changes: 217 additions & 163 deletions README.md

Large diffs are not rendered by default.

138 changes: 27 additions & 111 deletions Sources/FDB/AnyFDB.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Logging
import NIO

public protocol AnyFDB {
static var logger: Logger { get set }
Expand All @@ -17,125 +16,64 @@ public protocol AnyFDB {
/// Performs an explicit disconnect routine
func disconnect()

/// Begins a new FDB transaction without an event loop
/// Begins a new FDB transaction
func begin() throws -> AnyFDBTransaction

/// Begins a new FDB transaction with given event loop
///
/// - parameters:
/// - eventLoop: Swift-NIO EventLoop to run future computations
/// - returns: `EventLoopFuture` with a transaction instance as future value.
func begin(on eventLoop: EventLoop) -> EventLoopFuture<AnyFDBTransaction>

/// Executes given transactional closure with appropriate retry logic
///
/// Retry logic kicks in if `notCommitted` (1020) error was thrown during commit event. You must commit
/// the transaction yourself. Additionally, this transactional closure should be idempotent in order to exclude
/// unexpected behaviour.
func withTransaction<T>(
on eventLoop: EventLoop,
_ block: @escaping (AnyFDBTransaction) throws -> EventLoopFuture<T>
) -> EventLoopFuture<T>

/// Executes given transactional closure with appropriate retry logic
///
/// This function will block current thread during execution
///
/// Retry logic kicks in if `notCommitted` (1020) error was thrown during commit event. You must commit
/// the transaction yourself. Additionally, this transactional closure should be idempotent in order to exclude
/// unexpected behaviour.
func withTransaction<T>(_ block: @escaping (AnyFDBTransaction) throws -> T) throws -> T
func withTransaction<T>(_ block: @escaping (AnyFDBTransaction) async throws -> T) async throws -> T

/// Sets bytes to given key in FDB cluster
///
/// This function will block current thread during execution
///
/// - parameters:
/// - key: FDB key
/// - value: bytes value
func set(key: AnyFDBKey, value: Bytes) throws

/// Sets bytes to given key in FDB cluster
///
/// This function will block current thread during execution
///
/// - parameters:
/// - key: FDB key
/// - value: bytes value
func setSync(key: AnyFDBKey, value: Bytes) throws

/// Clears given key in FDB cluster
///
/// This function will block current thread during execution
///
/// - parameters:
/// - key: FDB key
func clear(key: AnyFDBKey) throws
func set(key: AnyFDBKey, value: Bytes) async throws

/// Clears given key in FDB cluster
///
/// This function will block current thread during execution
///
/// - parameters:
/// - key: FDB key
func clearSync(key: AnyFDBKey) throws
func clear(key: AnyFDBKey) async throws

/// Clears keys in given range in FDB cluster
///
/// This function will block current thread during execution
///
/// - parameters:
/// - begin: Begin key
/// - end: End key
func clear(begin: AnyFDBKey, end: AnyFDBKey) throws

/// Clears keys in given range in FDB cluster
///
/// This function will block current thread during execution
///
/// - parameters:
/// - begin: Begin key
/// - end: End key
func clearSync(begin: AnyFDBKey, end: AnyFDBKey) throws
func clear(begin: AnyFDBKey, end: AnyFDBKey) async throws

/// Clears keys in given range in FDB cluster
///
/// This function will block current thread during execution
///
/// - parameters:
/// - range: Range key
func clear(range: FDB.RangeKey) throws
func clear(range: FDB.RangeKey) async throws

/// Clears keys in given subspace in FDB cluster
///
/// This function will block current thread during execution
///
/// - parameters:
/// - subspace: Subspace to clear
func clear(subspace: FDB.Subspace) throws
func clear(subspace: FDB.Subspace) async throws

/// Returns bytes value for given key (or `nil` if no key)
///
/// This function will block current thread during execution
///
/// - parameters:
/// - key: FDB key
/// - snapshot: Snapshot read (i.e. whether this read create a conflict range or not)
func get(key: AnyFDBKey, snapshot: Bool) throws -> Bytes?
func get(key: AnyFDBKey, snapshot: Bool) async throws -> Bytes?

/// Returns a range of keys and their respective values under given subspace
///
/// This function will block current thread during execution
///
/// - parameters:
/// - subspace: Subspace
/// - snapshot: Snapshot read (i.e. whether this read create a conflict range or not)
func get(subspace: FDB.Subspace, snapshot: Bool) throws -> FDB.KeyValuesResult
func get(subspace: FDB.Subspace, snapshot: Bool) async throws -> FDB.KeyValuesResult

/// Returns a range of keys and their respective values in given key range
///
/// This function will block current thread during execution
///
/// - parameters:
/// - begin: Begin key
/// - end: End key
Expand All @@ -162,12 +100,10 @@ public protocol AnyFDB {
iteration: Int32,
snapshot: Bool,
reverse: Bool
) throws -> FDB.KeyValuesResult
) async throws -> FDB.KeyValuesResult

/// Returns a range of keys and their respective values in given key range
///
/// This function will block current thread during execution
///
/// - parameters:
/// - range: Range key
/// - beginEqual: Should begin key also include exact key value
Expand All @@ -192,32 +128,26 @@ public protocol AnyFDB {
iteration: Int32,
snapshot: Bool,
reverse: Bool
) throws -> FDB.KeyValuesResult
) async throws -> FDB.KeyValuesResult

/// Peforms an atomic operation in FDB cluster on given key with given value bytes
///
/// This function will block current thread during execution
///
/// - parameters:
/// - op: Atomic operation
/// - key: FDB key
/// - value: Value bytes
func atomic(_ op: FDB.MutationType, key: AnyFDBKey, value: Bytes) throws
func atomic(_ op: FDB.MutationType, key: AnyFDBKey, value: Bytes) async throws

/// Peforms an atomic operation in FDB cluster on given key with given signed integer value
///
/// This function will block current thread during execution
///
/// - parameters:
/// - op: Atomic operation
/// - key: FDB key
/// - value: Integer
func atomic<T: SignedInteger>(_ op: FDB.MutationType, key: AnyFDBKey, value: T) throws
func atomic<T: SignedInteger>(_ op: FDB.MutationType, key: AnyFDBKey, value: T) async throws

/// Peforms a quasi-atomic increment operation in FDB cluster on given key with given integer
///
/// This function will block current thread during execution
///
/// Warning: though this function uses atomic `.add` increment, immediate serializable read of incremented key
/// negates all benefits of atomicity, and therefore it shouldn't be considered truly atomical. However, it still
/// works, and it gives you guarantees that only you will get the incremented value. It may lead to increased
Expand All @@ -227,24 +157,20 @@ public protocol AnyFDB {
/// - key: FDB key
/// - value: Integer
@discardableResult
func increment(key: AnyFDBKey, value: Int64) throws -> Int64
func increment(key: AnyFDBKey, value: Int64) async throws -> Int64

/// Peforms a quasi-atomic decrement operation in FDB cluster on given key with given integer
///
/// This function will block current thread during execution
///
/// - parameters:
/// - key: FDB key
/// - value: Integer
@discardableResult
func decrement(key: AnyFDBKey, value: Int64) throws -> Int64
func decrement(key: AnyFDBKey, value: Int64) async throws -> Int64
}

public extension AnyFDB {
/// Returns a range of keys and their respective values in given key range
///
/// This function will block current thread during execution
///
/// - parameters:
/// - begin: Begin key
/// - end: End key
Expand All @@ -271,8 +197,8 @@ public extension AnyFDB {
iteration: Int32 = 1,
snapshot: Bool = false,
reverse: Bool = false
) throws -> FDB.KeyValuesResult {
return try self.get(
) async throws -> FDB.KeyValuesResult {
try await self.get(
begin: begin,
end: end,
beginEqual: beginEqual,
Expand All @@ -290,8 +216,6 @@ public extension AnyFDB {

/// Returns a range of keys and their respective values in given key range
///
/// This function will block current thread during execution
///
/// - parameters:
/// - range: Range key
/// - beginEqual: Should begin key also include exact key value
Expand All @@ -316,8 +240,8 @@ public extension AnyFDB {
iteration: Int32 = 1,
snapshot: Bool = false,
reverse: Bool = false
) throws -> FDB.KeyValuesResult {
return try self.get(
) async throws -> FDB.KeyValuesResult {
try await self.get(
range: range,
beginEqual: beginEqual,
beginOffset: beginOffset,
Expand All @@ -334,30 +258,24 @@ public extension AnyFDB {

/// Returns bytes value for given key (or `nil` if no key)
///
/// This function will block current thread during execution
///
/// - parameters:
/// - key: FDB key
/// - snapshot: Snapshot read (i.e. whether this read create a conflict range or not)
func get(key: AnyFDBKey, snapshot: Bool = false) throws -> Bytes? {
return try self.get(key: key, snapshot: snapshot)
func get(key: AnyFDBKey, snapshot: Bool = false) async throws -> Bytes? {
return try await self.get(key: key, snapshot: snapshot)
}

/// Returns a range of keys and their respective values under given subspace
///
/// This function will block current thread during execution
///
/// - parameters:
/// - subspace: Subspace
/// - snapshot: Snapshot read (i.e. whether this read create a conflict range or not)
func get(subspace: FDB.Subspace, snapshot: Bool = false) throws -> FDB.KeyValuesResult {
return try self.get(subspace: subspace, snapshot: snapshot)
func get(subspace: FDB.Subspace, snapshot: Bool = false) async throws -> FDB.KeyValuesResult {
return try await self.get(subspace: subspace, snapshot: snapshot)
}

/// Peforms a quasi-atomic increment operation in FDB cluster on given key with given integer
///
/// This function will block current thread during execution
///
/// Warning: though this function uses atomic `.add` increment, immediate serializable read of incremented key
/// negates all benefits of atomicity, and therefore it shouldn't be considered truly atomical. However, it still
/// works, and it gives you guarantees that only you will get the incremented value. It may lead to increased
Expand All @@ -367,19 +285,17 @@ public extension AnyFDB {
/// - key: FDB key
/// - value: Integer
@discardableResult
func increment(key: AnyFDBKey, value: Int64 = 1) throws -> Int64 {
return try self.increment(key: key, value: value)
func increment(key: AnyFDBKey, value: Int64 = 1) async throws -> Int64 {
try await self.increment(key: key, value: value)
}

/// Peforms a quasi-atomic decrement operation in FDB cluster on given key with given integer
///
/// This function will block current thread during execution
///
/// - parameters:
/// - key: FDB key
/// - value: Integer
@discardableResult
func decrement(key: AnyFDBKey, value: Int64 = 1) throws -> Int64 {
return try self.decrement(key: key, value: value)
func decrement(key: AnyFDBKey, value: Int64 = 1) async throws -> Int64 {
try await self.decrement(key: key, value: value)
}
}
Loading

0 comments on commit d13cda2

Please sign in to comment.