Skip to content

Commit

Permalink
improved caching array count
Browse files Browse the repository at this point in the history
  • Loading branch information
Joannis committed Oct 18, 2017
1 parent 370f6a3 commit 3edad32
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
10 changes: 6 additions & 4 deletions Sources/BSON/Document+ParsingSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension Document {
@discardableResult
internal func index(recursive keys: [IndexKey]? = nil, lookingFor matcher: [IndexKey]?, levels: Int? = nil) -> ElementMetadata? {
// If the key path is indexes, return the data about this key path
if searchTree.fullyIndexed {
if searchTree.recursivelyIndexed {
guard let matcher = matcher, let pos = searchTree[position: matcher] else {
return nil
}
Expand Down Expand Up @@ -60,7 +60,7 @@ extension Document {
position = pos
} else if var keys = keys {
if let pos = searchTree[position: keys] {
if searchTree[keys]?.fullyIndexed == true {
if searchTree[keys]?.recursivelyIndexed == true {
return nil
}

Expand Down Expand Up @@ -182,11 +182,13 @@ extension Document {
}

if let keys = keys {
self.searchTree[keys]?.fullyIndexed = true
self.searchTree[keys]?.recursivelyIndexed = true
} else if levels == nil || levels == 0 {
self.searchTree.fullyIndexed = self.searchTree.storage.values.reduce(true) { $0.1.fullyIndexed && $0.0 }
self.searchTree.recursivelyIndexed = self.searchTree.storage.values.reduce(true) { $0.1.recursivelyIndexed && $0.0 }
}

self.searchTree.fullyIndexed = true

return nil
}

Expand Down
3 changes: 2 additions & 1 deletion Sources/BSON/Index.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class IndexTrieNode {
var storage = Dictionary<IndexKey, IndexTrieNode>()
var value: Int
var fullyIndexed: Bool = false
var recursivelyIndexed: Bool = false

init(_ value: Int) {
self.value = value
Expand Down Expand Up @@ -59,7 +60,7 @@ class IndexTrieNode {

func copy() -> IndexTrieNode {
let copy = IndexTrieNode(self.value)
copy.fullyIndexed = self.fullyIndexed
copy.recursivelyIndexed = self.recursivelyIndexed

for (key, value) in self.storage {
copy.storage[key] = value.copy()
Expand Down

0 comments on commit 3edad32

Please sign in to comment.