Skip to content

Commit

Permalink
use @_exported import RocksDB
Browse files Browse the repository at this point in the history
  • Loading branch information
RomeroYang committed Jul 22, 2024
1 parent e527ae5 commit d823636
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 127 deletions.
93 changes: 1 addition & 92 deletions Database/Sources/Database/Database.swift
Original file line number Diff line number Diff line change
@@ -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<T: RocksDBValueInitializable>(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<Key: RocksDBValueInitializable, Value: RocksDBValueInitializable>(
keyType _: Key.Type,
valueType _: Value.Type,
gte: String? = nil
) throws -> RocksDBSequence<Key, Value> {
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<Key: RocksDBValueInitializable, Value: RocksDBValueInitializable>(
keyType _: Key.Type,
valueType _: Value.Type,
lte: String
) throws -> RocksDBSequence<Key, Value> {
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<String>]) 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
31 changes: 12 additions & 19 deletions Database/Tests/DatabaseTests/DatabaseTests.swift
Original file line number Diff line number Diff line change
@@ -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: "😂")
Expand All @@ -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")
Expand Down Expand Up @@ -63,46 +63,46 @@ 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
}
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
}
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
}
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
}
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
Expand All @@ -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 😂")
Expand All @@ -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),
]
}
9 changes: 0 additions & 9 deletions Database/Tests/DatabaseTests/XCTestManifests.swift

This file was deleted.

7 changes: 0 additions & 7 deletions Database/Tests/LinuxMain.swift

This file was deleted.

0 comments on commit d823636

Please sign in to comment.