]> git.giacomolli.uk - onechat/blobdiff - memory.go
Name and order the probe code
[onechat] / memory.go
index ca8277c629889037c17c4ef221bcf94c049f0e8c..6937726b36de7998e18617d964ccc8fe77157e8f 100644 (file)
--- a/memory.go
+++ b/memory.go
@@ -56,7 +56,7 @@ type Memory struct {
        tree       *Tree
        chat       *View
        cview      *View
        tree       *Tree
        chat       *View
        cview      *View
-       queue      *queue
+       queue      *BuildQueue
        summarizer Summarizer
        ctx        context.Context
        cancel     context.CancelFunc
        summarizer Summarizer
        ctx        context.Context
        cancel     context.CancelFunc
@@ -66,8 +66,8 @@ type Memory struct {
 
 // viewFile is the content of view.json.
 type viewFile struct {
 
 // viewFile is the content of view.json.
 type viewFile struct {
-       Chat       viewState `json:"chat"`
-       Compaction viewState `json:"compaction"`
+       Chat       ViewState `json:"chat"`
+       Compaction ViewState `json:"compaction"`
 }
 
 // openMemory opens the chat in root and starts the builds that are ready. The
 }
 
 // openMemory opens the chat in root and starts the builds that are ready. The
@@ -82,7 +82,7 @@ func openMemory(ctx context.Context, root string, clock func() time.Time, summar
        m := &Memory{
                root:       root,
                log:        log,
        m := &Memory{
                root:       root,
                log:        log,
-               queue:      newQueue(),
+               queue:      newBuildQueue(),
                summarizer: summarizer,
                changed:    make(chan struct{}),
        }
                summarizer: summarizer,
                changed:    make(chan struct{}),
        }
@@ -126,8 +126,8 @@ func openMemory(ctx context.Context, root string, clock func() time.Time, summar
 func (m *Memory) load() error {
        data, err := os.ReadFile(m.viewPath())
        if errors.Is(err, os.ErrNotExist) && m.log.Len() == 0 {
 func (m *Memory) load() error {
        data, err := os.ReadFile(m.viewPath())
        if errors.Is(err, os.ErrNotExist) && m.log.Len() == 0 {
-               m.chat = newView(chatHigh, chatLow)
-               m.cview = newView(compactionHigh, compactionLow)
+               m.chat = newView(viewChatHigh, viewChatLow)
+               m.cview = newView(viewCompactionHigh, viewCompactionLow)
 
                return nil
        }
 
                return nil
        }
@@ -146,12 +146,12 @@ func (m *Memory) load() error {
                return fmt.Errorf("decode view: %w", err)
        }
 
                return fmt.Errorf("decode view: %w", err)
        }
 
-       m.chat, err = restoreView(file.Chat, chatHigh, chatLow, m.tree)
+       m.chat, err = restoreView(file.Chat, viewChatHigh, viewChatLow, m.tree)
        if err != nil {
                return fmt.Errorf("chat view: %w", err)
        }
 
        if err != nil {
                return fmt.Errorf("chat view: %w", err)
        }
 
-       m.cview, err = restoreView(file.Compaction, compactionHigh, compactionLow, m.tree)
+       m.cview, err = restoreView(file.Compaction, viewCompactionHigh, viewCompactionLow, m.tree)
        if err != nil {
                return fmt.Errorf("compaction view: %w", err)
        }
        if err != nil {
                return fmt.Errorf("compaction view: %w", err)
        }
@@ -543,6 +543,39 @@ func (m *Memory) View() string {
        return m.chat.Render()
 }
 
        return m.chat.Render()
 }
 
+// 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 {
+       m.mutex.Lock()
+       defer m.mutex.Unlock()
+
+       return m.chat.LinesWithout(skip)
+}
+
+// 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.
 // 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.