forked from ravendb/demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HRINT-2431 [Demo] Compare Exchange: Create CmpXchg Item & Index CmpXc…
…hg Values + Upgrade RavenDB.Client package to 5.3.1
- Loading branch information
1 parent
da46915
commit babc716
Showing
16 changed files
with
534 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
70 changes: 70 additions & 0 deletions
70
...ontrollers/Demos/CompareExchange/CreateCompareExchange/CreateCompareExchangeController.cs
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
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; } | ||
} | ||
} | ||
} |
160 changes: 160 additions & 0 deletions
160
DemoServer/Controllers/Demos/CompareExchange/CreateCompareExchange/metadata.json
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
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" | ||
} | ||
] | ||
} | ||
] | ||
} |
78 changes: 78 additions & 0 deletions
78
.../Controllers/Demos/CompareExchange/IndexCompareExchange/IndexCompareExchangeController.cs
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
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; } | ||
} | ||
} | ||
} |
Oops, something went wrong.