Skip to content

Commit

Permalink
Merge pull request #84 from kirilltitov/january-2022-cleanup
Browse files Browse the repository at this point in the history
Make `FDBTuplePackable` methods more specific (and less general). More.
  • Loading branch information
kirilltitov authored Jan 10, 2022
2 parents d414399 + a3a3dc9 commit 643e531
Show file tree
Hide file tree
Showing 19 changed files with 138 additions and 108 deletions.
12 changes: 6 additions & 6 deletions Sources/FDB/AnyFDBKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion Sources/FDB/FDB/FDB+Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
3 changes: 3 additions & 0 deletions Sources/FDB/Future/Future+Bytes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import CFDB

extension FDB.Future {
/// Blocks current thread until future is resolved
@inlinable
internal func wait() throws -> Bytes? {
try self.waitAndCheck().parseBytes()
}

/// 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<Byte>!
Expand All @@ -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<Byte>!
var readKeyLength: Int32 = 0
Expand Down
5 changes: 4 additions & 1 deletion Sources/FDB/Future/Future+Int64.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
1 change: 1 addition & 0 deletions Sources/FDB/Future/Future+KeyValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<FDBKeyValue>!
var outCount: Int32 = 0
Expand Down
22 changes: 12 additions & 10 deletions Sources/FDB/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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<R>() throws -> R {
guard MemoryLayout<R>.size == self.count else {
Expand All @@ -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: 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
Expand Down Expand Up @@ -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)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/FDB/KeyValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
12 changes: 6 additions & 6 deletions Sources/FDB/Subspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
)
Expand All @@ -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
}
}
1 change: 1 addition & 0 deletions Sources/FDB/Transaction/Transaction+Internal+Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
40 changes: 20 additions & 20 deletions Sources/FDB/Tuple.swift
Original file line number Diff line number Diff line change
@@ -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()
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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())
}
}
9 changes: 6 additions & 3 deletions Sources/FDB/Tuple/Tuple+Array.swift
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
}
}
4 changes: 2 additions & 2 deletions Sources/FDB/Tuple/Tuple+Bool.swift
Original file line number Diff line number Diff line change
@@ -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]
}
Expand Down
Loading

0 comments on commit 643e531

Please sign in to comment.