Skip to content

Instantly share code, notes, and snippets.

@monmohan
Created May 17, 2021 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monmohan/6f36518be332b4d9652da6dfc7943ceb to your computer and use it in GitHub Desktop.
Save monmohan/6f36518be332b4d9652da6dfc7943ceb to your computer and use it in GitHub Desktop.
func readBookDatasetOptimized(protoInFile string) error {
var rLen uint32
var book typedefs.Book
//Buffer to read bytes for decoding
var buf bytes.Buffer
done := false
//Fixed Length Buffer for reading from the file
rbuf := make([]byte, 1024)
r, err := os.Open(protoInFile)
if err != nil {
log.Fatalf("Failed to read file %s", err.Error())
}
defer r.Close()
for {
if !done {
err := fillBuffer(r, &buf, rbuf)
done = err == io.EOF
}
if done && buf.Len() == 0 {
return nil
}
if done && buf.Len() < 4 {
return fmt.Errorf("Bad encoding, unexpected format")
}
rLen = binary.LittleEndian.Uint32(buf.Next(4))
if done && buf.Len() < int(rLen) {
return fmt.Errorf("Bad encoding, buf len %d where as eof reached", buf.Len())
}
for !done && buf.Len() < int(rLen) {
err := fillBuffer(r, &buf, rbuf)
done = err == io.EOF
}
if err := proto.Unmarshal(buf.Next(int(rLen)), &book); err != nil {
return err
}
fmt.Printf("Title: %s \nAuthor: %s \nISBN: %s \nOverview: %s\n\n", book.Title, book.Author, book.Isbn, book.Overview)
}
}
//Transfer bytes read from the given reader(source) to the bytes.Buffer(target)
func fillBuffer(src io.Reader, target *bytes.Buffer, rbuf []byte) error {
n, err := src.Read(rbuf)
target.Write(rbuf[:n])
return err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment