Build a Log

Distributed Services with Go — by Travis Jeffery (26 / 84)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 How Logs Work | TOC | What You Learned 👉

We will build our log from the bottom up, starting with the store and index files, then the segment, and finally the log. That way we can write and run tests as we build each piece. Since the word log can refer to at least three different things — a record, the file that stores records, and the abstract data type that ties segments together — to make things less confusing, throughout this chapter, I will consistently use the following terms to mean these things:

  • Record — the data stored in our log.
  • Store — the file we store records in.
  • Index — the file we store index entries in.
  • Segment — the abstraction that ties a store and an index together.
  • Log — the abstraction that ties all the segments together.

Code the Store

To get started, create a directory at internal/log for our log package, then create a file called store.go in that directory that contains the following code:

WriteALogPackage/internal/log/store.go

​ ​package​ log

​ ​import​ (
​ ​"bufio"​
​ ​"encoding/binary"​
​ ​"os"​
​ ​"sync"​
​ )

​ ​var​ (
​ enc = binary.BigEndian
​ )

​ ​const​ (
​ lenWidth = 8
​ )

​ ​type​ store ​struct​ {
​ *os.File
​ mu…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.