/
RegexSearcher.cs
344 lines (328 loc) · 15.3 KB
/
RegexSearcher.cs
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
using System;
using System.Text;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;
using System.IO;
using System.Text.RegularExpressions;
namespace Imagibee {
namespace Gigantor {
//
// Fast regex search/replace of gigantic files
//
// The search process runs in the background, seperating the file into
// chunks, and searching the chunks in parallel. The search results
// of each chunk are ultimately joined into a continuous result which
// is accessible through the GetMatchData method after the search
// completes.
//
// Users should instantiate an instance with the file or stream to
// search and a Regex instance to perform the search. Use the helper
// functions from the Background class to control the process including
// start, wait, cancel, and error detection.
//
// After the process is finished, the results are stored until Start
// is called again. Although, calling Start while Running is true
// will have no effect.
//
// Exceptions during the background processing are caught and
// stored in Error.
//
// A balance between memory footprint and performance can be achieved
// by varying maxMatchCount, partitionSize and maxWorkers parameters.
//
public class RegexSearcher : Partitioner<PartitionData> {
// The number of matches found so far
public long MatchCount { get { return matchCount; } }
// A structure for storing capture data
public struct CaptureData {
public long StartFpos;
public string Value;
}
// A structure for storing match group data
public struct GroupData
{
public long StartFpos;
public string Name;
public string Value;
public IReadOnlyList<CaptureData> Captures;
}
// A structure for storing a single match value
public struct MatchData {
public long StartFpos;
public string Name;
public string Value;
public IReadOnlyList<GroupData> Groups;
public int RegexIndex;
}
// Create a new instance to search a file with single regex
//
// filePath - the path to the file to search
// regex - the regular expression to match against the file
// progress - signaled each time MatchCount is updated
// maxMatchCount - places an approximate limit on the number of matches, defaults to 100000
// partitionSize - the chunk size in bytes that each worker works on,
// defaults to 256 KiB
// maxWorkers - optional limit to the maximum number of simultaneous workers,
// defaults to unlimited
// overlap - size in bytes of partition overlap, used for finding matches,
// that span two partitions, may not exceed half the chunk size, defaults to 1024
public RegexSearcher(
string filePath,
System.Text.RegularExpressions.Regex regex,
AutoResetEvent progress,
int maxMatchCount = 100000,
int partitionSize = 256 * 1024,
int maxWorkers = 0,
int overlap = 1024) : this(
filePath,
new List<System.Text.RegularExpressions.Regex>() { regex },
progress,
maxMatchCount,
partitionSize,
maxWorkers,
overlap)
{
}
// Create a new instance to search a file with multiple regex
//
// filePath - the path to the file to search
// regexs - the list of regular expression to match against the file
// progress - signaled each time MatchCount is updated
// maxMatchCount - places an approximate limit on the number of matches, defaults to 100000
// partitionSize - the chunk size in bytes that each worker works on,
// defaults to 256 KiB
// maxWorkers - optional limit to the maximum number of simultaneous workers,
// defaults to unlimited
// overlap - size in bytes of partition overlap, used for finding matches,
// that span two partitions, may not exceed half the chunk size, defaults to 1024
public RegexSearcher(
string filePath,
List<System.Text.RegularExpressions.Regex> regexs,
AutoResetEvent progress,
int maxMatchCount = 100000,
int partitionSize = 256 * 1024,
int maxWorkers = 0,
int overlap = 1024) : base(
filePath,
progress,
JoinMode.None,
partitionSize,
maxWorkers: maxWorkers,
overlap: overlap)
{
matches = new();
matchQueue = new();
this.regexs = regexs;
this.maxMatchCount = maxMatchCount;
}
// Create a new instance to search a stream with single regex
//
// stream - the stream to search
// regex - the regular expression to match against the stream
// progress - signaled each time MatchCount is updated
// maxMatchCount - places an approximate limit on the number of matches, defaults to 100000
// partitionSize - the chunk size in bytes that each worker works on,
// defaults to 4 MiB
// maxWorkers - optional limit to the maximum number of simultaneous workers,
// defaults to unlimited
// overlap - size in bytes of partition overlap, used for finding matches,
// that span two partitions, may not exceed half the chunk size, defaults to 1024
public RegexSearcher(
Stream stream,
System.Text.RegularExpressions.Regex regex,
AutoResetEvent progress,
int maxMatchCount = 100000,
int partitionSize = 4096 * 1024,
int maxWorkers = 0,
int overlap = 1024) : this(
stream,
new List<System.Text.RegularExpressions.Regex>() { regex },
progress,
maxMatchCount,
partitionSize,
maxWorkers,
overlap)
{
}
// Create a new instance to search a stream with multiple regex
//
// stream - the stream to search
// regexs - the list of regular expressions to match against the stream
// progress - signaled each time MatchCount is updated
// maxMatchCount - places an approximate limit on the number of matches, defaults to 100000
// partitionSize - the chunk size in bytes that each worker works on,
// defaults to 4 MiB
// maxWorkers - optional limit to the maximum number of simultaneous workers,
// defaults to unlimited
// overlap - size in bytes of partition overlap, used for finding matches,
// that span two partitions, may not exceed half the chunk size, defaults to 1024
public RegexSearcher(
Stream stream,
List<System.Text.RegularExpressions.Regex> regexs,
AutoResetEvent progress,
int maxMatchCount = 100000,
int partitionSize = 4096 * 1024,
int maxWorkers = 0,
int overlap = 1024) : base(
"",
progress,
JoinMode.None,
partitionSize,
maxWorkers: maxWorkers,
overlap: overlap)
{
matches = new();
matchQueue = new();
this.regexs = regexs;
this.maxMatchCount = maxMatchCount;
base.Stream = stream;
}
// Start the background process
public override void Start()
{
if (!Running) {
matches.Clear();
matchQueue.Clear();
matchCount = 0;
base.Start();
}
}
// Finished, sort match data
protected override void Finish()
{
var dedupedMatchCount = 0;
HashSet<long> matchPositions = new();
var matchQueue = this.matchQueue;
while (matchQueue.TryDequeue(out MatchData result)) {
// Ignore duplicates
if (!matchPositions.Contains(result.StartFpos)) {
matches.Add(result);
matchPositions.Add(result.StartFpos);
}
}
//matches = matches.OrderBy(x => x.StartFpos).ToList();
matches.Sort((a, b) => a.StartFpos.CompareTo(b.StartFpos));
dedupedMatchCount += matches.Count;
// Adjust matchCount after dedup
matchCount = dedupedMatchCount;
// Adjust byte count for overlap
Interlocked.Add(ref byteCount, overlap);
}
// Return the MatchData of the completed search sorted by fpos
public IReadOnlyList<MatchData> GetMatchData()
{
if (Running) {
return new List<MatchData>().AsReadOnly();
}
else {
return matches.AsReadOnly();
}
}
// Replace matches after a completed search
//
// Data in Path that is between matches is copied as-is to the output stream.
// When a match is encountered matchEvaluator is called to determine how to
// replace the match. To erase the match the matchEvaluator should return an
// empty string. To overwrite the match with a new value the matchEvaluator
// should return the new value. To keep the existing match value the
// matchEvaluator should return the existing match value. Only works for
// file mode searches.
//
// output - the open output stream to receive the data
// matchEvaluator - callback to handle replacements
// encoding - the encoding of the replacement strings, defaults to UTF8
public void Replace(Stream output, Func<MatchData, string> matchEvaluator, Encoding? encoding = null)
{
encoding ??= Encoding.UTF8;
const int bufSize = 128 * 1024 * 1024;
if (!Running && Stream == null) {
var buf = new byte[bufSize];
using System.IO.FileStream input = Imagibee.Gigantor.FileStream.Create(Path, bufSize);
long endPos = input.Seek(0, SeekOrigin.End);
long readPos = input.Seek(0, SeekOrigin.Begin);
foreach (var match in matches) {
CopyBetweenMatches(buf, input, output, match.StartFpos);
CopyAtMatch(output, matchEvaluator(match), encoding);
input.Position += match.Value.Length;
}
CopyBetweenMatches(buf, input, output, endPos);
}
}
protected override PartitionData Join(PartitionData a, PartitionData b)
{
return a;
}
protected override PartitionData Map(PartitionerData data)
{
var str = Utilities.UnsafeByteToString(data.Buf);
for (var i = 0; i < regexs.Count; i++) {
DoMatch(data, str, i);
}
var bufLen = data.Buf == null ? 0:data.Buf.Length;
Interlocked.Add(ref byteCount, bufLen - overlap);
return new PartitionData();
}
void DoMatch(PartitionerData data, string partition, int regexIndex)
{
var partitionMatches = regexs[regexIndex].Matches(partition);
if (partitionMatches.Count > 0) {
for (int i = 0; i < partitionMatches.Count; i++) {
System.Text.RegularExpressions.Match match = partitionMatches[i];
if (match != null && matchQueue.Count < maxMatchCount) {
var groups = new List<GroupData>();
for (var j=0; j<match.Groups.Count; j++) {
var group = match.Groups[j];
List<CaptureData> cd = new();
for (var k=0; k<group.Captures.Count; k++) {
var capture = group.Captures[k];
cd.Add(
new CaptureData()
{
StartFpos = capture.Index + data.StartFpos,
Value = capture.Value,
});
}
groups.Add(
new GroupData()
{
StartFpos = data.StartFpos + group.Index,
Name = group.Name,
Value = group.Value,
Captures = cd.AsReadOnly(),
});
}
matchQueue.Enqueue(
new MatchData()
{
StartFpos = data.StartFpos + match.Index,
Name = match.Name,
Value = match.Value,
Groups = groups.AsReadOnly(),
RegexIndex = regexIndex,
});
}
}
}
}
void CopyBetweenMatches(byte[] buf, Stream input, Stream output, long endPos)
{
while (input.Position < endPos) {
int copySize = (int)Math.Min(endPos - input.Position, buf.Length);
input.Read(buf, 0, copySize);
output.Write(buf, 0, copySize);
}
}
void CopyAtMatch(Stream output, string replacement, Encoding encoding)
{
output.Write(encoding.GetBytes(replacement));
}
// private data
readonly ConcurrentQueue<MatchData> matchQueue;
readonly List<MatchData> matches;
readonly List<System.Text.RegularExpressions.Regex> regexs;
readonly int maxMatchCount;
long matchCount;
}
}
}