Esempio n. 1
0
 public void SerialAllocationAndRelease()
 {
     using (var pool = new UnmanagedBuffersPoolWithLowMemoryHandling(string.Empty))
     {
         var allocatedMemory = new List <AllocatedMemoryData>();
         for (var i = 0; i < 1000; i++)
         {
             allocatedMemory.Add(pool.Allocate(i));
         }
         foreach (var data in allocatedMemory)
         {
             pool.Return(data);
         }
     }
 }
Esempio n. 2
0
 public ReduceKeyProcessor(int numberOfReduceFields, UnmanagedBuffersPoolWithLowMemoryHandling buffersPool)
 {
     _numberOfReduceFields = numberOfReduceFields;
     _buffersPool          = buffersPool;
     if (numberOfReduceFields == 1)
     {
         _mode = Mode.SingleValue;
     }
     else
     {
         _mode      = Mode.MultipleValues;
         _buffer    = _buffersPool.Allocate(16);
         _bufferPos = 0;
     }
 }
Esempio n. 3
0
        public void ParallelAllocationAndReleaseSeperately()
        {
            using (var pool = new UnmanagedBuffersPoolWithLowMemoryHandling(string.Empty))
            {
                var allocatedMemory = new global::Sparrow.Collections.ConcurrentSet <AllocatedMemoryData>();
                Parallel.For(0, 100, RavenTestHelper.DefaultParallelOptions, x =>
                {
                    for (var i = 0; i < 10; i++)
                    {
                        allocatedMemory.Add(pool.Allocate(i));
                    }
                });

                Parallel.ForEach(allocatedMemory, RavenTestHelper.DefaultParallelOptions, item =>
                {
                    pool.Return(item);
                });
            }
        }
Esempio n. 4
0
        public void ParallelSerialAllocationAndRelease()
        {
            using (var pool = new UnmanagedBuffersPoolWithLowMemoryHandling(string.Empty))
            {
                var allocatedMemory = new BlockingCollection <AllocatedMemoryData>();
                Task.Run(() =>
                {
                    for (var i = 0; i < 100; i++)
                    {
                        allocatedMemory.Add(pool.Allocate(i));
                    }
                    allocatedMemory.CompleteAdding();
                });

                while (allocatedMemory.IsCompleted == false)
                {
                    AllocatedMemoryData tuple;
                    if (allocatedMemory.TryTake(out tuple, 100))
                    {
                        pool.Return(tuple);
                    }
                }
            }
        }
Esempio n. 5
0
        public void Process(ByteStringContext context, object value, bool internalCall = false)
        {
            if (internalCall == false)
            {
                _processedFields++;
            }

            if (value == null || value is DynamicNullObject)
            {
                return;
            }

            _hadAnyNotNullValue = true;

            var lsv = value as LazyStringValue;

            if (lsv != null)
            {
                switch (_mode)
                {
                case Mode.SingleValue:
                    _singleValueHash = Hashing.XXHash64.Calculate(lsv.Buffer, (ulong)lsv.Size);
                    break;

                case Mode.MultipleValues:
                    CopyToBuffer(lsv.Buffer, lsv.Size);
                    break;
                }

                return;
            }

            var s = value as string;

            if (s != null)
            {
                using (Slice.From(context, s, out Slice str))
                {
                    switch (_mode)
                    {
                    case Mode.SingleValue:
                        _singleValueHash = Hashing.XXHash64.Calculate(str.Content.Ptr, (ulong)str.Size);
                        break;

                    case Mode.MultipleValues:
                        CopyToBuffer(str.Content.Ptr, str.Size);
                        break;
                    }
                }

                return;
            }

            var lcsv = value as LazyCompressedStringValue;

            if (lcsv != null)
            {
                switch (_mode)
                {
                case Mode.SingleValue:
                    _singleValueHash = Hashing.XXHash64.Calculate(lcsv.Buffer, (ulong)lcsv.CompressedSize);
                    break;

                case Mode.MultipleValues:
                    CopyToBuffer(lcsv.Buffer, lcsv.CompressedSize);
                    break;
                }

                return;
            }

            if (value is long l)
            {
                switch (_mode)
                {
                case Mode.SingleValue:
                    unchecked
                    {
                        _singleValueHash = (ulong)l;
                    }
                    break;

                case Mode.MultipleValues:
                    CopyToBuffer((byte *)&l, sizeof(long));
                    break;
                }

                return;
            }

            if (value is decimal d)
            {
                switch (_mode)
                {
                case Mode.SingleValue:
                    _singleValueHash = Hashing.XXHash64.Calculate((byte *)&d, sizeof(decimal));
                    break;

                case Mode.MultipleValues:
                    CopyToBuffer((byte *)&d, sizeof(decimal));
                    break;
                }

                return;
            }

            if (value is int num)
            {
                switch (_mode)
                {
                case Mode.SingleValue:
                    _singleValueHash = (ulong)num;
                    break;

                case Mode.MultipleValues:
                    CopyToBuffer((byte *)&num, sizeof(int));
                    break;
                }

                return;
            }

            if (value is bool b)
            {
                switch (_mode)
                {
                case Mode.SingleValue:
                    _singleValueHash = b ? 0 : 1UL;
                    break;

                case Mode.MultipleValues:
                    CopyToBuffer((byte *)&b, sizeof(bool));
                    break;
                }

                return;
            }

            if (value is double dbl)
            {
                switch (_mode)
                {
                case Mode.SingleValue:
                    _singleValueHash = (ulong)dbl;
                    break;

                case Mode.MultipleValues:
                    CopyToBuffer((byte *)&d, sizeof(double));
                    break;
                }

                return;
            }

            if (value is LazyNumberValue lnv)
            {
                Process(context, lnv.Inner, true);
                return;
            }

            long?ticks = null;

            if (value is DateTime)
            {
                ticks = ((DateTime)value).Ticks;
            }
            if (value is DateTimeOffset)
            {
                ticks = ((DateTimeOffset)value).Ticks;
            }
            if (value is TimeSpan)
            {
                ticks = ((TimeSpan)value).Ticks;
            }

            if (ticks.HasValue)
            {
                var t = ticks.Value;
                switch (_mode)
                {
                case Mode.SingleValue:
                    _singleValueHash = (ulong)t;
                    break;

                case Mode.MultipleValues:
                    CopyToBuffer((byte *)&t, sizeof(long));
                    break;
                }

                return;
            }

            if (value is BlittableJsonReaderObject json)
            {
                _mode = Mode.MultipleValues;

                if (_buffer == null)
                {
                    _buffer = _buffersPool.Allocate(16);
                }

                var prop = new BlittableJsonReaderObject.PropertyDetails();

                for (int i = 0; i < json.Count; i++)
                {
                    // this call ensures properties to be returned in the same order, regardless their storing order
                    json.GetPropertyByIndex(i, ref prop);

                    Process(context, prop.Value, true);
                }

                return;
            }

            if (value is IEnumerable enumerable)
            {
                _mode = Mode.MultipleValues;

                if (_buffer == null)
                {
                    _buffer = _buffersPool.Allocate(16);
                }

                foreach (var item in enumerable)
                {
                    Process(context, item, true);
                }

                return;
            }

            throw new NotSupportedException($"Unhandled type: {value.GetType()}");
        }