package main
import (
"log"
"sort"
"craftdesign.group/quarry/internal/chat"
"craftdesign.group/quarry/internal/oltp"
)
// migrateChatKeys settles the chat keys of the repositories that existed
// before keys did, once, at start-up and before any repository is
// registered. Every other repository without a key is simply given its
// own, with an empty channel: a repository that appears later, whatever
// project code it holds, never inherits a history.
//
// Before channels were keyed by repository, a channel and its robot
// lines were stored under the store's project code, which two
// repositories of one project share. A repository's history is under
// its code, and where several repositories hold one code it is shared,
// private ones' included.
//
// - A code one repository holds keeps its history where it is: the
// code becomes that repository's key, and nothing at all is touched,
// however quiet the channel is.
// - Where several hold a code and any is private, the history goes to
// the private ones only: never to a public repository, whose
// readers were never meant to see it. The first of them by name
// keeps it in place and each other private one gets a copy; the
// public ones start with an empty channel.
// - Where all are public the history is genuinely shared: the first
// by name keeps it and each other gets a copy.
//
// A copy has new sequences (a message's identity is its stream
// sequence), so everything the repository keeps about its channel's
// messages is rewritten to follow them (RekeyChatSeqs), in the one
// transaction that gives it its key. A copy carries only what was said
// before the first run (ChatKeyCeiling). A repository that starts empty
// while sharing has those rows cleared.
//
// The pre-existing repositories are the cohort the oltp captured the
// first time this ran (ChatKeyCohort), and only they are ever settled.
// It is idempotent: a key is written last, and a copy cut short is
// resumed, not repeated. A cohort repository whose store cannot be read
// to learn its code is left alone and tried again at the next start. It
// logs counts only.
func migrateChatKeys(odb *oltp.DB, room *chat.Room, readCode func(path string) (string, error)) error {
cohort, err := odb.ChatKeyCohort()
if err != nil {
return err
}
// Recorded the first time, with the cohort: what is said after this
// is not history for anyone to inherit, whenever the copy is made.
last, err := room.LastSeq()
if err != nil {
return err
}
ceiling, err := odb.ChatKeyCeiling(last)
if err != nil {
return err
}
rows, err := odb.ChatKeyRows()
if err != nil {
return err
}
var kept, copiedRepos, copiedMsgs, fresh int
// Anything keyless outside the cohort is new: its own key, no history.
for i, r := range rows {
if r.Key == "" && !cohort[r.Name] {
if _, err := odb.AssignChatKey(r.Name, chat.RepoKey(r.Name)); err != nil {
return err
}
rows[i].Key = chat.RepoKey(r.Name)
fresh++
}
}
// A Ready cohort repository whose row records no code is asked its store.
for i, r := range rows {
if r.Key != "" || r.Code != "" || r.State != oltp.Ready {
continue
}
code, err := readCode(r.Path)
if err != nil || code == "" {
log.Printf("chat keys: a repository's project code could not be read; it is left for the next start")
rows[i].Key = "-" // not this run's to assign
continue
}
if err := odb.SetRepoCode(r.Name, code); err != nil {
return err
}
rows[i].Code = code
}
byCode := map[string][]oltp.ChatKeyRow{}
for _, r := range rows {
switch {
case r.Key == "-":
case r.Key == "" && r.Code == "":
// Not yet cloned: there is no store, so no history.
if _, err := odb.AssignChatKey(r.Name, chat.RepoKey(r.Name)); err != nil {
return err
}
fresh++
case r.Key == "" || r.Key == r.Code:
byCode[r.Code] = append(byCode[r.Code], r)
}
}
codes := make([]string, 0, len(byCode))
for c := range byCode {
codes = append(codes, c)
}
sort.Strings(codes)
for _, code := range codes {
members := byCode[code]
heirs := inheritors(members)
owner := heirs[0]
for _, m := range members {
if m.Key == code { // a holder from a run cut short
owner = m
break
}
}
isHeir := map[string]bool{}
for _, h := range heirs {
isHeir[h.Name] = true
}
var ownerPending bool
for _, m := range members {
switch {
case m.Key != "":
case m.Name == owner.Name:
ownerPending = true
case !isHeir[m.Name]:
if err := odb.RekeyChatSeqs(m.Name, chat.RepoKey(m.Name), nil); err != nil {
return err
}
fresh++
default:
n, err := copyChannel(odb, room, code, m.Name, ceiling)
if err != nil {
return err
}
copiedRepos++
copiedMsgs += n
}
}
// The owner last: until it has the code as its key, a run cut
// short sees the group as it was.
if ownerPending {
if _, err := odb.AssignChatKey(owner.Name, code); err != nil {
return err
}
kept++
}
}
if kept+copiedRepos+fresh > 0 {
log.Printf("chat keys: %d repositories keep their channel, %d got a copy of one (%d messages), %d have an empty channel of their own",
kept, copiedRepos, copiedMsgs, fresh)
}
return nil
}
// inheritors are the members whose reading of the history is
// legitimate, ordered by name: every private member if there is one,
// otherwise all of them. A public repository never inherits a channel
// that a private one shared.
func inheritors(members []oltp.ChatKeyRow) []oltp.ChatKeyRow {
var out []oltp.ChatKeyRow
for _, m := range members {
if m.Visibility != oltp.Public {
out = append(out, m)
}
}
if len(out) == 0 {
out = append(out, members...)
}
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
return out
}
// copyChannel gives a repository its own channel holding what the
// channel under code holds, and follows the new sequences in what it
// keeps, giving it its key in the same transaction. It answers how many
// messages were copied. A copy cut short is resumed (chat.Room.Copy).
func copyChannel(odb *oltp.DB, room *chat.Room, code, name string, ceiling uint64) (int, error) {
key := chat.RepoKey(name)
seqs, err := room.Copy(code, key, name, ceiling)
if err != nil {
return 0, err
}
if err := odb.RekeyChatSeqs(name, key, seqs); err != nil {
return 0, err
}
return len(seqs), nil
}