示例#1
0
        public void ApplyToWriterConfig(IndexWriterConfig config)
        {
            try
            {
                // possibly take in a MergePolicy or configure it elsewhere
                var mergePolicy = new LogByteSizeMergePolicy();
                if (MergeFactor != null)
                {
                    mergePolicy.MergeFactor = (int)MergeFactor;
                }

                if (MaxMergeDocs != null)
                {
                    mergePolicy.MaxMergeDocs = (int)MaxMergeDocs;
                }

                config.MergePolicy = mergePolicy;

                if (MaxBufferedDocs != null)
                {
                    config.SetMaxBufferedDocs((int)MaxBufferedDocs);
                }

                if (RamBufferSizeMb != null)
                {
                    config.SetRAMBufferSizeMB((int)RamBufferSizeMb);
                }

                if (TermIndexInterval != null)
                {
                    config.SetTermIndexInterval((int)TermIndexInterval);
                }
            }
            catch (ArgumentOutOfRangeException)
            {
                // TODO: Log it
            }
        }
        /// <summary>
        /// 1つのスレッドでFSIndexを作成する処理
        /// Delete-Insert
        /// </summary>
        /// <param name="args"></param>
        internal void CreateFSIndex(
            string buildDirPath,
            List <FileInfo> targetFileList,
            IProgress <ProgressReport> progress,
            string threadName)
        {
            IndexWriterConfig            config        = new IndexWriterConfig(AppObject.AppAnalyzer);
            IndexWriter                  indexWriter   = null;
            FSDirectory                  indexBuildDir = null;
            Dictionary <string, DocInfo> docDic        = null;

            try {
                indexBuildDir = FSDirectory.Open(FileSystems.getDefault().getPath(buildDirPath));
                if (_createMode == CreateModes.Update)
                {
                    //既存ドキュメントを辞書化
                    var liru = new LuceneIndexReaderUtil();
                    docDic = liru.CreateDocumentDic(indexBuildDir);
                }

                int bufferSize = Properties.Settings.Default.BufferSizeLimit;
                config.SetRAMBufferSizeMB(bufferSize); //デフォルト16MB
                indexWriter = new IndexWriter(indexBuildDir, config);

                //開始時刻を記録
                _startTime    = DateTime.Now;
                _indexedCount = 0;
                _skippedCount = 0;
                progress.Report(new ProgressReport()
                {
                    Percent       = 0,
                    ProgressCount = FinishedCount,
                    TargetCount   = _targetCount,
                    Status        = ProgressReport.ProgressStatus.Start
                });

                //NOTE すぐにログ出力すると、フリーズ現象が発生する。
                Thread.Sleep(1000);
                AppObject.Logger.Info(threadName + "Indexing start.");
                foreach (FileInfo fi in targetFileList)
                {
                    Thread.Sleep(10);
                    if (DoStop)
                    {
                        //中断
                        AppObject.Logger.Info("Stop command executed.");
                        indexWriter.Rollback();
                        return;
                    }
                    //Officeのテンポラリファイルは無視。
                    if (fi.Name.StartsWith("~"))
                    {
                        continue;
                    }

                    try {
                        if (AddDocument(fi.FullName, indexWriter, threadName, docDic))
                        {
                            //インデックス作成ファイル表示
                            AppObject.Logger.Info(threadName + ":" + fi.FullName);
                            Interlocked.Increment(ref _indexedCount);
                            Interlocked.Exchange(ref _totalBytes, _totalBytes + fi.Length);
                        }
                    } catch (IOException ioe) {
                        AppObject.Logger.Error(ioe.StackTrace);
                        Interlocked.Increment(ref _skippedCount);
                        AppObject.Logger.Info(threadName + ":" + "Skipped: " + fi.FullName);
                        GC.Collect();
                    } catch (Exception e) {
                        //インデックスが作成できなかったファイルを表示
                        AppObject.Logger.Warn(e.Message);
                        Interlocked.Increment(ref _skippedCount);
                        AppObject.Logger.Info(threadName + ":" + "Skipped: " + fi.FullName);
                        GC.Collect();
                        if (e.Message.IndexOf("IndexWriter is closed") >= 0)
                        {
                            AppObject.Logger.Warn(e.GetBaseException().ToString());
                            throw new AlreadyClosedException("Index file capacity over. Please divide index directory.");
                        }
                    }
                    //進捗度更新を呼び出し。
                    progress.Report(new ProgressReport()
                    {
                        Percent       = GetPercentage(),
                        ProgressCount = FinishedCount,
                        TargetCount   = _targetCount,
                        Status        = ProgressReport.ProgressStatus.Processing
                    });
                }
                if (docDic != null)
                {
                    //削除されたファイルをインデックスから除去
                    foreach (var kvp in docDic)
                    {
                        if (kvp.Value.Exists == false)
                        {
                            //削除
                            Term t = new Term(LuceneIndexBuilder.Path, kvp.Value.Path);
                            indexWriter.DeleteDocuments(t);
                        }
                    }
                }
                indexWriter.Commit();
                AppObject.Logger.Info(threadName + ":完了");
            } catch (Exception e) {
                AppObject.Logger.Error(e.Message);
                if (indexWriter != null)
                {
                    indexWriter.Rollback();
                }
                throw e;
            } finally {
                if (indexWriter != null && indexWriter.IsOpen())
                {
                    //クローズ時にIndexファイルがフラッシュされる
                    indexWriter.Close();
                }
                //完了時刻を記録
                _endTime = DateTime.Now;
                progress.Report(new ProgressReport()
                {
                    Percent       = GetPercentage(),
                    ProgressCount = FinishedCount,
                    TargetCount   = _targetCount,
                    Status        = ProgressReport.ProgressStatus.Finished
                });
                indexBuildDir.Close();
            }
        }