]> git.giacomolli.uk - onechat/blobdiff - log.go
Make intentional error discards explicit
[onechat] / log.go
diff --git a/log.go b/log.go
index dfb6e7098ab1df2048da5c96604f37ef6e591345..1ed70e78e36aaccaf862118cfe56c95fa107c671 100644 (file)
--- a/log.go
+++ b/log.go
@@ -7,16 +7,12 @@ import (
        "fmt"
        "os"
        "path/filepath"
-       "syscall"
        "time"
        "unicode/utf8"
 )
 
-// errLocked tells that another process owns the chat.
-var errLocked = errors.New("another process owns the chat")
-
-// kinds holds the kinds that a message can have.
-var kinds = map[string]bool{
+// logKinds holds the logKinds that a message can have.
+var logKinds = map[string]bool{
        "user":  true,
        "reply": true,
        "tool":  true,
@@ -25,21 +21,10 @@ var kinds = map[string]bool{
        "note":  true,
 }
 
-// Message is one entry of the chat log. ID is the permanent position of the
-// message in the chat, from 0. Size is the UTF-8 byte length of Text. Date is
-// the time of the write in RFC 3339 format.
-type Message struct {
-       ID   int    `json:"i"`
-       Kind string `json:"kind"`
-       Text string `json:"text"`
-       Size int    `json:"size"`
-       Date string `json:"date"`
-}
-
 // Log is the append-only message log of one chat. It writes one JSON line for
-// each message to main/YYYY-MM-DD.jsonl, named for the local day of the write.
-// It never edits or deletes a line. Each line goes to the operating system
-// before Append returns, so a crash of the process loses no message. An
+// each message to main/YYYY-MM-DD.jsonl, named for the local day of the
+// message. It never edits or deletes a line. Each line goes to the operating
+// system before Append returns, so a crash of the process loses no message. An
 // exclusive lock on the lock file makes sure that only one process writes the
 // log. After a failed write, the end of the file is not known, so the log
 // refuses all writes until it is opened again. The caller serializes all calls.
@@ -51,11 +36,23 @@ type Message struct {
 type Log struct {
        clock   func() time.Time
        lock    *os.File
-       journal journal
-       index   *records
+       journal Journal
+       index   *Records
        failure error
 }
 
+// Message is one entry of the chat log. ID is its position in the log, from 0.
+// A rebuild assigns positions in the merged log. Size is the UTF-8 byte length
+// of Text. Date is the message time in RFC 3339 format, supplied at append or
+// taken from the clock.
+type Message struct {
+       ID   int    `json:"i"`
+       Kind string `json:"kind"`
+       Text string `json:"text"`
+       Size int    `json:"size"`
+       Date string `json:"date"`
+}
+
 // openLog locks the chat in root and indexes its new lines. The clock gives the
 // date of each new message.
 func openLog(ctx context.Context, root string, clock func() time.Time) (*Log, error) {
@@ -74,7 +71,7 @@ func openLog(ctx context.Context, root string, clock func() time.Time) (*Log, er
        l := &Log{
                clock: clock,
                lock:  lock,
-               journal: journal{
+               journal: Journal{
                        dir: filepath.Join(root, "main"),
                },
        }
@@ -92,9 +89,25 @@ func openLog(ctx context.Context, root string, clock func() time.Time) (*Log, er
        return l, nil
 }
 
+// Close releases the lock. The log refuses all calls after Close.
+func (l *Log) Close() error {
+       err := l.journal.Close()
+       if l.index != nil {
+               err = errors.Join(err, l.index.Close())
+               l.index = nil
+       }
+
+       if l.lock != nil {
+               err = errors.Join(err, l.lock.Close())
+               l.lock = nil
+       }
+
+       return err
+}
+
 // catchUp indexes the lines after the last indexed line.
 func (l *Log) catchUp(ctx context.Context) error {
-       start := position{}
+       start := Position{}
        if l.index.count > 0 {
                record, _, err := l.index.Read(l.index.count - 1)
                if err != nil {
@@ -102,24 +115,25 @@ func (l *Log) catchUp(ctx context.Context) error {
                }
 
                last := decodePosition(record)
-               start = position{
+               start = Position{
                        day:    last.day,
                        offset: last.offset + uint64(last.length),
                }
        }
 
-       return l.journal.Scan(ctx, start, func(line []byte, place position) error {
+       return l.journal.Scan(ctx, start, func(line []byte, place Position) error {
                var message Message
+
                err := json.Unmarshal(line, &message)
                if err != nil {
-                       return err
+                       return fmt.Errorf("decode log message: %w", err)
                }
 
                if message.ID != l.index.count {
                        return fmt.Errorf("expected message %d, found %d", l.index.count, message.ID)
                }
 
-               err = validateMessage(message)
+               err = logValidateMessage(message)
                if err != nil {
                        return fmt.Errorf("message %d: %w", message.ID, err)
                }
@@ -128,52 +142,55 @@ func (l *Log) catchUp(ctx context.Context) error {
        })
 }
 
-func (l *Log) addIndex(place position) error {
+func (l *Log) addIndex(place Position) error {
        record := make([]byte, positionBytes)
        encodePosition(record, place)
 
        return l.index.Append(record)
 }
 
-// Close releases the lock. The log refuses all calls after Close.
-func (l *Log) Close() error {
-       err := l.journal.Close()
-       if l.index != nil {
-               err = errors.Join(err, l.index.Close())
-               l.index = nil
-       }
-
-       if l.lock != nil {
-               err = errors.Join(err, l.lock.Close())
-               l.lock = nil
-       }
-
-       return err
-}
-
 // Append writes a new message to the log and returns it with its ID, size, and
 // date.
 func (l *Log) Append(kind, text string) (Message, error) {
+       return l.AppendDated(kind, text, "")
+}
+
+// AppendDated writes a message with date in RFC 3339 format. An empty date uses
+// the clock. The day file uses the local day of the given date.
+func (l *Log) AppendDated(kind, text, date string) (Message, error) {
        if l.lock == nil {
-               return Message{}, os.ErrClosed
+               return Message{}, fmt.Errorf("append dated message: %w", os.ErrClosed)
        }
 
        if l.failure != nil {
                return Message{}, fmt.Errorf("log needs a reopen after a failed write: %w", l.failure)
        }
 
+       var err error
+
        now := l.clock()
+       if date == "" {
+               date = now.Format(time.RFC3339Nano)
+       } else {
+               now, err = time.Parse(time.RFC3339Nano, date)
+               if err != nil {
+                       return Message{}, fmt.Errorf("parse message date: %w", err)
+               }
+
+               now = now.In(time.Local)
+       }
+
        message := Message{
                ID:   l.index.count,
                Kind: kind,
                Text: text,
                Size: len(text),
-               Date: now.Format(time.RFC3339Nano),
+               Date: date,
        }
 
-       err := validateMessage(message)
+       err = logValidateMessage(message)
        if err != nil {
-               return Message{}, err
+               return Message{}, fmt.Errorf("validate dated message: %w", err)
        }
 
        line, err := json.Marshal(message)
@@ -216,6 +233,7 @@ func (l *Log) Message(id int) (Message, error) {
        }
 
        var message Message
+
        err = json.Unmarshal(line, &message)
        if err != nil {
                return Message{}, fmt.Errorf("decode message %d: %w", id, err)
@@ -228,10 +246,10 @@ func (l *Log) Message(id int) (Message, error) {
        return message, nil
 }
 
-// validateMessage makes sure that a message has a known kind, valid UTF-8 text,
-// the size of its text, and a valid date.
-func validateMessage(message Message) error {
-       if !kinds[message.Kind] {
+// logValidateMessage makes sure that a message has a known kind, valid UTF-8
+// text, the size of its text, and a valid date.
+func logValidateMessage(message Message) error {
+       if !logKinds[message.Kind] {
                return fmt.Errorf("unknown message kind %q", message.Kind)
        }
 
@@ -250,23 +268,3 @@ func validateMessage(message Message) error {
 
        return nil
 }
-
-// lockFile opens the file at path and takes an exclusive lock on it. The lock
-// stays until the file closes or the process ends.
-func lockFile(path string) (*os.File, error) {
-       file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600)
-       if err != nil {
-               return nil, fmt.Errorf("open lock file: %w", err)
-       }
-
-       err = syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
-       if errors.Is(err, syscall.EWOULDBLOCK) {
-               err = errLocked
-       }
-
-       if err != nil {
-               return nil, errors.Join(fmt.Errorf("lock chat: %w", err), file.Close())
-       }
-
-       return file, nil
-}