示例#1
0
        private async Task <BlittableJsonReaderObject> Receive(ClientWebSocket webSocket,
                                                               JsonOperationContext context)
        {
            BlittableJsonDocumentBuilder builder = null;

            try
            {
                if (webSocket.State != WebSocketState.Open)
                {
                    throw new InvalidOperationException(
                              $"Trying to 'ReceiveAsync' WebSocket while not in Open state. State is {webSocket.State}");
                }

                var state = new JsonParserState();
                using (context.GetManagedBuffer(out var buffer))
                    using (var parser = new UnmanagedJsonParser(context, state, ""))
                    {
                        builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None,
                                                                   nameof(TrafficRec) + "." + nameof(Receive), parser, state);
                        builder.ReadObjectDocument();
                        while (builder.Read() == false)
                        {
                            var result = await webSocket.ReceiveAsync(buffer.Buffer, CancellationToken.None);

                            if (result.MessageType == WebSocketMessageType.Close)
                            {
                                if (_logger.IsInfoEnabled)
                                {
                                    _logger.Info("Client got close message from server and is closing connection");
                                }

                                builder.Dispose();
                                // actual socket close from dispose
                                return(null);
                            }

                            if (result.EndOfMessage == false)
                            {
                                throw new EndOfStreamException("Stream ended without reaching end of json content.");
                            }

                            parser.SetBuffer(buffer, 0, result.Count);
                        }
                        builder.FinalizeDocument();

                        return(builder.CreateReader());
                    }
            }
            catch (WebSocketException ex)
            {
                builder?.Dispose();

                if (_logger.IsInfoEnabled)
                {
                    _logger.Info("Failed to receive a message, client was probably disconnected", ex);
                }

                throw;
            }
        }
示例#2
0
        public static IEnumerable <BlittableJsonReaderObject> ReadArrayToMemory(JsonOperationContext context, PeepingTomStream peepingTomStream, UnmanagedJsonParser parser, JsonParserState state, JsonOperationContext.ManagedPinnedBuffer buffer)
        {
            if (Read(peepingTomStream, parser, state, buffer) == false)
            {
                ThrowInvalidJson(peepingTomStream);
            }

            if (state.CurrentTokenType != JsonParserToken.StartArray)
            {
                ThrowInvalidJson(peepingTomStream);
            }

            while (true)
            {
                if (Read(peepingTomStream, parser, state, buffer) == false)
                {
                    ThrowInvalidJson(peepingTomStream);
                }

                if (state.CurrentTokenType == JsonParserToken.EndArray)
                {
                    break;
                }

                using (var builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, "readArray/singleResult", parser, state))
                {
                    ReadObject(builder, peepingTomStream, parser, buffer);

                    yield return(builder.CreateReader());
                }
            }
        }
示例#3
0
            public async Task <bool> MoveNextAsync()
            {
                if (_initialized == false)
                {
                    await InitializeAsync().ConfigureAwait(false);
                }

                if (await UnmanagedJsonParserHelper.ReadAsync(_peepingTomStream, _parser, _state, _buffer).ConfigureAwait(false) == false)
                {
                    UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);
                }

                if (_state.CurrentTokenType == JsonParserToken.EndArray)
                {
                    if (await UnmanagedJsonParserHelper.ReadAsync(_peepingTomStream, _parser, _state, _buffer).ConfigureAwait(false) == false)
                    {
                        UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);
                    }

                    if (_state.CurrentTokenType != JsonParserToken.EndObject)
                    {
                        UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);
                    }

                    return(false);
                }

                using (var builder = new BlittableJsonDocumentBuilder(_session.Context, BlittableJsonDocumentBuilder.UsageMode.ToDisk, "readArray/singleResult", _parser, _state))
                {
                    await UnmanagedJsonParserHelper.ReadObjectAsync(builder, _peepingTomStream, _parser, _buffer).ConfigureAwait(false);

                    Current = builder.CreateReader();
                    return(true);
                }
            }
示例#4
0
        private IEnumerable <(string key, long index, BlittableJsonReaderObject value)> InternalGetCompareExchangeValues()
        {
            var state = new JsonParserState();

            using (var parser = new UnmanagedJsonParser(_context, state, "Import/CompareExchange"))
                using (var builder = new BlittableJsonDocumentBuilder(_context,
                                                                      BlittableJsonDocumentBuilder.UsageMode.ToDisk, "Import/CompareExchange", parser, state))
                {
                    foreach (var reader in ReadArray())
                    {
                        using (reader)
                        {
                            if (reader.TryGet("Key", out string key) == false ||
                                reader.TryGet("Value", out LazyStringValue value) == false)
                            {
                                _result.CompareExchange.ErroredCount++;
                                _result.AddWarning("Could not read compare exchange entry.");

                                continue;
                            }

                            builder.ReadNestedObject();
                            SetBuffer(parser, value);
                            parser.Read();
                            builder.Read();
                            builder.FinalizeDocument();
                            yield return(key, 0, builder.CreateReader());

                            builder.Renew("import/cmpxchg", BlittableJsonDocumentBuilder.UsageMode.ToDisk);
                        }
                    }
                }
        }
示例#5
0
        public unsafe void Valid_String_With_66K_EscChar()
        {
            using (var ctx = JsonOperationContext.ShortTermSingleUse())
            {
                var state = new JsonParserState();
                using (var parser = new UnmanagedJsonParser(ctx, state, "test"))
                {
                    var temp = new Str
                    {
                        str = createRandomEscString(66 * 1024)
                    };

                    var obj       = JObject.FromObject(temp);
                    var objString = obj.ToString(Formatting.None);
                    var buffer    = Encoding.UTF8.GetBytes(objString);
                    fixed(byte *pBuffer = buffer)
                    {
                        parser.SetBuffer(pBuffer, buffer.Length);
                        using (var writer = new BlittableJsonDocumentBuilder(ctx,
                                                                             BlittableJsonDocumentBuilder.UsageMode.None,
                                                                             "test", parser, state))
                        {
                            writer.ReadObjectDocument();
                            var x = writer.Read();
                            writer.FinalizeDocument();
                            using (var reader = writer.CreateReader())
                                reader.BlittableValidation();
                        }
                    }
                }
            }
        }
示例#6
0
        public static async Task <BlittableJsonReaderObject> SimulateSavingToFileAndLoadingAsync(JsonOperationContext context, BlittableJsonReaderObject toStream)
        {
            //Simulates saving to file and loading
            BlittableJsonReaderObject fromStream;

            await using (Stream stream = new MemoryStream())
            {
                //Pass to stream
                await using (var textWriter = new AsyncBlittableJsonTextWriter(context, stream))
                {
                    context.Write(textWriter, toStream);
                }

                //Get from stream
                stream.Position = 0;

                var state            = new JsonParserState();
                var parser           = new UnmanagedJsonParser(context, state, "some tag");
                var peepingTomStream = new PeepingTomStream(stream, context);

                using (context.GetMemoryBuffer(out var buffer))
                    using (var builder =
                               new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, "some tag", parser, state))
                    {
                        await UnmanagedJsonParserHelper.ReadAsync(peepingTomStream, parser, state, buffer);

                        await UnmanagedJsonParserHelper.ReadObjectAsync(builder, peepingTomStream, parser, buffer);

                        fromStream = builder.CreateReader();
                    }
            }

            return(fromStream);
        }
示例#7
0
        private static unsafe BlittableJsonReaderObject ParseJsonOneByteIncrements(string s, JsonOperationContext ctx)
        {
            var jsonParserState = new JsonParserState();
            var parser          = new UnmanagedJsonParser(ctx, jsonParserState, "test");
            var builder         = new BlittableJsonDocumentBuilder(ctx, BlittableJsonDocumentBuilder.UsageMode.ToDisk, "test", parser, jsonParserState);

            builder.ReadObjectDocument();
            fixed(char *pStr = s)
            {
                var buffer = stackalloc byte[1];

                for (int i = 0; i < s.Length - 1; i++)
                {
                    buffer[0] = (byte)pStr[i];
                    parser.SetBuffer(buffer, 1);
                    Assert.False(builder.Read());
                }
                buffer[0] = (byte)pStr[s.Length - 1];
                parser.SetBuffer(buffer, 1);
                Assert.True(builder.Read());
            }

            builder.FinalizeDocument();
            var reader = builder.CreateReader();

            return(reader);
        }
示例#8
0
        private static async Task <BlittableJsonReaderObject> ReadJsonObject(JsonOperationContext ctx, Stream stream, string id, UnmanagedJsonParser parser,
                                                                             JsonParserState state, JsonOperationContext.ManagedPinnedBuffer buffer, CancellationToken token)
        {
            if (state.CurrentTokenType == JsonParserToken.Null)
            {
                return(null);
            }

            BlittableJsonReaderObject reader;

            using (var builder = new BlittableJsonDocumentBuilder(ctx,
                                                                  BlittableJsonDocumentBuilder.UsageMode.ToDisk,
                                                                  id, parser, state, modifier: new BlittableMetadataModifier(ctx)))
            {
                ctx.CachedProperties.NewDocument();
                builder.ReadNestedObject();
                while (true)
                {
                    if (builder.Read())
                    {
                        break;
                    }
                    await RefillParserBuffer(stream, buffer, parser, token);
                }
                builder.FinalizeDocument();
                reader         = builder.CreateReader();
                reader.NoCache = true;
            }
            return(reader);
        }
示例#9
0
            public async Task <bool> MoveNextAsync()
            {
                if (_initialized == false)
                {
                    await InitializeAsync().ConfigureAwait(false);
                }

                if (_docsCountOnCachedRenewSession <= 16 * 1024)
                {
                    if (_cachedItemsRenew)
                    {
                        _session.Context.CachedProperties = new CachedProperties(_session.Context);
                        ++_docsCountOnCachedRenewSession;
                    }
                }
                else
                {
                    _session.Context.Renew();
                    _docsCountOnCachedRenewSession = 0;
                }

                if (await UnmanagedJsonParserHelper.ReadAsync(_peepingTomStream, _parser, _state, _buffer).ConfigureAwait(false) == false)
                {
                    UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);
                }

                if (_state.CurrentTokenType == JsonParserToken.EndArray)
                {
                    if (await UnmanagedJsonParserHelper.ReadAsync(_peepingTomStream, _parser, _state, _buffer).ConfigureAwait(false) == false)
                    {
                        UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);
                    }

                    if (_state.CurrentTokenType != JsonParserToken.EndObject)
                    {
                        UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);
                    }

                    return(false);
                }

                using (var builder = new BlittableJsonDocumentBuilder(_session.Context, BlittableJsonDocumentBuilder.UsageMode.ToDisk, "readArray/singleResult", _parser, _state))
                {
                    if (_cachedItemsRenew == false)
                    {
                        _cachedItemsRenew = builder.NeedResetPropertiesCache();
                    }

                    await UnmanagedJsonParserHelper.ReadObjectAsync(builder, _peepingTomStream, _parser, _buffer).ConfigureAwait(false);

                    Current = builder.CreateReader();
                    return(true);
                }
            }
示例#10
0
            public bool MoveNext()
            {
                AssertInitialized();

                CheckIfContextOrCacheNeedToBeRenewed();

                _timeSeriesIt?.Dispose();

                if (UnmanagedJsonParserHelper.Read(_peepingTomStream, _parser, _state, _buffer) == false)
                    UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);

                if (_state.CurrentTokenType == JsonParserToken.EndArray)
                {
                    if (UnmanagedJsonParserHelper.Read(_peepingTomStream, _parser, _state, _buffer) == false)
                        UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);

                    if (_state.CurrentTokenType != JsonParserToken.EndObject)
                        UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);

                    return false;
                }

                _builder.Renew("readArray/singleResult", BlittableJsonDocumentBuilder.UsageMode.ToDisk);

                if (_isTimeSeriesStream)
                    UnmanagedJsonParserHelper.ReadProperty(_builder, _peepingTomStream, _parser, _buffer);
                else
                    UnmanagedJsonParserHelper.ReadObject(_builder, _peepingTomStream, _parser, _buffer);

                Current = _builder.CreateReader();

                _builder.Reset();

                if (_isTimeSeriesStream)
                {
                    _timeSeriesIt = new TimeSeriesStreamEnumerator(_builderContext, _peepingTomStream, _parser, _state, _buffer);
                    _timeSeriesIt.Initialize();
                }

                return true;
            }
示例#11
0
        public static IEnumerable <BlittableJsonReaderObject> ReadArrayToMemory(JsonOperationContext context, PeepingTomStream peepingTomStream, UnmanagedJsonParser parser, JsonParserState state, JsonOperationContext.ManagedPinnedBuffer buffer)
        {
            if (Read(peepingTomStream, parser, state, buffer) == false)
            {
                ThrowInvalidJson(peepingTomStream);
            }

            if (state.CurrentTokenType != JsonParserToken.StartArray)
            {
                ThrowInvalidJson(peepingTomStream);
            }

            int  docsCountOnCachedRenewSession = 0;
            bool cachedItemsRenew = false;

            while (true)
            {
                if (docsCountOnCachedRenewSession <= 16 * 1024)
                {
                    if (cachedItemsRenew)
                    {
                        context.CachedProperties = new CachedProperties(context);
                        ++docsCountOnCachedRenewSession;
                    }
                }
                else
                {
                    context.Renew();
                    docsCountOnCachedRenewSession = 0;
                }

                if (Read(peepingTomStream, parser, state, buffer) == false)
                {
                    ThrowInvalidJson(peepingTomStream);
                }

                if (state.CurrentTokenType == JsonParserToken.EndArray)
                {
                    break;
                }

                using (var builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, "readArray/singleResult", parser, state))
                {
                    if (cachedItemsRenew == false)
                    {
                        cachedItemsRenew = builder.NeedResetPropertiesCache();
                    }
                    ReadObject(builder, peepingTomStream, parser, buffer);

                    yield return(builder.CreateReader());
                }
            }
        }
示例#12
0
        public unsafe void Valid_String()
        {
            using (var ctx = JsonOperationContext.ShortTermSingleUse())
            {
                var state = new JsonParserState();
                using (var parser = new UnmanagedJsonParser(ctx, state, "test"))
                {
                    var temp = new Str
                    {
                        str = "\nabcdefghijklmnopqrstuvwxyz" +
                              "abcdefghijklmnopqrstuvwxyz" +
                              "abcdefghijklmnopqrstuvwxyz" +
                              "abcdefghijklmnopqrstuvwxyz" +
                              "abcdefghijklmnopqrstuvwxyz" +
                              "abcdefghijklmnopqrstuvwxyz" +
                              "abcdefghijklmnopqrstuvwxyz" +
                              "abcdefghijklmnopqrstuvwxyz" +
                              "abcdefghijklmnopqrstuvwxyz" +
                              "abcdefghijklmnopqrstuvwxyz" +
                              "abcdefghijklmnopqrstuvwxyz" +
                              "abcdefghijklmnopqrstuvwxyz" +
                              "abcdefghijklmnopqrstuvwxyz" +
                              "abcdefghijklmnopqrstuvwxyz" +
                              "abcdefghijklmnopqrstuvwxyz" +
                              "abcdefghijklmnopqrstuvwxyz" +
                              "abcdefghijklmnopqrstuvwxyz" +
                              "abcdefghijklmnopqrstuvwxyz" +
                              "abcdefghijklmnopqrstuvwxyz\n"
                    };
                    var obj       = RavenJObject.FromObject(temp);
                    var objString = obj.ToString(Formatting.None);
                    var buffer    = Encoding.UTF8.GetBytes(objString);
                    fixed(byte *pBuffer = buffer)
                    {
                        parser.SetBuffer(pBuffer, buffer.Length);
                        var writer = new BlittableJsonDocumentBuilder(ctx,
                                                                      BlittableJsonDocumentBuilder.UsageMode.None,
                                                                      "test", parser, state);

                        writer.ReadObjectDocument();
                        var x = writer.Read();

                        writer.FinalizeDocument();
                        var reader = writer.CreateReader();

                        reader.BlittableValidation();
                    }
                }
            }
        }
示例#13
0
        public unsafe void Valid_object_read_from_non_zero_offset()
        {
            using (var context = JsonOperationContext.ShortTermSingleUse())
            {
                var jsonParserState = new JsonParserState();
                using (var parser = new UnmanagedJsonParser(context, jsonParserState, "changes/1"))
                {
                    byte[] buffer       = new byte[4096];
                    var    bufferOffset = 128; //non-zero offset

                    var allTokens = new AllTokensTypes
                    {
                        Bool     = true,
                        Float    = 123.4567F,
                        Int      = 45679123,
                        IntArray = new[] { 1, 2, 3 },
                        Null     = null,
                        Object   = new Empty(),
                        String   = "qwertyuio"
                    };
                    var obj       = JObject.FromObject(allTokens);
                    var objString = obj.ToString(Formatting.None);

                    var data = Encoding.UTF8.GetBytes(objString);

                    data.CopyTo(buffer, bufferOffset);

                    fixed(byte *pBuffer = buffer)
                    {
                        using (var builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, "order/1", parser, jsonParserState))
                        {
                            parser.SetBuffer(pBuffer + bufferOffset, data.Length);

                            builder.ReadObjectDocument();

                            Assert.True(builder.Read());

                            builder.FinalizeDocument();

                            using (var reader = builder.CreateReader())
                            {
                                var value = reader.ToString();
                                Assert.NotNull(value);
                            }
                        }
                    }
                }
            }
        }
示例#14
0
        private static unsafe BlittableJsonReaderObject ParseJsonAllAtOnce(string s, JsonOperationContext ctx)
        {
            var jsonParserState = new JsonParserState();
            var parser          = new UnmanagedJsonParser(ctx, jsonParserState, "test");
            var builder         = new BlittableJsonDocumentBuilder(ctx, BlittableJsonDocumentBuilder.UsageMode.ToDisk, "test", parser, jsonParserState);

            builder.ReadObjectDocument();
            var value = ctx.GetLazyString(s);

            parser.SetBuffer(value.Buffer, value.Size);
            Assert.True(builder.Read());
            builder.FinalizeDocument();
            var reader = builder.CreateReader();

            return(reader);
        }
示例#15
0
            public override async Task <ResponseDisposeHandling> ProcessResponse(JsonOperationContext context, HttpCache cache, HttpResponseMessage response, string url)
            {
                var    state  = new JsonParserState();
                Stream stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                using (context.GetMemoryBuffer(out var buffer))
                    using (var parser = new UnmanagedJsonParser(context, state, "attachments/receive"))
                        using (var builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, "attachments/list", parser, state))
                            using (var peepingTomStream = new PeepingTomStream(stream, context))
                            {
                                if (await UnmanagedJsonParserHelper.ReadAsync(peepingTomStream, parser, state, buffer).ConfigureAwait(false) == false)
                                {
                                    throw new Exception("cannot parse stream");
                                }

                                if (state.CurrentTokenType != JsonParserToken.StartObject)
                                {
                                    throw new Exception($"Expected token {nameof(JsonParserToken.StartObject)}, but got {nameof(state.CurrentTokenType)}.");
                                }

                                await UnmanagedJsonParserHelper.ReadObjectAsync(builder, peepingTomStream, parser, buffer).ConfigureAwait(false);

                                using (var data = builder.CreateReader())
                                {
                                    if (data.TryGetMember(nameof(AttachmentsMetadata), out object obj) && obj is BlittableJsonReaderArray bjra)
                                    {
                                        foreach (BlittableJsonReaderObject e in bjra)
                                        {
                                            var cur = JsonDeserializationClient.AttachmentDetails(e);
                                            AttachmentsMetadata.Add(cur);
                                        }
                                    }
                                }

                                var bufferSize = parser.BufferSize - parser.BufferOffset;
                                var copy       = ArrayPool <byte> .Shared.Rent(bufferSize);

                                var copyMemory = new Memory <byte>(copy);
                                buffer.Memory.Memory.Slice(parser.BufferOffset, bufferSize).CopyTo(copyMemory);

                                Result = Iterate(stream, copy, bufferSize).GetEnumerator();
                            }

                return(ResponseDisposeHandling.Manually);
            }
示例#16
0
        public static BlittableJsonReaderObject ParseToMemory(
            this JsonOperationContext.SyncJsonOperationContext syncContext,
            Stream stream,
            string debugTag,
            BlittableJsonDocumentBuilder.UsageMode mode,
            JsonOperationContext.MemoryBuffer bytes,
            IBlittableDocumentModifier modifier = null)
        {
            syncContext.EnsureNotDisposed();

            syncContext.JsonParserState.Reset();
            using (var parser = new UnmanagedJsonParser(syncContext.Context, syncContext.JsonParserState, debugTag))
                using (var builder = new BlittableJsonDocumentBuilder(syncContext.Context, mode, debugTag, parser, syncContext.JsonParserState, modifier: modifier))
                {
                    syncContext.Context.CachedProperties.NewDocument();
                    builder.ReadObjectDocument();
                    while (true)
                    {
                        if (bytes.Valid == bytes.Used)
                        {
                            var read = stream.Read(bytes.Memory.Memory.Span);
                            syncContext.EnsureNotDisposed();
                            if (read == 0)
                            {
                                throw new EndOfStreamException("Stream ended without reaching end of json content");
                            }
                            bytes.Valid = read;
                            bytes.Used  = 0;
                        }
                        parser.SetBuffer(bytes);
                        var result = builder.Read();
                        bytes.Used += parser.BufferOffset;
                        if (result)
                        {
                            break;
                        }
                    }
                    builder.FinalizeDocument();

                    var reader = builder.CreateReader();
                    return(reader);
                }
        }
示例#17
0
        private static unsafe void WriteToStream(Stream stream, BlittableJsonDocumentBuilder builder,
                                                 byte[] buffer)
        {
            using (var reader = builder.CreateReader())
            {
                fixed(byte *pBuffer = buffer)
                {
                    var bytes         = reader.BasePointer;
                    var remainingSize = reader.Size;

                    while (remainingSize > 0)
                    {
                        var size = Math.Min(remainingSize, buffer.Length);
                        Memory.Copy(pBuffer, bytes + (reader.Size - remainingSize), size);
                        remainingSize -= size;
                        stream.Write(buffer, 0, size);
                    }
                }
            }
        }
示例#18
0
            public async ValueTask<bool> MoveNextAsync()
            {
                if (await UnmanagedJsonParserHelper.ReadAsync(_peepingTomStream, _parser, _state, _buffer, _token).ConfigureAwait(false) == false)
                    UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);

                if (_state.CurrentTokenType == JsonParserToken.EndArray)
                {
                    _done = true;
                    _current = null;
                    return false;
                }

                using (var builder = new BlittableJsonDocumentBuilder(_context, BlittableJsonDocumentBuilder.UsageMode.None, "readArray/singleResult", _parser, _state))
                {
                    await UnmanagedJsonParserHelper.ReadObjectAsync(builder, _peepingTomStream, _parser, _buffer, _token).ConfigureAwait(false);

                    _current = builder.CreateReader();
                    return true;
                }
            }
示例#19
0
            public bool MoveNext()
            {
                AssertInitialized();

                CheckIfContextNeedsToBeRenewed();

                if (UnmanagedJsonParserHelper.Read(_peepingTomStream, _parser, _state, _buffer) == false)
                {
                    UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);
                }

                if (_state.CurrentTokenType == JsonParserToken.EndArray)
                {
                    if (UnmanagedJsonParserHelper.Read(_peepingTomStream, _parser, _state, _buffer) == false)
                    {
                        UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);
                    }

                    if (_state.CurrentTokenType != JsonParserToken.EndObject)
                    {
                        UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);
                    }

                    return(false);
                }

                using (var builder = new BlittableJsonDocumentBuilder(_session.Context, BlittableJsonDocumentBuilder.UsageMode.ToDisk, "readArray/singleResult", _parser, _state))
                {
                    if (_cachedItemsRenew == false)
                    {
                        _cachedItemsRenew = builder.NeedResetPropertiesCache();
                    }

                    UnmanagedJsonParserHelper.ReadObject(builder, _peepingTomStream, _parser, _buffer);

                    Current = builder.CreateReader();
                    return(true);
                }
            }
示例#20
0
        public async Task <BlittableJsonReaderObject> TryReadFromWebSocket(
            JsonOperationContext context,
            RavenClientWebSocket webSocket,
            string debugTag,
            CancellationToken cancellationToken)
        {
            var jsonParserState = new JsonParserState();

            JsonOperationContext.ManagedPinnedBuffer bytes;
            using (context.GetManagedBuffer(out bytes))
                using (var parser = new UnmanagedJsonParser(context, jsonParserState, debugTag))
                {
                    var writer = new BlittableJsonDocumentBuilder(context,
                                                                  BlittableJsonDocumentBuilder.UsageMode.None, debugTag, parser, jsonParserState);

                    writer.ReadObjectDocument();

                    var result = await webSocket.ReceiveAsync(bytes.Buffer, cancellationToken).ConfigureAwait(false);

                    parser.SetBuffer(bytes, result.Count);
                    while (writer.Read() == false)
                    {
                        // we got incomplete json response.
                        // This might happen if we close the connection but still server sends something
                        if (result.CloseStatus != null)
                        {
                            return(null);
                        }

                        result = await webSocket.ReceiveAsync(bytes.Buffer, cancellationToken).ConfigureAwait(false);

                        parser.SetBuffer(bytes, result.Count);
                    }
                    writer.FinalizeDocument();
                    return(writer.CreateReader());
                }
        }
示例#21
0
        private async Task ProcessChanges()
        {
            using (_requestExecutor.ContextPool.AllocateOperationContext(out var context))
            {
                while (_cts.IsCancellationRequested == false)
                {
                    context.Reset();
                    context.Renew();

                    var state = new JsonParserState();

                    using (var stream = new WebSocketStream(_client, _cts.Token))
                        using (context.GetMemoryBuffer(out JsonOperationContext.MemoryBuffer buffer))
                            using (var parser = new UnmanagedJsonParser(context, state, "changes/receive"))
                                using (var builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, "readArray/singleResult", parser, state))
                                    using (var peepingTomStream = new PeepingTomStream(stream, context))
                                    {
                                        if (await UnmanagedJsonParserHelper.ReadAsync(peepingTomStream, parser, state, buffer).ConfigureAwait(false) == false)
                                        {
                                            continue;
                                        }

                                        if (state.CurrentTokenType != JsonParserToken.StartArray)
                                        {
                                            continue;
                                        }

                                        while (true)
                                        {
                                            builder.Reset();
                                            builder.Renew("changes/receive", BlittableJsonDocumentBuilder.UsageMode.None);

                                            if (await UnmanagedJsonParserHelper.ReadAsync(peepingTomStream, parser, state, buffer).ConfigureAwait(false) == false)
                                            {
                                                continue;
                                            }

                                            if (state.CurrentTokenType == JsonParserToken.EndArray)
                                            {
                                                break;
                                            }

                                            await UnmanagedJsonParserHelper.ReadObjectAsync(builder, peepingTomStream, parser, buffer).ConfigureAwait(false);

                                            using (var json = builder.CreateReader())
                                            {
                                                try
                                                {
                                                    if (json.TryGet(nameof(TopologyChange), out bool supports) && supports)
                                                    {
                                                        GetOrAddConnectionState("Topology", "watch-topology-change", "", "");
                                                        await _requestExecutor.UpdateTopologyAsync(new RequestExecutor.UpdateTopologyParameters(_serverNode)
                                                        {
                                                            TimeoutInMs = 0, ForceUpdate = true, DebugTag = "watch-topology-change"
                                                        }).ConfigureAwait(false);

                                                        continue;
                                                    }

                                                    if (json.TryGet("Type", out string type) == false)
                                                    {
                                                        continue;
                                                    }

                                                    switch (type)
                                                    {
                                                    case "Error":
                                                        json.TryGet("Exception", out string exceptionAsString);
                                                        NotifyAboutError(new Exception(exceptionAsString));
                                                        break;

                                                    case "Confirm":
                                                        if (json.TryGet("CommandId", out int commandId) &&
                                                            _confirmations.TryRemove(commandId, out var tcs))
                                                        {
                                                            tcs.TrySetResult(null);
                                                        }

                                                        break;

                                                    default:
                                                        json.TryGet("Value", out BlittableJsonReaderObject value);
                                                        NotifySubscribers(type, value, _counters.ForceEnumerateInThreadSafeManner().Select(x => x.Value).ToList());
                                                        break;
                                                    }
                                                }
                                                catch (Exception e)
                                                {
                                                    NotifyAboutError(e);
                                                    throw new ChangeProcessingException(e);
                                                }
                                            }
                                        }
                                    }
                }
            }
        }
示例#22
0
        public void JsonDeserialize_WhenHasBlittableObjectPropertyAndWriteAndReadFromStream_ShouldResultInCommandWithTheProperty()
        {
            using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
            {
                var jsonSerializer = new JsonSerializer
                {
                    ContractResolver = new DefaultRavenContractResolver(DocumentConventions.Default.Serialization),
                };
                jsonSerializer.Converters.Add(BlittableJsonConverter.Instance);

                var data     = new { SomeProperty = "SomeValue" };
                var expected = DocumentConventions.Default.Serialization.DefaultConverter.ToBlittable(data, context);
                var command  = new Command {
                    BlittableObject = expected
                };

                //Serialize
                BlittableJsonReaderObject toStream;
                using (var writer = new BlittableJsonWriter(context))
                {
                    jsonSerializer.Serialize(writer, command);
                    writer.FinalizeDocument();

                    toStream = writer.CreateReader();
                }

                //Simulates copying to file and loading
                BlittableJsonReaderObject fromStream;
                using (Stream stream = new MemoryStream())
                {
                    //Pass to stream
                    using (var textWriter = new BlittableJsonTextWriter(context, stream))
                    {
                        context.Write(textWriter, toStream);
                    }

                    //Get from stream
                    stream.Position = 0;

                    var state            = new JsonParserState();
                    var parser           = new UnmanagedJsonParser(context, state, "some tag");
                    var peepingTomStream = new PeepingTomStream(stream, context);

                    using (context.GetMemoryBuffer(out var buffer))
                        using (var builder =
                                   new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, "some tag", parser, state))
                        {
                            UnmanagedJsonParserHelper.Read(peepingTomStream, parser, state, buffer);
                            UnmanagedJsonParserHelper.ReadObject(builder, peepingTomStream, parser, buffer);

                            fromStream = builder.CreateReader();
                        }
                }

                //Deserialize
                BlittableJsonReaderObject actual;
                using (var reader = new BlittableJsonReader(context))
                {
                    reader.Initialize(fromStream);
                    var deserialized = jsonSerializer.Deserialize <Command>(reader);
                    actual = deserialized.BlittableObject;
                }

                Assert.Equal(expected, actual);
            }
        }
示例#23
0
        private async Task ProcessChanges()
        {
            using (_requestExecutor.ContextPool.AllocateOperationContext(out var context))
            {
                while (_cts.IsCancellationRequested == false)
                {
                    var state = new JsonParserState();

                    using (var stream = new WebSocketStream(_client, _cts.Token))
                        using (context.GetManagedBuffer(out var buffer))
                            using (var parser = new UnmanagedJsonParser(context, state, "changes/receive"))
                                using (var builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, "readArray/singleResult", parser, state))
                                    using (var peepingTomStream = new PeepingTomStream(stream, context))
                                    {
                                        if (await UnmanagedJsonParserHelper.ReadAsync(peepingTomStream, parser, state, buffer).ConfigureAwait(false) == false)
                                        {
                                            continue;
                                        }

                                        if (state.CurrentTokenType != JsonParserToken.StartArray)
                                        {
                                            continue;
                                        }

                                        while (true)
                                        {
                                            if (await UnmanagedJsonParserHelper.ReadAsync(peepingTomStream, parser, state, buffer).ConfigureAwait(false) == false)
                                            {
                                                continue;
                                            }

                                            if (state.CurrentTokenType == JsonParserToken.EndArray)
                                            {
                                                break;
                                            }

                                            builder.Renew("changes/receive", BlittableJsonDocumentBuilder.UsageMode.None);

                                            await UnmanagedJsonParserHelper.ReadObjectAsync(builder, peepingTomStream, parser, buffer).ConfigureAwait(false);

                                            var json = builder.CreateReader();

                                            try
                                            {
                                                if (json.TryGet("Type", out string type) == false)
                                                {
                                                    continue;
                                                }

                                                switch (type)
                                                {
                                                case "Error":
                                                    json.TryGet("Exception", out string exceptionAsString);
                                                    NotifyAboutError(new Exception(exceptionAsString));
                                                    break;

                                                case "Confirm":
                                                    if (json.TryGet("CommandId", out int commandId) &&
                                                        _confirmations.TryRemove(commandId, out var tcs))
                                                    {
                                                        tcs.TrySetResult(null);
                                                    }
                                                    break;

                                                default:
                                                    json.TryGet("Value", out BlittableJsonReaderObject value);
                                                    NotifySubscribers(type, value, _counters.ValuesSnapshot);
                                                    break;
                                                }
                                            }
                                            catch (Exception e)
                                            {
                                                NotifyAboutError(e);
                                                throw new ChangeProcessingException(e);
                                            }
                                        }
                                    }
                }
            }
        }
示例#24
0
        public async Task ConnectInternal()
        {
            try
            {
                var urlBuilder = new StringBuilder(_url).Append("/admin/traffic-watch");

                if (string.IsNullOrWhiteSpace(_database) == false)
                {
                    urlBuilder.Append("?resourceName=").Append(_database);
                }

                var stringUrl = ToWebSocketPath(urlBuilder.ToString().ToLower());
                var url       = new Uri(stringUrl, UriKind.Absolute);
                _client = new ClientWebSocket();
                if (_cert != null)
                {
                    _client.Options.ClientCertificates.Add(_cert);
                }

                await _client.ConnectAsync(url, _cancellationTokenSource.Token).ConfigureAwait(false);

                _firstConnection = false;

                Console.WriteLine($"Connected to RavenDB server. Collecting traffic watch entries to {_path}");

                const int maxFileSize = 128 * 1024 * 1024;
                while (_cancellationTokenSource.IsCancellationRequested == false)
                {
                    string file = Path.Combine(_path, _logNameCreator.GetNewFileName());

                    var state = new JsonParserState();

                    using (var context = JsonOperationContext.ShortTermSingleUse())
                        // Read
                        await using (var stream = new WebSocketStream(_client, _cancellationTokenSource.Token))
                            using (context.GetMemoryBuffer(out var buffer))
                                using (var parser = new UnmanagedJsonParser(context, state, "trafficwatch/receive"))
                                    using (var builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, "readObject/singleResult", parser, state))
                                        // Write
                                        await using (var fileStream = new FileStream(file, FileMode.Append, FileAccess.Write, FileShare.Read, 32 * 1024, false))
                                            await using (var gZipStream = new GZipStream(fileStream, CompressionMode.Compress, false))
                                                using (var peepingTomStream = new PeepingTomStream(stream, context))
                                                    await using (var writer = new AsyncBlittableJsonTextWriter(context, gZipStream))
                                                    {
                                                        writer.WriteStartArray();
                                                        var isFirst = true;

                                                        while (fileStream.Length < maxFileSize)
                                                        {
                                                            if (_cancellationTokenSource.IsCancellationRequested)
                                                            {
                                                                writer.WriteEndArray();
                                                                break;
                                                            }

                                                            try
                                                            {
                                                                var flushCount = 0;
                                                                while (fileStream.Length < maxFileSize && _cancellationTokenSource.IsCancellationRequested == false)
                                                                {
                                                                    builder.Reset();
                                                                    builder.Renew("trafficwatch/receive", BlittableJsonDocumentBuilder.UsageMode.None);

                                                                    if (await UnmanagedJsonParserHelper.ReadAsync(peepingTomStream, parser, state, buffer).ConfigureAwait(false) == false)
                                                                    {
                                                                        continue;
                                                                    }

                                                                    await UnmanagedJsonParserHelper.ReadObjectAsync(builder, peepingTomStream, parser, buffer).ConfigureAwait(false);

                                                                    using (var json = builder.CreateReader())
                                                                    {
                                                                        if (_changeTypes != null)
                                                                        {
                                                                            if (json.TryGet("Type", out TrafficWatchChangeType type) == false)
                                                                            {
                                                                                continue;
                                                                            }
                                                                            if (_changeTypes.Contains(type) == false)
                                                                            {
                                                                                continue;
                                                                            }
                                                                        }

                                                                        if (_database != null)
                                                                        {
                                                                            if (json.TryGet("DatabaseName", out LazyStringValue databaseName) == false ||
                                                                                _database.Equals(databaseName, StringComparison.OrdinalIgnoreCase) == false)
                                                                            {
                                                                                continue;
                                                                            }
                                                                        }

                                                                        if (isFirst == false)
                                                                        {
                                                                            writer.WriteComma();
                                                                        }

                                                                        isFirst = false;
                                                                        if (_verbose)
                                                                        {
                                                                            Console.WriteLine(json);
                                                                        }

                                                                        writer.WriteObject(json);
                                                                        _errorCount = 0;
                                                                        if (flushCount++ % 128 == 0)
                                                                        {
                                                                            await writer.FlushAsync();
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                            catch (Exception)
                                                            {
                                                                writer.WriteEndArray();
                                                                throw;
                                                            }
                                                        }
                                                    }
                }
            }
            catch (ObjectDisposedException)
            {
                // closing
            }
        }
示例#25
0
 public static void Import(BlittableJsonDocumentBuilder builder, DocumentDatabase database, long buildVersion, bool removeAnalyzers)
 {
     using (var reader = builder.CreateReader())
         Import(reader, database, buildVersion, removeAnalyzers);
 }
示例#26
0
        private async Task ListenForCommands()
        {
            await _clusterDashboardNotifications.EnsureWatcher(); // in current impl cluster dashboard senders talk to a single watcher

            using (_readContext.GetMemoryBuffer(out JsonOperationContext.MemoryBuffer segment1))
                using (_readContext.GetMemoryBuffer(out JsonOperationContext.MemoryBuffer segment2))
                {
                    try
                    {
                        var segments        = new[] { segment1, segment2 };
                        int index           = 0;
                        var receiveAsync    = _webSocket.ReceiveAsync(segments[index].Memory.Memory, _resourceShutdown);
                        var jsonParserState = new JsonParserState();
                        using (var parser = new UnmanagedJsonParser(_readContext, jsonParserState, "cluster-dashboard"))
                        {
                            var result = await receiveAsync;
                            _resourceShutdown.ThrowIfCancellationRequested();

                            parser.SetBuffer(segments[index], 0, result.Count);
                            index++;
                            receiveAsync = _webSocket.ReceiveAsync(segments[index].Memory.Memory, _resourceShutdown);

                            while (true)
                            {
                                using (var builder = new BlittableJsonDocumentBuilder(_readContext, BlittableJsonDocumentBuilder.UsageMode.None,
                                                                                      "cluster-dashboard", parser, jsonParserState))
                                {
                                    parser.NewDocument();
                                    builder.ReadObjectDocument();

                                    while (builder.Read() == false)
                                    {
                                        result = await receiveAsync;
                                        _resourceShutdown.ThrowIfCancellationRequested();

                                        parser.SetBuffer(segments[index], 0, result.Count);
                                        if (++index >= segments.Length)
                                        {
                                            index = 0;
                                        }

                                        receiveAsync = _webSocket.ReceiveAsync(segments[index].Memory.Memory, _resourceShutdown);
                                    }

                                    builder.FinalizeDocument();

                                    using (var reader = builder.CreateReader())
                                    {
                                        await HandleCommand(reader);
                                    }
                                }
                            }
                        }
                    }
                    catch (IOException ex)
                    {
                        /* Client was disconnected, write to log */
                        if (Logger.IsInfoEnabled)
                        {
                            Logger.Info("Client was disconnected", ex);
                        }
                    }
                    catch (Exception ex)
                    {
                        // if we received close from the client, we want to ignore it and close the websocket (dispose does it)
                        if (ex is WebSocketException webSocketException &&
                            webSocketException.WebSocketErrorCode == WebSocketError.InvalidState &&
                            _webSocket.State == WebSocketState.CloseReceived)
                        {
                            // ignore
                        }
示例#27
0
        public async Task <ImportResult> Import(DocumentsOperationContext context, Stream stream, Action <IOperationProgress> onProgress = null)
        {
            var result   = new ImportResult();
            var progress = new IndeterminateProgress();
            var state    = new JsonParserState();

            JsonOperationContext.ManagedPinnedBuffer buffer;
            using (context.GetManagedBuffer(out buffer))
                using (var parser = new UnmanagedJsonParser(context, state, "fileName"))
                {
                    var operateOnType = "__top_start_object";
                    var buildVersion  = 0L;
                    var identities    = new Dictionary <string, long>();
                    VersioningStorage versioningStorage = null;

                    while (true)
                    {
                        if (parser.Read() == false)
                        {
                            var read = await stream.ReadAsync(buffer.Buffer.Array, buffer.Buffer.Offset, buffer.Length);

                            if (read == 0)
                            {
                                if (state.CurrentTokenType != JsonParserToken.EndObject)
                                {
                                    throw new EndOfStreamException("Stream ended without reaching end of json content");
                                }
                                break;
                            }
                            parser.SetBuffer(buffer, read);
                            continue;
                        }

                        switch (state.CurrentTokenType)
                        {
                        case JsonParserToken.String:
                            unsafe
                            {
                                operateOnType =
                                    new LazyStringValue(null, state.StringBuffer, state.StringSize, context).ToString();
                            }
                            break;

                        case JsonParserToken.Integer:
                            switch (operateOnType)
                            {
                            case "BuildVersion":
                                buildVersion = state.Long;
                                break;
                            }
                            break;

                        case JsonParserToken.StartObject:
                            if (operateOnType == "__top_start_object")
                            {
                                operateOnType = null;
                                break;
                            }
                            context.CachedProperties.NewDocument();
                            var builder = new BlittableJsonDocumentBuilder(_batchPutCommand.Context, BlittableJsonDocumentBuilder.UsageMode.ToDisk, "ImportObject", parser, state);
                            builder.ReadNestedObject();
                            while (builder.Read() == false)
                            {
                                var read = await stream.ReadAsync(buffer.Buffer.Array, buffer.Buffer.Offset, buffer.Length);

                                if (read == 0)
                                {
                                    throw new EndOfStreamException("Stream ended without reaching end of json content");
                                }
                                parser.SetBuffer(buffer, read);
                            }
                            builder.FinalizeDocument();

                            if (operateOnType == "Docs" && Options.OperateOnTypes.HasFlag(DatabaseItemType.Documents))
                            {
                                progress.Progress = "Importing Documents";
                                onProgress?.Invoke(progress);
                                PatchDocument patch        = null;
                                PatchRequest  patchRequest = null;
                                if (string.IsNullOrWhiteSpace(Options.TransformScript) == false)
                                {
                                    patch        = new PatchDocument(context.DocumentDatabase);
                                    patchRequest = new PatchRequest
                                    {
                                        Script = Options.TransformScript
                                    };
                                }

                                result.DocumentsCount++;
                                var reader   = builder.CreateReader();
                                var document = new Document
                                {
                                    Data = reader,
                                };

                                if (Options.IncludeExpired == false && document.Expired(_database.Time.GetUtcNow()))
                                {
                                    continue;
                                }

                                TransformScriptOrDisableVersioningIfNeeded(context, patch, reader, document,
                                                                           patchRequest);

                                _batchPutCommand.Add(document.Data);

                                if (result.DocumentsCount % 1000 == 0)
                                {
                                    progress.Progress = $"Imported {result.DocumentsCount} documents";
                                    onProgress?.Invoke(progress);
                                }

                                await HandleBatchOfDocuments(context, parser, buildVersion).ConfigureAwait(false);
                            }
                            else if (operateOnType == "RevisionDocuments" &&
                                     Options.OperateOnTypes.HasFlag(DatabaseItemType.RevisionDocuments))
                            {
                                if (versioningStorage == null)
                                {
                                    break;
                                }

                                result.RevisionDocumentsCount++;
                                var reader = builder.CreateReader();
                                _batchPutCommand.Add(reader);
                                await HandleBatchOfDocuments(context, parser, buildVersion).ConfigureAwait(false);;
                            }
                            else
                            {
                                using (builder)
                                {
                                    switch (operateOnType)
                                    {
                                    case "Attachments":
                                        result.Warnings.Add("Attachments are not supported anymore. Use RavenFS isntead. Skipping.");
                                        break;

                                    case "Indexes":
                                        if (Options.OperateOnTypes.HasFlag(DatabaseItemType.Indexes) == false)
                                        {
                                            continue;
                                        }

                                        result.IndexesCount++;
                                        progress.Progress = "importing Indexes";
                                        onProgress?.Invoke(progress);
                                        try
                                        {
                                            IndexProcessor.Import(builder, _database, buildVersion, Options.RemoveAnalyzers);
                                        }
                                        catch (Exception e)
                                        {
                                            result.Warnings.Add($"Could not import index. Message: {e.Message}");
                                        }

                                        break;

                                    case "Transformers":
                                        if (Options.OperateOnTypes.HasFlag(DatabaseItemType.Transformers) == false)
                                        {
                                            continue;
                                        }

                                        result.TransformersCount++;
                                        progress.Progress = "Importing Transformers";
                                        onProgress?.Invoke(progress);

                                        try
                                        {
                                            TransformerProcessor.Import(builder, _database, buildVersion);
                                        }
                                        catch (Exception e)
                                        {
                                            result.Warnings.Add($"Could not import transformer. Message: {e.Message}");
                                        }
                                        break;

                                    case "Identities":
                                        if (Options.OperateOnTypes.HasFlag(DatabaseItemType.Identities))
                                        {
                                            result.IdentitiesCount++;
                                            progress.Progress = "Importing Identities";
                                            onProgress?.Invoke(progress);

                                            using (var reader = builder.CreateReader())
                                            {
                                                try
                                                {
                                                    string identityKey, identityValueString;
                                                    long   identityValue;
                                                    if (reader.TryGet("Key", out identityKey) == false || reader.TryGet("Value", out identityValueString) == false || long.TryParse(identityValueString, out identityValue) == false)
                                                    {
                                                        result.Warnings.Add($"Cannot import the following identity: '{reader}'. Skipping.");
                                                    }
                                                    else
                                                    {
                                                        identities[identityKey] = identityValue;
                                                    }
                                                }
                                                catch (Exception e)
                                                {
                                                    result.Warnings.Add($"Cannot import the following identity: '{reader}'. Error: {e}. Skipping.");
                                                }
                                            }
                                        }
                                        break;

                                    default:
                                        result.Warnings.Add(
                                            $"The following type is not recognized: '{operateOnType}'. Skipping.");
                                        break;
                                    }
                                }
                            }
                            break;

                        case JsonParserToken.StartArray:
                            switch (operateOnType)
                            {
                            case "RevisionDocuments":
                                // We are taking a reference here since the documents import can activate or disable the versioning.
                                // We hold a local copy because the user can disable the bundle during the import process, exteranly.
                                // In this case we want to continue to import the revisions documents.
                                versioningStorage           = _database.BundleLoader.VersioningStorage;
                                _batchPutCommand.IsRevision = true;
                                break;
                            }
                            break;

                        case JsonParserToken.EndArray:
                            switch (operateOnType)
                            {
                            case "Docs":
                                await FinishBatchOfDocuments();

                                _batchPutCommand = new MergedBatchPutCommand(_database, buildVersion);
                                break;

                            case "RevisionDocuments":
                                await FinishBatchOfDocuments();

                                break;

                            case "Identities":
                                if (identities.Count > 0)
                                {
                                    using (var tx = context.OpenWriteTransaction())
                                    {
                                        _database.DocumentsStorage.UpdateIdentities(context, identities);
                                        tx.Commit();
                                    }
                                }
                                identities = null;
                                break;
                            }
                            break;
                        }
                    }
                }

            return(result);
        }
示例#28
0
        private async Task HandleConnection(WebSocket webSocket, JsonOperationContext context)
        {
            var fromStudio         = GetBoolValueQueryString(StudioMarker, false) ?? false;
            var throttleConnection = GetBoolValueQueryString("throttleConnection", false).GetValueOrDefault(false);

            var connection = new ChangesClientConnection(webSocket, Database, fromStudio);

            Database.Changes.Connect(connection);
            var sendTask = connection.StartSendingNotifications(throttleConnection);
            var debugTag = "changes/" + connection.Id;

            using (context.GetManagedBuffer(out JsonOperationContext.ManagedPinnedBuffer segment1))
                using (context.GetManagedBuffer(out JsonOperationContext.ManagedPinnedBuffer segment2))
                {
                    try
                    {
                        var segments        = new[] { segment1, segment2 };
                        int index           = 0;
                        var receiveAsync    = webSocket.ReceiveAsync(segments[index].Buffer, Database.DatabaseShutdown);
                        var jsonParserState = new JsonParserState();
                        using (var parser = new UnmanagedJsonParser(context, jsonParserState, debugTag))
                        {
                            var result = await receiveAsync;
                            Database.DatabaseShutdown.ThrowIfCancellationRequested();

                            parser.SetBuffer(segments[index], 0, result.Count);
                            index++;
                            receiveAsync = webSocket.ReceiveAsync(segments[index].Buffer, Database.DatabaseShutdown);

                            while (true)
                            {
                                using (var builder =
                                           new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, debugTag, parser, jsonParserState))
                                {
                                    parser.NewDocument();
                                    builder.ReadObjectDocument();

                                    while (builder.Read() == false)
                                    {
                                        result = await receiveAsync;
                                        Database.DatabaseShutdown.ThrowIfCancellationRequested();

                                        parser.SetBuffer(segments[index], 0, result.Count);
                                        if (++index >= segments.Length)
                                        {
                                            index = 0;
                                        }
                                        receiveAsync = webSocket.ReceiveAsync(segments[index].Buffer, Database.DatabaseShutdown);
                                    }

                                    builder.FinalizeDocument();

                                    using (var reader = builder.CreateReader())
                                    {
                                        if (reader.TryGet("Command", out string command) == false)
                                        {
                                            throw new ArgumentNullException(nameof(command), "Command argument is mandatory");
                                        }

                                        reader.TryGet("Param", out string commandParameter);
                                        reader.TryGet("Params", out BlittableJsonReaderArray commandParameters);

                                        connection.HandleCommand(command, commandParameter, commandParameters);

                                        if (reader.TryGet("CommandId", out int commandId))
                                        {
                                            connection.Confirm(commandId);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (IOException ex)
                    {
                        /* Client was disconnected, write to log */
                        if (Logger.IsInfoEnabled)
                        {
                            Logger.Info("Client was disconnected", ex);
                        }
                    }
                    catch
                    {
#pragma warning disable 4014
                        sendTask.IgnoreUnobservedExceptions();
#pragma warning restore 4014

                        throw;
                    }
                    finally
                    {
                        Database.Changes.Disconnect(connection.Id);
                    }
                }
            await sendTask;
        }
示例#29
0
        private async Task HandleConnection(WebSocket webSocket, JsonOperationContext context)
        {
            // this flag can be used to detect if server was restarted between changes connections on client side
            var sendStartTime      = GetBoolValueQueryString("sendServerStartTime", false).GetValueOrDefault(false);
            var throttleConnection = GetBoolValueQueryString("throttleConnection", false).GetValueOrDefault(false);

            var connection = new NotificationsClientConnection(webSocket, Database);

            Database.Notifications.Connect(connection);
            var sendTask = connection.StartSendingNotifications(sendStartTime, throttleConnection);
            var debugTag = "changes/" + connection.Id;

            JsonOperationContext.ManagedPinnedBuffer segment1, segment2;
            using (context.GetManagedBuffer(out segment1))
                using (context.GetManagedBuffer(out segment2))
                {
                    try
                    {
                        var segments        = new[] { segment1, segment2 };
                        int index           = 0;
                        var receiveAsync    = webSocket.ReceiveAsync(segments[index].Buffer, Database.DatabaseShutdown);
                        var jsonParserState = new JsonParserState();
                        using (var parser = new UnmanagedJsonParser(context, jsonParserState, debugTag))
                        {
                            var result = await receiveAsync;
                            parser.SetBuffer(segments[index], result.Count);
                            index++;
                            receiveAsync = webSocket.ReceiveAsync(segments[index].Buffer, Database.DatabaseShutdown);

                            while (true)
                            {
                                using (var builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, debugTag, parser, jsonParserState))
                                {
                                    parser.NewDocument();
                                    builder.ReadObjectDocument();

                                    while (builder.Read() == false)
                                    {
                                        result = await receiveAsync;

                                        parser.SetBuffer(segments[index], result.Count);
                                        if (++index >= segments.Length)
                                        {
                                            index = 0;
                                        }
                                        receiveAsync = webSocket.ReceiveAsync(segments[index].Buffer, Database.DatabaseShutdown);
                                    }

                                    builder.FinalizeDocument();

                                    using (var reader = builder.CreateReader())
                                    {
                                        string command, commandParameter;
                                        if (reader.TryGet("Command", out command) == false)
                                        {
                                            throw new ArgumentNullException(nameof(command), "Command argument is mandatory");
                                        }

                                        reader.TryGet("Param", out commandParameter);
                                        connection.HandleCommand(command, commandParameter);

                                        int commandId;
                                        if (reader.TryGet("CommandId", out commandId))
                                        {
                                            connection.Confirm(commandId);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (IOException ex)
                    {
                        /* Client was disconnected, write to log */
                        if (Logger.IsInfoEnabled)
                        {
                            Logger.Info("Client was disconnected", ex);
                        }
                    }
                    finally
                    {
                        Database.Notifications.Disconnect(connection.Id);
                    }
                }
            await sendTask;
        }
示例#30
0
        private static unsafe GetResponse ReadResponse(JsonOperationContext context, PeepingTomStream peepingTomStream, UnmanagedJsonParser parser, JsonParserState state, JsonOperationContext.MemoryBuffer buffer)
        {
            if (state.CurrentTokenType != JsonParserToken.StartObject)
            {
                ThrowInvalidJsonResponse(peepingTomStream);
            }

            var getResponse = new GetResponse();

            while (true)
            {
                if (UnmanagedJsonParserHelper.Read(peepingTomStream, parser, state, buffer) == false)
                {
                    ThrowInvalidJsonResponse(peepingTomStream);
                }

                if (state.CurrentTokenType == JsonParserToken.EndObject)
                {
                    break;
                }

                if (state.CurrentTokenType != JsonParserToken.String)
                {
                    ThrowInvalidJsonResponse(peepingTomStream);
                }

                var property = context.AllocateStringValue(null, state.StringBuffer, state.StringSize).ToString();
                switch (property)
                {
                case nameof(GetResponse.Result):
                    if (UnmanagedJsonParserHelper.Read(peepingTomStream, parser, state, buffer) == false)
                    {
                        ThrowInvalidJsonResponse(peepingTomStream);
                    }

                    if (state.CurrentTokenType == JsonParserToken.Null)
                    {
                        continue;
                    }

                    if (state.CurrentTokenType != JsonParserToken.StartObject)
                    {
                        ThrowInvalidJsonResponse(peepingTomStream);
                    }

                    using (var builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, "multi_get/result", parser, state))
                    {
                        UnmanagedJsonParserHelper.ReadObject(builder, peepingTomStream, parser, buffer);
                        getResponse.Result = builder.CreateReader();
                    }
                    continue;

                case nameof(GetResponse.Headers):
                    if (UnmanagedJsonParserHelper.Read(peepingTomStream, parser, state, buffer) == false)
                    {
                        ThrowInvalidJsonResponse(peepingTomStream);
                    }

                    if (state.CurrentTokenType == JsonParserToken.Null)
                    {
                        continue;
                    }

                    if (state.CurrentTokenType != JsonParserToken.StartObject)
                    {
                        ThrowInvalidJsonResponse(peepingTomStream);
                    }

                    using (var builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, "multi_get/result", parser, state))
                    {
                        UnmanagedJsonParserHelper.ReadObject(builder, peepingTomStream, parser, buffer);
                        using (var headersJson = builder.CreateReader())
                        {
                            foreach (var propertyName in headersJson.GetPropertyNames())
                            {
                                getResponse.Headers[propertyName] = headersJson[propertyName].ToString();
                            }
                        }
                    }
                    continue;

                case nameof(GetResponse.StatusCode):
                    if (UnmanagedJsonParserHelper.Read(peepingTomStream, parser, state, buffer) == false)
                    {
                        ThrowInvalidJsonResponse(peepingTomStream);
                    }

                    if (state.CurrentTokenType != JsonParserToken.Integer)
                    {
                        ThrowInvalidJsonResponse(peepingTomStream);
                    }

                    getResponse.StatusCode = (HttpStatusCode)state.Long;
                    continue;

                default:
                    ThrowInvalidJsonResponse(peepingTomStream);
                    break;
                }
            }

            return(getResponse);
        }