RiSE4fun samples for Concurrent RevisionsList of built-in samples for the Concurrent Revisions in RiSE4funen-US<a target='_blank' href='https://go.microsoft.com/?linkid=2028325'>Contact Us</a>| <a target='_blank' href='https://go.microsoft.com/fwlink/?LinkId=521839'>Privacy &amp; Cookies</a> | <a target='_blank' href='https://go.microsoft.com/fwlink/?LinkID=246338'>Terms of Use</a> | <a target='_blank' href='https://go.microsoft.com/fwlink/?LinkId=506942 '>Trademarks</a>| &copy; 2017 Microsofthttps://rise4fun.com//Images/Rise.gifRiSE4fun samples for Concurrent Revisionshttps://rise4fun.com/Revisions/HelloHellousing System; using Microsoft.Revisions; class Sample { [Versioned] string s; // a versioned string public void Run() { // Fork a revision: conceptually all versioned state is copied and each revision is fully isolated var r = CurrentRevision.Fork(() => { s = "world"; // write to s in the forked revision }); s = "Hello"; // and write to s in the main revision Console.WriteLine(s); // revision are isolated so it always shows 'hello' CurrentRevision.Join(r); // writes are merged back on the join Console.WriteLine(s); // by default writes in the child win when conflicting (i.e. 'world') } } class Program { static void Main(string[] args) { new Sample().Run(); } } https://rise4fun.com/Revisions/VersionedIntVersionedIntusing System; using Microsoft.Revisions; class Sample { [Versioned] int x = 0; [Versioned] int y = 0; public void Run() { // Question: what would be the possible outcomes without [Versioned] on x and y ? // and what if we would have used atomic blocks? // Fork a revision: conceptually all versioned state is copied and each revision is fully isolated RevisionTask r = CurrentRevision.Fork(() => { if (x==0) y++; // each revision is isolated, so x will be 0, and y incremented }); if (y==0) x++; // each revision is isolated, so y will be 0, and x incremented CurrentRevision.Join(r); // on the join, all changes are merged back Console.WriteLine("(x,y) == (" + x + "," + y + ")"); } } class Program { static void Main(string[] args) { new Sample().Run(); } } https://rise4fun.com/Revisions/HelloWorldHelloWorld// This sample shows how to add custom merge function to resolve write-write conflicts using System; using Microsoft.Revisions; class Sample { [Versioned,MergeWith("MergeString")] string s; static string MergeString( string current, string join, string original ) { Console.WriteLine("[merging '" + current + "' and '" + join + "']"); return current + join; } public void Run() { // Fork a revision: conceptually all versioned state is copied and each revision is fully isolated var r = CurrentRevision.Fork(() => { s = " world"; }); s = "hello"; CurrentRevision.Join(r); // on the join, the merge function is called to resolve conflicting writes Console.WriteLine(s); // the strings have merged now } } class Program { static void Main(string[] args) { new Sample().Run(); } } https://rise4fun.com/Revisions/CumulativeIntCumulativeInt// This sample shows how to define cumulative merge functions on additive integers using System; using Microsoft.Revisions; class Sample { [Versioned,MergeWith("MergeInt")] int i = 0; static int MergeInt(int current, int join, int original) { // add the values of the current and joined revision, and subtract the original (or we would count it twice) return current + join - original; } public void Run() { RevisionTask r2 = null; RevisionTask r1 = CurrentRevision.Fork(() => { i += 2; r2 = CurrentRevision.Fork(() => { i += 3; }); }); i += 1; Console.WriteLine("i = " + i ); CurrentRevision.Join(r1); Console.WriteLine("i = " + i ); CurrentRevision.Join(r2); Console.WriteLine("i = " + i ); // note: the i+=2 is not counted twice } } class Program { static void Main(string[] args) { new Sample().Run(); } } https://rise4fun.com/Revisions/FlakyCodeFlakyCode// This sample demonstrates how to use revisions without concurrency // This can be used for example to 'sandbox' code that may raise exceptions and leave the state indeterminate. using System; using Microsoft.Revisions; class Sample { [Versioned] string s = "Hello"; public void Run() { // Branch off a revision without forking a task Revision r = CurrentRevision.Branch(); try { // run code using revision r r.Run( () => { // suppose this is bad code that modifies state indeterminately and raises an exception s = "Flaky Code"; throw new Exception("Oops, flaky code"); }); CurrentRevision.Merge(r); // Merge back changes in r on success. } catch { CurrentRevision.Abandon(r); // Abandon r if an exception was raised. } Console.WriteLine(s); // Is always "Hello" } } class Program { static void Main(string[] args) { new Sample().Run(); } }