March 2021
Intermediate to advanced
260 pages
5h 45m
English
We’ll explore commit logs in depth in Chapter 3, Write a Log Package, when we build a persisted commit log library. For now, all you need to know about commit logs is that they’re a data structure for an append-only sequence of records, ordered by time, and you can build a simple commit log with a slice.
Create an internal/server directory tree in the root of your project and put the following code under the server directory in a file called log.go:
| | package server |
| | |
| | import ( |
| | "fmt" |
| | "sync" |
| | ) |
| | |
| | type Log struct { |
| | mu sync.Mutex |
| | records []Record |
| | } |
| | |
| | func NewLog() *Log { |
| | return &Log{} |
| | } |
| | |
| | func (c *Log) Append(record Record) (uint64, error) { |
| | c.mu.Lock() ... |
Read now
Unlock full access