X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/a606686934b4028c29dca8e9dae6b2e188cee9c5..6004a6d0f7af1ecf3d0ac976f50bb4f93c619887:/memory.go?ds=sidebyside diff --git a/memory.go b/memory.go index bcc1095..c9fe1c5 100644 --- a/memory.go +++ b/memory.go @@ -5,7 +5,7 @@ import ( "encoding/json" "errors" "fmt" - "log" + "log/slog" "os" "path/filepath" "strings" @@ -25,7 +25,7 @@ var ( // openMemory opens the chat in root and starts the builds that are ready. The // clock gives the dates of new messages and the days of new nodes. logger gets // one line for each failed build. The builds stop when ctx ends or at Close. -func openMemory(ctx context.Context, root string, clock func() time.Time, summarizer Summarizer, logger *log.Logger) (*Memory, error) { +func openMemory(ctx context.Context, root string, clock func() time.Time, summarizer Summarizer, logger *slog.Logger) (*Memory, error) { log, err := openLog(ctx, root, clock) if err != nil { return nil, err @@ -108,7 +108,7 @@ type Memory struct { cview *View queue *BuildQueue summarizer Summarizer - logger *log.Logger + logger *slog.Logger ctx context.Context cancel context.CancelFunc workers sync.WaitGroup @@ -125,6 +125,7 @@ func (m *Memory) Close() error { defer m.mutex.Unlock() var err error + if m.syncer != nil { err = m.syncer.Close() } @@ -174,6 +175,7 @@ func (m *Memory) AppendDated(kind, text, date string) (Message, error) { // children reads the two children of the node key. func (m *Memory) children(key NodeKey) (Node, Node, error) { var children [2]Node + for n := range children { child, built, err := m.tree.Node(key.level-1, 2*key.index+n) if err != nil { @@ -194,6 +196,7 @@ func (m *Memory) children(key NodeKey) (Node, Node, error) { func (m *Memory) Wait(ctx context.Context) error { for { m.mutex.Lock() + running := m.queue.Running() changed := m.changed m.mutex.Unlock() @@ -217,6 +220,7 @@ func (m *Memory) Wait(ctx context.Context) error { func (m *Memory) Settle(ctx context.Context) error { for { m.mutex.Lock() + pending := m.chat.Pending() running := m.queue.Running() changed := m.changed @@ -243,6 +247,26 @@ func (m *Memory) Flush(ctx context.Context) error { return m.syncer.Flush(ctx) } +// Snapshot flushes all memory writes and runs snapshot while no append or +// summary write can change the log, indexes, tree, or views. snapshot must not +// call Memory. +func (m *Memory) Snapshot(ctx context.Context, snapshot func() error) error { + m.mutex.Lock() + defer m.mutex.Unlock() + + err := m.Flush(ctx) + if err != nil { + return fmt.Errorf("flush memory snapshot: %w", err) + } + + err = snapshot() + if err != nil { + return fmt.Errorf("copy memory snapshot: %w", err) + } + + return nil +} + // ViewLines returns the lines of the chat view, each with its newline, without // the leaf lines of the messages in skip. func (m *Memory) ViewLines(skip map[int]bool) []string { @@ -295,6 +319,7 @@ func (m *Memory) load() error { } var file MemoryViewFile + err = json.Unmarshal(data, &file) if err != nil { return fmt.Errorf("decode view: %w", err) @@ -342,6 +367,7 @@ func (m *Memory) restoreQueue() error { leaf, free := freeLeaf(message) if !free { m.queue.AddLeaf(id) + continue } @@ -410,6 +436,7 @@ func (m *Memory) dispatch() { } m.workers.Add(1) + go m.run(key, task) } } @@ -508,6 +535,7 @@ func (m *Memory) settle(key NodeKey) error { node, free := freeMerge(left, right) if !free { m.queue.AddMerge(parent) + return nil } @@ -583,13 +611,14 @@ func (m *Memory) run(key NodeKey, task Task) { } close(m.changed) + m.changed = make(chan struct{}) m.dispatch() } // failed logs the failed build of the node key with its cause err. func (m *Memory) failed(key NodeKey, err error) { - m.logger.Printf("summary failed, retry at next message: node (%d, %d): %v", key.level, key.index, err) + m.logger.Warn("summary failed", "level", key.level, "index", key.index, "err", err) } // ready tells if both children of the node key are built and the node is not. @@ -659,6 +688,7 @@ func (m *Memory) buildModelLines() ([]int, error) { } var sizes []int + for _, key := range keys { var node Node