MongoEngine / MongoEngine/mongoengine
Feature request: Automatic optimistic concurrency validation
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 1.2k
- Avg merge
- 4h 41m
- Merged PRs (30d)
- 11
Description
Hi guys,
I see the Document.save method takes a save_condition dict. With a very minor change (basically a new subclass of Document and a convention) this could automatically enforce and verify that it is safe to save the document.
I put together an example, but it's not as a PR because I'm not sure the best way to fit it into your object hierarchy. Basically, Document does not allow subclassing so I'm going to attach the sample but you'll need to move it into the package if you like it.
I propose a new class derived from Document, called OptimisticDocument:
The idea is that in addition to
id = mongoengine.ObjectIdField()
It also has a field
_doc_ver = mongoengine.UUIDField(binary=False)
Then anytime you save an OptimisticDocument, it uses that and updates it only if there are pending changes. Here's a possible implementation (not save_raw is the current Document.save)
def save(self, force_insert=False, validate=True, clean=True,
write_concern=None, cascade=None, cascade_kwargs=None,
_refs=None, save_condition=None, signal_kwargs=None, **kwargs):
if not hasattr(self, '_changed_fields') or not self._changed_fields:
return self._save_raw(force_insert, validate, clean,
write_concern, cascade, cascade_kwargs,
_refs, save_condition, signal_kwargs, **kwargs)
print(self._changed_fields)
op_save_condition = {'_doc_ver': str(self._doc_ver)}
self._doc_ver = str(uuid.uuid4())
print("Changed doc ver from {} to {}".format(op_save_condition.get('_doc_ver'), self._doc_ver))
if save_condition:
if not isinstance(save_condition, dict):
raise ValueError("save_condition must be empty or a dictionary")
save_condition.update(op_save_condition)
op_save_condition = save_condition
self._save_raw(force_insert, validate, clean,
write_concern, cascade, cascade_kwargs,
_refs, op_save_condition, signal_kwargs, **kwargs)
To see it in action, you can run these two attached files. But they are highly hacked due to the fact that I couldn't subclass Document.
- Run the code straight through, and it works fine.
- Run the code, stop at the input, change the underlying
_doc_verdirectly in the db, continue the program, see the save is stopped.
optimistic_document_example.zip
Thanks,
Michael
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing Document.save and the attached optimistic_document_example.zip to understand the proposed subclass and save_condition behavior. The work is done when the project has a supported OptimisticDocument design and tests showing that changed documents reject saves after the stored _doc_ver changes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, python
- Domain
- backend, databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100