From e617e5e146a56463699ed0500e31a28cfac8600b Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 2 Nov 2020 15:02:31 +0300 Subject: [PATCH] Added BigFileParser --- .../Services/FileSystem/BigFileParser.cs | 85 +++++++++++++++++++ ZeroLevel/ZeroLevel.csproj | 8 +- 2 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 ZeroLevel/Services/FileSystem/BigFileParser.cs diff --git a/ZeroLevel/Services/FileSystem/BigFileParser.cs b/ZeroLevel/Services/FileSystem/BigFileParser.cs new file mode 100644 index 0000000..5a148d8 --- /dev/null +++ b/ZeroLevel/Services/FileSystem/BigFileParser.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace ZeroLevel.Services.FileSystem +{ + public class BigFileParser + { + private readonly string _filePath; + private readonly Func _parser; + private readonly int _bufferSize; + + public BigFileParser(string filePath, Func parser, int bufferSize = 1024 * 1024 * 32) + { + if (string.IsNullOrWhiteSpace(filePath)) + { + throw new ArgumentNullException(nameof(filePath)); + } + if (parser == null) + { + throw new ArgumentNullException(nameof(parser)); + } + if (!File.Exists(filePath)) + { + throw new FileNotFoundException(filePath); + } + _filePath = filePath; + _parser = parser; + _bufferSize = bufferSize; + } + + public IEnumerable> ReadBatches(int batchSize) + { + var buffer = new T[batchSize]; + var buffer_index = 0; + using (FileStream fs = File.Open(_filePath, FileMode.Open, FileAccess.Read, FileShare.None)) + { + using (BufferedStream bs = new BufferedStream(fs, _bufferSize)) + { + using (StreamReader sr = new StreamReader(bs)) + { + string line; + while ((line = sr.ReadLine()) != null) + { + buffer[buffer_index] = _parser.Invoke(line); + buffer_index++; + if (buffer_index >= batchSize) + { + buffer_index = 0; + yield return buffer; + } + } + } + } + } + if (buffer_index > 0) + { + if (buffer_index < batchSize) + { + var bias = new T[buffer_index]; + Array.Copy(buffer, 0, bias, 0, buffer_index); + yield return bias; + } + } + } + + public IEnumerable Read(int batchSize) + { + using (FileStream fs = File.Open(_filePath, FileMode.Open, FileAccess.Read, FileShare.None)) + { + using (BufferedStream bs = new BufferedStream(fs, _bufferSize)) + { + using (StreamReader sr = new StreamReader(bs)) + { + string line; + while ((line = sr.ReadLine()) != null) + { + yield return _parser.Invoke(line); + } + } + } + } + } + } +} diff --git a/ZeroLevel/ZeroLevel.csproj b/ZeroLevel/ZeroLevel.csproj index 68acd05..98884fd 100644 --- a/ZeroLevel/ZeroLevel.csproj +++ b/ZeroLevel/ZeroLevel.csproj @@ -6,16 +6,16 @@ ogoun ogoun - 3.3.4.7 - Fix PeriodicFileSystemWatcher + 3.3.4.8 + Added BigFileParser https://github.com/ogoun/Zero/wiki Copyright Ogoun 2020 https://github.com/ogoun/Zero GitHub - 3.3.4.7 - 3.3.4.7 + 3.3.4.8 + 3.3.4.8 AnyCPU;x64 zero.png