Exemplo n.º 1
0
 public IDisposable DisableAllCaching()
 {
     return(databaseCommands.DisableAllCaching());
 }
Exemplo n.º 2
0
        private RangeValue GetNextRange(IDatabaseCommands databaseCommands)
        {
#if !DNXCORE50
            using (new TransactionScope(TransactionScopeOption.Suppress))
                using (RavenTransactionAccessor.SupressExplicitRavenTransaction())
#endif
            using (databaseCommands.ForceReadFromMaster())
                using (databaseCommands.DisableAllCaching())
                {
                    // we need the latest value of the capacity
                    var calculatedCapacity = Interlocked.Read(ref capacity);
                    ModifyCapacityIfRequired(ref calculatedCapacity);

                    while (true)
                    {
                        try
                        {
                            var          minNextMax = Range.Max;
                            JsonDocument document;

                            try
                            {
                                document = GetDocument(databaseCommands);
                            }
                            catch (ConflictException e)
                            {
                                // resolving the conflict by selecting the highest number
                                var highestMax = e.ConflictedVersionIds
                                                 .Select(conflictedVersionId => GetMaxFromDocument(databaseCommands.Get(conflictedVersionId), minNextMax, calculatedCapacity))
                                                 .Max();

                                PutDocument(databaseCommands, new JsonDocument
                                {
                                    Etag       = e.Etag,
                                    Metadata   = new RavenJObject(),
                                    DataAsJson = RavenJObject.FromObject(new { Max = highestMax }),
                                    Key        = HiLoDocumentKey
                                });

                                continue;
                            }

                            IncreaseCapacityIfRequired(ref calculatedCapacity);

                            long min, max;
                            if (document == null)
                            {
                                min      = minNextMax + 1;
                                max      = minNextMax + calculatedCapacity;
                                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, calculatedCapacity);
                                min = oldMax + 1;
                                max = oldMax + calculatedCapacity;

                                document.DataAsJson["Max"] = max;
                            }
                            PutDocument(databaseCommands, document);

                            return(new RangeValue(min, max));
                        }
                        catch (ConcurrencyException)
                        {
                            // expected, we need to retry
                            // we'll try to increase the capacity
                            ModifyCapacityIfRequired(ref calculatedCapacity);
                        }
                    }
                }
        }