浏览代码

修正并发下的bug

懒得勤快 6 年之前
父节点
当前提交
64921c538f

+ 44 - 12
Masuit.LuceneEFCore.SearchEngine.Test/SearchEngineTests.cs

@@ -1,4 +1,7 @@
-using Masuit.LuceneEFCore.SearchEngine.Interfaces;
+using JiebaNet.Segmenter;
+using Lucene.Net.Analysis.JieBa;
+using Lucene.Net.Store;
+using Masuit.LuceneEFCore.SearchEngine.Interfaces;
 using Masuit.LuceneEFCore.SearchEngine.Test.Helpers;
 using Masuit.LuceneEFCore.SearchEngine.Test.Models;
 using Microsoft.EntityFrameworkCore;
@@ -36,7 +39,10 @@ namespace Masuit.LuceneEFCore.SearchEngine.Test
             {
                 Path = "lucene"
             };
-            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(options, _context, new MemoryCache(new MemoryCacheOptions()));
+            var memoryCache = new MemoryCache(new MemoryCacheOptions());
+            var directory = FSDirectory.Open(options.Path);
+            JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
+            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
 
             searchProvider.CreateIndex();
 
@@ -53,7 +59,10 @@ namespace Masuit.LuceneEFCore.SearchEngine.Test
             {
                 Path = "lucene"
             };
-            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(options, _context, new MemoryCache(new MemoryCacheOptions()));
+            var memoryCache = new MemoryCache(new MemoryCacheOptions());
+            var directory = FSDirectory.Open(options.Path);
+            JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
+            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
             searchProvider.CreateIndex();
             SearchOptions searchOptions = new SearchOptions("John", "FirstName");
 
@@ -71,7 +80,10 @@ namespace Masuit.LuceneEFCore.SearchEngine.Test
             {
                 Path = "lucene"
             };
-            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(options, _context, new MemoryCache(new MemoryCacheOptions()));
+            var memoryCache = new MemoryCache(new MemoryCacheOptions());
+            var directory = FSDirectory.Open(options.Path);
+            JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
+            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
             searchProvider.CreateIndex();
             SearchOptions searchOptions = new SearchOptions("John", "FirstName");
 
@@ -100,7 +112,10 @@ namespace Masuit.LuceneEFCore.SearchEngine.Test
             {
                 Path = "lucene"
             };
-            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(options, _context, new MemoryCache(new MemoryCacheOptions()));
+            var memoryCache = new MemoryCache(new MemoryCacheOptions());
+            var directory = FSDirectory.Open(options.Path);
+            JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
+            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
             searchProvider.CreateIndex();
             SearchOptions searchOptions = new SearchOptions("Joh*", "FirstName");
 
@@ -122,7 +137,10 @@ namespace Masuit.LuceneEFCore.SearchEngine.Test
             {
                 Path = "lucene"
             };
-            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(options, _context, new MemoryCache(new MemoryCacheOptions()));
+            var memoryCache = new MemoryCache(new MemoryCacheOptions());
+            var directory = FSDirectory.Open(options.Path);
+            JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
+            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
             searchProvider.CreateIndex();
             SearchOptions searchOptions = new SearchOptions("Joh*", "FirstName");
 
@@ -142,7 +160,10 @@ namespace Masuit.LuceneEFCore.SearchEngine.Test
             {
                 Path = "lucene"
             };
-            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(options, _context, new MemoryCache(new MemoryCacheOptions()));
+            var memoryCache = new MemoryCache(new MemoryCacheOptions());
+            var directory = FSDirectory.Open(options.Path);
+            JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
+            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
             searchProvider.CreateIndex();
             SearchOptions searchOptions = new SearchOptions("Burns", "FirstName,Surname");
 
@@ -166,7 +187,10 @@ namespace Masuit.LuceneEFCore.SearchEngine.Test
             {
                 Path = "lucene"
             };
-            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(options, _context, new MemoryCache(new MemoryCacheOptions()));
+            var memoryCache = new MemoryCache(new MemoryCacheOptions());
+            var directory = FSDirectory.Open(options.Path);
+            JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
+            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
             searchProvider.CreateIndex();
             SearchOptions searchOptions = new SearchOptions("Jeremy Burns", "FirstName,Surname");
 
@@ -187,8 +211,10 @@ namespace Masuit.LuceneEFCore.SearchEngine.Test
             {
                 Path = "lucene"
             };
-            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(options, _context, new MemoryCache(new MemoryCacheOptions()));
-
+            var memoryCache = new MemoryCache(new MemoryCacheOptions());
+            var directory = FSDirectory.Open(options.Path);
+            JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
+            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
             User jc = new User()
             {
                 FirstName = "John",
@@ -219,7 +245,10 @@ namespace Masuit.LuceneEFCore.SearchEngine.Test
             {
                 Path = "lucene"
             };
-            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(options, _context, new MemoryCache(new MemoryCacheOptions()));
+            var memoryCache = new MemoryCache(new MemoryCacheOptions());
+            var directory = FSDirectory.Open(options.Path);
+            JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
+            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
             searchProvider.CreateIndex();
             var newUser = new User()
             {
@@ -248,7 +277,10 @@ namespace Masuit.LuceneEFCore.SearchEngine.Test
             {
                 Path = "lucene"
             };
-            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(options, _context, new MemoryCache(new MemoryCacheOptions()));
+            var memoryCache = new MemoryCache(new MemoryCacheOptions());
+            var directory = FSDirectory.Open(options.Path);
+            JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
+            SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
 
             searchProvider.CreateIndex();
 

+ 1 - 1
Masuit.LuceneEFCore.SearchEngine/LuceneIndexSearcher.cs

@@ -26,7 +26,7 @@ namespace Masuit.LuceneEFCore.SearchEngine
         private readonly Analyzer _analyzer;
         private readonly IMemoryCache _memoryCache;
 
-        private static readonly HttpClient HttpClient = new HttpClient()
+        private static readonly HttpClient HttpClient = new HttpClient
         {
             BaseAddress = new Uri("http://zhannei.baidu.com")
         };

+ 2 - 2
Masuit.LuceneEFCore.SearchEngine/LuceneIndexer.cs

@@ -12,12 +12,12 @@ namespace Masuit.LuceneEFCore.SearchEngine
         /// <summary>
         /// 索引目录
         /// </summary>
-        private static Directory _directory;
+        private readonly Directory _directory;
 
         /// <summary>
         /// 索引分析器
         /// </summary>
-        private static Analyzer _analyzer;
+        private readonly Analyzer _analyzer;
 
         /// <summary>
         /// 构造函数

+ 32 - 37
Masuit.LuceneEFCore.SearchEngine/SearchEngine.cs

@@ -1,6 +1,4 @@
-using JiebaNet.Segmenter;
-using Lucene.Net.Analysis;
-using Lucene.Net.Analysis.JieBa;
+using Lucene.Net.Analysis;
 using Lucene.Net.Documents;
 using Lucene.Net.Store;
 using Masuit.LuceneEFCore.SearchEngine.Extensions;
@@ -25,14 +23,11 @@ namespace Masuit.LuceneEFCore.SearchEngine
         /// <summary>
         /// 数据库上下文
         /// </summary>
-        public TContext Context { get; protected set; }
-
-        private static Directory _directory;
-        private static Analyzer _analyzer;
-        private static LuceneIndexer _indexer;
-        private static LuceneIndexSearcher _searcher;
-        private static bool isInitialized = false;
-        private readonly IMemoryCache _memoryCache;
+        public TContext Context { get; }
+
+        private readonly LuceneIndexer _indexer;
+        private readonly LuceneIndexSearcher _searcher;
+
         /// <summary>
         /// 索引条数
         /// </summary>
@@ -41,37 +36,37 @@ namespace Masuit.LuceneEFCore.SearchEngine
         /// <summary>
         /// 搜索引擎
         /// </summary>
-        /// <param name="indexerOptions">索引选项</param>
         /// <param name="context">数据库上下文</param>
-        /// <param name="overrideIfExists">是否被覆盖</param>
-        public SearchEngine(LuceneIndexerOptions indexerOptions, TContext context, IMemoryCache memoryCache, bool overrideIfExists = false)
+        /// <param name="directory"></param>
+        /// <param name="analyzer"></param>
+        /// <param name="memoryCache"></param>
+        public SearchEngine(TContext context, Directory directory, Analyzer analyzer, IMemoryCache memoryCache)
         {
             Context = context;
-            _memoryCache = memoryCache;
-            if (isInitialized == false || overrideIfExists)
-            {
-                InitializeLucene(indexerOptions);
-                isInitialized = true;
-            }
-
+            _indexer = new LuceneIndexer(directory, analyzer);
+            _searcher = new LuceneIndexSearcher(directory, analyzer, memoryCache);
+            //if (isInitialized == false || overrideIfExists)
+            //{
+            //    InitializeLucene(indexerOptions);
+            //    isInitialized = true;
+            //}
         }
 
-        /// <summary>
-        /// 初始化索引库
-        /// </summary>
-        /// <param name="options"></param>
-        private void InitializeLucene(LuceneIndexerOptions options)
-        {
-            if (_directory == null)
-            {
-                _directory = FSDirectory.Open(options.Path);
-            }
-
-            _analyzer = new JieBaAnalyzer(TokenizerMode.Search);
-            _indexer = new LuceneIndexer(_directory, _analyzer);
-            _searcher = new LuceneIndexSearcher(_directory, _analyzer, _memoryCache);
-
-        }
+        ///// <summary>
+        ///// 初始化索引库
+        ///// </summary>
+        ///// <param name="options"></param>
+        //private void InitializeLucene(LuceneIndexerOptions options)
+        //{
+        //    if (_directory == null)
+        //    {
+        //        _directory = FSDirectory.Open(options.Path);
+        //    }
+
+        //    _analyzer = new JieBaAnalyzer(TokenizerMode.Search);
+        //    _indexer = new LuceneIndexer(_directory, _analyzer);
+        //    _searcher = new LuceneIndexSearcher(_directory, _analyzer, _memoryCache);
+        //}
 
         /// <summary>
         /// 检查数据库上下文更改,并返回LuceneIndexChanges类型的集合

+ 9 - 0
WebSearchDemo/WebSearchDemo.csproj.user

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
+    <DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
+  </PropertyGroup>
+  <PropertyGroup>
+    <ActiveDebugProfile>WebSearchDemo</ActiveDebugProfile>
+  </PropertyGroup>
+</Project>