Skip to content

Instantly share code, notes, and snippets.

@rfdickerson
Last active April 5, 2017 18:06
Show Gist options
  • Save rfdickerson/11acd6c9ea2dc2daa0c23b9199b8bc6a to your computer and use it in GitHub Desktop.
Save rfdickerson/11acd6c9ea2dc2daa0c23b9199b8bc6a to your computer and use it in GitHub Desktop.
MongoKitten Concurrency
import Foundation
import Dispatch
import MongoKitten
let numberOfDocuments = 100
let settings = ClientSettings(host: MongoHost(hostname: "127.0.0.1",
port: UInt16(27017)),
sslSettings: nil,
credentials: nil)
let server = try Server(settings)
let database = server["tutorial"]
let collection = database["flight"]
if server.isConnected {
print("Successfully connected to the server")
}
try? database.drop()
var documents = [Document]()
for id in 0..<numberOfDocuments {
let doc: Document = [
"_id": "\(id)",
"customerId": "128374",
"flightId": "AA231",
"dateOfBooking": Date(),
]
documents.append(doc)
}
let queue = DispatchQueue(label: "insertion", attributes: .concurrent)
let dispatchGroup = DispatchGroup()
let parallel = true
documents.forEach { doc in
if parallel {
queue.async(group: dispatchGroup) {
do {
try collection.insert( doc )
} catch let error as InsertErrors {
print("error: \(error)")
} catch let error as MongoError {
print("error: \(error)")
} catch let error {
print("error: \(error)")
// returns timeout
}
}
} else {
do {
try collection.insert( doc )
} catch let error as InsertErrors {
print("error")
} catch {
print("Anything else")
}
}
}
if parallel {
dispatchGroup.wait()
}
print("Finished adding \(documents.count) documents to the database")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment