Skip to content

Commit

Permalink
Change reader to handle huge content
Browse files Browse the repository at this point in the history
  • Loading branch information
HappyTobi committed Mar 5, 2024
1 parent 9f24516 commit 358bab6
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions trace/jsonFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"encoding/json"
"fmt"
"io"
"log"
"os"

Expand All @@ -19,16 +20,26 @@ func requestTracesFromJsonFile(input string) ([]types.RequestTrace, error) {

defer fileData.Close()

scanner := bufio.NewScanner(fileData)
reader := bufio.NewReader(fileData)

var jsonLine map[string]interface{}

traces := make([]types.RequestTrace, 0)

traceLines, requestCount, responseCount := 0, 0, 0
for scanner.Scan() {
if err := json.Unmarshal(scanner.Bytes(), &jsonLine); err != nil {
return nil, fmt.Errorf("could not unmarhal text into json %v", err)

for {
lineData, err := read(reader)
if err != nil {
if err == io.EOF {
break
}

return nil, fmt.Errorf("could not read line %v", err)
}

if err := json.Unmarshal(lineData, &jsonLine); err != nil {
return nil, fmt.Errorf("could not unmarhal text into json %v - json data %s", err, string(lineData))
}

l, err := rawlog.NewRawLogJson(jsonLine)
Expand All @@ -55,13 +66,28 @@ func requestTracesFromJsonFile(input string) ([]types.RequestTrace, error) {
traceLines++
}

if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("failed to read input file: %v", err)
}

log.Printf("[INFO] total traces: %d", traceLines)
log.Printf("[INFO] request count: %d", requestCount)
log.Printf("[INFO] response count: %d", responseCount)

return mergeTraces(traces), nil
}

func read(reader *bufio.Reader) ([]byte, error) {
lineData := make([]byte, 0)

for {
line, prefix, err := reader.ReadLine()
if err != nil {
return line, err
}

lineData = append(lineData, line...)

if !prefix {
break
}
}

return lineData, nil
}

0 comments on commit 358bab6

Please sign in to comment.