0
votes

I am trying to parse a very large log text file (100 000 lines +) after a test run on a certain software. In order to do this, I divide the file in windows of 1 000 lines and read incrementally each of these windows for each test in our test run. When I have finished reading those 1 000 lines, to make sure that I won't parse them for nothing again, I want to set a Boolean to true that means that I should just skip that window since I am sure I have finished parsing it.

This is what I have come up with so far:

...
let windowedAMLogSeq = Seq.windowed 1000 fullAMLogSeq
                                |> Seq.map (fun (window:string[]) -> (window , false))
for category in sortedTrxFailedTests do
    for failedTest in category do
        ...
        match failedTest.StartTime with
        | Some x ->
            match failedTest.EndTime with
            | Some y ->
                let accessManagerLogLines = parseAccessManagerLogFile x y windowedAMLogSeq
                ...

with

let parseAccessManagerLogFile (testStartTime:DateTime) (testEndTime:DateTime) (windowedAMLogSeq:seq<string[] * bool>) =
    Seq.map (parseWindow testStartTime testEndTime) windowedAMLogSeq

and

let parseWindow testStartTime testEndTime (window:(string[] * bool)) =
    match (snd window) with
    | true ->
        [||]
    | false ->
        let lineArray = Array.map (fun (line:string) -> if (isBetweenStartAndEndTime testStartTime testEndTime line) then line else "") (fst window)
        let cleansedArray = Array.partition (fun (line:string) -> line <> "") lineArray
                                |> fst

        match cleansedArray.Length > 0 with
        | true ->
            ()
        | false ->
            window <- ((fst window), true)  // Problem here, not mutable or by ref
        cleansedArray

Obviously, this won't compile as window is not mutable or passed by reference. I have tried multiple combinations of using mutable and ref or byref keywords with no success. Being fairly new to F#, I am unsure of the correct syntax to use.

What would be the syntax to use in order to modify by reference the window value directly, or to be able to modify by reference the Boolean value in the window tuple?

2

2 Answers

1
votes

The solution to my question was to use the ref keyword like follows. Also, notice that you have to use the .Value property on the tuple now.

...
let windowedAMLogSeq = Seq.windowed 1000 fullAMLogSeq
                            |> Seq.map (fun (window:string[]) -> 
                                                let mutable tuple = ref (window , false)
                                                tuple
                                            )
for category in sortedTrxFailedTests do
    for failedTest in category do
        ...
        match failedTest.StartTime with
        | Some x ->
            match failedTest.EndTime with
            | Some y ->
                let accessManagerLogLines = parseAccessManagerLogFile x y windowedAMLogSeq
                ...

with

let parseAccessManagerLogFile (testStartTime:DateTime) (testEndTime:DateTime) (windowedAMLogSeq:seq<(string[] * bool) ref>) =
    Seq.map (parseWindow testStartTime testEndTime) windowedAMLogSeq

and

let parseWindow testStartTime testEndTime (window:(string[] * bool) ref) =
    match (snd window.Value) with
    | true ->
        [||]
    | false ->
        let lineArray = Array.map (fun (line:string) -> if (isBetweenStartAndEndTime testStartTime testEndTime line) then line else "") (fst window.Value)
        let cleansedArray = Array.partition (fun (line:string) -> line <> "") lineArray
                                |> fst

        match cleansedArray.Length > 0 with
        | true ->
            ()
        | false ->
            window.Value <- ((fst window.Value), true)

        cleansedArray
1
votes

You can make the whole thing slightly cleaner by holding your window in a record type with a mutable field:

type LogWindow =
    {
        lines: string [] 
        mutable parsed: bool
    }

You create it like this:

let windowedAMLogSeq = Seq.windowed 1000 fullAMLogSeq
                       |> Seq.map (fun window -> { lines = window; parsed = false })

And then in your parseWindow:

window.parsed <- true

But your code looks really odd to me. Do you really want to create sliding windows of your log, rather than split it into chunks? Because what Seq.windowed creates are sliding windows.