WIP: offloading to disk

This commit is contained in:
2023-02-19 00:58:42 +02:00
parent 3c04345560
commit 3ddd238552
11 changed files with 372 additions and 64 deletions
+139
View File
@@ -22,6 +22,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/illusionman1212/gorc/irc"
"github.com/illusionman1212/gorc/irc/commands"
)
type ConnectMsg struct{}
@@ -40,6 +41,15 @@ func Quit(client *irc.Client) tea.Cmd {
}
}
type TickMsg struct{}
func Tick(client *irc.Client) tea.Cmd {
return tea.Tick(time.Second/60, func(time.Time) tea.Msg {
client.SendCommand(commands.PRIVMSG, "#hell", fmt.Sprintf("%d", client.Ticks))
return TickMsg{}
})
}
type SendPrivMsgMsg struct {
Msg string
Timestamp string
@@ -74,3 +84,132 @@ type UpdateTabBarMsg struct{}
func UpdateTabBar() tea.Msg {
return UpdateTabBarMsg{}
}
type ReadHistoryFromDBMsg struct {
LinesRead uint64
Direction string
}
func ReadHistoryFromDB(client *irc.Client, linesToRead int, direction string) tea.Cmd {
hash := client.Channels[client.ActiveChannelIndex].Hash
scrollBackBufFirstLine := client.Channels[client.ActiveChannelIndex].ScrollBackBufFirstLine
messages := make([]string, 0)
switch direction {
case "top":
client.DB.
Table("histories").
Select("message").
Where("hash = ?", hash).
Limit(linesToRead).
Scan(&messages).
Order("line ASC")
// set the new messages
copy(client.Channels[client.ActiveChannelIndex].ScrollBackBuf, messages)
// adjust the scrollback buf first line
client.Channels[client.ActiveChannelIndex].ScrollBackBufFirstLine = 1
return func() tea.Msg {
return ReadHistoryFromDBMsg{
LinesRead: 0,
Direction: "top",
}
}
case "up":
limit := linesToRead
linesToGet := 0
if linesToRead > int(scrollBackBufFirstLine) {
limit = int(scrollBackBufFirstLine) - 1
linesToGet = int(scrollBackBufFirstLine) - limit
} else {
linesToGet = int(scrollBackBufFirstLine) - linesToRead
}
client.DB.
Table("histories").
Select("message").
Where("hash = ? AND line >= ?", hash, linesToGet).
Limit(limit).
Scan(&messages).
Order("line ASC")
if len(messages) != 0 {
// prepend to the scroll back buf
client.Channels[client.ActiveChannelIndex].ScrollBackBuf = append(messages, client.Channels[client.ActiveChannelIndex].ScrollBackBuf...)
client.Channels[client.ActiveChannelIndex].ScrollBackBuf = client.Channels[client.ActiveChannelIndex].ScrollBackBuf[:irc.SCROLLBACK_MAX]
// adjust the scrollback buf first line
client.Channels[client.ActiveChannelIndex].ScrollBackBufFirstLine -= uint64(limit)
return func() tea.Msg {
return ReadHistoryFromDBMsg{
LinesRead: uint64(limit),
Direction: "up",
}
}
}
case "down":
client.DB.
Table("histories").
Select("message").
Where("hash = ? AND line >= ?", hash, client.Channels[client.ActiveChannelIndex].ScrollBackBufFirstLine+irc.SCROLLBACK_MAX).
Limit(linesToRead).
Scan(&messages).
Order("line ASC")
linesGot := len(messages)
if len(messages) != 0 {
extraMessagesLen := 0
// append to the scroll back buf
client.Channels[client.ActiveChannelIndex].ScrollBackBuf = append(client.Channels[client.ActiveChannelIndex].ScrollBackBuf, messages...)
// if we reached the end of the messages in the db. try appending the messages in the ToInsert buffer
if linesGot < linesToRead {
for _, history := range client.Channels[client.ActiveChannelIndex].ToInsert {
client.Channels[client.ActiveChannelIndex].ScrollBackBuf = append(client.Channels[client.ActiveChannelIndex].ScrollBackBuf, history.Message)
}
extraMessagesLen = len(client.Channels[client.ActiveChannelIndex].ToInsert)
}
client.Channels[client.ActiveChannelIndex].ScrollBackBuf = client.Channels[client.ActiveChannelIndex].ScrollBackBuf[len(client.Channels[client.ActiveChannelIndex].ScrollBackBuf)-irc.SCROLLBACK_MAX:]
// adjust the scrollback buf first line
client.Channels[client.ActiveChannelIndex].ScrollBackBufFirstLine += uint64(linesGot) + uint64(extraMessagesLen)
return func() tea.Msg {
return ReadHistoryFromDBMsg{
LinesRead: uint64(linesGot) + uint64(extraMessagesLen),
Direction: "down",
}
}
}
case "bottom":
client.DB.Raw("SELECT t.message FROM (SELECT line, message FROM histories WHERE hash = ? ORDER BY line DESC LIMIT ?) t ORDER BY t.line ASC;", hash, linesToRead).
Scan(&messages)
toInsert := make([]string, 0)
for _, history := range client.Channels[client.ActiveChannelIndex].ToInsert {
toInsert = append(toInsert, history.Message)
}
// set the new messages
copy(client.Channels[client.ActiveChannelIndex].ScrollBackBuf, messages)
client.Channels[client.ActiveChannelIndex].ScrollBackBuf = append(client.Channels[client.ActiveChannelIndex].ScrollBackBuf, toInsert...)
client.Channels[client.ActiveChannelIndex].ScrollBackBuf = client.Channels[client.ActiveChannelIndex].ScrollBackBuf[len(toInsert):]
// adjust the scrollback buf first line
client.Channels[client.ActiveChannelIndex].ScrollBackBufFirstLine = client.Channels[client.ActiveChannelIndex].TotalLines - irc.SCROLLBACK_MAX + uint64(len(toInsert))
return func() tea.Msg {
return ReadHistoryFromDBMsg{
LinesRead: 0,
Direction: "bottom",
}
}
}
return func() tea.Msg {
return ReadHistoryFromDBMsg{
LinesRead: 0,
Direction: "unknown",
}
}
}