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

CRDT presentation on 07-02-2020 #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
76 changes: 76 additions & 0 deletions crdts/Code/CRDTs.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{258C7F2F-56B9-4AAD-B60E-6599B4C5AB88}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CRDTs</RootNamespace>
<AssemblyName>CRDTs</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="GCounter.cs" />
<Compile Include="GSet.cs" />
<Compile Include="LWWRegister.cs" />
<Compile Include="MVRegister.cs" />
<Compile Include="OpBasedGCounter.cs" />
<Compile Include="OpBasedORSet.cs" />
<Compile Include="PNCounter.cs" />
<Compile Include="Tests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" />
</Project>
50 changes: 50 additions & 0 deletions crdts/Code/GCounter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace CRDTs
{
class GCounter
{
private int index;
public int[] State = new int[10];


public GCounter(int index)
{
this.index = index;
}

/// <summary>
/// Query method
/// </summary>
public int Query()
{
int sum = 0;
for (int i = 0; i < 10; i++)
{
sum += State[i];
}

return sum;
}

/// <summary>
/// Update method
/// </summary>
public void Increment()
{
State[this.index]++;
}

/// <summary>
/// Merge method
/// </summary>
public void Merge(GCounter gCounter2)
{
for (int i = 0; i < 10; i++)
{
this.State[i] = Math.Max(this.State[i], gCounter2.State[i]);
}
}
}
}
33 changes: 33 additions & 0 deletions crdts/Code/GSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CRDTs
{
class GSet
{
int index;
public HashSet<int> hs = new HashSet<int>();
public GSet(int index)
{
this.index = index;
}

public bool Query(int a)
{
return hs.Contains(a);
}

public void AddItem(int a)
{
hs.Add(a);
}

public void Merge(GSet g)
{
hs.UnionWith(g.hs);
}
}
}
51 changes: 51 additions & 0 deletions crdts/Code/LWWRegister.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CRDTs
{
class LWWRegister
{
private int index;

public DateTime TimeStamp { get; set; }
public int Value { get; set; }


public LWWRegister(int index)
{
this.index = index;
}

/// <summary>
/// Query method
/// </summary>
public int Query()
{
return this.Value;
}

/// <summary>
/// Update method
/// </summary>
public void SetValue(int value)
{
this.TimeStamp = DateTime.Now;
this.Value = value;
}

/// <summary>
/// Merge method
/// </summary>
public void Merge(LWWRegister lWWRegister)
{
if (lWWRegister.TimeStamp > this.TimeStamp)
{
this.Value = lWWRegister.Value;
}
}

}
}
31 changes: 31 additions & 0 deletions crdts/Code/MVRegister.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CRDTs
{
class MVRegister
{
int index;

int curValue = -1;

List<int> causalHistory;


public MVRegister(int index)
{
this.index = index;
}

public void Upate(int value)
{
causalHistory.Add(curValue);
this.curValue = value;
}


}
}
62 changes: 62 additions & 0 deletions crdts/Code/OpBasedGCounter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CRDTs
{
public class OpBasedGCounter
{
int index;
int value =0;
List<Guid> alreadySeen = new List<Guid>();

public OpBasedGCounter(int index)
{
this.index = index;
}

public int Query()
{
return value;
}

public Message Increment()
{
Message m = Prepare();
Effect(m);
return m;
}

public Message Prepare()
{
Action<OpBasedGCounter> A = (opBasedGCounter) => opBasedGCounter.value = (opBasedGCounter.value + 1);
return new Message(Guid.NewGuid(), A);
}

public void Effect(Message m)
{
// to make sure We do not process already seen message We need to keep track of message already seen.
if (alreadySeen.Contains(m.OperationId))
{
return;
}

m.A(this);

alreadySeen.Add(m.OperationId);
}
}

public class Message
{
public Guid OperationId;

public Action<OpBasedGCounter> A;

public Message(Guid operationId, Action<OpBasedGCounter> a) =>
(OperationId, A) = (operationId, a);
}

}
88 changes: 88 additions & 0 deletions crdts/Code/OpBasedORSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace CRDTs
{
public class OpBasedORSet
{
int index;

HashSet<Element> allElements = new HashSet<Element>();

public OpBasedORSet(int index)
{
this.index = index;
}

public HashSet<int> Query()
{
HashSet<int> uniqueElements = new HashSet<int>();
foreach (var element in allElements)
{
uniqueElements.Add(element.A);
}
return uniqueElements;
}

public ORAddSetMessage Add(int a)
{
ORAddSetMessage oRAddSetMessage = PrepareAdd(a);
EffectAdd(oRAddSetMessage);
return oRAddSetMessage;
}

public void EffectAdd(ORAddSetMessage m)
{
allElements.Add(new Element(m.A, m.ElementGuid));
}

public ORRemoveSetMessage Remove(int a)
{
ORRemoveSetMessage oRAddSetMessage = PrepareRemove(a);
EffectRemove(oRAddSetMessage);
return oRAddSetMessage;
}

public void EffectRemove(ORRemoveSetMessage oRAddSetMessage)
{
allElements.RemoveWhere(x => oRAddSetMessage.Elements.Contains(x));
}

private ORRemoveSetMessage PrepareRemove(int a)
{
return new ORRemoveSetMessage(allElements.Where(x => x.A == a).ToList());
}

private ORAddSetMessage PrepareAdd(int a)
{
Guid g = new Guid();
return new ORAddSetMessage(a,g);
}
}

public class Element
{
public int A { get; }

public Guid ElementGuid { get; }
public Element(int a, Guid elementGuid) => (A, ElementGuid) = (a, elementGuid);
}

public class ORAddSetMessage
{
public int A { get; }

public Guid ElementGuid { get; }

public ORAddSetMessage(int a, Guid elementGuid) => (A, ElementGuid) = (a, elementGuid);
}

public class ORRemoveSetMessage
{
public List<Element> Elements { get; }

public ORRemoveSetMessage(List<Element> elements) => (Elements) = (elements);
}

}
Loading