Skip to content

Commit

Permalink
Copying the search tree on mutation works better in setting keys now.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joannis committed Jun 20, 2017
1 parent f932d77 commit 4efeda0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Sources/BSON/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,11 @@ public struct Document : Collection, ExpressibleByDictionaryLiteral, Expressible
node.storage = trie
}

if isKnownUniquelyReferenced(&searchTree) {
self.searchTree[key] = node
} else {
self.searchTree[key] = node.copy()
if !isKnownUniquelyReferenced(&searchTree) {
searchTree = searchTree.copy()
}

self.searchTree[key] = node
}

// If the key already has a value
Expand Down
17 changes: 17 additions & 0 deletions Tests/BSONTests/BSONPublicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ final class BSONPublicTests: XCTestCase {
XCTAssert(!document.validatesAsArray())
}

func testTrieCopy() {
var doc = Document()
doc["username"] = "bob"
doc["password"] = "Secrat"

var doc2 = doc

let id = ObjectId()
let id2 = ObjectId()

doc["_id"] = id
doc2["_id"] = id2

XCTAssertEqual(ObjectId(doc["_id"]), id)
XCTAssertEqual(ObjectId(doc2["_id"]), id2)
}

func testNullToNilInt() {
var doc: Document = [
"_id": Null(),
Expand Down

0 comments on commit 4efeda0

Please sign in to comment.