From d823636c8f5e40dafee05ba228ce878519f6888b Mon Sep 17 00:00:00 2001 From: shawn Date: Mon, 22 Jul 2024 23:28:44 +0800 Subject: [PATCH] use @_exported import RocksDB --- Database/Sources/Database/Database.swift | 93 +------------------ .../Tests/DatabaseTests/DatabaseTests.swift | 31 +++---- .../Tests/DatabaseTests/XCTestManifests.swift | 9 -- Database/Tests/LinuxMain.swift | 7 -- 4 files changed, 13 insertions(+), 127 deletions(-) delete mode 100644 Database/Tests/DatabaseTests/XCTestManifests.swift delete mode 100644 Database/Tests/LinuxMain.swift diff --git a/Database/Sources/Database/Database.swift b/Database/Sources/Database/Database.swift index 6f4147b2..07b11d65 100644 --- a/Database/Sources/Database/Database.swift +++ b/Database/Sources/Database/Database.swift @@ -1,92 +1 @@ -// The Swift Programming Language -// https://docs.swift.org/swift-book -import Foundation -import RocksDB - -public class Database { - public let path: URL - public let prefix: String? - private var db: RocksDB? - - private static let errorDomain = "DatabaseErrorDomain" - - public init(path: URL, prefix: String? = nil) throws { - self.path = path - self.prefix = prefix - - do { - db = try RocksDB(path: path, prefix: prefix) - } catch { - throw NSError(domain: Database.errorDomain, code: 2, userInfo: [NSLocalizedDescriptionKey: "Failed to init RocksDB: \(error)"]) - } - } - - public func put(key: String, value: some RocksDBValueRepresentable) throws { - guard let db else { - throw NSError(domain: Database.errorDomain, code: 1, userInfo: [NSLocalizedDescriptionKey: "Database is not opened"]) - } - do { - try db.put(key: key, value: value) - } catch { - throw error - } - } - - public func get(type: T.Type, key: String) throws -> T? { - guard let db else { - throw NSError(domain: Database.errorDomain, code: 1, userInfo: [NSLocalizedDescriptionKey: "Database is not opened"]) - } - do { - return try type.init(data: db.get(key: key)) - } catch { - throw error - } - } - - public func delete(key: String) throws { - guard let db else { - throw NSError(domain: Database.errorDomain, code: 1, userInfo: [NSLocalizedDescriptionKey: "Database is not opened"]) - } - do { - try db.delete(key: key) - } catch { - throw error - } - } - - public func iterate( - keyType _: Key.Type, - valueType _: Value.Type, - gte: String? = nil - ) throws -> RocksDBSequence { - guard let db else { - throw NSError(domain: Database.errorDomain, code: 1, userInfo: [NSLocalizedDescriptionKey: "Database is not opened"]) - } - - return db.sequence(gte: gte) - } - - public func iterate( - keyType _: Key.Type, - valueType _: Value.Type, - lte: String - ) throws -> RocksDBSequence { - guard let db else { - throw NSError(domain: Database.errorDomain, code: 1, userInfo: [NSLocalizedDescriptionKey: "Database is not opened"]) - } - - return db.sequence(lte: lte) - } - - public func batch(operations: [RocksDBBatchOperation]) throws { - guard let db else { - throw NSError(domain: Database.errorDomain, code: 1, userInfo: [NSLocalizedDescriptionKey: "Database is not opened"]) - } - - do { - try db.batch(operations: operations) - } catch { - throw error - } - } -} +@_exported import RocksDB diff --git a/Database/Tests/DatabaseTests/DatabaseTests.swift b/Database/Tests/DatabaseTests/DatabaseTests.swift index 48015087..63516da6 100644 --- a/Database/Tests/DatabaseTests/DatabaseTests.swift +++ b/Database/Tests/DatabaseTests/DatabaseTests.swift @@ -1,14 +1,14 @@ -@testable import Database +@testable import RocksDB import XCTest -final class DatabaseTests: XCTestCase, @unchecked Sendable { - var database: Database! +final class DatabaseTests: XCTestCase { + var database: RocksDB! func testSimplePut() { let path = URL(fileURLWithPath: "/tmp/\(UUID().uuidString)") do { - database = try Database(path: path) + database = try RocksDB(path: path) try database.put(key: "testText", value: "lolamkhaha") try database.put(key: "testEmoji", value: "😂") @@ -35,7 +35,7 @@ final class DatabaseTests: XCTestCase, @unchecked Sendable { let path = URL(fileURLWithPath: "/tmp/\(UUID().uuidString)") do { - database = try Database(path: path) + database = try RocksDB(path: path) try database.put(key: "testDeleteKey", value: "this is a simple value 😘") try database.delete(key: "testDeleteKey") @@ -63,14 +63,14 @@ final class DatabaseTests: XCTestCase, @unchecked Sendable { do { let path = URL(fileURLWithPath: "/tmp/\(UUID().uuidString)") - database = try Database(path: path) + database = try RocksDB(path: path) for (k, v) in orderedKeysAndValues { try database.put(key: k, value: v) } var i = 0 - for (key, val) in try database.iterate(keyType: String.self, valueType: String.self) { + for (key, val) in database.sequence(keyType: String.self, valueType: String.self) { XCTAssertEqual(key, orderedKeysAndValues[i].key) XCTAssertEqual(val, orderedKeysAndValues[i].value) i += 1 @@ -78,7 +78,7 @@ final class DatabaseTests: XCTestCase, @unchecked Sendable { XCTAssertEqual(i, 4) i = 1 - for (key, val) in try database.iterate(keyType: String.self, valueType: String.self, gte: "testMultipleEmoji") { + for (key, val) in database.sequence(keyType: String.self, valueType: String.self, gte: "testMultipleEmoji") { XCTAssertEqual(key, orderedKeysAndValues[i].key) XCTAssertEqual(val, orderedKeysAndValues[i].value) i += 1 @@ -86,7 +86,7 @@ final class DatabaseTests: XCTestCase, @unchecked Sendable { XCTAssertEqual(i, 4) i = 2 - for (key, val) in try database.iterate(keyType: String.self, valueType: String.self, gte: "testText") { + for (key, val) in database.sequence(keyType: String.self, valueType: String.self, gte: "testText") { XCTAssertEqual(key, orderedKeysAndValues[i].key) XCTAssertEqual(val, orderedKeysAndValues[i].value) i += 1 @@ -94,7 +94,7 @@ final class DatabaseTests: XCTestCase, @unchecked Sendable { XCTAssertEqual(i, 4) i = 3 - for (key, val) in try database.iterate(keyType: String.self, valueType: String.self, lte: "testTextEmoji") { + for (key, val) in database.sequence(keyType: String.self, valueType: String.self, lte: "testTextEmoji") { XCTAssertEqual(key, orderedKeysAndValues[i].key) XCTAssertEqual(val, orderedKeysAndValues[i].value) i -= 1 @@ -102,7 +102,7 @@ final class DatabaseTests: XCTestCase, @unchecked Sendable { XCTAssertEqual(i, -1) i = 2 - for (key, val) in try database.iterate(keyType: String.self, valueType: String.self, lte: "testText") { + for (key, val) in database.sequence(keyType: String.self, valueType: String.self, lte: "testText") { XCTAssertEqual(key, orderedKeysAndValues[i].key) XCTAssertEqual(val, orderedKeysAndValues[i].value) i -= 1 @@ -123,7 +123,7 @@ final class DatabaseTests: XCTestCase, @unchecked Sendable { func testBatchOperations() { do { let prefixedPath = "/tmp/\(UUID().uuidString)" - let prefixedDB = try Database(path: URL(fileURLWithPath: prefixedPath), prefix: "correctprefix") + let prefixedDB = try RocksDB(path: URL(fileURLWithPath: prefixedPath), prefix: "correctprefix") try prefixedDB.put(key: "testText", value: "lolamkhaha") try prefixedDB.put(key: "testEmoji", value: "😂") try prefixedDB.put(key: "testTextEmoji", value: "emojitext 😂") @@ -148,11 +148,4 @@ final class DatabaseTests: XCTestCase, @unchecked Sendable { XCTFail("An error occurred during batch test: \(error)") } } - - static let allTests = [ - ("testSimplePut", testSimplePut), - ("testSimpleDelete", testSimpleDelete), - ("testSimpleIterator", testSimpleIterator), - ("testBatchOperations", testBatchOperations), - ] } diff --git a/Database/Tests/DatabaseTests/XCTestManifests.swift b/Database/Tests/DatabaseTests/XCTestManifests.swift deleted file mode 100644 index 54655d89..00000000 --- a/Database/Tests/DatabaseTests/XCTestManifests.swift +++ /dev/null @@ -1,9 +0,0 @@ -import XCTest - -#if !canImport(ObjectiveC) - public func allTests() -> [XCTestCaseEntry] { - [ - testCase(DatabaseTests.allTests), - ] - } -#endif diff --git a/Database/Tests/LinuxMain.swift b/Database/Tests/LinuxMain.swift deleted file mode 100644 index 80ca1c70..00000000 --- a/Database/Tests/LinuxMain.swift +++ /dev/null @@ -1,7 +0,0 @@ -import XCTest - -import DatabaseTests - -var tests = [XCTestCaseEntry]() -tests += DatabaseTests.allTests() -XCTMain(tests)