diff --git a/README.md b/README.md index 6ee26e6..51b265c 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ import PackageDescription let package = Package( name: "MyApp", - dependencies: [.Package(url: "https://github.com/OpenKitten/BSON.git", majorVersion: 3, minor: 6)] + dependencies: [.Package(url: "https://github.com/OpenKitten/BSON.git", majorVersion: 3, minor: 7)] ) ``` diff --git a/Sources/Document+Subscripts.swift b/Sources/Document+Subscripts.swift index d38fe1b..7ed50ff 100644 --- a/Sources/Document+Subscripts.swift +++ b/Sources/Document+Subscripts.swift @@ -54,6 +54,7 @@ extension Document { storage.insert(contentsOf: newValue.bytes, at: meta.dataPosition) storage[meta.elementTypePosition] = newValue.typeIdentifier updateDocumentHeader() + self.elementPositions = buildElementPositionsCache() return } diff --git a/Sources/ObjectId.swift b/Sources/ObjectId.swift index f547a2e..63d1fd6 100644 --- a/Sources/ObjectId.swift +++ b/Sources/ObjectId.swift @@ -7,6 +7,7 @@ // import Foundation +import Dispatch #if os(Linux) import Glibc @@ -38,6 +39,8 @@ public struct ObjectId { internal var _storage: [UInt8] + private static let counterQueue = DispatchQueue(label: "org.mongokitten.bson.oidcounter") + /// Generate a new random ObjectId. public init() { let currentTime = Date() @@ -51,8 +54,10 @@ public struct ObjectId { data += ObjectId.random.bytes // And add a counter as 2 bytes and increment it - data += ObjectId.counter.bytes - ObjectId.counter = ObjectId.counter &+ 1 + ObjectId.counterQueue.sync { + data += ObjectId.counter.bytes + ObjectId.counter = ObjectId.counter &+ 1 + } self._storage = data }