diff options
author | Joop Kiefte <ikojba@gmail.com> | 2021-12-15 00:03:03 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-15 00:03:03 +0000 |
commit | 06a07547d88eda6c6911ff8604109422fcadab1a (patch) | |
tree | 817f36f4c444afa368d132915ffbc8c2c7cc1eab /message | |
parent | 99ba9783f80aec04aabfe57df2c3bc8fa4754eae (diff) |
Add trim functionality
Diffstat (limited to 'message')
-rw-r--r-- | message/message.go | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/message/message.go b/message/message.go index 7a1699d..4c0fc71 100644 --- a/message/message.go +++ b/message/message.go @@ -158,6 +158,25 @@ func MessagesFromIPFS(cid string) (*Messages, error) { return &messages, nil } +// Trim the Messages map to the given number of messages based on the importance of the messages +func (m *Messages) Trim(n int) { + // Create a slice of Messages sorted by importance + // Cannot lock the Messages map yet, Messages.MessageList() will lock it + msgs := m.MessageList() + // Now lock the Messages map and trim the map to the given number of messages + m.lock.Lock() + defer m.lock.Unlock() + // If the number of messages is less than or equal to the number of messages to keep, do nothing + if len(msgs) <= n { + return + } + fmt.Println("Trimming messages") + // Otherwise, remove the messages after the nth message from the map + for i := n; i < len(msgs); i++ { + delete(m.msgs, msgs[i].Stamp()) + } +} + // Add a message to the Messages map func (m *Messages) Add(msg *Message) { m.lock.Lock() |