Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Sequential Number Generator for RavenDB
private static readonly object GeneratorLock = new object();
///<summary>
/// Create the next id (numeric)
///</summary>
private int NextAccountNumber()
{
lock (GeneratorLock)
{
using (new TransactionScope(TransactionScopeOption.Suppress))
{
while (true)
{
try
{
var document = GetDocument();
if (document == null)
{
PutDocument(new JsonDocument {
Etag = Guid.Empty, // sending empty guid means - ensure the that the document does NOT exists
Metadata = new RavenJObject(),
DataAsJson = RavenJObject.FromObject(new { Current = 1000 }),
Key = "Raven/InvoiceNumber"
});
return 1000;
}
int current;
current = document.DataAsJson.Value<int>("Current");
current++;
document.DataAsJson["Current"] = current;
PutDocument(document);
return current;
}
catch (ConcurrencyException)
{
// expected, we need to retry
}
}
}
}
}
private void PutDocument(JsonDocument document)
{
DocumentStore.DatabaseCommands.Put(
"Raven/InvoiceNumber",
document.Etag,
document.DataAsJson,
document.Metadata);
}
private JsonDocument GetDocument()
{
return DocumentStore.DatabaseCommands.Get("Raven/InvoiceNumber");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.