// start. It never builds the views again from the log, because a new view
// differs from the live one and so loses the prompt cache.
//
+// Each write goes to the operating system at once, and a syncer makes the
+// writes durable by group commit in the background. Flush waits until the
+// writes so far are durable.
+//
// The summarizer builds the nodes in the queue in the background. The start
// reads the indexes once to fill the queue again. After that, only a new
// message or a new node adds work. Memory keeps the nodes of the views and the
mutex sync.Mutex
root string
log *Log
+ syncer *syncer
tree *Tree
chat *View
cview *View
m.ctx, m.cancel = context.WithCancel(ctx)
+ m.syncer, err = openSyncer(root)
+ if err != nil {
+ return nil, errors.Join(err, m.Close())
+ }
+
m.tree, err = openTree(ctx, root, log.Len(), clock)
if err != nil {
return nil, errors.Join(err, m.Close())
}
m.mutex.Lock()
+ m.syncer.Written()
m.dispatch()
m.mutex.Unlock()
return m.save()
}
-// Close stops the builds, waits for them, closes the files of the chat, and
-// releases its lock.
+// Close stops the builds, waits for them, makes the writes durable, closes the
+// files of the chat, and releases its lock.
func (m *Memory) Close() error {
m.cancel()
m.workers.Wait()
defer m.mutex.Unlock()
var err error
+ if m.syncer != nil {
+ err = m.syncer.Close()
+ }
+
if m.tree != nil {
- err = m.tree.Close()
+ err = errors.Join(err, m.tree.Close())
}
return errors.Join(err, m.log.Close())
return Message{}, err
}
+ defer m.syncer.Written()
+
err = m.addMessage(message)
if err != nil {
return Message{}, err
}
// addMessage builds the leaf of message when it needs no model call, or else
-// adds the leaf to the queue. Then it appends the leaf to the views.
+// adds the leaf to the queue. Then it appends the leaf to the views. A crash can
+// stop an append after the leaf build and before the view save, so a leaf that
+// is built already stays as it is, and restoreQueue settles its parent.
func (m *Memory) addMessage(message Message) error {
+ built, err := m.tree.Built(0, message.ID)
+ if err != nil {
+ return err
+ }
+
leaf, free := freeLeaf(message)
- if free {
- err := m.build(leaf)
+ switch {
+ case built:
+ case free:
+ err = m.build(leaf)
if err != nil {
return err
}
- } else {
+ default:
m.queue.AddLeaf(message.ID)
}
Text: text,
Size: len(text),
})
+ m.syncer.Written()
}
if err != nil {
}
}
+// Flush returns when every write before the call is durable.
+func (m *Memory) Flush(ctx context.Context) error {
+ return m.syncer.Flush(ctx)
+}
+
// save writes the views to view.json.
func (m *Memory) save() error {
data, err := json.Marshal(viewFile{
return m.chat.Render()
}
+// Len returns the number of messages in the log.
+func (m *Memory) Len() int {
+ m.mutex.Lock()
+ defer m.mutex.Unlock()
+
+ return m.log.Len()
+}
+
+// Message reads the message with the given ID.
+func (m *Memory) Message(id int) (Message, error) {
+ m.mutex.Lock()
+ defer m.mutex.Unlock()
+
+ return m.log.Message(id)
+}
+
+// Node reads the node (l, i) if it is built.
+func (m *Memory) Node(level, index int) (Node, bool, error) {
+ m.mutex.Lock()
+ defer m.mutex.Unlock()
+
+ return m.tree.Node(level, index)
+}
+
// Context returns the compaction view for the build of node (l, i). It ends
// before the message of a leaf, or after the last message of a merge, and it
// stops at the first line that is not built.
return m.cview.Context(end)
}
-
-// writeFileAtomic replaces the file at path with data through a temporary file
-// and a rename. A reader or a crash of the process sees the old file or the new
-// file, never a part of one.
-func writeFileAtomic(path string, data []byte) error {
- file, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".*")
- if err != nil {
- return fmt.Errorf("create temporary file: %w", err)
- }
-
- _, err = file.Write(data)
- err = errors.Join(err, file.Close())
- if err == nil {
- err = os.Rename(file.Name(), path)
- }
-
- if err != nil {
- return errors.Join(fmt.Errorf("replace file: %w", err), os.Remove(file.Name()))
- }
-
- return nil
-}