Skip to content

Conversation

@kmahar
Copy link
Contributor

@kmahar kmahar commented Aug 19, 2020

This PR works around the fact that invalid events do not have associated namespaces by accepting a MongoNamespace in ChangeStream.init, and then storing that namespace under ns in the ChangeStream's BSONDecoder's userInfo.

ChangeStreamEvent then looks for the matching key when decoding, and falls back to it if there is no key present in the original document.

Per conversation in driver-devs it appears that as of now anyway it's impossible to encounter an invalidate event on a client.

@kmahar kmahar requested a review from patrickfreed August 19, 2020 22:55
/// using the decoder e.g. `ChangeStreamEvent` to access the namespace even if it is not present in the raw
/// document the server returns. Ok to force unwrap as initialization never fails.
// swiftlint:disable:next force_unwrapping
internal let changeStreamNamespaceKey = CodingUserInfoKey(rawValue: "namespace")!
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally this would have lived on ChangeStream, but you can't have stored static properties on generic types 🤔

)
self.client = client
self.decoder = decoder
self.decoder = BSONDecoder(copies: decoder, options: nil)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we are updating the userInfo I figured it was a bit safer to make a copy.

this did make me think about the fact that right now users can't give us userInfo that we should use when decoding their custom types, even though they can configure all of the other encoder/decoder options via the strategies. I don't think that's too urgent though, we can wait until if/when someone asks for this capability.

self.ns = try container.decode(MongoNamespace.self, forKey: .ns)
} catch {
guard let ns = decoder.userInfo[changeStreamNamespaceKey] as? MongoNamespace else {
throw error
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered just filling in with an empty namespace or something here, but that seemed like it would hide errors from the server in cases where a namespace is unexpectedly missing.

Copy link
Contributor

@patrickfreed patrickfreed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like LinuxMain needs to be synced, but other than that LGTM!

@kmahar
Copy link
Contributor Author

kmahar commented Aug 20, 2020

someday I'll actually remember to sync it 🤦‍♀️ I should probably set up a githook

@codecov-commenter
Copy link

codecov-commenter commented Aug 20, 2020

Codecov Report

Merging #519 into master will decrease coverage by 0.22%.
The diff coverage is 36.48%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #519      +/-   ##
==========================================
- Coverage   77.87%   77.64%   -0.23%     
==========================================
  Files         129      130       +1     
  Lines       13787    13860      +73     
==========================================
+ Hits        10736    10762      +26     
- Misses       3051     3098      +47     
Impacted Files Coverage Δ
Sources/MongoSwift/ChangeStreamEvent.swift 0.00% <0.00%> (ø)
Sources/MongoSwift/MongoDatabase.swift 80.35% <ø> (ø)
...ts/MongoSwiftSyncTests/SyncChangeStreamTests.swift 37.95% <41.50%> (+0.28%) ⬆️
Sources/MongoSwift/Operations/WatchOperation.swift 73.80% <50.00%> (-3.97%) ⬇️
Sources/MongoSwift/ChangeStream.swift 31.74% <100.00%> (+1.10%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dc980c9...6173700. Read the comment docs.

@kmahar
Copy link
Contributor Author

kmahar commented Aug 20, 2020

@patrickfreed it turns out that on 3.6 the behavior is slightly different when a collection is dropped; there is no drop event, only an invalidate event. I updated the test accordingly.
when testing locally on 3.6 I also noticed that the skip message printed in the second part of the test was misleading and made it look like the whole test was skipped rather than just the second half, so I ended up splitting this up into two tests.

@kmahar kmahar merged commit e3de6ae into master Aug 20, 2020
@kmahar kmahar deleted the SWIFT-957/invalidate-event-fix branch August 20, 2020 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants