diff --git a/Sources/FDB/AnyFDBKey.swift b/Sources/FDB/AnyFDBKey.swift index d1dfa8d..78d32e7 100644 --- a/Sources/FDB/AnyFDBKey.swift +++ b/Sources/FDB/AnyFDBKey.swift @@ -10,25 +10,25 @@ public protocol AnyFDBKey: FDBTuplePackable { } public extension AnyFDBKey { - func pack() -> Bytes { - return self.asFDBKey().pack() + func getPackedFDBTupleValue() -> Bytes { + self.asFDBKey().getPackedFDBTupleValue() } } extension String: AnyFDBKey { public func asFDBKey() -> Bytes { - return Bytes(self.utf8) + Bytes(self.utf8) } } extension StaticString: AnyFDBKey { public func asFDBKey() -> Bytes { - return self.utf8Start.getBytes(count: Int32(self.utf8CodeUnitCount)) + self.utf8Start.getBytes(count: Int32(self.utf8CodeUnitCount)) } } -extension Array: AnyFDBKey where Element == Byte { +extension Bytes: AnyFDBKey { public func asFDBKey() -> Bytes { - return self + self } } diff --git a/Sources/FDB/FDB/FDB+Errors.swift b/Sources/FDB/FDB/FDB+Errors.swift index 8958630..381c64e 100644 --- a/Sources/FDB/FDB/FDB+Errors.swift +++ b/Sources/FDB/FDB/FDB+Errors.swift @@ -299,7 +299,7 @@ public extension FDB { /// Returns FDB error description from error number public static func getErrorInfo(for errno: fdb_error_t) -> String { - return String(cString: fdb_get_error(errno)) + String(cString: fdb_get_error(errno)) } } } diff --git a/Sources/FDB/Future/Future+Bytes.swift b/Sources/FDB/Future/Future+Bytes.swift index 096904b..8b6c3ea 100644 --- a/Sources/FDB/Future/Future+Bytes.swift +++ b/Sources/FDB/Future/Future+Bytes.swift @@ -2,6 +2,7 @@ import CFDB extension FDB.Future { /// Blocks current thread until future is resolved + @inlinable internal func wait() throws -> Bytes? { try self.waitAndCheck().parseBytes() } @@ -9,6 +10,7 @@ extension FDB.Future { /// Parses value bytes result from current future /// /// Warning: this should be only called if future is in resolved state + @inlinable internal func parseBytes() throws -> Bytes? { var readValueFound: Int32 = 0 var readValue: UnsafePointer! @@ -26,6 +28,7 @@ extension FDB.Future { /// Parses key bytes result from current future /// /// Warning: this should be only called if future is in resolved state + @inlinable internal func parseKeyBytes() throws -> Bytes { var readKey: UnsafePointer! var readKeyLength: Int32 = 0 diff --git a/Sources/FDB/Future/Future+Int64.swift b/Sources/FDB/Future/Future+Int64.swift index db7313b..b17f359 100644 --- a/Sources/FDB/Future/Future+Int64.swift +++ b/Sources/FDB/Future/Future+Int64.swift @@ -4,9 +4,12 @@ extension FDB.Future { /// Returns Future's version /// /// Should be called only when future is resolved - func getVersion() throws -> Int64 { + @inlinable + internal func getVersion() throws -> Int64 { var version: Int64 = 0 + try fdb_future_get_int64(self.pointer, &version).orThrow() + return version } } diff --git a/Sources/FDB/Future/Future+KeyValue.swift b/Sources/FDB/Future/Future+KeyValue.swift index 6982095..7649569 100644 --- a/Sources/FDB/Future/Future+KeyValue.swift +++ b/Sources/FDB/Future/Future+KeyValue.swift @@ -4,6 +4,7 @@ extension FDB.Future { /// Parses key values result from current future /// /// Warning: this should be only called if future is in resolved state + @inlinable internal func parseKeyValues() throws -> FDB.KeyValuesResult { var outRawValues: UnsafePointer! var outCount: Int32 = 0 diff --git a/Sources/FDB/Helpers.swift b/Sources/FDB/Helpers.swift index e320dd2..3da9dc7 100644 --- a/Sources/FDB/Helpers.swift +++ b/Sources/FDB/Helpers.swift @@ -7,12 +7,14 @@ public typealias Bytes = [Byte] internal extension String { @usableFromInline var bytes: Bytes { - return Bytes(self.utf8) + Bytes(self.utf8) } @usableFromInline var safe: String { - return self.unicodeScalars.lazy + self + .unicodeScalars + .lazy .map { scalar in scalar == "\n" ? "\n" @@ -25,11 +27,11 @@ internal extension String { internal extension Bool { @usableFromInline var int: fdb_bool_t { - return self ? 1 : 0 + self ? 1 : 0 } } -internal extension Array where Element == Byte { +internal extension Bytes { @usableFromInline func cast() throws -> R { guard MemoryLayout.size == self.count else { @@ -47,31 +49,31 @@ internal extension Array where Element == Byte { @usableFromInline var length: Int32 { - return numericCast(self.count) + numericCast(self.count) } @usableFromInline var string: String { - return String(bytes: self, encoding: .ascii)! + String(bytes: self, encoding: .ascii)! } } /// Returns little-endian binary representation of arbitrary value @usableFromInline internal func getBytes(_ input: Input) -> Bytes { - return withUnsafeBytes(of: input) { Bytes($0) } + withUnsafeBytes(of: input) { Bytes($0) } } /// Returns big-endian IEEE binary representation of a floating point number @usableFromInline internal func getBytes(_ input: Float32) -> Bytes { - return getBytes(input.bitPattern.bigEndian) + getBytes(input.bitPattern.bigEndian) } /// Returns big-endian IEEE binary representation of a double number @usableFromInline internal func getBytes(_ input: Double) -> Bytes { - return getBytes(input.bitPattern.bigEndian) + getBytes(input.bitPattern.bigEndian) } // taken from Swift-NIO @@ -106,7 +108,7 @@ internal extension UnsafeRawPointer { // Boy this is unsafe :D @usableFromInline func getBytes(count: Int32) -> Bytes { - return self.assumingMemoryBound(to: Byte.self).getBytes(count: count) + self.assumingMemoryBound(to: Byte.self).getBytes(count: count) } } diff --git a/Sources/FDB/KeyValues.swift b/Sources/FDB/KeyValues.swift index 6ffbacf..e90554f 100644 --- a/Sources/FDB/KeyValues.swift +++ b/Sources/FDB/KeyValues.swift @@ -17,12 +17,12 @@ public extension FDB { extension FDB.KeyValue: Equatable { public static func == (lhs: FDB.KeyValue, rhs: FDB.KeyValue) -> Bool { - return lhs.key == rhs.key && lhs.value == rhs.value + lhs.key == rhs.key && lhs.value == rhs.value } } extension FDB.KeyValuesResult: Equatable { public static func == (lhs: FDB.KeyValuesResult, rhs: FDB.KeyValuesResult) -> Bool { - return lhs.records == rhs.records && lhs.hasMore == rhs.hasMore + lhs.records == rhs.records && lhs.hasMore == rhs.hasMore } } diff --git a/Sources/FDB/Subspace.swift b/Sources/FDB/Subspace.swift index aa67c37..c2568f9 100644 --- a/Sources/FDB/Subspace.swift +++ b/Sources/FDB/Subspace.swift @@ -10,7 +10,7 @@ public extension FDB { public let itemsCount: Int public var range: FDB.RangeKey { - return ( + ( begin: self.prefix + [0], end: self.prefix + [255] ) @@ -26,25 +26,25 @@ public extension FDB { } public init(_ tuple: Tuple, items: Int = 1) { - self.init(tuple.pack(), items: items) + self.init(tuple.getPackedFDBTupleValue(), items: items) } func subspace(_ input: [FDBTuplePackable]) -> Subspace { - return Subspace(self.prefix + Tuple(input).pack(), items: self.itemsCount + input.count) + Subspace(self.prefix + Tuple(input).getPackedFDBTupleValue(), items: self.itemsCount + input.count) } func subspace(_ input: FDBTuplePackable...) -> Subspace { - return self.subspace(input) + self.subspace(input) } public subscript(index: FDBTuplePackable...) -> Subspace { - return self.subspace(index) + self.subspace(index) } } } extension FDB.Subspace: AnyFDBKey { public func asFDBKey() -> Bytes { - return self.prefix + self.prefix } } diff --git a/Sources/FDB/Transaction/Transaction+Internal+Async.swift b/Sources/FDB/Transaction/Transaction+Internal+Async.swift index 9ae13b0..170fcb5 100644 --- a/Sources/FDB/Transaction/Transaction+Internal+Async.swift +++ b/Sources/FDB/Transaction/Transaction+Internal+Async.swift @@ -4,6 +4,7 @@ internal extension FDB.Transaction { /// Commits current transaction func commit() -> FDB.Future { self.log("Committing transaction") + return fdb_transaction_commit(self.pointer).asFuture(ref: self) } diff --git a/Sources/FDB/Tuple.swift b/Sources/FDB/Tuple.swift index 5cf6924..4ab2eae 100644 --- a/Sources/FDB/Tuple.swift +++ b/Sources/FDB/Tuple.swift @@ -1,38 +1,38 @@ /// A type-erased Tuple packable protocol /// /// You may adopt this protocol with any of your custom types. -/// You should only implement pack() method, not _pack(). +/// You should only implement `getPackedFDBTupleValue()` method, not `_getPackedFDBTupleValue()`. /// Obviously, in most cases you would like to treat your /// class/struct as binary string, this is why simply returning /// bytes of your internal value representation is incorrect, /// because noone would know that your returned byte array /// should actually be treated as a binary string. /// It must be wrapped with control characters first. -/// This is why you should additionally call .pack() from your +/// This is why you should additionally call .getPackedFDBTupleValue() from your /// resulting byte array (see Tuple+Array.swift). Otherwise packing /// will be incorrect and will fail with an error. -/// Example of custom pack() implementation: +/// Example of custom getPackedFDBTupleValue() implementation: /// ``` /// extension MyValue: FDBTuplePackable { -/// func pack() -> Bytes { +/// func getPackedFDBTupleValue() -> Bytes { /// self /// .getBytesSomehow() // your method returns [UInt8] -/// .pack() // this will wrap your bytes -/// // with tuple binary string magic :) +/// .getPackedFDBTupleValue() // this will wrap your bytes +/// // with tuple binary string magic :) /// } /// } /// ``` public protocol FDBTuplePackable { /// Returns self bytes representation wrapped with control bytes. - func pack() -> Bytes + func getPackedFDBTupleValue() -> Bytes - /// Internal method extending `pack` method with more complicated logic, you ought not implement it. - func _pack() -> Bytes + /// Internal method extending `getPackedFDBTupleValue` method with more complicated logic, you ought not implement it. + func _getPackedFDBTupleValue() -> Bytes } public extension FDBTuplePackable { - func _pack() -> Bytes { - return self.pack() + func _getPackedFDBTupleValue() -> Bytes { + self.getPackedFDBTupleValue() } } @@ -72,22 +72,22 @@ public extension FDB { self.init(input) } - public func pack() -> Bytes { + public func getPackedFDBTupleValue() -> Bytes { var result = Bytes() self.tuple.forEach { - result.append(contentsOf: $0._pack()) + result.append(contentsOf: $0._getPackedFDBTupleValue()) } return result } - public func _pack() -> Bytes { + public func _getPackedFDBTupleValue() -> Bytes { var result = Bytes() result.append(Prefix.NESTED_TUPLE) self.tuple.forEach { if $0 is Null { result.append(contentsOf: Tuple.NULL_ESCAPE_SEQUENCE) } else { - result.append(contentsOf: $0._pack()) + result.append(contentsOf: $0._getPackedFDBTupleValue()) } } result.append(Tuple.NULL) @@ -97,25 +97,25 @@ public extension FDB { /// Represents `NULL` Tuple value. struct Null: FDBTuplePackable { - public func pack() -> Bytes { - return [Tuple.NULL] + public func getPackedFDBTupleValue() -> Bytes { + [Tuple.NULL] } } } extension FDB.Tuple: AnyFDBKey { public func asFDBKey() -> Bytes { - return self.pack() + self.getPackedFDBTupleValue() } } // I DON'T LIKE IT SO MUCH extension FDB.Tuple: Hashable { public static func == (lhs: FDB.Tuple, rhs: FDB.Tuple) -> Bool { - return lhs.pack() == rhs.pack() + lhs.getPackedFDBTupleValue() == rhs.getPackedFDBTupleValue() } public func hash(into hasher: inout Hasher) { - hasher.combine(self.pack()) + hasher.combine(self.getPackedFDBTupleValue()) } } diff --git a/Sources/FDB/Tuple/Tuple+Array.swift b/Sources/FDB/Tuple/Tuple+Array.swift index 3d806f2..c9abe53 100644 --- a/Sources/FDB/Tuple/Tuple+Array.swift +++ b/Sources/FDB/Tuple/Tuple+Array.swift @@ -1,6 +1,8 @@ /// Packs input bytes as BYTE STRING tuple value with null bytes escaping preprocessing +@usableFromInline internal func packBytes(_ bytes: Bytes) -> Bytes { var result = Bytes() + result.append(FDB.Tuple.Prefix.BYTE_STRING) bytes.forEach { if $0 == FDB.Tuple.NULL { @@ -10,11 +12,12 @@ internal func packBytes(_ bytes: Bytes) -> Bytes { } } result.append(FDB.Tuple.NULL) + return result } -extension Array: FDBTuplePackable where Element == Byte { - public func pack() -> Bytes { - return packBytes(self) +extension Bytes: FDBTuplePackable { + public func getPackedFDBTupleValue() -> Bytes { + packBytes(self) } } diff --git a/Sources/FDB/Tuple/Tuple+Bool.swift b/Sources/FDB/Tuple/Tuple+Bool.swift index 5f26e4b..7986ed6 100644 --- a/Sources/FDB/Tuple/Tuple+Bool.swift +++ b/Sources/FDB/Tuple/Tuple+Bool.swift @@ -1,6 +1,6 @@ extension Bool: FDBTuplePackable { - public func pack() -> Bytes { - return self + public func getPackedFDBTupleValue() -> Bytes { + self ? [FDB.Tuple.Prefix.BOOL_TRUE] : [FDB.Tuple.Prefix.BOOL_FALSE] } diff --git a/Sources/FDB/Tuple/Tuple+FloatingPoint.swift b/Sources/FDB/Tuple/Tuple+FloatingPoint.swift index 3f31a98..6bb3a40 100644 --- a/Sources/FDB/Tuple/Tuple+FloatingPoint.swift +++ b/Sources/FDB/Tuple/Tuple+FloatingPoint.swift @@ -13,20 +13,24 @@ internal func transformFloatingPoint(bytes: inout Bytes, start: Int, encode: Boo } } +@inlinable +internal func getGenericFloatFDBTupleValue(input: Bytes, prefix: Byte) -> Bytes { + var result = Bytes([prefix]) + + result.append(contentsOf: input) + transformFloatingPoint(bytes: &result, start: 1, encode: true) + + return result +} + extension Float32: FDBTuplePackable { - public func pack() -> Bytes { - var result = Bytes([FDB.Tuple.Prefix.FLOAT]) - result.append(contentsOf: getBytes(self)) - transformFloatingPoint(bytes: &result, start: 1, encode: true) - return result + public func getPackedFDBTupleValue() -> Bytes { + getGenericFloatFDBTupleValue(input: getBytes(self), prefix: FDB.Tuple.Prefix.FLOAT) } } extension Double: FDBTuplePackable { - public func pack() -> Bytes { - var result = Bytes([FDB.Tuple.Prefix.DOUBLE]) - result.append(contentsOf: getBytes(self)) - transformFloatingPoint(bytes: &result, start: 1, encode: true) - return result + public func getPackedFDBTupleValue() -> Bytes { + getGenericFloatFDBTupleValue(input: getBytes(self), prefix: FDB.Tuple.Prefix.DOUBLE) } } diff --git a/Sources/FDB/Tuple/Tuple+Int.swift b/Sources/FDB/Tuple/Tuple+Int.swift index 3d7af73..3c10b88 100644 --- a/Sources/FDB/Tuple/Tuple+Int.swift +++ b/Sources/FDB/Tuple/Tuple+Int.swift @@ -2,29 +2,34 @@ extension Collection { internal subscript(from i: Int) -> SubSequence { let _from = self.index(self.endIndex, offsetBy: i) let _to = self.endIndex + return self[_from ..< _to] } } internal func bisect(list: [Int], item: Int) -> Int { var count = 0 + for i in list { if i >= item { break } count += 1 } + return count } internal let sizeLimits = Array(0 ... 7).map { (1 << ($0 * 8)) - 1 } extension Int: FDBTuplePackable { - public func pack() -> Bytes { + public func getPackedFDBTupleValue() -> Bytes { if self == 0 { return [FDB.Tuple.Prefix.INT_ZERO_CODE] } + var result = Bytes() + if self > 0 { let n = bisect(list: sizeLimits, item: self) result.append(FDB.Tuple.Prefix.INT_ZERO_CODE + UInt8(n)) @@ -36,6 +41,7 @@ extension Int: FDBTuplePackable { let bytes = getBytes((maxv + self).bigEndian) result.append(contentsOf: bytes[from: -n]) } + return result } } diff --git a/Sources/FDB/Tuple/Tuple+String.swift b/Sources/FDB/Tuple/Tuple+String.swift index c3b63b6..b6c7cb8 100644 --- a/Sources/FDB/Tuple/Tuple+String.swift +++ b/Sources/FDB/Tuple/Tuple+String.swift @@ -1,9 +1,9 @@ extension String: FDBTuplePackable { - public func pack() -> Bytes { - let bytes = Bytes(self.utf8) + public func getPackedFDBTupleValue() -> Bytes { var result = Bytes() + result.append(FDB.Tuple.Prefix.UTF_STRING) - bytes.forEach { + Bytes(self.utf8).forEach { if $0 == FDB.Tuple.NULL { result.append(contentsOf: FDB.Tuple.NULL_ESCAPE_SEQUENCE) } else { @@ -11,6 +11,7 @@ extension String: FDBTuplePackable { } } result.append(FDB.Tuple.NULL) + return result } } diff --git a/Sources/FDB/Tuple/Tuple+UUID.swift b/Sources/FDB/Tuple/Tuple+UUID.swift index b11078c..25515c5 100644 --- a/Sources/FDB/Tuple/Tuple+UUID.swift +++ b/Sources/FDB/Tuple/Tuple+UUID.swift @@ -1,9 +1,11 @@ import Foundation extension UUID: FDBTuplePackable { - public func pack() -> Bytes { + public func getPackedFDBTupleValue() -> Bytes { var result: Bytes = [FDB.Tuple.Prefix.UUID] + result.append(contentsOf: getBytes(self.uuid)) + return result } } diff --git a/Sources/FDB/Tuple/Tuple+Unpack.swift b/Sources/FDB/Tuple/Tuple+Unpack.swift index 554f6da..9e3a2b7 100644 --- a/Sources/FDB/Tuple/Tuple+Unpack.swift +++ b/Sources/FDB/Tuple/Tuple+Unpack.swift @@ -1,7 +1,8 @@ import Foundation import LGNLog -fileprivate func findTerminator(input: Bytes, pos: Int) -> Int { +@inlinable +internal func findTerminator(input: Bytes, pos: Int) -> Int { let length = input.count var _pos = pos while true { diff --git a/Sources/FDB/Versionstamp.swift b/Sources/FDB/Versionstamp.swift index e23e0f9..07667f0 100644 --- a/Sources/FDB/Versionstamp.swift +++ b/Sources/FDB/Versionstamp.swift @@ -6,8 +6,10 @@ public extension FDB { struct Versionstamp: Equatable { /// 8-bytes: A big-endian, unsigned version corresponding to the commit version of a transaction, immutable. public let transactionCommitVersion: UInt64 + /// 2-bytes: A big-endian, unsigned batch number ordering transactions that are committed at the same version, immutable. public let batchNumber: UInt16 + /// Optional 2-byes: Extra ordering information to order writes within a single transaction, thereby providing a global order for all versions. public var userData: UInt16? @@ -35,8 +37,9 @@ public extension FDB { } extension FDB.Versionstamp: FDBTuplePackable { - public func pack() -> Bytes { + public func getPackedFDBTupleValue() -> Bytes { var result = Bytes() + if userData == nil { result.append(FDB.Tuple.Prefix.VERSIONSTAMP_80BIT) } else { diff --git a/Tests/FDBTests/TupleTests.swift b/Tests/FDBTests/TupleTests.swift index 22c67c3..54562c9 100644 --- a/Tests/FDBTests/TupleTests.swift +++ b/Tests/FDBTests/TupleTests.swift @@ -25,7 +25,7 @@ class TupleTests: XCTestCase { expected.append(0xFF) expected.append(contentsOf: "bar".bytes) expected.append(0x00) - XCTAssertEqual("F\u{00d4}O\u{0000}bar".pack(), expected) + XCTAssertEqual("F\u{00d4}O\u{0000}bar".getPackedFDBTupleValue(), expected) } func testPackBinaryString() { @@ -36,7 +36,7 @@ class TupleTests: XCTestCase { expected.append(0xFF) expected.append(contentsOf: "bar".bytes) expected.append(0x00) - XCTAssertEqual("foo\u{00}bar".bytes.pack(), expected) + XCTAssertEqual("foo\u{00}bar".bytes.getPackedFDBTupleValue(), expected) } func testPackNestedTuple() { @@ -60,7 +60,7 @@ class TupleTests: XCTestCase { expected.append(0x05) expected.append(0x00) expected.append(0x00) - XCTAssertEqual(tuple.pack(), expected) + XCTAssertEqual(tuple.getPackedFDBTupleValue(), expected) } func testPackInts() { @@ -111,7 +111,7 @@ class TupleTests: XCTestCase { // 100000000000000000322: [29, 9, 5, 107, 199, 94, 45, 99, 16, 1, 66], ] for (input, expected) in cases { - XCTAssertEqual(input.pack(), expected) + XCTAssertEqual(input.getPackedFDBTupleValue(), expected) } } @@ -120,7 +120,7 @@ class TupleTests: XCTestCase { expected.append(contentsOf: [0x13, 0xFE, 0x14, 0x15, 0x05, 0x05, 0x02]) expected.append(contentsOf: "foo".bytes) expected.append(contentsOf: [0x00, 0x00, 0x00]) - XCTAssertEqual(FDB.Tuple(-1, 0, 5, FDB.Tuple("foo"), FDB.Null()).pack(), expected) + XCTAssertEqual(FDB.Tuple(-1, 0, 5, FDB.Tuple("foo"), FDB.Null()).getPackedFDBTupleValue(), expected) } func testUnpack() throws { @@ -143,15 +143,15 @@ class TupleTests: XCTestCase { FDB.Versionstamp(userData: 73), ] let etalonTuple = FDB.Tuple(input) - let packed = etalonTuple.pack() - let repacked = try FDB.Tuple(from: packed).pack() + let packed = etalonTuple.getPackedFDBTupleValue() + let repacked = try FDB.Tuple(from: packed).getPackedFDBTupleValue() XCTAssertEqual(packed, repacked) } // Fixes https://github.com/kirilltitov/FDBSwift/issues/10 func testNullEscapes() throws { - let packed = Bytes([0, 0, 0,]).pack() - let repacked = try FDB.Tuple(from: packed).pack() + let packed = Bytes([0, 0, 0,]).getPackedFDBTupleValue() + let repacked = try FDB.Tuple(from: packed).getPackedFDBTupleValue() XCTAssertEqual(packed, repacked) } @@ -168,10 +168,10 @@ class TupleTests: XCTestCase { func testFloat() throws { for _ in 0...1000 { let random = Float32.random(in: -1000...1000) - let packed = random.pack() + let packed = random.getPackedFDBTupleValue() let unpacked = try FDB.Tuple(from: packed) XCTAssertEqual(random, unpacked.tuple[0] as! Float) - let repacked = unpacked.pack() + let repacked = unpacked.getPackedFDBTupleValue() XCTAssertEqual(packed, repacked) } @@ -191,17 +191,17 @@ class TupleTests: XCTestCase { ] for (inputFloat, expectedBytes) in cases { - XCTAssertEqual(expectedBytes, inputFloat.pack()) + XCTAssertEqual(expectedBytes, inputFloat.getPackedFDBTupleValue()) } } func testDouble() throws { for _ in 0...1000 { let random = Double.random(in: -1000...1000) - let packed = random.pack() + let packed = random.getPackedFDBTupleValue() let unpacked = try FDB.Tuple(from: packed) XCTAssertEqual(random, unpacked.tuple[0] as! Double) - let repacked = unpacked.pack() + let repacked = unpacked.getPackedFDBTupleValue() XCTAssertEqual(packed, repacked) } @@ -222,19 +222,19 @@ class TupleTests: XCTestCase { ] for (inputFloat, expectedBytes) in cases { - XCTAssertEqual(expectedBytes, inputFloat.pack()) + XCTAssertEqual(expectedBytes, inputFloat.getPackedFDBTupleValue()) } } func testBool() throws { - XCTAssertEqual([0x26], false.pack()) - XCTAssertEqual([0x27], true.pack()) + XCTAssertEqual([0x26], false.getPackedFDBTupleValue()) + XCTAssertEqual([0x27], true.getPackedFDBTupleValue()) for bool in [true, false] { - let packed = bool.pack() + let packed = bool.getPackedFDBTupleValue() let unpacked = try FDB.Tuple(from: packed) XCTAssertEqual(bool, unpacked.tuple[0] as! Bool) - let repacked = unpacked.pack() + let repacked = unpacked.getPackedFDBTupleValue() XCTAssertEqual(packed, repacked) } } @@ -242,11 +242,11 @@ class TupleTests: XCTestCase { func testUUID() throws { let etalon: uuid_t = (136,167,235,150,108,115,69,118,164,45,145,99,222,237,56,59) let uuid = UUID(uuid: etalon) - let packed = uuid.pack() + let packed = uuid.getPackedFDBTupleValue() XCTAssertEqual([0x30] + getBytes(etalon), packed) let unpacked = try FDB.Tuple(from: packed) XCTAssertEqual(uuid, unpacked.tuple[0] as! UUID) - XCTAssertEqual(packed, unpacked.pack()) + XCTAssertEqual(packed, unpacked.getPackedFDBTupleValue()) } func testVersionstamp() throws { @@ -262,7 +262,7 @@ class TupleTests: XCTestCase { ] for (input, expectedBytes) in cases { - XCTAssertEqual(expectedBytes, input.pack()) + XCTAssertEqual(expectedBytes, input.getPackedFDBTupleValue()) let unpacked = try FDB.Tuple(from: expectedBytes) XCTAssertEqual(input, unpacked.tuple[0] as! FDB.Versionstamp) } @@ -270,16 +270,16 @@ class TupleTests: XCTestCase { func testIncompleteVersionstampDetection() throws { let cases: [(Bytes, UInt32)] = [ - (FDB.Tuple(FDB.Versionstamp()).pack(), 1), - (FDB.Tuple(FDB.Versionstamp(userData: 0)).pack(), 1), - (FDB.Tuple("foo", FDB.Versionstamp()).pack(), 6), - (FDB.Tuple("foo", FDB.Versionstamp(userData: 0)).pack(), 6), - (FDB.Tuple("foo", FDB.Versionstamp(), FDB.Versionstamp()).pack(), 6), - (FDB.Tuple("foo", FDB.Versionstamp(transactionCommitVersion: 12, batchNumber: 0), FDB.Versionstamp(), FDB.Versionstamp()).pack(), 17), - (FDB.Tuple("foo", FDB.Versionstamp(transactionCommitVersion: 0, batchNumber: 12), FDB.Versionstamp(), FDB.Versionstamp()).pack(), 17), - (FDB.Tuple("foo", FDB.Tuple(FDB.Versionstamp())).pack(), 7), - (FDB.Tuple("foo", FDB.Tuple(FDB.Versionstamp()), FDB.Versionstamp()).pack(), 7), - (FDB.Tuple("foo", FDB.Tuple("bar", FDB.Versionstamp()), FDB.Versionstamp()).pack(), 12), + (FDB.Tuple(FDB.Versionstamp()).getPackedFDBTupleValue(), 1), + (FDB.Tuple(FDB.Versionstamp(userData: 0)).getPackedFDBTupleValue(), 1), + (FDB.Tuple("foo", FDB.Versionstamp()).getPackedFDBTupleValue(), 6), + (FDB.Tuple("foo", FDB.Versionstamp(userData: 0)).getPackedFDBTupleValue(), 6), + (FDB.Tuple("foo", FDB.Versionstamp(), FDB.Versionstamp()).getPackedFDBTupleValue(), 6), + (FDB.Tuple("foo", FDB.Versionstamp(transactionCommitVersion: 12, batchNumber: 0), FDB.Versionstamp(), FDB.Versionstamp()).getPackedFDBTupleValue(), 17), + (FDB.Tuple("foo", FDB.Versionstamp(transactionCommitVersion: 0, batchNumber: 12), FDB.Versionstamp(), FDB.Versionstamp()).getPackedFDBTupleValue(), 17), + (FDB.Tuple("foo", FDB.Tuple(FDB.Versionstamp())).getPackedFDBTupleValue(), 7), + (FDB.Tuple("foo", FDB.Tuple(FDB.Versionstamp()), FDB.Versionstamp()).getPackedFDBTupleValue(), 7), + (FDB.Tuple("foo", FDB.Tuple("bar", FDB.Versionstamp()), FDB.Versionstamp()).getPackedFDBTupleValue(), 12), ] for (packedInput, offset) in cases { @@ -288,14 +288,14 @@ class TupleTests: XCTestCase { } let invalidCases: [Bytes] = [ - FDB.Tuple().pack(), - FDB.Tuple(42, "foo").pack(), - FDB.Tuple(FDB.Versionstamp(transactionCommitVersion: 42, batchNumber: 0)).pack(), - FDB.Tuple(FDB.Versionstamp(transactionCommitVersion: 0, batchNumber: 12)).pack(), - FDB.Tuple(FDB.Versionstamp(transactionCommitVersion: 42, batchNumber: 12, userData: 0)).pack(), - FDB.Tuple(42, "foo", FDB.Versionstamp(transactionCommitVersion: 42, batchNumber: 0)).pack(), - FDB.Tuple(42, "foo", FDB.Versionstamp(transactionCommitVersion: 0, batchNumber: 12)).pack(), - FDB.Tuple(42, "foo", FDB.Versionstamp(transactionCommitVersion: 42, batchNumber: 12, userData: 0)).pack(), + FDB.Tuple().getPackedFDBTupleValue(), + FDB.Tuple(42, "foo").getPackedFDBTupleValue(), + FDB.Tuple(FDB.Versionstamp(transactionCommitVersion: 42, batchNumber: 0)).getPackedFDBTupleValue(), + FDB.Tuple(FDB.Versionstamp(transactionCommitVersion: 0, batchNumber: 12)).getPackedFDBTupleValue(), + FDB.Tuple(FDB.Versionstamp(transactionCommitVersion: 42, batchNumber: 12, userData: 0)).getPackedFDBTupleValue(), + FDB.Tuple(42, "foo", FDB.Versionstamp(transactionCommitVersion: 42, batchNumber: 0)).getPackedFDBTupleValue(), + FDB.Tuple(42, "foo", FDB.Versionstamp(transactionCommitVersion: 0, batchNumber: 12)).getPackedFDBTupleValue(), + FDB.Tuple(42, "foo", FDB.Versionstamp(transactionCommitVersion: 42, batchNumber: 12, userData: 0)).getPackedFDBTupleValue(), ] for packedInput in invalidCases {