Skip to content

Commit

Permalink
HRINT-2431 [Demo] Compare Exchange: Create CmpXchg Item & Index CmpXc…
Browse files Browse the repository at this point in the history
…hg Values + Upgrade RavenDB.Client package to 5.3.1
  • Loading branch information
Danielle9897 committed Jan 5, 2022
1 parent da46915 commit babc716
Show file tree
Hide file tree
Showing 16 changed files with 534 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DemoCommon/DemoCommon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="RavenDB.Client" Version="4.2.119" />
<PackageReference Include="RavenDB.Client" Version="5.3.1" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion DemoCron/DemoCron.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="3.1.22" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.22" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.5.0" />
<PackageReference Include="RavenDB.Client" Version="4.2.119" />
<PackageReference Include="RavenDB.Client" Version="5.3.1" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion DemoParser.Tests/DemoParser.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="RavenDB.Client" Version="4.2.119" />
<PackageReference Include="RavenDB.Client" Version="5.3.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using DemoServer.Utils.Cache;
using DemoServer.Utils.Database;
using DemoServer.Utils.UserId;
using Microsoft.AspNetCore.Mvc;
#region Usings
using Raven.Client.Documents.Operations.CompareExchange;
#endregion

namespace DemoServer.Controllers.Demos.CompareExchange.CreateCompareExchange
{
public class CreateCompareExchangeController : DemoCodeController
{
public CreateCompareExchangeController(UserIdContainer userId, UserStoreCache userStoreCache, MediaStoreCache mediaStoreCache,
DatabaseSetup databaseSetup) : base(userId, userStoreCache, mediaStoreCache, databaseSetup)
{
}

[HttpPost]
public IActionResult Run(RunParams runParams)
{
string cmpXchgKey = runParams.CmpXchgKey ?? "[email protected]";
string cmpXchgValue = runParams.CmpXchgValue?? "employee/1-A";

string result = null;

#region Demo
#region Step_1
var putCmpXchgOperation =
new PutCompareExchangeValueOperation<string>(cmpXchgKey, cmpXchgValue, 0);

CompareExchangeResult<string> putCmpXchgResult =
DocumentStoreHolder.Store.Operations.Send(putCmpXchgOperation);
#endregion

#region Step_2
var success = putCmpXchgResult.Successful;
var putValue = putCmpXchgResult.Value;
var putVersion = putCmpXchgResult.Index;

if (success == false)
result = "Key already exists";
#endregion

#region Step_3
var getCmpXchgOperation =
new GetCompareExchangeValueOperation<string>(cmpXchgKey);

CompareExchangeValue<string> getCmpXchgResult =
DocumentStoreHolder.Store.Operations.Send(getCmpXchgOperation);
#endregion

#region Step_4
var key = getCmpXchgResult.Key;
var value = getCmpXchgResult.Value;
var valueVersion = getCmpXchgResult.Index;
var metadata = getCmpXchgResult.Metadata;
#endregion
#endregion

result = result?? $"Created a new Compare-Exchange Key: {key}, Value: {value}, Value Version: {valueVersion}";
return Ok(result);
}

public class RunParams
{
public string CmpXchgKey { get; set; }
public string CmpXchgValue { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
{
"Slug": "create-compare-exchange",
"SourceFileName": "CreateCompareExchangeController.cs",
"Title": "Create CmpXchg Item",
"StudioLinkToMediaDatabase": true,
"DescriptionLines": [
"**Create a CmpXchg Item** to ensure having a unique key in your database.",
"Uniqueness is ensured cluster-wide, across all your database-group nodes.",
"",
"Each CmpXchg item has:<br>",
"",
"<p>* Key - A unique identifier<br>",
"* Value - An associated value<br>",
"* Index - The value's version number<br>",
"* Metadata - An optional dictionary<p>",
"",
"Creating & modifying CmpXchg items are **interlocked compare-exchange operations**.",
"The feature allows performing atomic compare and swap operations at the cluster level."
],

"Assets": [
{
"Title": "Distributed Compare-Exchange operations",
"Type" : "link",
"Url" : "https://ravendb.net/learn/inside-ravendb-book/reader/4.0/2-zero-to-ravendb#distributed-compare-exchange-operations-with-ravendb"
},
{
"Title": "Atomic Compare-Exchange operations",
"Type" : "link",
"Url" : "https://ravendb.net/learn/inside-ravendb-book/reader/4.0/6-ravendb-clusters#distributed-atomic-compare-exchange-operations"
},
{
"Title": "Compare-Exchange overview",
"Type" : "link",
"Url" : "https://ravendb.net/docs/article-page/latest/csharp/client-api/operations/compare-exchange/overview"
},
{
"Title": "Put Compare-Exchange",
"Type" : "link",
"Url" : "https://ravendb.net/docs/article-page/latest/csharp/client-api/operations/compare-exchange/put-compare-exchange-value"
},
{
"Title": "Get Compare-Exchange",
"Type" : "link",
"Url" : "https://ravendb.net/docs/article-page/5.3/csharp/client-api/operations/compare-exchange/get-compare-exchange-values"
},
{
"Title": "Compare-Exchange operations - Blog",
"Type" : "link",
"Url" : "https://ayende.com/blog/182948-C/distributed-compare-exchange-operations-with-ravendb"
},
{
"Title": "Distributed transactions - Blog",
"Type" : "link",
"Url" : "https://ayende.com/blog/190978-B/complex-distributed-transactions-with-ravendb"
},
{
"Title": "Simplifying cluster wide transactions - Blog",
"Type" : "link",
"Url" : "https://ayende.com/blog/194405-A/ravendb-5-2-simplifying-atomic-cluster-wide-transactions"
}
],

"Walkthroughs": [
{
"Title": "Put compare-exchange item",
"Slug": "step-1",
"DescriptionLines": [
"* Use `PutCompareExchangeValueOperation` to define the Put operation. Pass:",
" * Key - a unique string in the database scope across the cluster",
" * Value - can be a number/string/boolean/array or any JSON object",
" * Value Version (Index) - pass _0_ to indicate that this is a request to create a new item.",
"",
"* Send the operation to the document store using `Store.Operations.Send()`.",
"",
"* Variable _putCmpXchgResult_ will contain the operation result (see next step).<br>",
" The new CmpXchg item will be created only if the Key passed doesn't exist yet.",
"",
"* Note:<br>",
" Since the compare-exchange operation guarantees atomicity across the entire cluster,<br>",
" the session object is not used as session transactions span only a single node.<br>",
" In the case where operation is used inside a session, it will Not be rolled back if the session.SaveChanges has failed."
],
"Assets": [
{
"Title": "Compare-Exchange overview",
"Type" : "link",
"Url" : "https://ravendb.net/docs/article-page/latest/csharp/client-api/operations/compare-exchange/overview"
},
{
"Title": "Put Compare-Exchange",
"Type" : "link",
"Url" : "https://ravendb.net/docs/article-page/latest/csharp/client-api/operations/compare-exchange/put-compare-exchange-value"
}
]
},
{
"Title": "The Put result",
"Slug": "step-2",
"DescriptionLines": [
"* Check _putCmpXchgResult_ for the Put operation result.",
"",
"* In addition to the Value itself, the result includes the value-version number (index) assigned in this operation,",
" which is a unique sequential number assigned to the value per any change."
],
"Assets": [
{
"Title": "Compare-Exchange overview",
"Type" : "link",
"Url" : "https://ravendb.net/docs/article-page/latest/csharp/client-api/operations/compare-exchange/overview"
},
{
"Title": "Put Compare-Exchange",
"Type" : "link",
"Url" : "https://ravendb.net/docs/article-page/latest/csharp/client-api/operations/compare-exchange/put-compare-exchange-value"
}
]
},
{
"Title": "Get compare-exchange item",
"Slug": "step-3",
"DescriptionLines": [
"* Use `GetCompareExchangeValueOperation` to define the Get by Key operation.",
"",
"* Send the operation to the document store using `Store.Operations.Send()`.",
"",
"* Variable _getCmpXchgResult_ will contain the matching CmpXchg item.<br>"
],
"Assets": [
{
"Title": "Compare-Exchange overview",
"Type" : "link",
"Url" : "https://ravendb.net/docs/article-page/latest/csharp/client-api/operations/compare-exchange/overview"
},
{
"Title": "Get Compare-Exchange",
"Type" : "link",
"Url" : "https://ravendb.net/docs/article-page/5.3/csharp/client-api/operations/compare-exchange/get-compare-exchange-values"
}
]
},
{
"Title": "The compare-exchage item",
"Slug": "step-4",
"DescriptionLines": [
"* Key - CmpXchg item key",
"* Value - Current associated value",
"* Index - Current version number of the value",
"* Metadata - Metadata dictionary for this key"
],
"Assets": [
{
"Title": "Compare-Exchange overview",
"Type" : "link",
"Url" : "https://ravendb.net/docs/article-page/latest/csharp/client-api/operations/compare-exchange/overview"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Threading.Tasks;
using DemoCommon.Models;
using DemoServer.Utils.Cache;
using DemoServer.Utils.Database;
using DemoServer.Utils.UserId;
using Microsoft.AspNetCore.Mvc;
#region Usings
using System.Linq;
using System.Collections.Generic;
using Raven.Client.Documents.Linq;
using Raven.Client.Documents.Session;
using Raven.Client.Documents.Indexes;
#endregion

namespace DemoServer.Controllers.Demos.CompareExchange.IndexCompareExchange
{
public class IndexCompareExchangeController : DemoCodeController
{
public IndexCompareExchangeController(UserIdContainer userId, UserStoreCache userStoreCache, MediaStoreCache mediaStoreCache,
DatabaseSetup databaseSetup) : base(userId, userStoreCache, mediaStoreCache, databaseSetup)
{
}

#region Demo
#region Step_1
public class Products_byUnitsInStock : AbstractIndexCreationTask<Product, Products_byUnitsInStock.IndexEntry>
#endregion
{
#region Step_2
public class IndexEntry
{
public int UnitsInStock { get; set; }
}
#endregion

#region Step_3
public Products_byUnitsInStock()
{
Map = products => from product in products
select new IndexEntry
{
UnitsInStock = LoadCompareExchangeValue<int>(product.Id)
};
}
#endregion
}
#endregion

protected override Task SetDemoPrerequisites() => new Products_byUnitsInStock().ExecuteAsync(DocumentStoreHolder.Store);

[HttpPost]
public IActionResult Run(RunParams runParams)
{
int minValue = runParams.MinValue?? 25;

#region Demo
List<Product> products;

using (IDocumentSession session = DocumentStoreHolder.Store.OpenSession())
{
#region Step_4
products = session.Query<Products_byUnitsInStock.IndexEntry, Products_byUnitsInStock>()
.Where(indexEntry => indexEntry.UnitsInStock > minValue)
.OfType<Product>()
.ToList();
#endregion
}
#endregion

return Ok(products);
}

public class RunParams
{
public int? MinValue { get; set; }
}
}
}
Loading

0 comments on commit babc716

Please sign in to comment.