-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTextSearchManager.cs
More file actions
211 lines (173 loc) * 6.66 KB
/
TextSearchManager.cs
File metadata and controls
211 lines (173 loc) * 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Adriva.Extensions.TextSearch
{
public abstract class TextSearchManager : IDisposable
{
private long IsSearcherReady = 0;
private long IsRecycling = 0;
private long ActiveSearchCount = 0;
private Lucene.Net.Store.Directory SearchDirectory;
private DirectoryReader Reader;
private IndexSearcher Searcher;
public bool IsSearcherCreated
{
get
{
return 1 == Interlocked.Read(ref this.IsSearcherReady);
}
}
public string IndexPath { get; private set; }
protected TextSearchManager(string indexPath)
{
this.IndexPath = indexPath;
}
protected virtual bool CanReuseIndex(DirectoryInfo directory)
{
return false;
}
protected abstract Task<RawDataResult> GetRawDataAsync(object lastRowId);
protected abstract Document ResolveDocument(object dataItem);
protected virtual void OnIndexCreated() { }
protected virtual void OnIndexCreating() { }
public Task CreateIndexFileAsync(bool forceCreate)
{
return this.CreateIndexFileAsync(forceCreate, false);
}
private async Task CreateIndexFileAsync(bool forceCreate, bool isRecycling)
{
bool isUsingMemoryIndex = this.SearchDirectory is RAMDirectory;
SpinWait.SpinUntil(() => 0 == Interlocked.Read(ref this.ActiveSearchCount), 30_000);
if (!isRecycling || !isUsingMemoryIndex)
{
Interlocked.Exchange(ref this.IsSearcherReady, 0);
}
this.OnIndexCreating();
DirectoryInfo indexDirectoryInfo = new DirectoryInfo(this.IndexPath);
if (!forceCreate && this.CanReuseIndex(indexDirectoryInfo)) return;
var files = indexDirectoryInfo.GetFiles();
Array.ForEach(files, file =>
{
if (!file.Name.Equals("placeholder.txt", StringComparison.OrdinalIgnoreCase)
&& !file.Name.Equals("writer.lock", StringComparison.OrdinalIgnoreCase))
{
file.Delete();
}
});
using (SimpleFSDirectory fsDirectory = new SimpleFSDirectory(indexDirectoryInfo))
{
using (var analyzer = new AdrivaAnalyzer(LuceneVersion.LUCENE_48))
{
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(LuceneVersion.LUCENE_48, analyzer) { };
using (IndexWriter writer = new IndexWriter(fsDirectory, indexWriterConfig))
{
RawDataResult result = new RawDataResult
{
HasMore = false
};
do
{
result = await this.GetRawDataAsync(result.LastRowId);
var rawDataEnumerator = result.Items.GetEnumerator();
while (rawDataEnumerator.MoveNext())
{
Document document = this.ResolveDocument(rawDataEnumerator.Current);
if (null != document) writer.AddDocument(document, analyzer);
}
} while (result.HasMore);
writer.Commit();
}
}
}
this.OnIndexCreated();
}
public void CreateSearcher(bool storeIndexInMemory = false)
{
DirectoryInfo directoryInfo = new DirectoryInfo(this.IndexPath);
if (null != this.Searcher)
{
this.Searcher = null;
}
if (null != this.Reader)
{
this.Reader.Dispose();
this.Reader = null;
}
if (null != this.SearchDirectory)
{
this.SearchDirectory.Dispose();
this.SearchDirectory = null;
}
if (storeIndexInMemory)
{
this.SearchDirectory = new RAMDirectory(new SimpleFSDirectory(directoryInfo), IOContext.DEFAULT);
}
else
{
this.SearchDirectory = new SimpleFSDirectory(directoryInfo);
}
this.Reader = DirectoryReader.Open(this.SearchDirectory);
this.Searcher = new IndexSearcher(this.Reader);
Interlocked.Exchange(ref this.IsSearcherReady, 1);
}
public IEnumerable<T> Search<T>(Query query, int count, Sort sort, Func<Document, ScoreDoc, T> func)
{
if (1 != Interlocked.Read(ref this.IsSearcherReady)) return Array.Empty<T>();
Interlocked.Increment(ref this.ActiveSearchCount);
try
{
TopFieldCollector collector = TopFieldCollector.Create(sort, count, true, true, false, false);
this.Searcher.Search(query, collector);
return collector.GetTopDocs().ScoreDocs.Select<ScoreDoc, T>(
scoreDoc =>
{
var document = this.Searcher.Doc(scoreDoc.Doc);
return func(document, scoreDoc);
}
);
}
finally
{
Interlocked.Decrement(ref this.ActiveSearchCount);
}
}
public virtual async Task RecycleAsync(bool forceCreateIndex = false, bool storeIndexInMemory = false)
{
if (0 == Interlocked.CompareExchange(ref this.IsRecycling, 1, 0))
{
try
{
await this.CreateIndexFileAsync(forceCreateIndex, true);
this.CreateSearcher(storeIndexInMemory);
}
finally
{
Interlocked.Exchange(ref this.IsRecycling, 0);
}
}
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Interlocked.Exchange(ref this.IsSearcherReady, 0);
this.Reader?.Dispose();
this.SearchDirectory?.Dispose();
}
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
}
}