Created
July 12, 2012 19:39
-
-
Save darkiri/3100418 to your computer and use it in GitHub Desktop.
Optimistic Concurrency MongoDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Subject("Mongo")] | |
public class when_issuing_an_update_on_a_changed_entity : mongo_context | |
{ | |
private static long DocumentsAffected; | |
private Establish context = () => SetUpDatabase(); | |
private Because of = () => DocumentsAffected = UpdateSameObjectTwice(); | |
private It should_not_affect_any_documents = () => Assert.That(DocumentsAffected, Is.EqualTo(0)); | |
private static long UpdateSameObjectTwice() | |
{ | |
var events = db.GetCollection("events"); | |
var oldRevision = events.AsQueryable().First().Revision; | |
SomeoneChangedDocument(); | |
var res = events.Update( | |
WhereDocHasRevision(oldRevision), | |
Update.Set("Revision", new BsonInt32(100)), | |
SafeMode.True); | |
return res.DocumentsAffected; | |
} | |
private static void SomeoneChangedDocument() | |
{ | |
var events = db.GetCollection("events"); | |
var evt = events.AsQueryable().First(); | |
evt.Revision++; | |
events.Save(evt); | |
} | |
private static IMongoQuery WhereDocHasRevision(int oldRevision) | |
{ | |
return Query.And( | |
Query.EQ("_id", "1"), | |
Query.EQ("Revision", new BsonInt32(oldRevision)) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment