From 25d88a7604e985202235d0a16d0c9bc664e1a98b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Mon, 7 Mar 2022 14:38:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=96=87=E4=BB=B6=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E5=8C=B9=E9=85=8D=E9=94=99=E8=AF=AF=E7=9A=84bug=20?= =?UTF-8?q?=E7=B4=A2=E5=BC=95=E8=87=AA=E5=8A=A8=E6=9B=B4=E6=96=B0=E6=9C=BA?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/Enums/FileType.cs | 2 +- TextLocator/Index/IndexCore.cs | 1 - TextLocator/MainWindow.xaml.cs | 33 ++++++++++++++++-- TextLocator/Properties/AssemblyInfo.cs | 4 +-- TextLocator/Util/FileTypeUtil.cs | 8 +++-- TextLocator/Util/FileUtil.cs | 46 ++++++++++++++++++-------- 6 files changed, 71 insertions(+), 23 deletions(-) diff --git a/TextLocator/Enums/FileType.cs b/TextLocator/Enums/FileType.cs index 4abc2ea..9818096 100644 --- a/TextLocator/Enums/FileType.cs +++ b/TextLocator/Enums/FileType.cs @@ -40,7 +40,7 @@ namespace TextLocator.Enums /// /// 代码 /// - [Description("cs,java,js,css,md,py,c,cpp,lua,sql,jsp,json,php,rs,rb,yml,yaml,bat")] + [Description("cs,java,js,css,md,py,c,h,cpp,lua,sql,jsp,json,php,rs,rb,yml,yaml,bat")] 代码, /// /// 纯文本 diff --git a/TextLocator/Index/IndexCore.cs b/TextLocator/Index/IndexCore.cs index 7977be6..22b6b7f 100644 --- a/TextLocator/Index/IndexCore.cs +++ b/TextLocator/Index/IndexCore.cs @@ -58,7 +58,6 @@ namespace TextLocator.Index { _callback = callback; - // 判断是创建索引还是增量索引(如果索引目录不存在,重建) bool create = !Directory.Exists(AppConst.APP_INDEX_DIR); // 入参为true,表示重建 diff --git a/TextLocator/MainWindow.xaml.cs b/TextLocator/MainWindow.xaml.cs index a8fccb5..27869df 100644 --- a/TextLocator/MainWindow.xaml.cs +++ b/TextLocator/MainWindow.xaml.cs @@ -37,6 +37,10 @@ namespace TextLocator /// 索引文件夹列表 /// private List _indexFolders = new List(); + /// + /// 时间戳 + /// + private long _timestamp; /// /// 索引构建中 @@ -78,7 +82,7 @@ namespace TextLocator } // 软件每次启动时执行索引更新逻辑? - + CheckingIndexUpdates(); } #region 初始化 @@ -268,7 +272,7 @@ namespace TextLocator /// 排序类型 /// 仅文件名 /// 匹配全词 - private void Search(List keywords, string fileType, SortType sortType, bool onlyFileName = false, bool matchWords = false) + private void Search(List keywords, string fileType, SortType sortType, long timestamp, bool onlyFileName = false, bool matchWords = false) { if (!CheckIndexExist()) { @@ -403,6 +407,10 @@ namespace TextLocator // 获取并显示列表 for (int i = start; i < end; i++) { + if (_timestamp != timestamp) + { + continue; + } // 该文件的在索引里的文档号,Doc是该文档进入索引时Lucene的编号,默认按照顺序编的 int docId = scores[i].Doc; // 获取文档对象 @@ -816,6 +824,20 @@ namespace TextLocator #endregion #region 辅助方法 + /// + /// 检查索引是否需要更新 + /// + private void CheckingIndexUpdates() + { + string lastIndexTime = AppUtil.ReadValue("AppConfig", "LastIndexTime", ""); + // 时间不存在 或 已经超过7天 + if (string.IsNullOrEmpty(lastIndexTime) || (DateTime.Now - DateTime.Parse(lastIndexTime)).TotalDays > 7) + { + // 执行索引更新,扫描新文件。 + BuildIndex(false); + } + } + /// /// 检查索引是否存在 /// @@ -843,7 +865,6 @@ namespace TextLocator { var taskMark = TaskTime.StartNew(); - var fileMark = TaskTime.StartNew(); // 定义文件列表 List filePaths = new List(); @@ -880,6 +901,8 @@ namespace TextLocator Message.ShowSuccess("MessageContainer", msg); })); + AppUtil.WriteValue("AppConfig", "LastIndexTime", DateTime.Now.ToString()); + // 构建结束 build = false; }); @@ -1022,11 +1045,15 @@ namespace TextLocator PreviewImage.Source = null; PreviewFileTypeIcon.Source = null; + // 记录时间戳 + _timestamp = Convert.ToInt64((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds); + // 搜索 Search( keywords, filter == null ? null : filter + "", (SortType)SortOptions.SelectedValue, + _timestamp, (bool)OnlyFileName.IsChecked, (bool)MatchWords.IsChecked ); diff --git a/TextLocator/Properties/AssemblyInfo.cs b/TextLocator/Properties/AssemblyInfo.cs index e32b67f..e169e29 100644 --- a/TextLocator/Properties/AssemblyInfo.cs +++ b/TextLocator/Properties/AssemblyInfo.cs @@ -49,8 +49,8 @@ using System.Windows; //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 //通过使用 "*",如下所示: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.2.2")] -[assembly: AssemblyFileVersion("1.2.1.0")] +[assembly: AssemblyVersion("1.2.3")] +[assembly: AssemblyFileVersion("1.2.3.0")] // log4net [assembly: log4net.Config.XmlConfigurator(Watch = true)] \ No newline at end of file diff --git a/TextLocator/Util/FileTypeUtil.cs b/TextLocator/Util/FileTypeUtil.cs index 01b0515..9ad6104 100644 --- a/TextLocator/Util/FileTypeUtil.cs +++ b/TextLocator/Util/FileTypeUtil.cs @@ -40,10 +40,12 @@ namespace TextLocator.Util { // 获取描述 string description = ft.GetDescription(); - if (description.Contains(fileExt.Replace(".", ""))) + foreach(var ext in description.Split(',')) { - fileType = ft; - break; + if (ext.Equals(fileExt.Replace(".", ""))) { + fileType = ft; + break; + } } } diff --git a/TextLocator/Util/FileUtil.cs b/TextLocator/Util/FileUtil.cs index 9dc8575..df30fe0 100644 --- a/TextLocator/Util/FileUtil.cs +++ b/TextLocator/Util/FileUtil.cs @@ -20,6 +20,14 @@ namespace TextLocator.Util { private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + /// + /// 程序员节,文件大小单位换算常量 + /// + private const long PROGRAMMERS_SECTION = 1024; + /// + /// 文件大小单位 + /// + private static readonly string[] suffixes = new string[] { " B", " KB", " MB", " GB", " TB", " PB" }; /// /// 根据文件类型获取文件图标 /// @@ -83,27 +91,39 @@ namespace TextLocator.Util /// /// 获取文件大小友好显示 /// - /// + /// /// - public static string GetFileSizeFriendly(long fileSize) + public static string GetFileSizeFriendly(long number) { - string fileSizeUnit = "b"; - if (fileSize > 1024) + /*string sizeUnit = "B"; + if (fileSize > PROGRAMMERS_SECTION) { - fileSize = fileSize / 1024; - fileSizeUnit = "KB"; + fileSize = fileSize / PROGRAMMERS_SECTION; + sizeUnit = "KB"; } - if (fileSize > 1024) + if (fileSize > PROGRAMMERS_SECTION) { - fileSize = fileSize / 1024; - fileSizeUnit = "MB"; + fileSize = fileSize / PROGRAMMERS_SECTION; + sizeUnit = "MB"; } - if (fileSize > 1024) + if (fileSize > PROGRAMMERS_SECTION) { - fileSize = fileSize / 1024; - fileSizeUnit = "GB"; + fileSize = fileSize / PROGRAMMERS_SECTION; + sizeUnit = "GB"; + } + return fileSize + "" + sizeUnit;*/ + double last = 1; + for (int i = 0; i < suffixes.Length; i++) + { + var current = Math.Pow(1024, i + 1); + var temp = number / current; + if (temp < 1) + { + return (number / last).ToString("n2") + suffixes[i]; + } + last = current; } - return fileSize + "" + fileSizeUnit; + return number.ToString(); } /// -- Gitee