Beispiel #1
0
        public void Add(long id, BlittableJsonReaderObject result)
        {
            switch (Type)
            {
            case MapResultsStorageType.Tree:
                Slice entrySlice;

                using (Slice.External(_indexContext.Allocator, (byte *)&id, sizeof(long), out entrySlice))
                {
                    var pos = Tree.DirectAdd(entrySlice, result.Size);
                    result.CopyTo(pos);
                }

                break;

            case MapResultsStorageType.Nested:
                var section = GetNestedResultsSection();

                if (_mapReduceContext.ReducePhaseTree.ShouldGoToOverflowPage(_nestedSection.SizeAfterAdding(result)))
                {
                    // would result in an overflow, that would be a space waste anyway, let's move to tree mode
                    MoveExistingResultsToTree(section);
                    Add(id, result);     // now re-add the value
                }
                else
                {
                    section.Add(id, result);
                }
                break;

            default:
                throw new ArgumentOutOfRangeException(Type.ToString());
            }
        }
Beispiel #2
0
        public unsafe void Set(string url, long etag, BlittableJsonReaderObject result)
        {
            var mem = _unmanagedBuffersPool.Allocate(result.Size);

            result.CopyTo((byte *)mem.Address);
            if (Interlocked.Add(ref _totalSize, result.Size) > _maxSize)
            {
                if (_cleanupTask == null)
                {
                    var cleanup = new Task(FreeSpace);
                    if (Interlocked.CompareExchange(ref _cleanupTask, cleanup, null) == null)
                    {
                        cleanup.ContinueWith(_ => Interlocked.Exchange(ref _cleanupTask, null));
                        cleanup.Start();
                    }
                }
            }
            var httpCacheItem = new HttpCacheItem
            {
                Usages           = 1,
                Etag             = etag,
                Ptr              = (byte *)mem.Address,
                Size             = result.Size,
                Allocation       = mem,
                LastServerUpdate = SystemTime.UtcNow,
                Cache            = this,
            };

            _items.AddOrUpdate(url, httpCacheItem, (s, oldItem) =>
            {
                oldItem.Release();
                return(httpCacheItem);
            });
        }
Beispiel #3
0
        public unsafe void Set(string url, string changeVector, BlittableJsonReaderObject result)
        {
            var mem = _unmanagedBuffersPool.Allocate(result.Size);

            result.CopyTo(mem.Address);
            if (Interlocked.Add(ref _totalSize, result.Size) > _maxSize && _isFreeSpaceRunning.Raise())
            {
                Task.Run(() => FreeSpace());
            }
            var httpCacheItem = new HttpCacheItem
            {
                Usages           = 1,
                ChangeVector     = changeVector,
                Ptr              = mem.Address,
                Size             = result.Size,
                Allocation       = mem,
                LastServerUpdate = SystemTime.UtcNow,
                Cache            = this,
                Generation       = Generation
            };

            _items.AddOrUpdate(url, httpCacheItem, (s, oldItem) =>
            {
                oldItem.Release();
                return(httpCacheItem);
            });
        }
Beispiel #4
0
        public unsafe void PutLocalState(Transaction tx, string key, BlittableJsonReaderObject value)
        {
            var localState = tx.CreateTree(LocalNodeStateTreeName);

            using (localState.DirectAdd(key, value.Size, out var ptr))
            {
                value.CopyTo(ptr);
            }
        }
        private byte[] GetReduceResult(BlittableJsonReaderObject reduceResult)
        {
            var necessarySize = Bits.NextPowerOf2(reduceResult.Size);

            if (_reduceValueBuffer.Length < necessarySize)
            {
                _reduceValueBuffer = new byte[necessarySize];
            }

            unsafe
            {
                fixed(byte *v = _reduceValueBuffer)
                reduceResult.CopyTo(v);
            }

            return(_reduceValueBuffer);
        }
Beispiel #6
0
        public unsafe void Set(string url, string changeVector, BlittableJsonReaderObject result)
        {
#if DEBUG
            result.BlittableValidation();
#endif
            var mem = _unmanagedBuffersPool.Allocate(result.Size);
            result.CopyTo(mem.Address);
            if (Interlocked.Add(ref _totalSize, result.Size) > _maxSize)
            {
                if (_isFreeSpaceRunning == false)
                {
                    Task.Run(FreeSpace);
                }
            }

            var httpCacheItem = new HttpCacheItem
            {
                ChangeVector = changeVector,
                Ptr          = mem.Address,
                Size         = result.Size,
                Allocation   = mem,
                Cache        = this,
                Generation   = Generation
            };

            HttpCacheItem old = null;
            _items.AddOrUpdate(url, httpCacheItem, (s, oldItem) =>
            {
                old = oldItem;
                return(httpCacheItem);
            });
            //We need to check if the cache is been disposed after the item was added otherwise we will run into another race condition
            //where it started been disposed right after we checked it and before we managed to insert the new cache item.
            if (_disposing)
            {
                //We might have double release here but we have a protection for that.
                httpCacheItem.ReleaseRef();
            }
            old?.ReleaseRef();
        }