|
| 1 | +// |
| 2 | +// Created by yechentide on 2025/11/12 |
| 3 | +// |
| 4 | + |
| 5 | +import Foundation |
| 6 | +import LvDBWrapper |
| 7 | + |
| 8 | +/// Protocol abstraction for a key-value store used to persist Minecraft Bedrock world data. |
| 9 | +/// |
| 10 | +/// This protocol defines the interface for accessing world data through a LevelDB-like store. |
| 11 | +/// Implementations can provide real LevelDB access or mock stores for testing and SwiftUI Previews. |
| 12 | +/// |
| 13 | +/// ## Topics |
| 14 | +/// |
| 15 | +/// ### Database State |
| 16 | +/// - ``isClosed`` |
| 17 | +/// - ``close()`` |
| 18 | +/// |
| 19 | +/// ### Key Operations |
| 20 | +/// - ``containsKey(_:)`` |
| 21 | +/// - ``data(forKey:)`` |
| 22 | +/// - ``putData(_:forKey:)`` |
| 23 | +/// - ``removeValue(forKey:)`` |
| 24 | +/// |
| 25 | +/// ### Advanced Operations |
| 26 | +/// - ``makeIterator()`` |
| 27 | +/// - ``writeBatch(_:)`` |
| 28 | +/// - ``compactRange(from:to:)`` |
| 29 | +/// |
| 30 | +/// ## Usage |
| 31 | +/// |
| 32 | +/// The default implementation is `LvDB` from `LvDBWrapper`. To inject a custom store: |
| 33 | +/// |
| 34 | +/// ```swift |
| 35 | +/// let customStore: LevelKeyValueStore = MyCustomStore() |
| 36 | +/// let world = try MCWorld(from: worldURL, database: customStore) |
| 37 | +/// ``` |
| 38 | +/// |
| 39 | +/// This is especially useful for: |
| 40 | +/// - **Testing**: Inject in-memory stores without file I/O |
| 41 | +/// - **SwiftUI Previews**: Use mock data without real database files |
| 42 | +/// - **Remote storage**: Implement cloud-backed world storage |
| 43 | +public protocol LevelKeyValueStore: AnyObject { |
| 44 | + /// Returns `true` if the database has been closed. |
| 45 | + var isClosed: Bool { get } |
| 46 | + |
| 47 | + /// Closes the database and releases associated resources. |
| 48 | + /// |
| 49 | + /// After calling this method, all operations on this store will fail. |
| 50 | + /// All active iterators created by this store will also be destroyed. |
| 51 | + func close() |
| 52 | + |
| 53 | + /// Checks whether the specified key exists in the database. |
| 54 | + /// |
| 55 | + /// - Parameter key: The key to check for existence. |
| 56 | + /// - Returns: `true` if the key exists; otherwise, `false`. |
| 57 | + func containsKey(_ key: Data) -> Bool |
| 58 | + |
| 59 | + /// Retrieves the value associated with the specified key. |
| 60 | + /// |
| 61 | + /// - Parameter key: The key to retrieve the value for. |
| 62 | + /// - Returns: The data associated with the key. |
| 63 | + /// - Throws: An error if the operation fails or the key does not exist. |
| 64 | + func data(forKey key: Data) throws -> Data |
| 65 | + |
| 66 | + /// Stores a key-value pair in the database. |
| 67 | + /// |
| 68 | + /// If the key already exists, its value will be updated. |
| 69 | + /// |
| 70 | + /// - Parameters: |
| 71 | + /// - key: The key to store. |
| 72 | + /// - value: The data value to associate with the key. |
| 73 | + /// - Throws: An error if the operation fails. |
| 74 | + func putData(_ data: Data, forKey key: Data) throws |
| 75 | + |
| 76 | + /// Removes the key-value pair associated with the specified key. |
| 77 | + /// |
| 78 | + /// - Parameter key: The key to remove. |
| 79 | + /// - Throws: An error if the operation fails. |
| 80 | + func removeValue(forKey key: Data) throws |
| 81 | + |
| 82 | + /// Creates an iterator for traversing the database entries. |
| 83 | + /// |
| 84 | + /// - Returns: An `LvDBIterator` instance for iterating over the database. |
| 85 | + /// - Throws: An error if iterator creation fails. |
| 86 | + func makeIterator() throws -> LvDBIterator |
| 87 | + |
| 88 | + /// Applies a batch of write operations atomically. |
| 89 | + /// |
| 90 | + /// - Parameter batch: The write batch containing operations to apply. |
| 91 | + /// - Throws: An error if the batch write fails. |
| 92 | + func writeBatch(_ batch: LvDBWriteBatch) throws |
| 93 | + |
| 94 | + /// Compacts the database in the specified key range. |
| 95 | + /// |
| 96 | + /// This operation optimizes storage by removing deleted entries and reorganizing data. |
| 97 | + /// |
| 98 | + /// - Parameters: |
| 99 | + /// - begin: The beginning of the range to compact, or `nil` for the start of the database. |
| 100 | + /// - end: The end of the range to compact, or `nil` for the end of the database. |
| 101 | + /// - Throws: An error if the compaction fails. |
| 102 | + func compactRange(from begin: Data?, to end: Data?) throws |
| 103 | +} |
| 104 | + |
| 105 | +// MARK: - LvDB Conformance |
| 106 | + |
| 107 | +/// Extends `LvDB` to conform to `LevelKeyValueStore`. |
| 108 | +/// |
| 109 | +/// This extension allows `LvDB` from `LvDBWrapper` to be used as a `LevelKeyValueStore` |
| 110 | +/// without requiring any changes to the `LvDBWrapper` package itself. |
| 111 | +/// |
| 112 | +/// The Objective-C methods (`has:`, `get:error:`, `put::error:`, `remove:error:`, |
| 113 | +/// `newIterator:`, `write:error:`, and `compactRange:end:error:`) are bridged to Swift |
| 114 | +/// and called by the protocol methods. |
| 115 | +extension LvDB: LevelKeyValueStore { |
| 116 | + /// Checks whether the specified key exists in the database. |
| 117 | + public func containsKey(_ key: Data) -> Bool { |
| 118 | + // Call the Objective-C has: method (bridged to Swift as has(_:)) |
| 119 | + return self.has(key) |
| 120 | + } |
| 121 | + |
| 122 | + /// Retrieves the value for the specified key. |
| 123 | + public func data(forKey key: Data) throws -> Data { |
| 124 | + // Call the Objective-C get:error: method (bridged to Swift as get(_:)) |
| 125 | + return try self.get(key) |
| 126 | + } |
| 127 | + |
| 128 | + /// Stores a key-value pair in the database. |
| 129 | + public func putData(_ data: Data, forKey key: Data) throws { |
| 130 | + // Call the Objective-C put::error: method (bridged to Swift as put(_:_:)) |
| 131 | + try self.put(key, data) |
| 132 | + } |
| 133 | + |
| 134 | + /// Removes the key-value pair for the specified key. |
| 135 | + public func removeValue(forKey key: Data) throws { |
| 136 | + // Call the Objective-C remove:error: method (bridged to Swift as remove(_:)) |
| 137 | + try self.remove(key) |
| 138 | + } |
| 139 | + |
| 140 | + /// Creates an iterator for traversing the database. |
| 141 | + public func makeIterator() throws -> LvDBIterator { |
| 142 | + // Call the Objective-C newIterator: method (bridged to Swift as newIterator()) |
| 143 | + return try self.newIterator() |
| 144 | + } |
| 145 | + |
| 146 | + /// Applies a write batch atomically. |
| 147 | + public func writeBatch(_ batch: LvDBWriteBatch) throws { |
| 148 | + // Call the Objective-C write:error: method (bridged to Swift as write(_:)) |
| 149 | + try self.write(batch) |
| 150 | + } |
| 151 | + |
| 152 | + /// Compacts the database in the specified range. |
| 153 | + public func compactRange(from begin: Data?, to end: Data?) throws { |
| 154 | + // Call the Objective-C compactRange:end:error: method (bridged to Swift as compactRange(_:_:)) |
| 155 | + try self.compactRange(begin, end) |
| 156 | + } |
| 157 | +} |
0 commit comments