Example #1
0
        public unsafe void WriteString(LazyCompressedStringValue str)
        {
            var strBuffer = str.DecompressToTempBuffer();

            var size = str.UncompressedSize;

            EnsureBuffer(1);
            _buffer[_pos++] = Quote;
            var escapeSequencePos       = str.CompressedSize;
            var numberOfEscapeSequences = BlittableJsonReaderBase.ReadVariableSizeInt(str.Buffer, ref escapeSequencePos);

            while (numberOfEscapeSequences > 0)
            {
                numberOfEscapeSequences--;
                var bytesToSkip = BlittableJsonReaderBase.ReadVariableSizeInt(str.Buffer, ref escapeSequencePos);
                WriteRawString(strBuffer, bytesToSkip);
                strBuffer += bytesToSkip;
                size      -= bytesToSkip + 1 /*for the escaped char we skip*/;
                var b = *(strBuffer++);
                EnsureBuffer(2);
                _buffer[_pos++] = (byte)'\\';
                _buffer[_pos++] = GetEscapeCharacter(b);
            }
            // write remaining (or full string) to the buffer in one shot
            WriteRawString(strBuffer, size);

            EnsureBuffer(1);
            _buffer[_pos++] = Quote;
        }
        public bool Equals(LazyCompressedStringValue other)
        {
            if (other.UncompressedSize != UncompressedSize)
            {
                return(false);
            }

            if (other.CompressedSize == CompressedSize)
            {
                return(Memory.Compare(Buffer, other.Buffer, CompressedSize) == 0);
            }

            AllocatedMemoryData otherAllocated = null;
            AllocatedMemoryData allocated      = null;

            try
            {
                var otherTempBuffer = other.DecompressToTempBuffer(out otherAllocated);
                var tempBuffer      = DecompressToTempBuffer(out allocated);

                return(Memory.Compare(tempBuffer, otherTempBuffer, UncompressedSize) == 0);
            }
            finally
            {
                if (otherAllocated != null)
                {
                    other._context.ReturnMemory(otherAllocated);
                }

                if (allocated != null)
                {
                    _context.ReturnMemory(allocated);
                }
            }
        }
        public void WriteString(LazyCompressedStringValue str)
        {
            var strBuffer = str.DecompressToTempBuffer(out AllocatedMemoryData allocated, _context);

            try
            {
                var strSrcBuffer = str.Buffer;

                var size = str.UncompressedSize;
                var escapeSequencePos       = str.CompressedSize;
                var numberOfEscapeSequences = BlittableJsonReaderBase.ReadVariableSizeInt(strSrcBuffer, ref escapeSequencePos);

                // We ensure our buffer will have enough space to deal with the whole string.
                int bufferSize = 2 * numberOfEscapeSequences + size + 2;
                if (bufferSize >= JsonOperationContext.ManagedPinnedBuffer.Size)
                {
                    goto WriteLargeCompressedString; // OK, do it the slow way instead.
                }
                EnsureBuffer(bufferSize);

                _buffer[_pos++] = Quote;
                while (numberOfEscapeSequences > 0)
                {
                    numberOfEscapeSequences--;
                    var bytesToSkip = BlittableJsonReaderBase.ReadVariableSizeInt(strSrcBuffer, ref escapeSequencePos);
                    WriteRawString(strBuffer, bytesToSkip);
                    strBuffer += bytesToSkip;
                    size      -= bytesToSkip + 1 /*for the escaped char we skip*/;
                    var b = *(strBuffer++);

                    var auxPos = _pos;
                    _buffer[auxPos++] = (byte)'\\';
                    _buffer[auxPos++] = GetEscapeCharacter(b);
                    _pos = auxPos;
                }

                // write remaining (or full string) to the buffer in one shot
                WriteRawString(strBuffer, size);

                _buffer[_pos++] = Quote;

                return;

WriteLargeCompressedString:
                UnlikelyWriteLargeString(numberOfEscapeSequences, strSrcBuffer, escapeSequencePos, strBuffer, size);
            }
            finally
            {
                if (allocated != null) //precaution
                {
                    _context.ReturnMemory(allocated);
                }
            }
        }
Example #4
0
        private unsafe void WriteValue(BlittableJsonTextWriter writer, JsonParserState state, ObjectJsonParser parser)
        {
            switch (state.CurrentTokenType)
            {
            case JsonParserToken.Null:
                writer.WriteNull();
                break;

            case JsonParserToken.False:
                writer.WriteBool(false);
                break;

            case JsonParserToken.True:
                writer.WriteBool(true);
                break;

            case JsonParserToken.String:
                if (state.CompressedSize.HasValue)
                {
                    var lazyCompressedStringValue = new LazyCompressedStringValue(null, state.StringBuffer,
                                                                                  state.StringSize, state.CompressedSize.Value, this);
                    writer.WriteString(lazyCompressedStringValue);
                }
                else
                {
                    writer.WriteString(AllocateStringValue(null, state.StringBuffer, state.StringSize));
                }
                break;

            case JsonParserToken.Float:
                writer.WriteDouble(new LazyNumberValue(AllocateStringValue(null, state.StringBuffer, state.StringSize)));
                break;

            case JsonParserToken.Integer:
                writer.WriteInteger(state.Long);
                break;

            case JsonParserToken.StartObject:
                WriteObject(writer, state, parser);
                break;

            case JsonParserToken.StartArray:
                WriteArray(writer, state, parser);
                break;

            default:
                throw new ArgumentOutOfRangeException("Could not understand " + state.CurrentTokenType);
            }
        }
Example #5
0
        public unsafe int WriteValue(LazyCompressedStringValue str, out BlittableJsonToken token,
                                     UsageMode mode)
        {
            var startPos = _position;

            token = BlittableJsonToken.CompressedString;

            _position += WriteVariableSizeInt(str.UncompressedSize);

            _position += WriteVariableSizeInt(str.CompressedSize);

            // compressed size include escape positions
            _unmanagedWriteBuffer.Write(str.Buffer, str.CompressedSize);
            _position += str.CompressedSize;
            return(startPos);
        }
Example #6
0
        public unsafe int WriteValue(LazyCompressedStringValue str, out BlittableJsonToken token,
                                     UsageMode mode)
        {
            var startPos = _position;

            token = BlittableJsonToken.CompressedString;

            _position += WriteVariableSizeInt(str.UncompressedSize);

            _position += WriteVariableSizeInt(str.CompressedSize);

            var escapeSequencePos = GetSizeIncludingEscapeSequences(str.Buffer, str.CompressedSize);

            _unmanagedWriteBuffer.Write(str.Buffer, escapeSequencePos);
            _position += escapeSequencePos;
            return(startPos);
        }
Example #7
0
        public void WriteValue(LazyCompressedStringValue value)
        {
            var currentState = _continuationState.Pop();
            BlittableJsonToken stringToken;

            //public unsafe int WriteValue(byte* buffer, int size, out BlittableJsonToken token, UsageMode mode, int? initialCompressedSize)
            //var valuePos = _writer.WriteValue(value, out stringToken, UsageMode.None, null);
            var valuePos = _writer.WriteValue(value, out stringToken, UsageMode.None);
            _writeToken = new WriteToken
            {
                ValuePos = valuePos,
                WrittenToken = stringToken
            };

            if (currentState.FirstWrite == -1)
                currentState.FirstWrite = valuePos;

            currentState = FinishWritingScalarValue(currentState);
            _continuationState.Push(currentState);
        }
Example #8
0
        public void WriteString(LazyCompressedStringValue str)
        {
            AllocatedMemoryData allocated;
            var strBuffer = str.DecompressToTempBuffer(out allocated);

            try
            {
                var size = str.UncompressedSize;

                EnsureBuffer(1);
                _buffer[_pos++] = Quote;
                var escapeSequencePos       = str.CompressedSize;
                var numberOfEscapeSequences = BlittableJsonReaderBase.ReadVariableSizeInt(str.Buffer, ref escapeSequencePos);
                while (numberOfEscapeSequences > 0)
                {
                    numberOfEscapeSequences--;
                    var bytesToSkip = BlittableJsonReaderBase.ReadVariableSizeInt(str.Buffer, ref escapeSequencePos);
                    WriteRawString(strBuffer, bytesToSkip);
                    strBuffer += bytesToSkip;
                    size      -= bytesToSkip + 1 /*for the escaped char we skip*/;
                    var b = *(strBuffer++);
                    EnsureBuffer(2);
                    _buffer[_pos++] = (byte)'\\';
                    _buffer[_pos++] = GetEscapeCharacter(b);
                }
                // write remaining (or full string) to the buffer in one shot
                WriteRawString(strBuffer, size);

                EnsureBuffer(1);
                _buffer[_pos++] = Quote;
            }
            finally
            {
                if (allocated != null) //precaution
                {
                    _context.ReturnMemory(allocated);
                }
            }
        }