示例#1
0
        public IEnumerator <BlittableJsonReaderObject> SetResult(StreamResult response)
        {
            if (response == null)
            {
                throw new InvalidOperationException("The index does not exists, failed to stream results");
            }

            var state = new JsonParserState();

            JsonOperationContext.ManagedPinnedBuffer buffer;

            using (response.Response)
                using (response.Stream)
                    using (var parser = new UnmanagedJsonParser(_session.Context, state, "stream contents"))
                        using (_session.Context.GetManagedBuffer(out buffer))
                            using (var peepingTomStream = new PeepingTomStream(response.Stream, _session.Context))
                            {
                                if (UnmanagedJsonParserHelper.Read(peepingTomStream, parser, state, buffer) == false)
                                {
                                    UnmanagedJsonParserHelper.ThrowInvalidJson(peepingTomStream);
                                }

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


                                if (_isQueryStream)
                                {
                                    HandleStreamQueryStats(_session.Context, response, parser, state, buffer, _statistics);
                                }

                                var property = UnmanagedJsonParserHelper.ReadString(_session.Context, peepingTomStream, parser, state, buffer);
                                if (string.Equals(property, "Results") == false)
                                {
                                    UnmanagedJsonParserHelper.ThrowInvalidJson(peepingTomStream);
                                }

                                foreach (var result in UnmanagedJsonParserHelper.ReadArrayToMemory(_session.Context, peepingTomStream, parser, state, buffer))
                                {
                                    yield return(result);
                                }

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

                                if (state.CurrentTokenType != JsonParserToken.EndObject)
                                {
                                    UnmanagedJsonParserHelper.ThrowInvalidJson(peepingTomStream);
                                }
                            }
        }
        internal static IEnumerable <ReplayProgress> Replay(DocumentDatabase database, Stream replayStream)
        {
            DocumentsOperationContext txCtx   = null;
            IDisposable          txDisposable = null;
            DocumentsTransaction previousTx   = null;

            using (database.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                using (context.GetManagedBuffer(out var buffer))
                    using (var gZipStream = new GZipStream(replayStream, CompressionMode.Decompress, leaveOpen: true))
                    {
                        var peepingTomStream = new PeepingTomStream(gZipStream, context);
                        var state            = new JsonParserState();
                        var parser           = new UnmanagedJsonParser(context, state, "file");

                        var commandsProgress = 0;
                        var readers          = UnmanagedJsonParserHelper.ReadArrayToMemory(context, peepingTomStream, parser, state, buffer);
                        using (var readersItr = readers.GetEnumerator())
                        {
                            ReadStartRecordingDetails(readersItr, context, peepingTomStream);
                            while (readersItr.MoveNext())
                            {
                                using (readersItr.Current)
                                {
                                    if (readersItr.Current.TryGet(nameof(RecordingDetails.Type), out string strType) == false)
                                    {
                                        throw new ReplayTransactionsException($"Can't read {nameof(RecordingDetails.Type)} of replay detail", peepingTomStream);
                                    }

                                    if (Enum.TryParse <TxInstruction>(strType, true, out var type))
                                    {
                                        switch (type)
                                        {
                                        case TxInstruction.BeginTx:
                                            txDisposable = database.DocumentsStorage.ContextPool.AllocateOperationContext(out txCtx);
                                            txCtx.OpenWriteTransaction();
                                            break;

                                        case TxInstruction.Commit:
                                            txCtx.Transaction.Commit();
                                            break;

                                        case TxInstruction.DisposeTx:
                                            txDisposable.Dispose();
                                            break;

                                        case TxInstruction.BeginAsyncCommitAndStartNewTransaction:
                                            previousTx        = txCtx.Transaction;
                                            txCtx.Transaction = txCtx.Transaction.BeginAsyncCommitAndStartNewTransaction(txCtx);
                                            txDisposable      = txCtx.Transaction;
                                            break;

                                        case TxInstruction.EndAsyncCommit:
                                            previousTx.EndAsyncCommit();
                                            break;

                                        case TxInstruction.DisposePrevTx:
                                            previousTx.Dispose();
                                            break;
                                        }
                                        continue;
                                    }

                                    try
                                    {
                                        var cmd = DeserializeCommand(context, database, strType, readersItr.Current, peepingTomStream);
                                        commandsProgress += cmd.ExecuteDirectly(txCtx);
                                        TransactionOperationsMerger.UpdateGlobalReplicationInfoBeforeCommit(txCtx);
                                    }
                                    catch (Exception)
                                    {
                                        //TODO To accept exceptions that was thrown while recording
                                        txDisposable.Dispose();
                                        throw;
                                    }

                                    yield return(new ReplayProgress
                                    {
                                        CommandsProgress = commandsProgress
                                    });
                                }
                            }
                        }
                    }
        }