Beispiel #1
0
        public virtual async Task <bool> IsUpToDateAsync(ISet <string> spaces, long timestamp /* H2.1 has Long here */, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (await _isUpToDate.LockAsync())
            {
                if (_batchReadOnlyUpdateTimestamps != null)
                {
                    if (spaces.Count == 0)
                    {
                        return(true);
                    }

                    var keys  = new object[spaces.Count];
                    var index = 0;
                    foreach (var space in spaces)
                    {
                        keys[index++] = space;
                    }
                    var lastUpdates = await(_batchReadOnlyUpdateTimestamps.GetManyAsync(keys, cancellationToken)).ConfigureAwait(false);
                    return(lastUpdates.All(lastUpdate => !IsOutdated(lastUpdate as long?, timestamp)));
                }

                return(spaces.Select(space => updateTimestamps.Get(space))
                       .All(lastUpdate => !IsOutdated(lastUpdate as long?, timestamp)));
            }
        }
Beispiel #2
0
            public override async Task <object> GenerateAsync(IAccessCallback callback, CancellationToken cancellationToken)
            {
                cancellationToken.ThrowIfCancellationRequested();
                using (await _generate.LockAsync())
                {
                    if (_lastSourceValue < 0)
                    {
                        _lastSourceValue = await(callback.GetNextValueAsync(cancellationToken)).ConfigureAwait(false);
                        while (_lastSourceValue <= 0)
                        {
                            _lastSourceValue = await(callback.GetNextValueAsync(cancellationToken)).ConfigureAwait(false);
                        }

                        // upperLimit defines the upper end of the bucket values
                        _upperLimit = (_lastSourceValue * IncrementSize) + 1;

                        // initialize value to the low end of the bucket
                        _value = _upperLimit - IncrementSize;
                    }
                    else if (_upperLimit <= _value)
                    {
                        _lastSourceValue = await(callback.GetNextValueAsync(cancellationToken)).ConfigureAwait(false);
                        _upperLimit      = (_lastSourceValue * IncrementSize) + 1;
                    }
                    return(Make(_value++));
                }
            }
        public override async Task <object> GenerateAsync(ISessionImplementor session, object obj, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (await _generate.LockAsync())
            {
                if (maxLo < 1)
                {
                    //keep the behavior consistent even for boundary usages
                    long val = Convert.ToInt64(await(base.GenerateAsync(session, obj, cancellationToken)).ConfigureAwait(false));
                    if (val == 0)
                    {
                        val = Convert.ToInt64(await(base.GenerateAsync(session, obj, cancellationToken)).ConfigureAwait(false));
                    }
                    return(IdentifierGeneratorFactory.CreateNumber(val, returnClass));
                }

                if (lo > maxLo)
                {
                    long hival = Convert.ToInt64(await(base.GenerateAsync(session, obj, cancellationToken)).ConfigureAwait(false));
                    lo = (hival == 0) ? 1 : 0;
                    hi = hival * (maxLo + 1);
                    if (log.IsDebugEnabled)
                    {
                        log.Debug("new hi value: " + hival);
                    }
                }
                return(IdentifierGeneratorFactory.CreateNumber(hi + lo++, returnClass));
            }
        }
Beispiel #4
0
            public override async Task <object> GenerateAsync(IAccessCallback callback, CancellationToken cancellationToken)
            {
                cancellationToken.ThrowIfCancellationRequested();
                using (await _generate.LockAsync())
                {
                    if (_hiValue < 0)
                    {
                        _value = await(callback.GetNextValueAsync(cancellationToken)).ConfigureAwait(false);
                        if (_value < 1)
                        {
                            // unfortunately not really safe to normalize this
                            // to 1 as an initial value like we do the others
                            // because we would not be able to control this if
                            // we are using a sequence...
                            Log.Info("pooled optimizer source reported [" + _value + "] as the initial value; use of 1 or greater highly recommended");
                        }

                        if ((_initialValue == -1 && _value < IncrementSize) || _value == _initialValue)
                        {
                            _hiValue = await(callback.GetNextValueAsync(cancellationToken)).ConfigureAwait(false);
                        }
                        else
                        {
                            _hiValue = _value;
                            _value   = _hiValue - IncrementSize;
                        }
                    }
                    else if (_value >= _hiValue)
                    {
                        _hiValue = await(callback.GetNextValueAsync(cancellationToken)).ConfigureAwait(false);
                        _value   = _hiValue - IncrementSize;
                    }
                    return(Make(_value++));
                }
            }
Beispiel #5
0
 public virtual async Task <object> GenerateAsync(ISessionImplementor session, object obj, CancellationToken cancellationToken)
 {
     cancellationToken.ThrowIfCancellationRequested();
     using (await _generate.LockAsync())
     {
         return(await(Optimizer.GenerateAsync(new TableAccessCallback(session, this), cancellationToken)).ConfigureAwait(false));
     }
 }
Beispiel #6
0
        public virtual async Task <bool[]> AreUpToDateAsync(ISet <string>[] spaces, long[] timestamps, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (await _areUpToDate.LockAsync())
            {
                var results   = new bool[spaces.Length];
                var allSpaces = new HashSet <string>();
                foreach (var sp in spaces)
                {
                    allSpaces.UnionWith(sp);
                }

                if (_batchReadOnlyUpdateTimestamps != null)
                {
                    if (allSpaces.Count == 0)
                    {
                        for (var i = 0; i < spaces.Length; i++)
                        {
                            results[i] = true;
                        }

                        return(results);
                    }

                    var keys  = new object[allSpaces.Count];
                    var index = 0;
                    foreach (var space in allSpaces)
                    {
                        keys[index++] = space;
                    }

                    index = 0;
                    var lastUpdatesBySpace =
                        (await(_batchReadOnlyUpdateTimestamps
                               .GetManyAsync(keys, cancellationToken)).ConfigureAwait(false))
                        .ToDictionary(u => keys[index++], u => u as long?);

                    for (var i = 0; i < spaces.Length; i++)
                    {
                        var timestamp = timestamps[i];
                        results[i] = spaces[i].All(space => !IsOutdated(lastUpdatesBySpace[space], timestamp));
                    }
                }
                else
                {
                    for (var i = 0; i < spaces.Length; i++)
                    {
                        results[i] = await(IsUpToDateAsync(spaces[i], timestamps[i], cancellationToken)).ConfigureAwait(false);
                    }
                }

                return(results);
            }
        }
Beispiel #7
0
 public virtual async Task <object> GenerateAsync(ISessionImplementor session, object obj, CancellationToken cancellationToken)
 {
     cancellationToken.ThrowIfCancellationRequested();
     using (await _generate.LockAsync())
     {
         // This has to be done using a different connection to the containing
         // transaction becase the new hi value must remain valid even if the
         // containing transaction rolls back.
         return(await(DoWorkInNewTransactionAsync(session, cancellationToken)).ConfigureAwait(false));
     }
 }
Beispiel #8
0
        public virtual async Task PreInvalidateAsync(IReadOnlyCollection <string> spaces, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (await _preInvalidate.LockAsync())
            {
                //TODO: to handle concurrent writes correctly, this should return a Lock to the client
                var ts = updateTimestamps.NextTimestamp() + updateTimestamps.Timeout;
                await(SetSpacesTimestampAsync(spaces, ts, cancellationToken)).ConfigureAwait(false);

                //TODO: return new Lock(ts);
            }

            //TODO: return new Lock(ts);
        }
Beispiel #9
0
        public virtual async Task <bool> IsUpToDateAsync(ISet <string> spaces, long timestamp /* H2.1 has Long here */, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (await _isUpToDate.LockAsync())
            {
                if (spaces.Count == 0)
                {
                    return(true);
                }

                var lastUpdates = await(_updateTimestamps.GetManyAsync(spaces.ToArray <object>(), cancellationToken)).ConfigureAwait(false);
                return(lastUpdates.All(lastUpdate => !IsOutdated(lastUpdate as long?, timestamp)));
            }
        }
Beispiel #10
0
 public virtual async Task InvalidateAsync(IReadOnlyCollection <string> spaces, CancellationToken cancellationToken)
 {
     cancellationToken.ThrowIfCancellationRequested();
     using (await _invalidate.LockAsync())
     {
         //TODO: to handle concurrent writes correctly, the client should pass in a Lock
         long ts = updateTimestamps.NextTimestamp();
         //TODO: if lock.getTimestamp().equals(ts)
         if (log.IsDebugEnabled())
         {
             log.Debug("Invalidating spaces [{0}]", StringHelper.CollectionToString(spaces));
         }
         await(SetSpacesTimestampAsync(spaces, ts, cancellationToken)).ConfigureAwait(false);
     }
 }
Beispiel #11
0
 public override async Task <object> GenerateAsync(IAccessCallback callback, CancellationToken cancellationToken)
 {
     cancellationToken.ThrowIfCancellationRequested();
     using (await _generate.LockAsync())
     {
         if (_lastSourceValue < 0 || _value >= (_lastSourceValue + IncrementSize))
         {
             _lastSourceValue = await(callback.GetNextValueAsync(cancellationToken)).ConfigureAwait(false);
             _value           = _lastSourceValue;
             // handle cases where initial-value is less than one (hsqldb for instance).
             while (_value < 1)
             {
                 _value++;
             }
         }
         return(Make(_value++));
     }
 }
Beispiel #12
0
        public virtual async Task <bool[]> AreUpToDateAsync(ISet <string>[] spaces, long[] timestamps, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (await _areUpToDate.LockAsync())
            {
                if (spaces.Length == 0)
                {
                    return(Array.Empty <bool>());
                }

                var allSpaces = new HashSet <string>();
                foreach (var sp in spaces)
                {
                    allSpaces.UnionWith(sp);
                }

                if (allSpaces.Count == 0)
                {
                    return(ArrayHelper.Fill(true, spaces.Length));
                }

                var keys = allSpaces.ToArray <object>();

                var index = 0;
                var lastUpdatesBySpace =
                    (await(_updateTimestamps
                           .GetManyAsync(keys, cancellationToken)).ConfigureAwait(false))
                    .ToDictionary(u => keys[index++], u => u as long?);

                var results = new bool[spaces.Length];
                for (var i = 0; i < spaces.Length; i++)
                {
                    var timestamp = timestamps[i];
                    results[i] = spaces[i].All(space => !IsOutdated(lastUpdatesBySpace[space], timestamp));
                }

                return(results);
            }
        }