Skip to content

Commit

Permalink
Attempting to fix the issue again. #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Joannis committed May 17, 2017
1 parent 0a92b8a commit 40657e6
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions Sources/BSON/Document+Subscripts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,29 @@ extension Document {
let len = getLengthOfElement(withDataPosition: meta.dataPosition, type: meta.type)
let dataEndPosition = meta.dataPosition+len

storage.removeSubrange(meta.dataPosition..<dataEndPosition)

let relativeLength: Int

if let newValue = newValue {
storage.removeSubrange(meta.dataPosition..<dataEndPosition)
let oldLength = dataEndPosition - meta.dataPosition
let newBinary = newValue.makeBinary()
storage.insert(contentsOf: newBinary, at: meta.dataPosition)
storage[meta.elementTypePosition] = newValue.typeIdentifier
relativeLength = newBinary.count - oldLength

for (key, startPosition) in searchTree where startPosition > meta.elementTypePosition {
searchTree[key] = startPosition + relativeLength
}
} else {
storage.removeSubrange(meta.elementTypePosition..<(meta.elementTypePosition + part.bytes.count + 2 + len))
// key + null terminator + type
relativeLength = -((part.bytes.count + 2) + len)

searchTree[part] = nil
}

for (key, startPosition) in searchTree where startPosition > meta.elementTypePosition {
searchTree[key] = startPosition + relativeLength
for (key, startPosition) in searchTree where startPosition > meta.dataPosition + relativeLength {
searchTree[key] = startPosition + relativeLength
}
}

updateDocumentHeader()
Expand Down

0 comments on commit 40657e6

Please sign in to comment.