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 Sources/FoundationDB/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public final class FDBDatabase: DatabaseProtocol {
/// - value: The value for the option (optional).
/// - option: The database option to set.
/// - Throws: `FDBError` if the option cannot be set.
public func setOption(to value: FDB.Value? = nil, forOption option: FDB.DatabaseOption) throws {
public func setOption(to value: FDB.Bytes? = nil, forOption option: FDB.DatabaseOption) throws {
let error: Int32
if let value = value {
error = value.withUnsafeBytes { bytes in
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 @@ -61,7 +61,7 @@ extension FDB {
///
/// This design minimizes the impact of network latency on iteration performance.
public struct AsyncKVSequence: AsyncSequence {
public typealias Element = KeyValue
public typealias Element = (Bytes, Bytes)

/// The transaction used for range queries
let transaction: TransactionProtocol
Expand Down Expand Up @@ -172,7 +172,7 @@ extension FDB {
///
/// - Returns: The next key-value pair, or `nil` if sequence is exhausted
/// - Throws: `FDBError` if the database operation fails
public mutating func next() async throws -> KeyValue? {
public mutating func next() async throws -> Element? {
if isExhausted {
return nil
}
Expand Down
35 changes: 18 additions & 17 deletions Sources/FoundationDB/FoundationdDB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,26 @@ protocol TransactionProtocol: Sendable {
/// - snapshot: Whether to perform a snapshot read.
/// - Returns: The value associated with the key, or nil if not found.
/// - Throws: `FDBError` if the operation fails.
func getValue(for key: FDB.Key, snapshot: Bool) async throws -> FDB.Value?
func getValue(for key: FDB.Bytes, snapshot: Bool) async throws -> FDB.Bytes?

/// Sets a value for the given key.
///
/// - Parameters:
/// - value: The value to set as a byte array.
/// - key: The key to associate with the value.
func setValue(_ value: FDB.Value, for key: FDB.Key)
func setValue(_ value: FDB.Bytes, for key: FDB.Bytes)

/// Removes a key-value pair from the database.
///
/// - Parameter key: The key to remove as a byte array.
func clear(key: FDB.Key)
func clear(key: FDB.Bytes)

/// Removes all key-value pairs in the given range.
///
/// - Parameters:
/// - beginKey: The start of the range (inclusive) as a byte array.
/// - endKey: The end of the range (exclusive) as a byte array.
func clearRange(beginKey: FDB.Key, endKey: FDB.Key)
func clearRange(beginKey: FDB.Bytes, endKey: FDB.Bytes)

/// Resolves a key selector to an actual key.
///
Expand All @@ -88,7 +88,7 @@ protocol TransactionProtocol: Sendable {
/// - snapshot: Whether to perform a snapshot read.
/// - Returns: The resolved key, or nil if no key matches.
/// - Throws: `FDBError` if the operation fails.
func getKey(selector: FDB.Selectable, snapshot: Bool) async throws -> FDB.Key?
func getKey(selector: FDB.Selectable, snapshot: Bool) async throws -> FDB.Bytes?

/// Resolves a key selector to an actual key.
///
Expand All @@ -97,7 +97,7 @@ protocol TransactionProtocol: Sendable {
/// - snapshot: Whether to perform a snapshot read.
/// - Returns: The resolved key, or nil if no key matches.
/// - Throws: `FDBError` if the operation fails.
func getKey(selector: FDB.KeySelector, snapshot: Bool) async throws -> FDB.Key?
func getKey(selector: FDB.KeySelector, snapshot: Bool) async throws -> FDB.Bytes?

/// Returns an AsyncSequence that yields key-value pairs within a range.
///
Expand Down Expand Up @@ -139,7 +139,7 @@ protocol TransactionProtocol: Sendable {
/// - Returns: A `ResultRange` containing the key-value pairs and more flag.
/// - Throws: `FDBError` if the operation fails.
func getRangeNative(
beginKey: FDB.Key, endKey: FDB.Key, limit: Int, snapshot: Bool
beginKey: FDB.Bytes, endKey: FDB.Bytes, limit: Int, snapshot: Bool
) async throws -> ResultRange

/// Commits the transaction.
Expand All @@ -159,7 +159,7 @@ protocol TransactionProtocol: Sendable {
///
/// - Returns: The transaction's versionstamp as a key, or nil if not available.
/// - Throws: `FDBError` if the operation fails.
func getVersionstamp() async throws -> FDB.Key?
func getVersionstamp() async throws -> FDB.Bytes?

/// Sets the read version for snapshot reads.
///
Expand Down Expand Up @@ -191,7 +191,7 @@ protocol TransactionProtocol: Sendable {
/// - endKey: The end of the range (exclusive).
/// - Returns: The estimated size in bytes.
/// - Throws: `FDBError` if the operation fails.
func getEstimatedRangeSizeBytes(beginKey: FDB.Key, endKey: FDB.Key) async throws -> Int
func getEstimatedRangeSizeBytes(beginKey: FDB.Bytes, endKey: FDB.Bytes) async throws -> Int

/// Returns a list of keys that can split the given range into roughly equal chunks.
///
Expand All @@ -203,7 +203,7 @@ protocol TransactionProtocol: Sendable {
/// - chunkSize: The desired size of each chunk in bytes.
/// - Returns: An array of keys representing split points.
/// - Throws: `FDBError` if the operation fails.
func getRangeSplitPoints(beginKey: FDB.Key, endKey: FDB.Key, chunkSize: Int) async throws -> [[UInt8]]
func getRangeSplitPoints(beginKey: FDB.Bytes, endKey: FDB.Bytes, chunkSize: Int) async throws -> [[UInt8]]

/// Returns the version number at which a committed transaction modified the database.
///
Expand All @@ -228,7 +228,7 @@ protocol TransactionProtocol: Sendable {
/// - key: The key to operate on.
/// - param: The parameter for the atomic operation.
/// - mutationType: The type of atomic operation to perform.
func atomicOp(key: FDB.Key, param: FDB.Value, mutationType: FDB.MutationType)
func atomicOp(key: FDB.Bytes, param: FDB.Bytes, mutationType: FDB.MutationType)

/// Adds a conflict range to the transaction.
///
Expand All @@ -240,7 +240,7 @@ protocol TransactionProtocol: Sendable {
/// - endKey: The end of the range (exclusive) as a byte array.
/// - type: The type of conflict range (read or write).
/// - Throws: `FDBError` if the operation fails.
func addConflictRange(beginKey: FDB.Key, endKey: FDB.Key, type: FDB.ConflictRangeType) throws
func addConflictRange(beginKey: FDB.Bytes, endKey: FDB.Bytes, type: FDB.ConflictRangeType) throws

// MARK: - Transaction option methods

Expand All @@ -250,7 +250,7 @@ protocol TransactionProtocol: Sendable {
/// - value: Optional byte array value for the option.
/// - option: The transaction option to set.
/// - Throws: `FDBError` if the option cannot be set.
func setOption(to value: FDB.Value?, forOption option: FDB.TransactionOption) throws
func setOption(to value: FDB.Bytes?, forOption option: FDB.TransactionOption) throws

/// Sets a transaction option with a string value.
///
Expand Down Expand Up @@ -313,15 +313,15 @@ extension DatabaseProtocol {
}

extension TransactionProtocol {
public func getValue(for key: FDB.Key, snapshot: Bool = false) async throws -> FDB.Value? {
public func getValue(for key: FDB.Bytes, snapshot: Bool = false) async throws -> FDB.Bytes? {
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.Bytes? {
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.Bytes? {
try await getKey(selector: selector, snapshot: snapshot)
}

Expand Down Expand Up @@ -354,8 +354,9 @@ extension TransactionProtocol {
)
}


public func getRange(
beginKey: FDB.Key, endKey: FDB.Key, snapshot: Bool = false
beginKey: FDB.Bytes, endKey: FDB.Bytes, snapshot: Bool = false
) -> FDB.AsyncKVSequence {
let beginSelector = FDB.KeySelector.firstGreaterOrEqual(beginKey)
let endSelector = FDB.KeySelector.firstGreaterOrEqual(endKey)
Expand Down
5 changes: 3 additions & 2 deletions Sources/FoundationDB/Future.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ struct ResultInt64: FutureResult {
/// Used for operations like key selectors that resolve to actual keys.
struct ResultKey: FutureResult {
/// The extracted key, or nil if no key was returned.
let value: FDB.Key?
let value: FDB.Bytes?

/// Extracts a key from the future.
///
Expand Down Expand Up @@ -220,7 +220,7 @@ struct ResultKey: FutureResult {
/// Used for get operations that retrieve values associated with keys.
struct ResultValue: FutureResult {
/// The extracted value, or nil if no value was found.
let value: FDB.Value?
let value: FDB.Bytes?

/// Extracts a value from the future.
///
Expand Down Expand Up @@ -253,6 +253,7 @@ struct ResultValue: FutureResult {
public struct ResultRange: FutureResult {
/// The array of key-value pairs returned by the range operation.
public let records: FDB.KeyValueArray

/// Indicates whether there are more records beyond this result.
public let more: Bool

Expand Down
25 changes: 13 additions & 12 deletions Sources/FoundationDB/Transaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public final class FDBTransaction: TransactionProtocol, @unchecked Sendable {
fdb_transaction_destroy(transaction)
}

public func getValue(for key: FDB.Key, snapshot: Bool) async throws -> FDB.Value? {
public func getValue(for key: FDB.Bytes, snapshot: Bool) async throws -> FDB.Bytes? {
try await key.withUnsafeBytes { keyBytes in
Future<ResultValue>(
fdb_transaction_get(
Expand All @@ -43,7 +43,7 @@ public final class FDBTransaction: TransactionProtocol, @unchecked Sendable {
}.getAsync()?.value
}

public func setValue(_ value: FDB.Value, for key: FDB.Key) {
public func setValue(_ value: FDB.Bytes, for key: FDB.Bytes) {
key.withUnsafeBytes { keyBytes in
value.withUnsafeBytes { valueBytes in
fdb_transaction_set(
Expand All @@ -57,7 +57,7 @@ public final class FDBTransaction: TransactionProtocol, @unchecked Sendable {
}
}

public func clear(key: FDB.Key) {
public func clear(key: FDB.Bytes) {
key.withUnsafeBytes { keyBytes in
fdb_transaction_clear(
transaction,
Expand All @@ -67,7 +67,7 @@ public final class FDBTransaction: TransactionProtocol, @unchecked Sendable {
}
}

public func clearRange(beginKey: FDB.Key, endKey: FDB.Key) {
public func clearRange(beginKey: FDB.Bytes, endKey: FDB.Bytes) {
beginKey.withUnsafeBytes { beginKeyBytes in
endKey.withUnsafeBytes { endKeyBytes in
fdb_transaction_clear_range(
Expand All @@ -81,7 +81,7 @@ public final class FDBTransaction: TransactionProtocol, @unchecked Sendable {
}
}

public func atomicOp(key: FDB.Key, param: FDB.Value, mutationType: FDB.MutationType) {
public func atomicOp(key: FDB.Bytes, param: FDB.Bytes, mutationType: FDB.MutationType) {
key.withUnsafeBytes { keyBytes in
param.withUnsafeBytes { paramBytes in
fdb_transaction_atomic_op(
Expand All @@ -96,7 +96,7 @@ public final class FDBTransaction: TransactionProtocol, @unchecked Sendable {
}
}

public func setOption(to value: FDB.Value?, forOption option: FDB.TransactionOption) throws {
public func setOption(to value: FDB.Bytes?, forOption option: FDB.TransactionOption) throws {
let error: Int32
if let value = value {
error = value.withUnsafeBytes { bytes in
Expand All @@ -116,7 +116,7 @@ public final class FDBTransaction: TransactionProtocol, @unchecked Sendable {
}
}

public func getKey(selector: FDB.KeySelector, snapshot: Bool) async throws -> FDB.Key? {
public func getKey(selector: FDB.KeySelector, snapshot: Bool) async throws -> FDB.Bytes? {
try await selector.key.withUnsafeBytes { keyBytes in
Future<ResultKey>(
fdb_transaction_get_key(
Expand All @@ -141,7 +141,7 @@ public final class FDBTransaction: TransactionProtocol, @unchecked Sendable {
fdb_transaction_cancel(transaction)
}

public func getVersionstamp() async throws -> FDB.Key? {
public func getVersionstamp() async throws -> FDB.Bytes? {
try await Future<ResultKey>(
fdb_transaction_get_versionstamp(transaction)
).getAsync()?.value
Expand All @@ -163,7 +163,7 @@ public final class FDBTransaction: TransactionProtocol, @unchecked Sendable {
).getAsync()
}

public func getEstimatedRangeSizeBytes(beginKey: FDB.Key, endKey: FDB.Key) async throws -> Int {
public func getEstimatedRangeSizeBytes(beginKey: FDB.Bytes, endKey: FDB.Bytes) async throws -> Int {
try Int(await beginKey.withUnsafeBytes { beginKeyBytes in
endKey.withUnsafeBytes { endKeyBytes in
Future<ResultInt64>(
Expand All @@ -179,7 +179,7 @@ public final class FDBTransaction: TransactionProtocol, @unchecked Sendable {
}.getAsync()?.value ?? 0)
}

public func getRangeSplitPoints(beginKey: FDB.Key, endKey: FDB.Key, chunkSize: Int) async throws -> [[UInt8]] {
public func getRangeSplitPoints(beginKey: FDB.Bytes, endKey: FDB.Bytes, chunkSize: Int) async throws -> [[UInt8]] {
try await beginKey.withUnsafeBytes { beginKeyBytes in
endKey.withUnsafeBytes { endKeyBytes in
Future<ResultKeyArray>(
Expand Down Expand Up @@ -211,7 +211,7 @@ public final class FDBTransaction: TransactionProtocol, @unchecked Sendable {
).getAsync()?.value ?? 0)
}

public func addConflictRange(beginKey: FDB.Key, endKey: FDB.Key, type: FDB.ConflictRangeType) throws {
public func addConflictRange(beginKey: FDB.Bytes, endKey: FDB.Bytes, type: FDB.ConflictRangeType) throws {
let error = beginKey.withUnsafeBytes { beginKeyBytes in
endKey.withUnsafeBytes { endKeyBytes in
fdb_transaction_add_conflict_range(
Expand Down Expand Up @@ -261,8 +261,9 @@ public final class FDBTransaction: TransactionProtocol, @unchecked Sendable {
return try await future.getAsync() ?? ResultRange(records: [], more: false)
}


func getRangeNative(
beginKey: FDB.Key, endKey: FDB.Key, limit: Int = 0, snapshot: Bool
beginKey: FDB.Bytes, endKey: FDB.Bytes, limit: Int = 0, snapshot: Bool
) async throws -> ResultRange {
let future = beginKey.withUnsafeBytes { beginKeyBytes in
endKey.withUnsafeBytes { endKeyBytes in
Expand Down
24 changes: 10 additions & 14 deletions Sources/FoundationDB/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,12 @@ typealias CCallback = @convention(c) (UnsafeRawPointer?, UnsafeRawPointer?) -> V
public enum FDB {
/// A FoundationDB version number (64-bit integer).
public typealias Version = Int64

/// Raw byte data used throughout the FoundationDB API.
public typealias Bytes = [UInt8]
/// A FoundationDB key (sequence of bytes).
public typealias Key = Bytes
/// A FoundationDB value (sequence of bytes).
public typealias Value = Bytes
/// A key-value pair tuple.
public typealias KeyValue = (Key, Value)

/// An array of key-value pairs.
public typealias KeyValueArray = [KeyValue]
public typealias KeyValueArray = [(Bytes, Bytes)]

/// Protocol for types that can be converted to key selectors.
///
Expand Down Expand Up @@ -70,7 +66,7 @@ public enum FDB {
/// ```
public struct KeySelector: Selectable, Sendable {
/// The reference key for this selector.
public let key: Key
public let key: Bytes
/// Whether to include the reference key itself in selection.
public let orEqual: Bool
/// Offset from the selected key position.
Expand All @@ -82,7 +78,7 @@ public enum FDB {
/// - key: The reference key.
/// - orEqual: Whether to include the reference key itself.
/// - offset: Offset from the selected position.
public init(key: Key, orEqual: Bool, offset: Int) {
public init(key: Bytes, orEqual: Bool, offset: Int) {
self.key = key
self.orEqual = orEqual
self.offset = offset
Expand All @@ -101,31 +97,31 @@ public enum FDB {
///
/// - Parameter key: The reference key as a byte array.
/// - Returns: A key selector that selects the first key >= the reference key.
public static func firstGreaterOrEqual(_ key: Key) -> KeySelector {
public static func firstGreaterOrEqual(_ key: FDB.Bytes) -> KeySelector {
return KeySelector(key: key, orEqual: false, offset: 1)
}

/// Creates a key selector for the first key greater than the given key.
///
/// - Parameter key: The reference key as a byte array.
/// - Returns: A key selector that selects the first key > the reference key.
public static func firstGreaterThan(_ key: Key) -> KeySelector {
public static func firstGreaterThan(_ key: FDB.Bytes) -> KeySelector {
return KeySelector(key: key, orEqual: true, offset: 1)
}

/// Creates a key selector for the last key less than or equal to the given key.
///
/// - Parameter key: The reference key as a byte array.
/// - Returns: A key selector that selects the last key <= the reference key.
public static func lastLessOrEqual(_ key: Key) -> KeySelector {
public static func lastLessOrEqual(_ key: FDB.Bytes) -> KeySelector {
return KeySelector(key: key, orEqual: true, offset: 0)
}

/// Creates a key selector for the last key less than the given key.
///
/// - Parameter key: The reference key as a byte array.
/// - Returns: A key selector that selects the last key < the reference key.
public static func lastLessThan(_ key: Key) -> KeySelector {
public static func lastLessThan(_ key: FDB.Bytes) -> KeySelector {
return KeySelector(key: key, orEqual: false, offset: 0)
}
}
Expand All @@ -135,7 +131,7 @@ public enum FDB {
///
/// This allows key byte arrays to be used directly in range operations
/// by converting them to "first greater or equal" key selectors.
extension FDB.Key: FDB.Selectable {
extension FDB.Bytes: FDB.Selectable {
/// Converts this key to a key selector using "first greater or equal" semantics.
///
/// - Returns: A key selector that selects the first key >= this key.
Expand Down
Loading