Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import PackageDescription
let package = Package(
name: "FoundationDB",
platforms: [
.macOS(.v14),
.macOS(.v15),
],
products: [
.library(name: "FoundationDB", targets: ["FoundationDB"]),
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ try await database.withTransaction { transaction in

```swift
// Efficient streaming over large result sets
let sequence = transaction.readRange(
let sequence = transaction.getRange(
beginSelector: .firstGreaterOrEqual("user:"),
endSelector: .firstGreaterOrEqual("user;")
)
Expand Down
2 changes: 1 addition & 1 deletion Sources/FoundationDB/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public final class FDBDatabase: DatabaseProtocol {
///
/// - Returns: A new transaction instance conforming to `TransactionProtocol`.
/// - Throws: `FDBError` if the transaction cannot be created.
public func createTransaction() throws -> any TransactionProtocol {
public func createTransaction() throws -> FDBTransaction {
var transaction: OpaquePointer?
let error = fdb_database_create_transaction(database, &transaction)
if error != 0 {
Expand Down
4 changes: 2 additions & 2 deletions Sources/FoundationDB/Fdb+AsyncKVSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension FDB {
/// ## Usage
///
/// ```swift
/// let sequence = transaction.readRange(
/// let sequence = transaction.getRange(
/// beginSelector: .firstGreaterOrEqual("user:"),
/// endSelector: .firstGreaterOrEqual("user;")
/// )
Expand Down Expand Up @@ -228,7 +228,7 @@ extension FDB {
private mutating func startBackgroundPreFetch() {
preFetchTask = Task {
[transaction, nextBeginSelector, endSelector, batchLimit, snapshot] in
return try await transaction.getRange(
return try await transaction.getRangeNative(
beginSelector: nextBeginSelector,
endSelector: endSelector,
limit: batchLimit,
Expand Down
79 changes: 29 additions & 50 deletions Sources/FoundationDB/FoundationdDB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
/// and transaction retry logic. Implementations handle the underlying database
/// connection and resource management.
/// Database interface for FoundationDB operations
public protocol DatabaseProtocol {
protocol DatabaseProtocol {
associatedtype Transaction: TransactionProtocol

/// Creates a new transaction for database operations.
///
/// - Returns: A new transaction instance conforming to `TransactionProtocol`.
/// - Throws: `FDBError` if the transaction cannot be created.
func createTransaction() throws -> any TransactionProtocol
func createTransaction() throws -> Transaction

/// Executes a transaction with automatic retry logic.
///
Expand All @@ -50,7 +52,7 @@ public protocol DatabaseProtocol {
/// a FoundationDB transaction, including reads, writes, atomic operations,
/// and transaction management.
/// Transaction interface for FoundationDB operations
public protocol TransactionProtocol: Sendable {
protocol TransactionProtocol: Sendable {
/// Retrieves a value for the given key.
///
/// - Parameters:
Expand Down Expand Up @@ -104,46 +106,39 @@ public protocol TransactionProtocol: Sendable {
/// - endSelector: The key selector for the end of the range.
/// - snapshot: Whether to perform a snapshot read.
/// - Returns: An async sequence that yields key-value pairs.
func readRange(
func getRange(
beginSelector: FDB.KeySelector, endSelector: FDB.KeySelector, snapshot: Bool
) -> FDB.AsyncKVSequence

/// Retrieves key-value pairs within a range using selectable endpoints.
///
/// - Parameters:
/// - begin: The start of the range (converted to key selector).
/// - end: The end of the range (converted to key selector).
/// - limit: Maximum number of key-value pairs to return (0 for no limit).
/// - snapshot: Whether to perform a snapshot read.
/// - Returns: A `ResultRange` containing the key-value pairs and more flag.
/// - Throws: `FDBError` if the operation fails.
func getRange(
begin: FDB.Selectable, end: FDB.Selectable, limit: Int, snapshot: Bool
) async throws -> ResultRange

/// Retrieves key-value pairs within a range using key selectors.
///
/// This method directly calls FDB's C `fdb_transaction_get_range()` API.
/// Prefer using `getRange()` instead, which returns an `AsyncSequence` for better ergonomics.
///
/// - Parameters:
/// - beginSelector: The key selector for the start of the range.
/// - endSelector: The key selector for the end of the range.
/// - limit: Maximum number of key-value pairs to return (0 for no limit).
/// - snapshot: Whether to perform a snapshot read.
/// - Returns: A `ResultRange` containing the key-value pairs and more flag.
/// - Throws: `FDBError` if the operation fails.
func getRange(
func getRangeNative(
beginSelector: FDB.KeySelector, endSelector: FDB.KeySelector, limit: Int, snapshot: Bool
) async throws -> ResultRange

/// Retrieves key-value pairs within a range using byte array keys.
///
/// This method directly calls FDB's C `fdb_transaction_get_range()` API.
/// Prefer using `getRange()` instead, which returns an `AsyncSequence` for better ergonomics.
///
/// - Parameters:
/// - beginKey: The start key of the range as a byte array.
/// - endKey: The end key of the range as a byte array.
/// - limit: Maximum number of key-value pairs to return (0 for no limit).
/// - snapshot: Whether to perform a snapshot read.
/// - Returns: A `ResultRange` containing the key-value pairs and more flag.
/// - Throws: `FDBError` if the operation fails.
func getRange(
func getRangeNative(
beginKey: FDB.Key, endKey: FDB.Key, limit: Int, snapshot: Bool
) async throws -> ResultRange

Expand Down Expand Up @@ -318,19 +313,19 @@ extension DatabaseProtocol {
}

extension TransactionProtocol {
public func getValue(for key: FDB.Key, snapshot: Bool = false) async throws -> FDB.Value? {
public func getValue(for key: FDB.Key, snapshot: Bool = false) async throws -> FDB.Value? {
try await getValue(for: key, snapshot: snapshot)
}

public func getKey(selector: FDB.Selectable, snapshot: Bool = false) async throws -> FDB.Key? {
public func getKey(selector: FDB.Selectable, snapshot: Bool = false) async throws -> FDB.Key? {
try await getKey(selector: selector.toKeySelector(), snapshot: snapshot)
}

public func getKey(selector: FDB.KeySelector, snapshot: Bool = false) async throws -> FDB.Key? {
public func getKey(selector: FDB.KeySelector, snapshot: Bool = false) async throws -> FDB.Key? {
try await getKey(selector: selector, snapshot: snapshot)
}

public func readRange(
public func getRange(
beginSelector: FDB.KeySelector, endSelector: FDB.KeySelector, snapshot: Bool = false
) -> FDB.AsyncKVSequence {
FDB.AsyncKVSequence(
Expand All @@ -341,69 +336,53 @@ extension TransactionProtocol {
)
}

public func readRange(
public func getRange(
beginSelector: FDB.KeySelector, endSelector: FDB.KeySelector
) -> FDB.AsyncKVSequence {
readRange(
getRange(
beginSelector: beginSelector, endSelector: endSelector, snapshot: false
)
}

public func readRange(
public func getRange(
begin: FDB.Selectable, end: FDB.Selectable, snapshot: Bool = false
) -> FDB.AsyncKVSequence {
let beginSelector = begin.toKeySelector()
let endSelector = end.toKeySelector()
return readRange(
return getRange(
beginSelector: beginSelector, endSelector: endSelector, snapshot: snapshot
)
}

public func readRange(
public func getRange(
beginKey: FDB.Key, endKey: FDB.Key, snapshot: Bool = false
) -> FDB.AsyncKVSequence {
let beginSelector = FDB.KeySelector.firstGreaterOrEqual(beginKey)
let endSelector = FDB.KeySelector.firstGreaterOrEqual(endKey)
return readRange(
return getRange(
beginSelector: beginSelector, endSelector: endSelector, snapshot: snapshot
)
}

public func getRange(
begin: FDB.Selectable, end: FDB.Selectable, limit: Int = 0, snapshot: Bool = false
) async throws -> ResultRange {
let beginSelector = begin.toKeySelector()
let endSelector = end.toKeySelector()
return try await getRange(
beginSelector: beginSelector, endSelector: endSelector, limit: limit, snapshot: snapshot
)
}

public func getRange(
func getRangeNative(
beginSelector: FDB.KeySelector, endSelector: FDB.KeySelector, limit: Int = 0,
snapshot: Bool = false
) async throws -> ResultRange {
try await getRange(
try await getRangeNative(
beginSelector: beginSelector, endSelector: endSelector, limit: limit, snapshot: snapshot
)
}

public func getRange(
beginKey: FDB.Key, endKey: FDB.Key, limit: Int = 0, snapshot: Bool = false
) async throws -> ResultRange {
try await getRange(beginKey: beginKey, endKey: endKey, limit: limit, snapshot: snapshot)
}

public func setOption(forOption option: FDB.TransactionOption) throws {
public func setOption(forOption option: FDB.TransactionOption) throws {
try setOption(to: nil, forOption: option)
}

public func setOption(to value: String, forOption option: FDB.TransactionOption) throws {
public func setOption(to value: String, forOption option: FDB.TransactionOption) throws {
let valueBytes = [UInt8](value.utf8)
try setOption(to: valueBytes, forOption: option)
}

public func setOption(to value: Int, forOption option: FDB.TransactionOption) throws {
public func setOption(to value: Int, forOption option: FDB.TransactionOption) throws {
let valueBytes = withUnsafeBytes(of: Int64(value)) { [UInt8]($0) }
try setOption(to: valueBytes, forOption: option)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/FoundationDB/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ final class FDBNetwork: Sendable {

/// Stops the FoundationDB network and waits for the network thread to complete.
deinit {
try networkThread.withLock { networkThread in
_ = networkThread.withLock { networkThread in
if networkThread == nil {
return networkThread
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/FoundationDB/Transaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public final class FDBTransaction: TransactionProtocol, @unchecked Sendable {
}

public func onError(_ error: FDBError) async throws {
try await Future<ResultVoid>(
_ = try await Future<ResultVoid>(
fdb_transaction_on_error(transaction, error.code)
).getAsync()
}
Expand Down Expand Up @@ -230,7 +230,7 @@ public final class FDBTransaction: TransactionProtocol, @unchecked Sendable {
}
}

public func getRange(
func getRangeNative(
beginSelector: FDB.KeySelector, endSelector: FDB.KeySelector, limit: Int = 0,
snapshot: Bool
) async throws -> ResultRange {
Expand Down Expand Up @@ -261,7 +261,7 @@ public final class FDBTransaction: TransactionProtocol, @unchecked Sendable {
return try await future.getAsync() ?? ResultRange(records: [], more: false)
}

public func getRange(
func getRangeNative(
beginKey: FDB.Key, endKey: FDB.Key, limit: Int = 0, snapshot: Bool
) async throws -> ResultRange {
let future = beginKey.withUnsafeBytes { beginKeyBytes in
Expand Down
Loading