Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No monkey business #4854

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 32 additions & 50 deletions fiftyone/core/odm/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,63 +713,45 @@ def _save(
# Update existing document
updates = {}

try:
# OPTIMIZATION: we monkey patch `to_mongo()` and
# `_get_changed_fields()` here so that mongoengine's
# implementations of `_delta()` and `_clear_changed_fields()`,
# which call these methods, will not serialize unnecessary
# fields and recompute changed fields unecessarily
self._get_changed_fields_orig = self._get_changed_fields
self._to_mongo_orig = self.to_mongo

if hasattr(self, "_changed_fields"):
changed_fields = self._get_changed_fields()
roots, paths = self._parse_changed_fields(changed_fields)
else:
# Changes aren't yet tracked, so validate everything
roots, paths = None, None

if validate:
self._validate_updates(
roots,
paths,
clean=clean,
enforce_read_only=enforce_read_only,
)
if hasattr(self, "_changed_fields"):
changed_fields = self._get_changed_fields()
roots, paths = self._parse_changed_fields(changed_fields)
else:
# Changes aren't yet tracked, so validate everything
roots, paths = None, None

# OPTIMIZATION: apply monkey patch
doc = self.to_mongo(fields=roots)
self._get_changed_fields = lambda: changed_fields
self.to_mongo = lambda: doc
if validate:
self._validate_updates(
roots,
paths,
clean=clean,
enforce_read_only=enforce_read_only,
)

sets, unsets = self._delta()
sets, unsets = self._delta()

if sets:
updates["$set"] = sets
if sets:
updates["$set"] = sets

if unsets:
updates["$unset"] = unsets
if unsets:
updates["$unset"] = unsets

if updates:
ops, updated_existing = self._update(
_id,
updates,
deferred=deferred,
upsert=upsert,
**kwargs,
)

if updates:
ops, updated_existing = self._update(
_id,
updates,
deferred=deferred,
upsert=upsert,
**kwargs,
if not deferred and not upsert and not updated_existing:
raise ValueError(
"Failed to update %s with ID '%s'"
% (self._doc_name().lower(), str(_id))
)

if not deferred and not upsert and not updated_existing:
raise ValueError(
"Failed to update %s with ID '%s'"
% (self._doc_name().lower(), str(_id))
)

self._clear_changed_fields()
finally:
# OPTIMIZATION: revert monkey patch
self._get_changed_fields = self._get_changed_fields_orig
self.to_mongo = self._to_mongo_orig
self._clear_changed_fields()

return ops

Expand Down
Loading