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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
.DS_Store
/.build
.build
/Packages
/Package.resolved
/*.xcodeproj
.vscode
fdb.cluster
*.pyc
.claude
73 changes: 73 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
## TODO: Find SDK directory instead of looking at hardcoded directory.
## TODO: -enable-testing only on debug builds.

if(NOT TARGET fdb_c)
message(FATAL_ERROR "Swift bindings require C bindings to be built first. Make sure fdb_c target exists.")
endif()

if(CMAKE_Swift_COMPILER_VERSION VERSION_LESS "6.0")
message(FATAL_ERROR "Swift 6.0 or later is required for Swift Testing support")
endif()

set(FDB_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/bindings/c)
set(SWIFT_SDK_DIR "/usr/lib/swift/linux/")
set(SWIFT_SDK_STATIC_DIR "/usr/libexec/swift/6.0.3/lib/swift_static/linux")

file(GLOB_RECURSE SWIFT_BINDING_SOURCES "Sources/**/*.swift")
add_library(FoundationDB-Swift ${SWIFT_BINDING_SOURCES})

add_dependencies(FoundationDB-Swift fdb_c)

target_include_directories(FoundationDB-Swift PUBLIC
${FDB_INCLUDE_DIR}
${CMAKE_BINARY_DIR}/bindings/c/foundationdb
)

set(STATIC_SWIFT_LIBS
# ${SWIFT_SDK_STATIC_DIR}/libswiftCore.a
# ${SWIFT_SDK_STATIC_DIR}/libswiftDispatch.a
# ${SWIFT_SDK_STATIC_DIR}/libswiftSynchronization.a
# ${SWIFT_SDK_STATIC_DIR}/libswift_Concurrency.a
)

target_link_directories(FoundationDB-Swift PUBLIC
"${SWIFT_SDK_DIR}"
)

target_link_libraries(FoundationDB-Swift
PUBLIC
fdb_c
${STATIC_SWIFT_LIBS}
)

target_compile_options(FoundationDB-Swift PUBLIC
"SHELL:-I ${FDB_INCLUDE_DIR}"
"SHELL:-I ${CMAKE_CURRENT_SOURCE_DIR}/Sources"
"SHELL:-I ${SWIFT_SDK_DIR}"
"SHELL:-enable-testing"
)

set_target_properties(FoundationDB-Swift PROPERTIES
Swift_MODULE_NAME "FoundationDB"
)

# Tests - Configure for Swift Testing
file(GLOB_RECURSE SWIFT_BINDING_TEST_SOURCES "Tests/**/*.swift")
add_executable(FoundationDB-Swift-Tests
${SWIFT_BINDING_TEST_SOURCES}
)

add_dependencies(FoundationDB-Swift-Tests FoundationDB-Swift fdb_c)
target_link_libraries(FoundationDB-Swift-Tests PRIVATE FoundationDB-Swift Testing)

set_target_properties(FoundationDB-Swift-Tests PROPERTIES
Swift_MODULE_NAME "FoundationDBTests"
)

install(TARGETS FoundationDB-Swift
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
)

# Configure test to run with Swift Testing
add_test(NAME FoundationDB-Swift-Tests COMMAND FoundationDB-Swift-Tests)
34 changes: 0 additions & 34 deletions Package.resolved

This file was deleted.

57 changes: 39 additions & 18 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
// swift-tools-version:5.2
/*
* Package.swift
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2016-2025 Apple Inc. and the FoundationDB project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// swift-tools-version: 6.0
import PackageDescription

let package = Package(
name: "FoundationDB",
products: [
.library(name: "FoundationDB", targets: ["FoundationDB"]),
.executable(name: "FoundationDBBindingTestRunner", targets: ["FoundationDBBindingTestRunner"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio", from: "1.2.0"),
.package(url: "https://github.com/FoundationDB/fdb-swift-c-packaging", .branch("main"))
],
targets: [
.target(name: "FoundationDB", dependencies: [.product(name: "NIO", package: "swift-nio"), "CFoundationDB"]),
.target(name: "CFoundationDB"),
.target(name: "FoundationDBBindingTest", dependencies: ["FoundationDB"]),
.target(name: "FoundationDBBindingTestRunner", dependencies: ["FoundationDBBindingTest"]),
.testTarget(name: "FoundationDBTests", dependencies: ["FoundationDB"]),
.testTarget(name: "FoundationDBBindingTestTests", dependencies: ["FoundationDBBindingTest"]),
]
name: "FoundationDB",
products: [
.library(name: "FoundationDB", targets: ["FoundationDB"]),
],
targets: [
.systemLibrary(
name: "CFoundationDB"
),
.target(
name: "FoundationDB",
dependencies: ["CFoundationDB"],
path: "Sources/FoundationDB"
),
.testTarget(
name: "FoundationDBTests",
dependencies: ["FoundationDB"]
),
]
)
1 change: 0 additions & 1 deletion README.md

This file was deleted.

108 changes: 108 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# FoundationDB Swift Bindings

Swift bindings for FoundationDB, providing a native Swift API for interacting with FoundationDB clusters.

## Features

- **Native Swift API** - Idiomatic Swift interfaces for FoundationDB operations
- **Async/Await Support** - Modern Swift concurrency with async sequences
- **High Performance** - Optimized range iteration with background pre-fetching
- **Type Safety** - Swift's type system for safer database operations

## Quick Start

### Initialize the Client

```swift
import FoundationDB

// Initialize FoundationDB
try await FdbClient.initialize()
let database = try FdbClient.openDatabase()
```

### Basic Operations

```swift
// Simple key-value operations
try await database.withTransaction { transaction in
// Set a value
transaction.setValue("world", for: "hello")

// Get a value
if let value = try await transaction.getValue(for: "hello") {
print(String(bytes: value)) // "world"
}

// Delete a key
transaction.clear(key: "hello")
}
```

### Range Queries

```swift
// Efficient streaming over large result sets
let sequence = transaction.readRange(
beginSelector: .firstGreaterOrEqual("user:"),
endSelector: .firstGreaterOrEqual("user;")
)

for try await (key, value) in sequence {
let userId = String(bytes: key)
let userData = String(bytes: value)
// Process each key-value pair as it streams
}
```

### Atomic Operations

```swift
try await database.withTransaction { transaction in
// Atomic increment
let counterKey = "counter"
let increment = withUnsafeBytes(of: Int64(1).littleEndian) { Array($0) }
transaction.atomicOp(key: counterKey, param: increment, mutationType: .add)
}
```

## Key Components

- **Transaction Management** - Automatic retry logic and conflict resolution
- **AsyncKVSequence** - Memory-efficient streaming iteration with background pre-fetching
- **Key Selectors** - Flexible key positioning for range queries
- **Atomic Operations** - Built-in atomic mutations (ADD, AND, OR, etc.)
- **Network Options** - Configurable client behavior and performance tuning

## Performance

The bindings include several performance optimizations:

- **Background Pre-fetching** - Range queries pre-fetch next batch while processing current data
- **Streaming Results** - Large result sets don't require full buffering in memory
- **Connection Pooling** - Efficient connection management to FoundationDB clusters
- **Configurable Batching** - Tunable batch sizes for optimal throughput

## Requirements

- Swift 5.7+
- FoundationDB 7.1+
- macOS 12+ / Linux

## Installation

Add the package to your `Package.swift`:

```swift
dependencies: [
.package(url: "https://github.com/apple/fdb-swift-bindings", from: "1.0.0")
]
```

## Documentation

For detailed API documentation and advanced usage patterns, see the inline documentation in the source files.

## License

Licensed under the Apache License, Version 2.0. See LICENSE for details.
29 changes: 0 additions & 29 deletions Resources/docker/Dockerfile

This file was deleted.

24 changes: 0 additions & 24 deletions Resources/docker/entrypoint.bash

This file was deleted.

1 change: 0 additions & 1 deletion Resources/fdb_sample.cluster

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
* CFoundationDB.c
* fdb_c_wrapper.h
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2016-2018 Apple Inc. and the FoundationDB project authors
* Copyright 2016-2025 Apple Inc. and the FoundationDB project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,3 +18,10 @@
* limitations under the License.
*/

#ifndef FDB_C_WRAPPER_H
#define FDB_C_WRAPPER_H

#define FDB_API_VERSION 740
#include <foundationdb/fdb_c.h>

#endif
Loading