-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc8d7f7
commit 4b1be40
Showing
10 changed files
with
190 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
module Analog.Core.Batch | ||
|
||
open System | ||
open System.IO | ||
open System.Text.RegularExpressions | ||
open System.Diagnostics.CodeAnalysis | ||
open FSharp.Control | ||
|
||
type Capture = string -> string seq * string | ||
|
||
let capture ([<StringSyntax("regex")>] regex) : Capture = | ||
|
||
let regex = | ||
Regex(regex, RegexOptions.Multiline ||| RegexOptions.Compiled, TimeSpan.FromSeconds(int64 5)) | ||
|
||
fun input -> | ||
let entries, index = | ||
((List.empty<string>, 0), regex.Matches input) | ||
||> Seq.fold (fun (list, index) item -> | ||
let entry = input[index .. item.Index - 1].Trim() | ||
(if entry.Length > 0 then list @ [ entry ] else list), item.Index) | ||
|
||
match entries with | ||
| [] -> [], input | ||
| entries -> entries, input[index..] | ||
|
||
let sync (stream: Stream) (capture: Capture) : string seq = | ||
seq { | ||
use reader = new StreamReader(stream) | ||
let mutable leftover = String.Empty | ||
|
||
while not reader.EndOfStream do | ||
let buffer = Array.zeroCreate<char> 1024 | ||
let block = reader.ReadBlock buffer | ||
let cap = leftover + (buffer |> Array.take block |> String.Concat) |> capture | ||
leftover <- snd cap | ||
yield! fst cap | ||
|
||
yield! | ||
match leftover.Trim() with | ||
| "" -> Seq.empty | ||
| leftover -> Seq.singleton leftover | ||
} | ||
|
||
let async (stream: Stream) (capture: Capture) = | ||
taskSeq { | ||
use reader = new StreamReader(stream) | ||
let mutable leftover = String.Empty | ||
|
||
while not reader.EndOfStream do | ||
let buffer = Array.zeroCreate<char> 1024 | ||
let! block = reader.ReadBlockAsync(Memory(buffer)) | ||
let cap = leftover + (buffer |> Array.take block |> String.Concat) |> capture | ||
leftover <- snd cap | ||
yield! fst cap | ||
|
||
yield! | ||
match leftover.Trim() with | ||
| "" -> Seq.empty | ||
| leftover -> Seq.singleton leftover | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
module Analog.Core.Tests.BatchTest | ||
|
||
open System.IO | ||
open System.Text | ||
open Analog.Core | ||
open FSharp.Control | ||
open Swensen.Unquote | ||
open Xunit | ||
|
||
let input = | ||
""" | ||
[2018-10-15 15:38:23.184 +02:00] [INF] [Topshelf] Started | ||
[2018-10-15 15:40:55.598 +02:00] [INF] [Topshelf] Stopping | ||
[2018-10-15 15:40:55.599 +02:00] [INF] MAUTO Windows Service stopping | ||
[2018-10-15 15:40:55.599 +02:00] [INF] Cron scheduler stopping | ||
[2018-10-15 15:40:55.601 +02:00] [INF] Scheduler QuartzScheduler_$_NON_CLUSTERED shutting down. | ||
[2018-10-15 15:40:55.601 +02:00] [INF] Scheduler QuartzScheduler_$_NON_CLUSTERED paused. | ||
[2018-10-15 15:40:55.604 +02:00] [DBG] Shutting down threadpool... | ||
[2018-10-15 15:40:55.604 +02:00] [DBG] Shutdown of threadpool complete. | ||
[2018-10-15 15:40:55.604 +02:00] [INF] Scheduler QuartzScheduler_$_NON_CLUSTERED Shutdown complete. | ||
[2018-10-15 15:40:55.604 +02:00] [INF] Cron scheduler stopped | ||
[2018-10-15 15:40:55.604 +02:00] [INF] MAUTO Windows Service stopped | ||
[2018-10-15 15:40:55.606 +02:00] [INF] Cron scheduler stopping | ||
[2018-10-15 15:40:55.606 +02:00] [INF] Cron scheduler stopped | ||
[2018-10-15 15:40:55.660 +02:00] [INF] [Topshelf] Stopped | ||
[2018-10-15 15:40:55.953 +02:00] [DBG] WorkerThread is shut down | ||
""" | ||
|> Encoding.UTF8.GetBytes | ||
|
||
let expected = | ||
[ "[2018-10-15 15:38:23.184 +02:00] [INF] [Topshelf] Started" | ||
"[2018-10-15 15:40:55.598 +02:00] [INF] [Topshelf] Stopping" | ||
"[2018-10-15 15:40:55.599 +02:00] [INF] MAUTO Windows Service stopping" | ||
"[2018-10-15 15:40:55.599 +02:00] [INF] Cron scheduler stopping" | ||
"[2018-10-15 15:40:55.601 +02:00] [INF] Scheduler QuartzScheduler_$_NON_CLUSTERED shutting down." | ||
"[2018-10-15 15:40:55.601 +02:00] [INF] Scheduler QuartzScheduler_$_NON_CLUSTERED paused." | ||
"[2018-10-15 15:40:55.604 +02:00] [DBG] Shutting down threadpool..." | ||
"[2018-10-15 15:40:55.604 +02:00] [DBG] Shutdown of threadpool complete." | ||
"[2018-10-15 15:40:55.604 +02:00] [INF] Scheduler QuartzScheduler_$_NON_CLUSTERED Shutdown complete." | ||
"[2018-10-15 15:40:55.604 +02:00] [INF] Cron scheduler stopped" | ||
"[2018-10-15 15:40:55.604 +02:00] [INF] MAUTO Windows Service stopped" | ||
"[2018-10-15 15:40:55.606 +02:00] [INF] Cron scheduler stopping" | ||
"[2018-10-15 15:40:55.606 +02:00] [INF] Cron scheduler stopped" | ||
"[2018-10-15 15:40:55.660 +02:00] [INF] [Topshelf] Stopped" | ||
"[2018-10-15 15:40:55.953 +02:00] [DBG] WorkerThread is shut down" ] | ||
|
||
[<Fact>] | ||
let ``load stream as sync sequence of strings chopped by regex`` () = | ||
use stream = new MemoryStream(input) | ||
|
||
|
||
let actual = | ||
Batch.capture "\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} [+-]\d{2}:\d{2}\]" | ||
|> Batch.sync stream | ||
|> Seq.toList | ||
|
||
test <@ actual = expected @> | ||
|
||
[<Fact>] | ||
let ``load stream as async sequence of strings chopped by regex`` () = | ||
task { | ||
use stream = new MemoryStream(input) | ||
|
||
let! actual = | ||
Batch.capture "\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} [+-]\d{2}:\d{2}\]" | ||
|> Batch.async stream | ||
|> TaskSeq.toListAsync | ||
|
||
test <@ actual = expected @> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters