Skip to content

Commit

Permalink
forgot a file
Browse files Browse the repository at this point in the history
  • Loading branch information
jewei1997 committed Jul 12, 2024
1 parent 2271605 commit f8685be
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions types/genesis/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package genesis

import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"strings"
)

type GenesisImportConfig struct {
StreamGenesisImport bool
GenesisStreamFile string
}

const bufferSize = 100000

func IngestGenesisFileLineByLine(filename string) <-chan string {
lines := make(chan string)

go func() {
defer close(lines)

file, err := os.Open(filename)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()

reader := bufio.NewReader(file)

buffer := make([]byte, bufferSize)
lineBuf := new(strings.Builder)

for {
bytesRead, err := reader.Read(buffer)
if err != nil && err != io.EOF {
fmt.Println("Error reading file:", err)
return
}

chunk := buffer[:bytesRead]
for len(chunk) > 0 {
i := bytes.IndexByte(chunk, '\n')
if i >= 0 {
lineBuf.Write(chunk[:i])
lines <- lineBuf.String()
lineBuf.Reset()
chunk = chunk[i+1:]
} else {
lineBuf.Write(chunk)
break
}
}

if err == io.EOF {
if lineBuf.Len() > 0 {
lines <- lineBuf.String()
}
break
}
}
}()

return lines
}

0 comments on commit f8685be

Please sign in to comment.