Exemplo n.º 1
0
        public async Task <Operation> ExportAsync(DatabaseSmugglerExportOptions options, DatabaseSmuggler toDatabase, CancellationToken token = default)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (toDatabase == null)
            {
                throw new ArgumentNullException(nameof(toDatabase));
            }

            Operation operation     = null;
            var       importOptions = new DatabaseSmugglerImportOptions(options);

            var tcs = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously);

            await ExportAsync(options, async stream =>
            {
                try
                {
                    operation = await toDatabase.ImportAsync(importOptions, stream, token).ConfigureAwait(false);
                    tcs.TrySetResult(null);
                }
                catch (Exception e)
                {
                    tcs.TrySetException(e);
                    throw;
                }
            }, token).ConfigureAwait(false);

            await tcs.Task.ConfigureAwait(false);

            return(operation);
        }
Exemplo n.º 2
0
 public Task <Operation> ExportAsync(DatabaseSmugglerExportOptions options, string toFile, CancellationToken token = default)
 {
     return(ExportAsync(options, async stream =>
     {
         using (var file = File.OpenWrite(toFile))
             await stream.CopyToAsync(file, 8192, token).ConfigureAwait(false);
     }, token));
 }
Exemplo n.º 3
0
        private async Task <Operation> ExportAsync(DatabaseSmugglerExportOptions options, Func <Stream, Task> handleStreamResponse, Task additionalTask, CancellationToken token = default)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (_requestExecutor == null)
            {
                throw new InvalidOperationException("Cannot use Smuggler without a database defined, did you forget to call ForDatabase?");
            }

            using (_requestExecutor.ContextPool.AllocateOperationContext(out JsonOperationContext context))
            {
                var getOperationIdCommand = new GetNextOperationIdCommand();
                await _requestExecutor.ExecuteAsync(getOperationIdCommand, context, sessionInfo : null, token : token).ConfigureAwait(false);

                var operationId = getOperationIdCommand.Result;

                var tcs = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously);
                var cancellationTokenRegistration = token.Register(() => tcs.TrySetCanceled(token));

                var command     = new ExportCommand(_requestExecutor.Conventions, context, options, handleStreamResponse, operationId, tcs);
                var requestTask = _requestExecutor.ExecuteAsync(command, context, sessionInfo: null, token: token)
                                  .ContinueWith(t =>
                {
                    cancellationTokenRegistration.Dispose();
                    if (t.IsFaulted)
                    {
                        tcs.TrySetException(t.Exception);

                        if (Logger.IsOperationsEnabled)
                        {
                            Logger.Operations("Could not execute export", t.Exception);
                        }
                    }
                }, token);

                try
                {
                    await tcs.Task.ConfigureAwait(false);
                }
                catch (Exception)
                {
                    await requestTask.ConfigureAwait(false);

                    await tcs.Task.ConfigureAwait(false);
                }

                return(new Operation(
                           _requestExecutor,
                           () => _store.Changes(_databaseName),
                           _requestExecutor.Conventions,
                           operationId,
                           null,
                           additionalTask));
            }
        }
Exemplo n.º 4
0
        public async Task <Operation> ExportAsync(DatabaseSmugglerExportOptions options, string toFile, CancellationToken token = default(CancellationToken))
        {
            using (var file = File.OpenWrite(toFile))
            {
                var result = await ExportAsync(options, async stream =>
                {
                    await stream.CopyToAsync(file, 8192, token).ConfigureAwait(false);
                }, token).ConfigureAwait(false);

                await file.FlushAsync(token).ConfigureAwait(false);

                return(result);
            }
        }
Exemplo n.º 5
0
 public ExportCommand(JsonOperationContext context, DatabaseSmugglerExportOptions options, Func <Stream, Task> handleStreamResponse, long operationId, TaskCompletionSource <object> tcs)
 {
     if (options == null)
     {
         throw new ArgumentNullException(nameof(options));
     }
     if (context == null)
     {
         throw new ArgumentNullException(nameof(context));
     }
     _handleStreamResponse = handleStreamResponse ?? throw new ArgumentNullException(nameof(handleStreamResponse));
     _options     = DocumentConventions.Default.Serialization.DefaultConverter.ToBlittable(options, context);
     _operationId = operationId;
     _tcs         = tcs ?? throw new ArgumentNullException(nameof(tcs));
 }
Exemplo n.º 6
0
        public async Task <Operation> ExportAsync(DatabaseSmugglerExportOptions options, DatabaseSmuggler toDatabase, CancellationToken token = default(CancellationToken))
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (toDatabase == null)
            {
                throw new ArgumentNullException(nameof(toDatabase));
            }

            var importOptions = new DatabaseSmugglerImportOptions(options);
            var result        = await ExportAsync(options, async stream =>
            {
                await toDatabase.ImportAsync(importOptions, stream, token).ConfigureAwait(false);
            }, token).ConfigureAwait(false);

            return(result);
        }
Exemplo n.º 7
0
        private async Task <Operation> ExportAsync(DatabaseSmugglerExportOptions options, Func <Stream, Task> handleStreamResponse, CancellationToken token = default(CancellationToken))
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            using (_requestExecutor.ContextPool.AllocateOperationContext(out JsonOperationContext context))
            {
                var getOperationIdCommand = new GetNextOperationIdCommand();
                await _requestExecutor.ExecuteAsync(getOperationIdCommand, context, token).ConfigureAwait(false);

                var operationId = getOperationIdCommand.Result;

                var command = new ExportCommand(_requestExecutor.Conventions, context, options, handleStreamResponse, operationId);
                await _requestExecutor.ExecuteAsync(command, context, token).ConfigureAwait(false);

                return(new Operation(_requestExecutor, () => _store.Changes(), _requestExecutor.Conventions, operationId));
            }
        }
Exemplo n.º 8
0
        public Task <Operation> ExportAsync(DatabaseSmugglerExportOptions options, string toFile, CancellationToken token = default)
        {
            var tcs = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously);

            return(ExportAsync(options, async stream =>
            {
                try
                {
                    var fileInfo = new FileInfo(toFile);
                    var directoryInfo = fileInfo.Directory;
                    if (directoryInfo != null && directoryInfo.Exists == false)
                    {
                        directoryInfo.Create();
                    }

                    using (var fileStream = fileInfo.OpenWrite())
                        await stream.CopyToAsync(fileStream, 8192, token).ConfigureAwait(false);

                    tcs.TrySetResult(null);
                }
                catch (Exception e)
                {
                    if (Logger.IsOperationsEnabled)
                    {
                        Logger.Operations("Could not save export file.", e);
                    }

                    tcs.TrySetException(e);

                    if (e is UnauthorizedAccessException || e is DirectoryNotFoundException || e is IOException)
                    {
                        throw new InvalidOperationException($"Cannot export to selected path {toFile}, please ensure you selected proper filename.", e);
                    }

                    throw new InvalidOperationException($"Could not save export file {toFile}.", e);
                }
            }, tcs.Task, token));
        }
Exemplo n.º 9
0
        private async Task <Operation> ExportAsync(DatabaseSmugglerExportOptions options, Func <Stream, Task> handleStreamResponse, CancellationToken token = default)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (_requestExecutor == null)
            {
                throw new InvalidOperationException("Cannot use Smuggler without a database defined, did you forget to call ForDatabase?");
            }

            using (_requestExecutor.ContextPool.AllocateOperationContext(out JsonOperationContext context))
            {
                var getOperationIdCommand = new GetNextOperationIdCommand();
                await _requestExecutor.ExecuteAsync(getOperationIdCommand, context, sessionInfo : null, token : token).ConfigureAwait(false);

                var operationId = getOperationIdCommand.Result;

                var command = new ExportCommand(_requestExecutor.Conventions, context, options, handleStreamResponse, operationId);
                await _requestExecutor.ExecuteAsync(command, context, sessionInfo : null, token : token).ConfigureAwait(false);

                return(new Operation(_requestExecutor, () => _store.Changes(_databaseName), _requestExecutor.Conventions, operationId));
            }
        }
Exemplo n.º 10
0
 private Task <Operation> ExportAsync(DatabaseSmugglerExportOptions options, Func <Stream, Task> handleStreamResponse, CancellationToken token = default)
 {
     return(ExportAsync(options, handleStreamResponse, null, token));
 }
Exemplo n.º 11
0
 public ExportCommand(DocumentConventions conventions, JsonOperationContext context, DatabaseSmugglerExportOptions options, Func <Stream, Task> handleStreamResponse, long operationId)
 {
     if (conventions == null)
     {
         throw new ArgumentNullException(nameof(conventions));
     }
     if (options == null)
     {
         throw new ArgumentNullException(nameof(options));
     }
     if (context == null)
     {
         throw new ArgumentNullException(nameof(context));
     }
     _handleStreamResponse = handleStreamResponse ?? throw new ArgumentNullException(nameof(handleStreamResponse));
     _options     = EntityToBlittable.ConvertEntityToBlittable(options, conventions, context);
     _operationId = operationId;
 }
Exemplo n.º 12
0
 public ExportCommand(DocumentConventions conventions, JsonOperationContext context, DatabaseSmugglerExportOptions options, Func <Stream, Task> handleStreamResponse, long operationId, TaskCompletionSource <object> tcs, string nodeTag)
 {
     if (conventions == null)
     {
         throw new ArgumentNullException(nameof(conventions));
     }
     if (options == null)
     {
         throw new ArgumentNullException(nameof(options));
     }
     if (context == null)
     {
         throw new ArgumentNullException(nameof(context));
     }
     _handleStreamResponse = handleStreamResponse ?? throw new ArgumentNullException(nameof(handleStreamResponse));
     _options        = EntityToBlittable.ConvertCommandToBlittable(options, context);
     _operationId    = operationId;
     _tcs            = tcs ?? throw new ArgumentNullException(nameof(tcs));
     SelectedNodeTag = nodeTag;
 }