Example #1
0
 public void ResetAndRenew()
 {
     _unmanagedWriteBuffer.Dispose();
     _unmanagedWriteBuffer = (TWriter)(object)_context.GetStream(_lastSize);
     _position             = 0;
     _innerBuffer          = _context.GetMemory(32);
 }
        public void ValidateFloat()
        {
            try
            {
                int numLength = _unmanagedWriteBuffer.SizeInBytes;

                if (numLength <= 100)
                {
                    byte *tmpBuff = stackalloc byte[numLength];
                    _unmanagedWriteBuffer.CopyTo(tmpBuff);
                    _ctx.ParseDouble(tmpBuff, numLength);
                }
                else
                {
                    var memoryForNumber = _ctx.GetMemory(numLength);

                    try
                    {
                        _unmanagedWriteBuffer.CopyTo(memoryForNumber.Address);
                        _ctx.ParseDouble(memoryForNumber.Address, numLength);
                    }
                    finally
                    {
                        _ctx.ReturnMemory(memoryForNumber);
                    }
                }
            }
#pragma warning disable RDB0004 // Exception handler is empty or just logging
            catch (Exception e)
            {
                ThrowException("Could not parse double", e);
            }
#pragma warning restore RDB0004 // Exception handler is empty or just logging
        }
Example #3
0
        private void SetStringBuffer(string str)
        {
            // max possible size - we avoid using GetByteCount because profiling showed it to take 2% of runtime
            // the buffer might be a bit longer, but we'll reuse it, and it is better than the computing cost
            int byteCount           = Encodings.Utf8.GetMaxByteCount(str.Length);
            int escapePositionsSize = JsonParserState.FindEscapePositionsMaxSize(str, out _);

            // If we do not have a buffer or the buffer is too small, return the memory and get more.
            var size = byteCount + escapePositionsSize;

            if (_currentStateBuffer == null || _currentStateBuffer.SizeInBytes < size)
            {
                if (_currentStateBuffer != null)
                {
                    _ctx.ReturnMemory(_currentStateBuffer);
                }
                _currentStateBuffer = _ctx.GetMemory(size);
                Debug.Assert(_currentStateBuffer != null && _currentStateBuffer.Address != null);
            }

            _state.StringBuffer = _currentStateBuffer.Address;

            fixed(char *pChars = str)
            {
                _state.StringSize     = Encodings.Utf8.GetBytes(pChars, str.Length, _state.StringBuffer, byteCount);
                _state.CompressedSize = null; // don't even try
                _state.FindEscapePositionsIn(_state.StringBuffer, ref _state.StringSize, escapePositionsSize);

                var escapePos = _state.StringBuffer + _state.StringSize;

                _state.WriteEscapePositionsTo(escapePos);
            }
        }
Example #4
0
        public BlittableJsonReaderObject Clone(JsonOperationContext context)
        {
            if (_parent != null)
            {
                return(context.ReadObject(this, "cloning nested obj"));
            }

            var mem = context.GetMemory(Size);

            CopyTo(mem.Address);
            var cloned = new BlittableJsonReaderObject(mem.Address, Size, context)
            {
                _allocatedMemory = mem
            };

            if (Modifications != null)
            {
                cloned.Modifications = new DynamicJsonValue(cloned);
                foreach (var property in Modifications.Properties)
                {
                    cloned.Modifications.Properties.Enqueue(property);
                }
            }

            return(cloned);
        }
 public UnmanagedJsonParser(JsonOperationContext ctx, JsonParserState state, string debugTag)
 {
     _ctx                  = ctx;
     _state                = state;
     _debugTag             = debugTag;
     _unmanagedWriteBuffer = new UnmanagedWriteBuffer(ctx, ctx.GetMemory(1024 * 16));
 }
Example #6
0
        public AllocatedMemoryData CloneToJsonContext(JsonOperationContext context, out Slice slice)
        {
            var mem = context.GetMemory(Size);

            Memory.Copy(mem.Address, Content._pointer, Size);
            slice = new Slice(new ByteString((ByteStringStorage *)mem.Address));
            return(mem);
        }
Example #7
0
        public AllocatedMemoryData DecompressToAllocatedMemoryData(JsonOperationContext externalContext)
        {
            var sizeOfEscapePositions = GetSizeOfEscapePositions();
            var allocatedBuffer       = externalContext.GetMemory(UncompressedSize + sizeOfEscapePositions);

            DecompressToBuffer(allocatedBuffer.Address, sizeOfEscapePositions);

            return(allocatedBuffer);
        }
Example #8
0
        public AbstractBlittableJsonTextWriter(JsonOperationContext context, Stream stream)
        {
            _context = context;
            _stream  = stream;

            _returnBuffer = context.GetManagedBuffer(out _pinnedBuffer);
            _buffer       = _pinnedBuffer.Pointer;

            _parserAuxiliarMemory = context.GetMemory(32);
        }
Example #9
0
        public AllocatedMemoryData Clone(JsonOperationContext context, out TimeSeriesValuesSegment segment)
        {
            // for better reuse let use the const size of 'MaxSegmentSize'
            var memory = context.GetMemory(TimeSeriesStorage.MaxSegmentSize);

            Memory.Set(memory.Address, 0, TimeSeriesStorage.MaxSegmentSize);
            CopyTo(memory.Address);
            segment = new TimeSeriesValuesSegment(memory.Address, _capacity);
            return(memory);
        }
Example #10
0
        private void AllocateNextSegment(int required, bool allowGrowth)
        {
            Debug.Assert(required > 0);

            // Grow by doubling segment size until we get to 1 MB, then just use 1 MB segments
            // otherwise a document with 17 MB will waste 15 MB and require very big allocations
            var powerOfTwoRequestSize = Bits.NextPowerOf2(required);
            var segmentSize           = Math.Max(powerOfTwoRequestSize, _head.Allocation.SizeInBytes * 2);

            if (powerOfTwoRequestSize < segmentSize ||
                segmentSize > ArenaMemoryAllocator.MaxArenaSize)
            {
                // don't grow _too_ much.
                segmentSize = powerOfTwoRequestSize;
            }
            const int oneMb = 1024 * 1024;

            if (segmentSize > oneMb && required <= oneMb)
            {
                segmentSize = oneMb;
            }

            // We can sometimes ask the context to grow the allocation size;
            // it may do so at its own discretion; if this happens, then we
            // are good to go.
            if (allowGrowth && _context.GrowAllocation(_head.Allocation, segmentSize))
            {
                return;
            }

            // Can't change _head because there may be copies of the current
            // instance of UnmanagedWriteBuffer going around. Thus, we simply
            // mutate it to ensure all copies have the same allocations.
            var allocation = _context.GetMemory(segmentSize);

            // Copy the head
            Segment previousHead = _head.ShallowCopy();

            // Reset the head (this change happens in all instances at the
            // same time, albeit not atomically).
            _head.Previous = previousHead;
            _head.DeallocationPendingPrevious = previousHead;
            _head.Allocation             = allocation;
            _head.Address                = allocation.Address;
            _head.Used                   = 0;
            _head.AccumulatedSizeInBytes = previousHead.AccumulatedSizeInBytes;
        }
Example #11
0
        protected override unsafe Document DirectGet(Lucene.Net.Documents.Document input, string id, DocumentFields fields, IState state)
        {
            var reduceValue = input.GetField(Constants.Documents.Indexing.Fields.ReduceKeyValueFieldName).GetBinaryValue(state);

            var allocation = _context.GetMemory(reduceValue.Length);

            UnmanagedWriteBuffer buffer = new UnmanagedWriteBuffer(_context, allocation);

            buffer.Write(reduceValue, 0, reduceValue.Length);

            var result = new BlittableJsonReaderObject(allocation.Address, reduceValue.Length, _context, buffer);

            return(new Document
            {
                Data = result
            });
        }
        /// <summary>
        /// This function Processes the to string format of the form "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffff" for date times in
        /// invariant culture scenarios. This implementation takes 20% of the time of a regular .ToString(format) call
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="context"></param>
        /// <param name="memory"></param>
        /// <param name="isUtc"></param>
        /// <returns></returns>
        public static unsafe int GetDefaultFormat(this DateTime dt, JsonOperationContext context, out AllocatedMemoryData memory, bool isUtc = false)
        {
            int size  = 27 + (isUtc ? 1 : 0);
            var ticks = dt.Ticks;

            memory = context.GetMemory(size);

            byte *ptr = memory.Address;

            ProcessDefaultFormat(ticks, ptr);

            if (isUtc)
            {
                ptr[size - 1] = (byte)'Z';
            }

            return(size);
        }
Example #13
0
        private unsafe LazyStringValue CreateLazyStringValueFromParserState(JsonParserState state)
        {
            int escapePositionsCount = state.EscapePositions.Count;

            var maxSizeOfEscapePos = escapePositionsCount * 5 // max size of var int
                                     + JsonParserState.VariableSizeIntSize(escapePositionsCount);

            var mem = _ctx.GetMemory(maxSizeOfEscapePos + state.StringSize);

            _allocations.Add(mem);
            Memory.Copy(mem.Address, state.StringBuffer, state.StringSize);
            var lazyStringValueFromParserState = _ctx.AllocateStringValue(null, mem.Address, state.StringSize);

            if (escapePositionsCount > 0)
            {
                lazyStringValueFromParserState.EscapePositions = state.EscapePositions.ToArray();
            }
            return(lazyStringValueFromParserState);
        }
        private unsafe DynamicJsonValue ExtractSubscriptionConfigValue(TableValueReader tvr, JsonOperationContext context)
        {
            int size;
            var subscriptionId =
                Bits.SwapBytes(*(long *)tvr.Read(SubscriptionSchema.SubscriptionTable.IdIndex, out size));
            var ackEtag =
                *(long *)tvr.Read(SubscriptionSchema.SubscriptionTable.AckEtagIndex, out size);
            var timeOfReceivingLastAck =
                *(long *)tvr.Read(SubscriptionSchema.SubscriptionTable.TimeOfReceivingLastAck, out size);
            var ptr  = tvr.Read(SubscriptionSchema.SubscriptionTable.CriteriaIndex, out size);
            var data = context.GetMemory(size);

            Memory.Copy((byte *)data.Address, ptr, size);
            var criteria = new BlittableJsonReaderObject((byte *)data.Address, size, context);

            return(new DynamicJsonValue
            {
                ["SubscriptionId"] = subscriptionId,
                ["Criteria"] = criteria,
                ["AckEtag"] = ackEtag,
                ["TimeOfReceivingLastAck"] = new DateTime(timeOfReceivingLastAck).ToString(CultureInfo.InvariantCulture),
            });
        }
Example #15
0
 public BlittableWriter(JsonOperationContext context)
 {
     _context     = context;
     _innerBuffer = _context.GetMemory(32);
 }
Example #16
0
 public BlittableWriter(JsonOperationContext context, TWriter writer)
 {
     _context = context;
     _unmanagedWriteBuffer = writer;
     _innerBuffer          = _context.GetMemory(32);
 }