Exemple #1
0
        private async Task <RangeValue> GetNextMaxAsyncInner(IAsyncDatabaseCommands databaseCommands)
        {
            var minNextMax = Range.Max;

            using (databaseCommands.ForceReadFromMaster())
                while (true)
                {
                    try
                    {
                        ConflictException ce = null;
                        JsonDocument      document;
                        try
                        {
                            document = await GetDocumentAsync(databaseCommands).ConfigureAwait(false);
                        }
                        catch (ConflictException e)
                        {
                            ce       = e;
                            document = null;
                        }
                        if (ce != null)
                        {
                            return(await HandleConflictsAsync(databaseCommands, ce, minNextMax).ConfigureAwait(false));
                        }

                        long min, max;
                        if (document == null)
                        {
                            min      = minNextMax + 1;
                            max      = minNextMax + capacity;
                            document = new JsonDocument
                            {
                                Etag = Etag.Empty,
                                // sending empty etag means - ensure the that the document does NOT exists
                                Metadata   = new RavenJObject(),
                                DataAsJson = RavenJObject.FromObject(new { Max = max }),
                                Key        = HiLoDocumentKey
                            };
                        }
                        else
                        {
                            var oldMax = GetMaxFromDocument(document, minNextMax);
                            min = oldMax + 1;
                            max = oldMax + capacity;

                            document.DataAsJson["Max"] = max;
                        }

                        await PutDocumentAsync(databaseCommands, document).ConfigureAwait(false);

                        return(new RangeValue(min, max));
                    }
                    catch (ConcurrencyException)
                    {
                        //expected & ignored, will retry this
                    }
                }
        }
Exemple #2
0
 private static IAsyncDatabaseCommands SetupCommandsAsync(IAsyncDatabaseCommands databaseCommands, string database, ICredentials credentialsForSession, OpenSessionOptions options)
 {
     if (database != null)
     {
         databaseCommands = databaseCommands.ForDatabase(database);
     }
     if (credentialsForSession != null)
     {
         databaseCommands = databaseCommands.With(credentialsForSession);
     }
     if (options.ForceReadFromMaster)
     {
         databaseCommands.ForceReadFromMaster();
     }
     return(databaseCommands);
 }
Exemple #3
0
 private static IAsyncDatabaseCommands SetupCommandsAsync(IAsyncDatabaseCommands databaseCommands, string database, ICredentials credentialsForSession, OpenSessionOptions options)
 {
     if (database != null)
         databaseCommands = databaseCommands.ForDatabase(database);
     if (credentialsForSession != null)
         databaseCommands = databaseCommands.With(credentialsForSession);
     if (options.ForceReadFromMaster)
         databaseCommands.ForceReadFromMaster();
     return databaseCommands;
 }
        private async Task<RangeValue> GetNextMaxAsyncInner(IAsyncDatabaseCommands databaseCommands)
        {
            var minNextMax = Range.Max;

            using (databaseCommands.ForceReadFromMaster())
            while (true)
            {
                try
                {
                    ConflictException ce = null;
                    JsonDocument document;
                    try
                    {
                        document = await GetDocumentAsync(databaseCommands).ConfigureAwait(false);
                    }
                    catch (ConflictException e)
                    {
                        ce = e;
                        document = null;
                    }
                    if (ce != null)
                        return await HandleConflictsAsync(databaseCommands, ce, minNextMax).ConfigureAwait(false);

                    long min, max;
                    if (document == null)
                    {
                        min = minNextMax + 1;
                        max = minNextMax + capacity;
                        document = new JsonDocument
                        {
                            Etag = Etag.Empty,
                            // sending empty etag means - ensure the that the document does NOT exists
                            Metadata = new RavenJObject(),
                            DataAsJson = RavenJObject.FromObject(new { Max = max }),
                            Key = HiLoDocumentKey
                        };
                    }
                    else
                    {
                        var oldMax = GetMaxFromDocument(document, minNextMax);
                        min = oldMax + 1;
                        max = oldMax + capacity;

                        document.DataAsJson["Max"] = max;
                    }

                    await PutDocumentAsync(databaseCommands, document).ConfigureAwait(false);
                    return new RangeValue(min, max);
                }
                catch (ConcurrencyException)
                {
                    //expected & ignored, will retry this
                }
            }
        }