| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "sort" |
| 6 | "strings" |
| 7 | |
| 8 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 9 | ) |
| 10 | |
| 11 | // Search (§6, §6.1-§6.3): one box over titles, aliases and bodies. |
| 12 | // Titles and aliases are prefix-matched and rank first; bodies are |
| 13 | // matched by term and ranked BM25-style over team-only statistics. |
| 14 | // Every read is scoped before traversal, and the result says nothing |
| 15 | // about what the viewer cannot see. |
| 16 | |
| 17 | // Hit is one search result. |
| 18 | type Hit struct { |
| 19 | ID int64 `json:"-"` |
| 20 | Label string `json:"label"` |
| 21 | Href string `json:"href"` |
| 22 | Kind string `json:"kind"` |
| 23 | // Look is a team page's icon (columns and icons spec §2.4). |
| 24 | Look *Icon `json:"look,omitempty"` |
| 25 | Snippet *Snippet `json:"snippet,omitempty"` |
| 26 | // Via is the alias a title hit matched, when it was not the title: |
| 27 | // "Old title of <Label>". |
| 28 | Via string `json:"via,omitempty"` |
| 29 | } |
| 30 | |
| 31 | // Results is a search's answer in ⌘K's groups (level-up §4.6). Pages |
| 32 | // is team titles and aliases; Text is team pages found by their words; |
| 33 | // Notes is the viewer's own daily and scratch pages, and only theirs. |
| 34 | // Date is the typed-date row, and Create the "Create" offer: the query |
| 35 | // as a title, when no team page holds it and it could be one. |
| 36 | type Results struct { |
| 37 | Query string `json:"query"` |
| 38 | Date *DateHit `json:"date,omitempty"` |
| 39 | Pages []Hit `json:"pages"` |
| 40 | Text []Hit `json:"text"` |
| 41 | Notes []Hit `json:"notes"` |
| 42 | Create string `json:"create,omitempty"` |
| 43 | } |
| 44 | |
| 45 | // All is every hit, group by group. |
| 46 | func (results Results) All() []Hit { |
| 47 | all := append([]Hit{}, results.Pages...) |
| 48 | return append(append(all, results.Text...), results.Notes...) |
| 49 | } |
| 50 | |
| 51 | // Count is how many rows answer the query: the hits and the date row, |
| 52 | // not the Create offer. |
| 53 | func (results Results) Count() int { |
| 54 | count := len(results.Pages) + len(results.Text) + len(results.Notes) |
| 55 | if results.Date != nil { |
| 56 | count++ |
| 57 | } |
| 58 | return count |
| 59 | } |
| 60 | |
| 61 | // withDate adds the row for a typed date, and takes the viewer's note |
| 62 | // for that day out of Notes: one row per day. |
| 63 | func (results *Results) withDate(row *DateHit) { |
| 64 | if row == nil { |
| 65 | return |
| 66 | } |
| 67 | results.Date = row |
| 68 | notes := results.Notes[:0] |
| 69 | for _, hit := range results.Notes { |
| 70 | if hit.Href != row.Href { |
| 71 | notes = append(notes, hit) |
| 72 | } |
| 73 | } |
| 74 | results.Notes = notes |
| 75 | } |
| 76 | |
| 77 | // titleRow is one team title or alias. |
| 78 | type titleRow struct { |
| 79 | fold, text string |
| 80 | words []string |
| 81 | pageID int64 |
| 82 | alias bool |
| 83 | } |
| 84 | |
| 85 | // searchText is what a page is found by: its title and aliases, and its |
| 86 | // words as a reader sees them. |
| 87 | func searchText(page *entry) string { |
| 88 | var text strings.Builder |
| 89 | text.WriteString(page.record.Title) |
| 90 | for _, alias := range page.aliases { |
| 91 | text.WriteString("\n" + alias.Title) |
| 92 | } |
| 93 | for _, block := range page.blocks { |
| 94 | text.WriteString("\n" + markup.Words(block)) |
| 95 | } |
| 96 | return text.String() |
| 97 | } |
| 98 | |
| 99 | // rebuildTitles sorts every team title and alias. Team pages only: |
| 100 | // private pages hold no titles (decision 7). |
| 101 | func (index *Index) rebuildTitles() { |
| 102 | rows := index.titleRows[:0] |
| 103 | for pageID := range index.team { |
| 104 | page := index.pages[pageID] |
| 105 | rows = append(rows, titleRow{fold: Fold(page.record.Title), |
| 106 | text: page.record.Title, words: folds(page.record.Title), |
| 107 | pageID: pageID}) |
| 108 | for _, alias := range page.aliases { |
| 109 | rows = append(rows, titleRow{fold: Fold(alias.Title), |
| 110 | text: alias.Title, words: folds(alias.Title), pageID: pageID, |
| 111 | alias: true}) |
| 112 | } |
| 113 | } |
| 114 | sort.Slice(rows, func(left, right int) bool { |
| 115 | return lessText(rows[left].fold, rows[right].fold, |
| 116 | rows[left].pageID, rows[right].pageID) |
| 117 | }) |
| 118 | index.titleRows, index.titlesDirty = rows, false |
| 119 | } |
| 120 | |
| 121 | // Search answers query for scope, at most limit hits in each group. |
| 122 | func (index *Index) Search(ctx context.Context, scope Scope, query string, |
| 123 | limit int) (Results, error) { |
| 124 | if err := index.Sync(ctx); err != nil { |
| 125 | return Results{}, err |
| 126 | } |
| 127 | index.mutex.RLock() |
| 128 | defer index.mutex.RUnlock() |
| 129 | results := Results{Query: strings.TrimSpace(query), Pages: []Hit{}, |
| 130 | Text: []Hit{}, Notes: []Hit{}} |
| 131 | tokens := queryWords(query) |
| 132 | if len(tokens) == 0 { |
| 133 | return results, nil |
| 134 | } |
| 135 | results.Pages = index.titleHits(query, tokens, limit) |
| 136 | results.Notes = index.dayHits(scope, query, limit) |
| 137 | taken := map[int64]bool{} |
| 138 | for _, hit := range results.All() { |
| 139 | taken[hit.ID] = true |
| 140 | } |
| 141 | for _, part := range index.searchPartitions(scope) { |
| 142 | hits := index.bodyHits(part, tokens, limit, taken, |
| 143 | scope.Locale) |
| 144 | if part == index.teamTerms { |
| 145 | results.Text = append(results.Text, hits...) |
| 146 | } else { |
| 147 | results.Notes = append(results.Notes, hits...) |
| 148 | } |
| 149 | } |
| 150 | results.Notes = results.Notes[:min(limit, len(results.Notes))] |
| 151 | results.Create = index.createOffer(query) |
| 152 | return results, nil |
| 153 | } |
| 154 | |
| 155 | // maxQueryWords bounds a query (review L6): a search box is a few |
| 156 | // words, and the match counts one byte per word. |
| 157 | const maxQueryWords = 16 |
| 158 | |
| 159 | // queryWords is a query's words, at most maxQueryWords of them, from a |
| 160 | // query cut to a sane length first. |
| 161 | func queryWords(query string) []string { |
| 162 | if len(query) > 512 { |
| 163 | query = query[:512] |
| 164 | } |
| 165 | words := folds(query) |
| 166 | return words[:min(len(words), maxQueryWords)] |
| 167 | } |
| 168 | |
| 169 | // createOffer is §6's "Create X": offered when no team title or alias |
| 170 | // is the query and the query could be a title. It reads team titles |
| 171 | // only, so it cannot depend on anyone's private pages (gate 1). |
| 172 | func (index *Index) createOffer(query string) string { |
| 173 | title, err := CleanTitle(query) |
| 174 | if err != nil { |
| 175 | return "" |
| 176 | } |
| 177 | if _, held := index.titles[Fold(title)]; held { |
| 178 | return "" |
| 179 | } |
| 180 | return title |
| 181 | } |
| 182 | |
| 183 | // titleHits is the team titles and aliases every token starts a word |
| 184 | // of: the exact title first, then titles the query begins, then the |
| 185 | // rest, each page once. |
| 186 | func (index *Index) titleHits(query string, tokens []string, |
| 187 | limit int) []Hit { |
| 188 | whole := Fold(query) |
| 189 | best := map[int64]titleMatch{} |
| 190 | for _, row := range index.titleRows { |
| 191 | if !startsWords(row.words, tokens) { |
| 192 | continue |
| 193 | } |
| 194 | match := titleMatch{rank: 2, row: row} |
| 195 | switch { |
| 196 | case row.fold == whole: |
| 197 | match.rank = 0 |
| 198 | case strings.HasPrefix(row.fold, whole): |
| 199 | match.rank = 1 |
| 200 | } |
| 201 | if previous, seen := best[row.pageID]; !seen || |
| 202 | match.before(previous) { |
| 203 | best[row.pageID] = match |
| 204 | } |
| 205 | } |
| 206 | return index.rankedTitles(best, limit) |
| 207 | } |
| 208 | |
| 209 | // titleMatch is a page's best matching title or alias. |
| 210 | type titleMatch struct { |
| 211 | rank int |
| 212 | row titleRow |
| 213 | } |
| 214 | |
| 215 | func (match titleMatch) before(other titleMatch) bool { |
| 216 | if match.rank != other.rank { |
| 217 | return match.rank < other.rank |
| 218 | } |
| 219 | if match.row.alias != other.row.alias { |
| 220 | return !match.row.alias |
| 221 | } |
| 222 | return lessText(match.row.fold, other.row.fold, match.row.pageID, |
| 223 | other.row.pageID) |
| 224 | } |
| 225 | |
| 226 | func (index *Index) rankedTitles(best map[int64]titleMatch, |
| 227 | limit int) []Hit { |
| 228 | matches := make([]titleMatch, 0, len(best)) |
| 229 | for _, match := range best { |
| 230 | matches = append(matches, match) |
| 231 | } |
| 232 | hits := []Hit{} |
| 233 | for _, match := range topK(matches, limit, titleMatch.before) { |
| 234 | page := index.pages[match.row.pageID] |
| 235 | // Titles are team pages, so no locale is needed: no date. |
| 236 | hit := Hit{ID: page.id, Label: page.label(nil), Href: page.href(), |
| 237 | Kind: page.kind, Look: page.look()} |
| 238 | if match.row.alias { |
| 239 | hit.Via = match.row.text |
| 240 | } |
| 241 | hits = append(hits, hit) |
| 242 | } |
| 243 | return hits |
| 244 | } |
| 245 | |
| 246 | // startsWords reports whether every token starts some word. |
| 247 | func startsWords(words, tokens []string) bool { |
| 248 | for _, token := range tokens { |
| 249 | found := false |
| 250 | for _, word := range words { |
| 251 | if strings.HasPrefix(word, token) { |
| 252 | found = true |
| 253 | break |
| 254 | } |
| 255 | } |
| 256 | if !found { |
| 257 | return false |
| 258 | } |
| 259 | } |
| 260 | return true |
| 261 | } |
| 262 | |
| 263 | // dayHits is the viewer's own daily notes whose date starts with the |
| 264 | // query: their own partition, and nobody else's. |
| 265 | func (index *Index) dayHits(scope Scope, query string, limit int) []Hit { |
| 266 | query = strings.TrimSpace(query) |
| 267 | if scope.Member == 0 || limit <= 0 || query == "" || |
| 268 | query[0] < '0' || query[0] > '9' { |
| 269 | return []Hit{} |
| 270 | } |
| 271 | var dates []string |
| 272 | for date := range index.days[scope.Member] { |
| 273 | if strings.HasPrefix(date, query) { |
| 274 | dates = append(dates, date) |
| 275 | } |
| 276 | } |
| 277 | sort.Sort(sort.Reverse(sort.StringSlice(dates))) |
| 278 | hits := []Hit{} |
| 279 | for _, date := range dates[:min(limit, len(dates))] { |
| 280 | page := index.pages[index.days[scope.Member][date]] |
| 281 | hits = append(hits, Hit{ID: page.id, Label: page.label(scope.Locale), |
| 282 | Href: page.href(), Kind: page.kind}) |
| 283 | } |
| 284 | return hits |
| 285 | } |
| 286 | |