public Task HandleAsync(CleanDocumentDirectoryCommand command, CancellationToken cancellationToken = default(CancellationToken), IProgress <ProgressInfo> progress = null)
        {
            return(Task.Run(() => {
                var actualStep = 0;
                var totalSteps = 0;
                try {
                    var documentDirectory = _database.ExecuteReaderSingle(SQLs.GetDocumentDirectory, DocumentDirectory.Map, parameters: new[] {
                        Parameter.CreateInputParameter(Common.DatabaseSchema.DocumentDirectories.DocumentDirectoryID, command.DocumentDirectoryID, DbType.Int32)
                    });
                    var documents = _database.ExecuteReader(SQLs.ListDocumentsByDocumentDirectoryNoContent, Document.Map, parameters: new[] {
                        Parameter.CreateInputParameter(Common.DatabaseSchema.DocumentDirectories.DocumentDirectoryID, command.DocumentDirectoryID, DbType.Int32)
                    });

                    var filesOnDatabase = documents.Select(_ => _.Path).ToArray();
                    var filesOnDisk = Common.GetDocumentFiles(documentDirectory.Path);
                    var removeFromDatabase = filesOnDatabase.Except(filesOnDisk).ToArray();
                    var removeFromIndex = new List <Document>();

                    totalSteps = removeFromDatabase.Length + 1;

                    progress.Start(totalSteps, Strings.CleanDocumentDirectory_Progress_Start_Title);

                    using (var transaction = _database.Connection.BeginTransaction()) {
                        foreach (var filePath in removeFromDatabase)
                        {
                            progress.PerformStep(++actualStep, totalSteps, Strings.CleanDocumentDirectory_Progress_Step_Database_Message, Path.GetFileName(filePath));

                            if (cancellationToken.IsCancellationRequested)
                            {
                                progress.Cancel(actualStep, totalSteps);

                                cancellationToken.ThrowIfCancellationRequested();
                            }

                            var document = documents.SingleOrDefault(_ => _.Path == filePath);
                            _database.ExecuteNonQuery(SQLs.RemoveDocument, parameters: new[] {
                                Parameter.CreateInputParameter(Common.DatabaseSchema.Documents.DocumentID, document.DocumentID, DbType.Int32)
                            });

                            removeFromIndex.Add(document);
                        }

                        if (cancellationToken.IsCancellationRequested)
                        {
                            progress.Cancel(actualStep, totalSteps);

                            cancellationToken.ThrowIfCancellationRequested();
                        }
                        transaction.Commit();
                    }

                    progress.PerformStep(++actualStep, totalSteps, Strings.CleanDocumentDirectory_Progress_Step_Index_Message, documentDirectory.Label);
                    _indexProvider
                    .GetOrCreate(documentDirectory.Code)
                    .DeleteDocuments(removeFromIndex.Select(_ => _.DocumentID.ToString()).ToArray());

                    progress.Complete(actualStep, totalSteps);
                } catch (Exception ex) { progress.Error(actualStep, totalSteps, ex.Message); throw; }
            }, cancellationToken));
        }
Ejemplo n.º 2
0
        public void RestoreDatabase(string inputPath)
        {
            if (AppSettings.Instance.UseRemoteSearchDatabase)
            {
                throw new InvalidOperationException("Não é possível realizar a restauração a partir da máquina cliente.");
            }

            if (!IOHelper.IsFile(inputPath))
            {
                throw new InvalidOperationException("O caminho selecionado deve apontar para um arquivo válido.");
            }

            // Close database
            (_database as IDisposable).Dispose();

            // Close all indexes
            foreach (var indexName in _indexProvider.List())
            {
                (_indexProvider.GetOrCreate(indexName) as IDisposable).Dispose();
            }

            // Delete all current files
            Directory.Delete(AppSettings.Instance.ApplicationDataDirectoryPath, recursive: true);

            ZipFile.ExtractToDirectory(inputPath, AppSettings.Instance.ApplicationDataDirectoryPath);
        }
Ejemplo n.º 3
0
 private IEnumerable <IIndex> GetIndexes(IEnumerable <string> indexNames)
 {
     if (indexNames.IsNullOrEmpty())
     {
         indexNames = _indexProvider.List();
     }
     foreach (var indexName in indexNames)
     {
         yield return(_indexProvider.GetOrCreate(indexName));
     }
 }
Ejemplo n.º 4
0
#pragma warning disable CSE0003 // Use expression-bodied members
        public Task HandleAsync(IndexDocumentDirectoryCommand command, CancellationToken cancellationToken = default(CancellationToken), IProgress <ProgressInfo> progress = null)
        {
            return(Task.Run(() => {
                var documentDirectory = _database.ExecuteReaderSingle(SQLs.GetDocumentDirectory, DocumentDirectory.Map, parameters: new[] {
                    Parameter.CreateInputParameter(Common.DatabaseSchema.DocumentDirectories.DocumentDirectoryID, command.DocumentDirectoryID, DbType.Int32)
                });
                var documentCount = (long)_database.ExecuteScalar(SQLs.GetDocumentCountByDocumentDirectory, parameters: new[] {
                    Parameter.CreateInputParameter(Common.DatabaseSchema.Documents.DocumentDirectoryID, command.DocumentDirectoryID, DbType.Int32),
                    Parameter.CreateInputParameter(Common.DatabaseSchema.Documents.Index, 0 /* false */, DbType.Int32)
                });

                var actualStep = 0;
                var totalSteps = Convert.ToInt32(documentCount);

                try {
                    progress.Start(totalSteps, Strings.IndexDocumentDirectory_Progress_Start_Title);
                    if (totalSteps == 0)
                    {
                        progress.Complete(actualStep, totalSteps);
                        return;
                    }

                    // Gets the Lucene Index
                    var index = _indexProvider.GetOrCreate(documentDirectory.Code);
                    var documentIndexList = new List <IDocumentIndex>();
                    var pageCount = Convert.ToInt32(documentCount / command.BatchSize);
                    if (documentCount % command.BatchSize != 0)
                    {
                        pageCount++;
                    }
                    for (var page = 0; page < pageCount; page++)
                    {
                        var documents = _database.ExecuteReader(SQLs.PaginateDocumentsByDocumentDirectory, Document.Map, parameters: new[] {
                            Parameter.CreateInputParameter(Common.DatabaseSchema.DocumentDirectories.DocumentDirectoryID, command.DocumentDirectoryID, DbType.Int32),
                            Parameter.CreateInputParameter(Common.DatabaseSchema.Documents.Index, 0 /* false */, DbType.Int32),
                            Parameter.CreateInputParameter("limit", command.BatchSize, DbType.Int32)
                        }).ToArray();

                        foreach (var document in documents)
                        {
                            progress.PerformStep(++actualStep, totalSteps, Strings.IndexDocumentDirectory_Progress_Step_Message, document.FileName);
                            if (cancellationToken.IsCancellationRequested)
                            {
                                progress.Cancel(actualStep, totalSteps);

                                cancellationToken.ThrowIfCancellationRequested();
                            }
                            var documentIndexDto = new DocumentIndexDto {
                                DocumentID = document.DocumentID,
                                DocumentCode = document.Code,
                                DocumentDirectoryCode = documentDirectory.Code,
                                Content = document.Content,
                                FileName = document.FileName
                            };
                            documentIndexList.Add(documentIndexDto.Map());
                        }
                        index.StoreDocuments(documentIndexList.ToArray());
                        documentIndexList.Clear();
                        MarkAsIndex(documents);
                    }
                    progress.Complete(actualStep, totalSteps);
                } catch (Exception ex) { progress.Error(actualStep, totalSteps, ex.Message); throw; }
            }, cancellationToken));
        }
 public override void Execute()
 {
     _mediator
     .Query(new ListDocumentDirectoriesQuery())
     .Each(_ => _indexProvider.GetOrCreate(_.Code));
 }