Done with chapter 1. Basic http/json log server.
This commit is contained in:
parent
407c8a15c9
commit
9c9c3b4697
5 changed files with 172 additions and 0 deletions
46
internal/server/log.go
Normal file
46
internal/server/log.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Log represents a commit log.
|
||||
type Log struct {
|
||||
mu sync.Mutex
|
||||
records []Record
|
||||
}
|
||||
|
||||
// NewLog returns a new ready to use Log.
|
||||
func NewLog() *Log {
|
||||
return &Log{}
|
||||
}
|
||||
|
||||
// Append adds a new record to a log.
|
||||
func (c *Log) Append(record Record) (uint64, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
record.Offset = uint64(len(c.records))
|
||||
c.records = append(c.records, record)
|
||||
return record.Offset, nil
|
||||
}
|
||||
|
||||
// Read returns an individual record from the log given its offset.
|
||||
// Returns an error if the offset does not exist.
|
||||
func (c *Log) Read(offset uint64) (Record, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if offset >= uint64(len(c.records)) {
|
||||
return Record{}, ErrOffsetNotFound
|
||||
}
|
||||
|
||||
return c.records[offset], nil
|
||||
}
|
||||
|
||||
// Record represents a single record in a commit log.
|
||||
type Record struct {
|
||||
Value []byte `json:"value"`
|
||||
Offset uint64 `json:"offset"`
|
||||
}
|
||||
|
||||
var ErrOffsetNotFound = errors.New("offset not found")
|
||||
Loading…
Add table
Add a link
Reference in a new issue