コード例 #1
0
        public async Task WhenOptimizeImgWithRigidDiskBlockThenSizeIsChanged()
        {
            // arrange
            var path = $"{Guid.NewGuid()}.img";
            var rigidDiskBlockSize = 8192;
            var fakeCommandHelper  = new FakeCommandHelper(rigidDiskBlock: new RigidDiskBlock
            {
                DiskSize = rigidDiskBlockSize
            });

            // var bytes = fakeCommandHelper.CreateTestData();
            fakeCommandHelper.WriteableMedias.Add(new Media(path, path, rigidDiskBlockSize, Media.MediaType.Raw, false,
                                                            new MemoryStream(new byte[16384])));
            var cancellationTokenSource = new CancellationTokenSource();

            // optimize
            var optimizeCommand = new OptimizeCommand(new NullLogger <OptimizeCommand>(), fakeCommandHelper, path);
            var result          = await optimizeCommand.Execute(cancellationTokenSource.Token);

            Assert.True(result.IsSuccess);

            // assert media contains optimized rigid disk block size
            var optimizedBytes = fakeCommandHelper.GetMedia(path).GetBytes();

            Assert.Equal(rigidDiskBlockSize, optimizedBytes.Length);
        }
コード例 #2
0
        /// <summary>
        /// Optimizes Solr's index
        /// </summary>
        /// <param name="waitFlush">Wait for flush</param>
        /// <param name="waitSearcher">Wait for new searcher</param>
        /// <param name="expungeDeletes">Merge segments with deletes away</param>
        /// <param name="maxSegments">Optimizes down to, at most, this number of segments</param>
        public static void Optimize(bool waitFlush, bool waitSearcher, bool expungeDeletes, int maxSegments)
        {
            var cmd = new OptimizeCommand {
                WaitFlush = waitFlush, WaitSearcher = waitSearcher, ExpungeDeletes = expungeDeletes, MaxSegments = maxSegments
            };

            cmd.Execute(Connection);
        }
コード例 #3
0
        /// <summary>
        /// Optimizes Solr's index
        /// </summary>
        /// <param name="waitFlush">Wait for flush</param>
        /// <param name="waitSearcher">Wait for new searcher</param>
        public static void Optimize(bool waitFlush, bool waitSearcher)
        {
            var cmd = new OptimizeCommand {
                WaitFlush = waitFlush, WaitSearcher = waitSearcher
            };

            cmd.Execute(Connection);
        }
コード例 #4
0
        public ResponseHeader Optimize(CommitOptions options)
        {
            options = options ?? new CommitOptions();
            var cmd = new OptimizeCommand {
                WaitFlush      = options.WaitFlush,
                WaitSearcher   = options.WaitSearcher,
                ExpungeDeletes = options.ExpungeDeletes,
                MaxSegments    = options.MaxSegments,
            };

            return(SendAndParseHeader(cmd));
        }
コード例 #5
0
        private static OptimizeCommand GetOptimizeCommand(CommitOptions options)
        {
            options = options ?? new CommitOptions();
            var cmd = new OptimizeCommand
            {
                WaitFlush      = options.WaitFlush,
                WaitSearcher   = options.WaitSearcher,
                ExpungeDeletes = options.ExpungeDeletes,
                MaxSegments    = options.MaxSegments,
            };

            return(cmd);
        }
コード例 #6
0
ファイル: OptimizeCommandTests.cs プロジェクト: yorah/SolrNet
        public void ExecuteBasic()
        {
            var conn = new MSolrConnection();

            conn.post += (url, content) => {
                Assert.AreEqual("/update", url);
                Assert.AreEqual("<optimize />", content);
                Console.WriteLine(content);
                return(null);
            };
            var cmd = new OptimizeCommand();

            cmd.Execute(conn);
            Assert.AreEqual(1, conn.post.Calls);
        }
コード例 #7
0
ファイル: OptimizeCommandTests.cs プロジェクト: yorah/SolrNet
        public void ExecuteWithBasicOptions()
        {
            var conn = new MSolrConnection();

            conn.post += (url, content) => {
                Assert.AreEqual("/update", url);
                Assert.AreEqual("<optimize waitSearcher=\"true\" waitFlush=\"true\" />", content);
                Console.WriteLine(content);
                return(null);
            };
            var cmd = new OptimizeCommand {
                WaitFlush    = true,
                WaitSearcher = true
            };

            cmd.Execute(conn);
            Assert.AreEqual(1, conn.post.Calls);
        }
コード例 #8
0
        public async ValueTask Handle(IBackgroundTaskContext context)
        {
            if (context.BackgroundTask is not OptimizeBackgroundTask optimizeBackgroundTask)
            {
                return;
            }

            try
            {
                await progressHubContext.SendProgress(new Progress
                {
                    Title           = optimizeBackgroundTask.Title,
                    IsComplete      = false,
                    PercentComplete = 50,
                }, context.Token);

                var commandHelper   = new CommandHelper();
                var optimizeCommand = new OptimizeCommand(loggerFactory.CreateLogger <OptimizeCommand>(), commandHelper, optimizeBackgroundTask.Path);

                var result = await optimizeCommand.Execute(context.Token);

                await Task.Delay(1000, context.Token);

                await progressHubContext.SendProgress(new Progress
                {
                    Title           = optimizeBackgroundTask.Title,
                    IsComplete      = true,
                    HasError        = result.IsFaulted,
                    ErrorMessage    = result.IsFaulted ? result.Error.Message : null,
                    PercentComplete = 100
                }, context.Token);
            }
            catch (Exception e)
            {
                await progressHubContext.SendProgress(new Progress
                {
                    Title           = optimizeBackgroundTask.Title,
                    IsComplete      = true,
                    HasError        = true,
                    ErrorMessage    = e.Message,
                    PercentComplete = 100
                }, context.Token);
            }
        }
コード例 #9
0
        public void ExecuteBasic()
        {
            var mocks = new MockRepository();
            var conn  = mocks.StrictMock <ISolrConnection>();

            With.Mocks(mocks).Expecting(() =>
            {
                conn.Post("/update", "<optimize />");
                LastCall.On(conn).Repeat.Once().Do(new Writer(delegate(string ignored, string s)
                {
                    Console.WriteLine(s);
                    return(null);
                }));
            }).Verify(() =>
            {
                var cmd = new OptimizeCommand();
                cmd.Execute(conn);
            });
        }
コード例 #10
0
        public void ExecuteWithAllOptions()
        {
            var conn = new MSolrConnection();

            conn.post += (url, content) => {
                Assert.Equal("/update", url);
                Assert.Equal("<optimize waitSearcher=\"true\" waitFlush=\"true\" expungeDeletes=\"true\" maxSegments=\"2\" />", content);
                testOutputHelper.WriteLine(content);
                return(null);
            };
            var cmd = new OptimizeCommand {
                MaxSegments    = 2,
                ExpungeDeletes = true,
                WaitFlush      = true,
                WaitSearcher   = true
            };

            cmd.Execute(conn);

            Assert.Equal(1, conn.post.Calls);
        }
コード例 #11
0
        public void ExecuteWithAllOptions()
        {
            var mocks = new MockRepository();
            var conn  = mocks.StrictMock <ISolrConnection>();

            With.Mocks(mocks).Expecting(() =>
            {
                conn.Post("/update", "<optimize waitSearcher=\"true\" waitFlush=\"true\" expungeDeletes=\"true\" maxSegments=\"2\" />");
                LastCall.On(conn).Repeat.Once().Do(new Writer(delegate(string ignored, string s)
                {
                    Console.WriteLine(s);
                    return(null);
                }));
            }).Verify(() =>
            {
                var cmd            = new OptimizeCommand();
                cmd.MaxSegments    = 2;
                cmd.ExpungeDeletes = true;
                cmd.WaitFlush      = true;
                cmd.WaitSearcher   = true;
                cmd.Execute(conn);
            });
        }
コード例 #12
0
        public async Task WhenOptimizeImgWithoutRigidDiskBlockThenSizeIsNotChanged()
        {
            // arrange
            var path = $"{Guid.NewGuid()}.img";
            var fakeCommandHelper = new FakeCommandHelper();
            var bytes             = fakeCommandHelper.CreateTestData();

            fakeCommandHelper.WriteableMedias.Add(new Media(path, path, bytes.Length, Media.MediaType.Raw, false,
                                                            new MemoryStream(bytes)));
            var cancellationTokenSource = new CancellationTokenSource();

            // optimize
            var optimizeCommand = new OptimizeCommand(new NullLogger <OptimizeCommand>(), fakeCommandHelper, path);
            var result          = await optimizeCommand.Execute(cancellationTokenSource.Token);

            Assert.True(result.IsSuccess);

            // assert bytes and media optimized bytes are identical
            var optimizedBytes = fakeCommandHelper.GetMedia(path).GetBytes();

            Assert.Equal(bytes.Length, optimizedBytes.Length);
            Assert.Equal(bytes, optimizedBytes);
        }
コード例 #13
0
        /// <inheritdoc />
        public ResponseHeader Optimize(CommitOptions options)
        {
            OptimizeCommand cmd = GetOptimizeCommand(options);

            return(SendAndParseHeader(cmd));
        }
コード例 #14
0
        /// <summary>
        /// Optimizes Solr's index
        /// </summary>
        public static void Optimize()
        {
            var cmd = new OptimizeCommand();

            cmd.Execute(Connection);
        }
コード例 #15
0
        static async Task Run(Arguments arguments)
        {
            Log.Logger = new LoggerConfiguration()
                         .Enrich.FromLogContext()
                         .WriteTo.Console()
                         .CreateLogger();

            var serviceProvider = new ServiceCollection()
                                  .AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true))
                                  .BuildServiceProvider();

            var loggerFactory = serviceProvider.GetService <ILoggerFactory>();

            var isAdministrator = OperatingSystem.IsAdministrator();

            if (!isAdministrator)
            {
                Console.WriteLine("Requires administrator rights!");
            }

            var commandHelper           = new CommandHelper();
            var physicalDrives          = (await GetPhysicalDrives(arguments)).ToList();
            var cancellationTokenSource = new CancellationTokenSource();

            switch (arguments.Command)
            {
            case Arguments.CommandEnum.List:
                var listCommand = new ListCommand(loggerFactory.CreateLogger <ListCommand>(), commandHelper, physicalDrives);
                listCommand.ListRead += (_, args) =>
                {
                    //
                    // await Task.Run(() =>
                    // {
                    //     Console.WriteLine(JsonSerializer.Serialize(physicalDrivesList, JsonSerializerOptions));
                    // });
                    InfoPresenter.PresentInfo(args.MediaInfos);
                };
                var listResult = await listCommand.Execute(cancellationTokenSource.Token);

                Console.WriteLine(listResult.IsSuccess ? "Done" : $"ERROR: Read failed, {listResult.Error}");
                break;

            case Arguments.CommandEnum.Info:
                var infoCommand = new InfoCommand(loggerFactory.CreateLogger <InfoCommand>(), commandHelper, physicalDrives, arguments.SourcePath);
                infoCommand.DiskInfoRead += (_, args) => { InfoPresenter.PresentInfo(args.MediaInfo); };
                var infoResult = await infoCommand.Execute(cancellationTokenSource.Token);

                Console.WriteLine(infoResult.IsSuccess ? "Done" : $"ERROR: Read failed, {infoResult.Error}");
                break;

            case Arguments.CommandEnum.Read:
                Console.WriteLine("Reading physical drive to image file");

                GenericPresenter.PresentPaths(arguments);

                var readCommand = new ReadCommand(loggerFactory.CreateLogger <ReadCommand>(), commandHelper, physicalDrives, arguments.SourcePath,
                                                  arguments.DestinationPath,
                                                  arguments.Size);
                readCommand.DataProcessed += (_, args) => { GenericPresenter.Present(args); };
                var readResult = await readCommand.Execute(cancellationTokenSource.Token);

                Console.WriteLine(readResult.IsSuccess ? "Done" : $"ERROR: Read failed, {readResult.Error}");
                break;

            case Arguments.CommandEnum.Convert:
                Console.WriteLine("Converting source image to destination image file");

                GenericPresenter.PresentPaths(arguments);

                var convertCommand = new ConvertCommand(loggerFactory.CreateLogger <ConvertCommand>(), commandHelper, arguments.SourcePath,
                                                        arguments.DestinationPath,
                                                        arguments.Size);
                convertCommand.DataProcessed += (_, args) => { GenericPresenter.Present(args); };
                var convertResult = await convertCommand.Execute(cancellationTokenSource.Token);

                Console.WriteLine(
                    convertResult.IsSuccess ? "Done" : $"ERROR: Convert failed, {convertResult.Error}");
                break;

            case Arguments.CommandEnum.Write:
                Console.WriteLine("Writing source image file to physical drive");

                GenericPresenter.PresentPaths(arguments);

                var writeCommand = new WriteCommand(loggerFactory.CreateLogger <WriteCommand>(), commandHelper, physicalDrives, arguments.SourcePath,
                                                    arguments.DestinationPath,
                                                    arguments.Size);
                writeCommand.DataProcessed += (_, args) => { GenericPresenter.Present(args); };
                var writeResult = await writeCommand.Execute(cancellationTokenSource.Token);

                Console.WriteLine(writeResult.IsSuccess ? "Done" : $"ERROR: Write failed, {writeResult.Error}");
                break;

            case Arguments.CommandEnum.Verify:
                Console.WriteLine("Verifying source image to destination");

                GenericPresenter.PresentPaths(arguments);

                var verifyCommand = new VerifyCommand(loggerFactory.CreateLogger <VerifyCommand>(), commandHelper, physicalDrives, arguments.SourcePath,
                                                      arguments.DestinationPath,
                                                      arguments.Size);
                verifyCommand.DataProcessed += (_, args) => { GenericPresenter.Present(args); };
                var verifyResult = await verifyCommand.Execute(cancellationTokenSource.Token);

                Console.WriteLine(verifyResult.IsSuccess ? "Done" : $"ERROR: Verify failed, {verifyResult.Error}");
                break;

            case Arguments.CommandEnum.Blank:
                Console.WriteLine("Creating blank image");
                Console.WriteLine($"Path: {arguments.SourcePath}");
                var blankCommand = new BlankCommand(loggerFactory.CreateLogger <BlankCommand>(), commandHelper, arguments.SourcePath, arguments.Size);
                var blankResult  = await blankCommand.Execute(cancellationTokenSource.Token);

                Console.WriteLine(blankResult.IsSuccess ? "Done" : $"ERROR: Blank failed, {blankResult.Error}");
                break;

            case Arguments.CommandEnum.Optimize:
                Console.WriteLine("Optimizing image file");
                Console.WriteLine($"Path: {arguments.SourcePath}");
                var optimizeCommand = new OptimizeCommand(loggerFactory.CreateLogger <OptimizeCommand>(), commandHelper, arguments.SourcePath);
                var optimizeResult  = await optimizeCommand.Execute(cancellationTokenSource.Token);

                Console.WriteLine(optimizeResult.IsSuccess
                        ? "Done"
                        : $"ERROR: Optimize failed, {optimizeResult.Error}");
                break;
            }
        }