Skip to content

Commit 2a96293

Browse files
committed
Merge branch 'develop'
2 parents e7372d4 + c3720e2 commit 2a96293

File tree

12 files changed

+182
-75
lines changed

12 files changed

+182
-75
lines changed

CONTRIBUTING.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
By submitting a pull request, you represent that you have the right to license
2+
your contribution to the community, and agree by submitting the patch
3+
that your contributions are licensed under the Apache 2.0 license (see
4+
`LICENSE.txt`).
5+

Package.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ let package = Package(
1010
],
1111
dependencies: [
1212
.package(url: "https://github.com/apple/swift-nio.git",
13-
from: "1.3.1"),
13+
from: "1.8.0"),
1414
],
1515
targets: [
1616
.target(name: "NIORedis", dependencies: [ "NIO", "NIOFoundationCompat" ]),
17-
.target(name: "Redis", dependencies: [ "NIORedis" ])
17+
.target(name: "Redis", dependencies: [ "NIORedis" ]),
18+
.testTarget(name: "NIORedisTests", dependencies: [ "NIORedis"]),
19+
.testTarget(name: "RedisTests", dependencies: [ "Redis"]),
1820
]
1921
)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the swift-nio-redis open source project
4+
//
5+
// Copyright (c) 2018 ZeeZide GmbH. and the swift-nio-redis project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import struct NIO.ByteBuffer
16+
17+
fileprivate extension BinaryInteger {
18+
19+
var numberOfDecimalDigits : Int {
20+
@inline(__always) get {
21+
var value = self
22+
var count = 0
23+
24+
repeat {
25+
value /= 10
26+
count += 1
27+
}
28+
while value != 0
29+
30+
return count
31+
}
32+
}
33+
}
34+
35+
extension ByteBuffer {
36+
37+
@discardableResult
38+
public mutating func write<T: SignedInteger>(integerAsString integer: T,
39+
as: T.Type = T.self) -> Int
40+
{
41+
let bytesWritten = set(integerAsString: integer, at: self.writerIndex)
42+
moveWriterIndex(forwardBy: bytesWritten)
43+
return Int(bytesWritten)
44+
}
45+
46+
@discardableResult
47+
public mutating func set<T: SignedInteger>(integerAsString integer: T,
48+
at index: Int,
49+
as: T.Type = T.self) -> Int
50+
{
51+
let charCount = integer.numberOfDecimalDigits + (integer < 0 ? 1 : 0)
52+
let avail = capacity - index
53+
54+
if avail < charCount {
55+
changeCapacity(to: capacity + (charCount - avail))
56+
}
57+
58+
self.withVeryUnsafeBytes { rbpp in
59+
let mrbpp = UnsafeMutableRawBufferPointer(mutating: rbpp)
60+
let base = mrbpp.baseAddress!.assumingMemoryBound(to: UInt8.self)
61+
.advanced(by: index)
62+
var cursor = base.advanced(by: charCount)
63+
64+
let c0 : T = 48
65+
var negativeAbsoluteValue = integer < 0 ? integer : -integer
66+
repeat {
67+
cursor -= 1
68+
cursor.pointee = UInt8(c0 - (negativeAbsoluteValue % 10))
69+
negativeAbsoluteValue /= 10;
70+
}
71+
while negativeAbsoluteValue != 0
72+
73+
if integer < 0 {
74+
cursor -= 1
75+
cursor.pointee = 45 // -
76+
}
77+
78+
}
79+
80+
return charCount
81+
}
82+
}

Sources/NIORedis/RESPChannelHandler.swift

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -261,70 +261,3 @@ fileprivate enum ConstantBuffers {
261261
return bb
262262
}()
263263
}
264-
265-
fileprivate extension BinaryInteger {
266-
267-
var numberOfDecimalDigits : Int {
268-
@inline(__always) get {
269-
var value = self
270-
var count = 0
271-
272-
repeat {
273-
value /= 10
274-
count += 1
275-
}
276-
while value != 0
277-
278-
return count
279-
}
280-
}
281-
}
282-
283-
extension ByteBuffer {
284-
285-
@discardableResult
286-
public mutating func write<T: SignedInteger>(integerAsString integer: T,
287-
as: T.Type = T.self) -> Int
288-
{
289-
let bytesWritten = set(integerAsString: integer, at: self.writerIndex)
290-
moveWriterIndex(forwardBy: bytesWritten)
291-
return Int(bytesWritten)
292-
}
293-
294-
@discardableResult
295-
public mutating func set<T: SignedInteger>(integerAsString integer: T,
296-
at index: Int,
297-
as: T.Type = T.self) -> Int
298-
{
299-
let charCount = integer.numberOfDecimalDigits + (integer < 0 ? 1 : 0)
300-
let avail = capacity - index
301-
302-
if avail < charCount {
303-
changeCapacity(to: capacity + (charCount - avail))
304-
}
305-
306-
self.withVeryUnsafeBytes { rbpp in
307-
let mrbpp = UnsafeMutableRawBufferPointer(mutating: rbpp)
308-
let base = mrbpp.baseAddress!.assumingMemoryBound(to: UInt8.self)
309-
.advanced(by: index)
310-
var cursor = base.advanced(by: charCount)
311-
312-
let c0 : T = 48
313-
var negativeAbsoluteValue = integer < 0 ? integer : -integer
314-
repeat {
315-
cursor -= 1
316-
cursor.pointee = UInt8(c0 - (negativeAbsoluteValue % 10))
317-
negativeAbsoluteValue /= 10;
318-
}
319-
while negativeAbsoluteValue != 0
320-
321-
if integer < 0 {
322-
cursor -= 1
323-
cursor.pointee = 45 // -
324-
}
325-
326-
}
327-
328-
return charCount
329-
}
330-
}

Sources/NIORedis/RESPValue.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public enum RESPValue {
2323
case error (RESPError)
2424
}
2525

26-
public struct RESPError : Error {
26+
public struct RESPError : Error, CustomStringConvertible {
2727

2828
public init(code: String = "ERR", message: String = "Generic Error") {
2929
_storage = _Storage(code: code, message: message)
@@ -41,6 +41,10 @@ public struct RESPError : Error {
4141
}
4242
}
4343
private let _storage : _Storage
44+
45+
public var description: String {
46+
return "<RESPError: \(code) '\(message)'>"
47+
}
4448
}
4549

4650
// MARK: - Initializers

Sources/Redis/RedisClient.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import NIORedis
1818
public let DefaultRedisPort = 6379
1919

2020
/// Create a Redis client object
21-
public func createClient(port : Int = DefaultRedisPort,
22-
host : String = "127.0.0.1",
21+
public func createClient(port : Int = DefaultRedisPort,
22+
host : String = "127.0.0.1",
2323
password : String? = nil,
24-
db : Int? = nil,
24+
db : Int? = nil,
2525
eventLoopGroup : EventLoopGroup? = nil)
2626
-> RedisClient
2727
{
@@ -416,7 +416,7 @@ open class RedisClient : RedisCommandTarget {
416416

417417
state = .connecting
418418
retryInfo.attempt += 1
419-
return bootstrap.connect(host: "localhost", port: DefaultRedisPort)
419+
return bootstrap.connect(host: host, port: port)
420420
.map { channel in
421421
self.retryInfo.registerSuccessfulConnect()
422422

Sources/Redis/RedisClientOptions.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
import NIO
1616

17+
fileprivate let onDemandSharedEventLoopGroup =
18+
MultiThreadedEventLoopGroup(numberOfThreads: 1)
19+
1720
/// Configuration options for the socket connects
1821
open class ConnectOptions : CustomStringConvertible {
1922

@@ -27,7 +30,8 @@ open class ConnectOptions : CustomStringConvertible {
2730
self.hostname = hostname
2831
self.port = port
2932
self.eventLoopGroup = eventLoopGroup
30-
?? MultiThreadedEventLoopGroup(numThreads: 1)
33+
?? MultiThreadedEventLoopGroup.currentEventLoop
34+
?? onDemandSharedEventLoopGroup
3135
}
3236

3337
public var description: String {

Tests/LinuxMain.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import XCTest
2+
3+
import NIORedisTests
4+
import RedisTests
5+
6+
var tests = [XCTestCaseEntry]()
7+
tests += NIORedisTests.allTests()
8+
tests += RedisTests.allTests()
9+
XCTMain(tests)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import XCTest
2+
@testable import NIORedis
3+
4+
final class RESPValueTests: XCTestCase {
5+
6+
func testDescription() throws {
7+
XCTAssert(String(describing: RESPValue(1)) == "1")
8+
}
9+
10+
static var allTests = [
11+
("testDescription", testDescription)
12+
]
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import XCTest
2+
3+
#if !os(macOS)
4+
public func allTests() -> [XCTestCaseEntry] {
5+
return [
6+
testCase(RESPValueTests.allTests),
7+
]
8+
}
9+
#endif

0 commit comments

Comments
 (0)