Skip to content

Commit

Permalink
Updated to 07-25
Browse files Browse the repository at this point in the history
  • Loading branch information
Joannis Orlandos committed Jul 30, 2016
1 parent 474ef81 commit a0d3bec
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0-PREVIEW-2
DEVELOPMENT-SNAPSHOT-2016-07-25-a
12 changes: 6 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ var package = Package(
name: "MongoKitten",
dependencies: [
// For MongoCR authentication
.Package(url: "https://github.com/CryptoKitten/MD5.git", majorVersion: 0, minor: 8),
.Package(url: "https://github.com/CryptoKitten/MD5.git", majorVersion: 0, minor: 10),

// For SCRAM-SHA-1 authentication
.Package(url: "https://github.com/CryptoKitten/SCRAM.git", majorVersion: 0, minor: 8),
.Package(url: "https://github.com/CryptoKitten/SHA1.git", majorVersion: 0, minor: 8),
.Package(url: "https://github.com/CryptoKitten/SCRAM.git", majorVersion: 0, minor: 10),
.Package(url: "https://github.com/CryptoKitten/SHA1.git", majorVersion: 0, minor: 10),

// For MongoDB Documents
.Package(url: "https://github.com/OpenKitten/BSON.git", majorVersion: 3, minor: 4),
.Package(url: "https://github.com/OpenKitten/BSON.git", majorVersion: 3, minor: 5),

// Provides sockets
.Package(url: "https://github.com/czechboy0/Socks.git", majorVersion: 0, minor: 8),
.Package(url: "https://github.com/czechboy0/Socks.git", majorVersion: 0, minor: 10),

// Background queue
.Package(url: "https://github.com/ketzusaka/Strand.git", majorVersion: 1, minor: 5),
.Package(url: "https://github.com/ketzusaka/Strand.git", majorVersion: 1, minor: 6),

]
)
Expand Down
8 changes: 4 additions & 4 deletions Sources/Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public final class Collection {
throw InternalMongoError.incorrectReply(reply: reply)
}

guard let responseDoc = documents.first, cursorDoc = responseDoc["cursor"].documentValue else {
guard let responseDoc = documents.first, let cursorDoc = responseDoc["cursor"].documentValue else {
throw MongoError.invalidResponse(documents: documents)
}

Expand Down Expand Up @@ -863,7 +863,7 @@ public final class Collection {
///
/// - throws: When we can't send the request/receive the response, you don't have sufficient permissions or an error occurred
public func createIndexes(_ indexes: [(name: String, keys: [(key: String, ascending: Bool)], filter: Query?, buildInBackground: Bool, unique: Bool)]) throws {
guard let wireVersion = database.server.serverData?.maxWireVersion where wireVersion >= 2 else {
guard let wireVersion = database.server.serverData?.maxWireVersion , wireVersion >= 2 else {
throw MongoError.unsupportedOperations
}

Expand Down Expand Up @@ -924,7 +924,7 @@ public final class Collection {
///
/// - returns: A Cursor pointing to the Index results
public func listIndexes() throws -> Cursor<Document> {
guard let wireVersion = database.server.serverData?.maxWireVersion where wireVersion > 3 else {
guard let wireVersion = database.server.serverData?.maxWireVersion , wireVersion > 3 else {
throw MongoError.unsupportedOperations
}

Expand Down Expand Up @@ -965,7 +965,7 @@ public final class Collection {
throw InternalMongoError.incorrectReply(reply: reply)
}

guard let responseDoc = documents.first, cursorDoc = responseDoc["cursor"].documentValue else {
guard let responseDoc = documents.first, let cursorDoc = responseDoc["cursor"].documentValue else {
throw MongoError.invalidResponse(documents: documents)
}

Expand Down
31 changes: 0 additions & 31 deletions Sources/Compatiblity.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Sources/Cursor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public final class Cursor<T> {
}

internal convenience init(cursorDocument cursor: Document, server: Server, chunkSize: Int32, transform: Transformer) throws {
guard let cursorID = cursor["id"].int64Value, namespace = cursor["ns"].stringValue, firstBatch = cursor["firstBatch"].documentValue else {
guard let cursorID = cursor["id"].int64Value, let namespace = cursor["ns"].stringValue, let firstBatch = cursor["firstBatch"].documentValue else {
throw MongoError.cursorInitializationError(cursorDocument: cursor)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public final class Database {
let result = try firstDocument(in: reply)

let code = result["ok"].int32
guard let cursor = result["cursor"].documentValue where code == 1 else {
guard let cursor = result["cursor"].documentValue , code == 1 else {
throw MongoError.commandFailure(error: result)
}

Expand Down
12 changes: 6 additions & 6 deletions Sources/GridFS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public class GridFS {
public let chunkSize: Int32

/// The date on which this file has been uploaded
public let uploadDate: NSDate
public let uploadDate: Date

/// The file's MD5 hash
public let md5: String
Expand Down Expand Up @@ -221,10 +221,10 @@ public class GridFS {
/// - parameter chunksCollection: The `Collection` where the `File` data is stored
internal init?(document: Document, chunksCollection: Collection, filesCollection: Collection) {
guard let id = document["_id"].objectIdValue,
length = document["length"].int32Value,
chunkSize = document["chunkSize"].int32Value,
uploadDate = document["uploadDate"].dateValue,
md5 = document["md5"].stringValue
let length = document["length"].int32Value,
let chunkSize = document["chunkSize"].int32Value,
let uploadDate = document["uploadDate"].dateValue,
let md5 = document["md5"].stringValue
else {
return nil
}
Expand Down Expand Up @@ -318,7 +318,7 @@ public class GridFS {
/// Initializes with a `Document` found when looking for chunks
init?(document: Document, chunksCollection: Collection, filesCollection: Collection) {
guard let id = document["_id"].objectIdValue,
filesID = document["files_id"].objectIdValue,
let filesID = document["files_id"].objectIdValue,
case .binary(_, let data) = document["data"] else {
return nil
}
Expand Down
4 changes: 1 addition & 3 deletions Sources/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ enum Message {
}

// Get the message length
guard let length: Int32 = try Int32.instantiate(bytes: data[0...3]*) else {
throw DeserializationError.ParseError
}
let length = try Int32.instantiate(bytes: data[0...3]*)

// Check the message length
if length != Int32(data.count) {
Expand Down
6 changes: 3 additions & 3 deletions Sources/MongoError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import struct BSON.Document

/// All MongoDB errors
public enum MongoError : ErrorProtocol {
public enum MongoError : Error {
/// Can't connect to the MongoDB Server
case mongoDatabaseUnableToConnect

Expand Down Expand Up @@ -79,13 +79,13 @@ public enum MongoError : ErrorProtocol {
case invalidNSURL(url: NSURL)
}

public enum MongoAuthenticationError : ErrorProtocol {
public enum MongoAuthenticationError : Error {
case base64Failure
case authenticationFailure
case serverSignatureInvalid
case incorrectCredentials
}

internal enum InternalMongoError : ErrorProtocol {
internal enum InternalMongoError : Error {
case incorrectReply(reply: Message)
}
30 changes: 21 additions & 9 deletions Sources/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,22 @@ public final class Server {
internal var fullBuffer = [Byte]()

/// A cache for incoming responses
#if os(Linux)
private var incomingMutateLock = Lock()
private var incomingResponses = [(id: Int32, message: Message, date: NSDate)]()
#else
private var incomingMutateLock = NSLock()
#endif

private var incomingResponses = [(id: Int32, message: Message, date: Date)]()

/// Contains a map from an ID to a handler. The handlers handle the `incomingResponses`
private var responseHandlers = [Int32:ResponseHandler]()

#if os(Linux)
private var waitingForResponses = [Int32:Condition]()
#else
private var waitingForResponses = [Int32:NSCondition]()
#endif

/// `MongoTCP` Socket bound to the MongoDB Server
private var client: MongoTCP?
Expand All @@ -73,15 +82,15 @@ public final class Server {
///
/// - parameter automatically: Whether to connect automatically
public convenience init(_ url: NSURL, using tcpDriver: MongoTCP.Type = Socks.TCPClient.self, automatically connecting: Bool = true) throws {
guard let scheme = url.scheme, let host = url.host where scheme.lowercased() == "mongodb" else {
guard let scheme = url.scheme, let host = url.host , scheme.lowercased() == "mongodb" else {
throw MongoError.invalidNSURL(url: url)
}

var authentication: (username: String, password: String, against: String)? = nil

let path = url.path ?? "admin"

if let user = url.user, pass = url.password {
if let user = url.user, let pass = url.password {
authentication = (username: user, password: pass, against: path)
}

Expand All @@ -91,7 +100,7 @@ public final class Server {
let port: UInt16 = UInt16(url.port?.int16Value ?? 27017)
#endif

try self.init(at: host, port: port, using: authentication, automatically: connecting, using: tcpDriver)
try self.init(at: host, port: port, using: authentication, using: tcpDriver, automatically: connecting)
}

/// Sets up the `Server` to connect to the specified URL.
Expand Down Expand Up @@ -260,9 +269,7 @@ public final class Server {

do {
while fullBuffer.count >= 36 {
guard let length: Int = Int(try Int32.instantiate(bytes: fullBuffer[0...3]*)) else {
throw DeserializationError.ParseError
}
let length = Int(try Int32.instantiate(bytes: fullBuffer[0...3]*))

guard length <= fullBuffer.count else {
// Ignore: Wait for more data
Expand All @@ -274,7 +281,7 @@ public final class Server {
let reply = try Message.makeReply(from: responseData)

incomingMutateLock.lock()
incomingResponses.append((responseId, reply, NSDate()))
incomingResponses.append((responseId, reply, Date()))
incomingMutateLock.unlock()

fullBuffer.removeSubrange(0..<length)
Expand All @@ -292,7 +299,12 @@ public final class Server {
///
/// - returns: The reply
internal func await(response requestId: Int32, until timeout: TimeInterval = 60) throws -> Message {
let condition = Condition()
#if os(Linux)
let condition = Condition()
#else
let condition = NSCondition()
#endif

condition.lock()
waitingForResponses[requestId] = condition

Expand Down
2 changes: 1 addition & 1 deletion Sources/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public protocol MongoTCP : class {
func receive() throws -> [UInt8]
}

enum TCPError : ErrorProtocol {
enum TCPError : Error {
case ConnectionFailed
case NotConnected
case AlreadyConnected
Expand Down
2 changes: 1 addition & 1 deletion Tests/MongoKitten/AdministrationCommandsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AdministrationCommandsTests: XCTestCase {

try! TestManager.connect()
try! TestManager.clean()
}
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
Expand Down
2 changes: 1 addition & 1 deletion Tests/MongoKitten/TestManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import BSON
import Foundation

final class TestManager {
enum TestError : ErrorProtocol {
enum TestError : Error {
case TestDataNotPresent
}

Expand Down

0 comments on commit a0d3bec

Please sign in to comment.