A Swift wrapper for the CDB (Constant Database) implementation from howerj/cdb, providing a native Swift interface for creating and querying constant key-value databases.
Add this package to your Swift project dependencies:
// In your Package.swift
dependencies: [
.package(url: "https://github.com/aisk/swift-cdb.git", from: "0.1.0"),
]import CDB
// Open a CDB file and read values
do {
let db = try CDB(filename: "example.cdb", mode: .read)
// Read string value
let value = try db.string(forKey: "some_key")
print("Value: \(value ?? "not found")")
try db.close()
} catch {
print("Error: \(error)")
}init(filename: String, mode: CDB.AccessMode) throws- Open a CDB fileinit(fileURL: URL, mode: CDB.AccessMode) throws- Open a CDB file URLadd(key: String, value: String) throws- Add a string valueadd(key: String, value: Data) throws- Add binary dataadd(key: Data, value: String) throws- Add a string value with a binary keyadd(key: Data, value: Data) throws- Add a binary key and valuestring(forKey: String, at index: UInt64 = 0) throws -> String?- Get a string valuestring(forKey: Data, at index: UInt64 = 0) throws -> String?- Get a string value using a binary keydata(forKey: String, at index: UInt64 = 0) throws -> Data?- Get binary datadata(forKey: Data, at index: UInt64 = 0) throws -> Data?- Get binary data using a binary keycount(key: String) throws -> UInt64- Count values for a keycount(key: Data) throws -> UInt64- Count values for a binary keyforEach(_:) throws- Visit all keys and values as stringsforEachData(_:) throws- Visit all keys and values without decodingclose() throws- Close the databasewithDatabase(filename:mode:_:) throws- Use a database within a managed scopewithDatabase(fileURL:mode:_:) throws- Use a file URL within a managed scopesubscript(key: String) throws -> String?- Dictionary-like access
CDB.AccessMode.read- Open for reading onlyCDB.AccessMode.write- Open for writing (creates new database)
A CDB instance is not thread-safe. Serialize all operations on the same
instance, including close(). Separate CDB instances may be used
concurrently.
Do not call close() from inside a forEach callback.
String APIs require valid UTF-8 and throw CDBError.invalidUTF8 when stored
bytes cannot be decoded. Use data(forKey:) or forEachData(_:) for arbitrary
binary content.
This project is licensed under the same terms as the original CDB library.