| 196 | |
| 197 | for _, name := range names { |
| 198 | if name == newShard { |
| 199 | newShard = filepath.Join(filepath.Dir(s.IndexPath), name) |
| 200 | // We verified the given argument refers to an index shard within |
| 201 | // this directory, so let’s load this shard. |
| 202 | log.Printf("Trying to load %q\n", newShard) |
| 203 | if err := s.LoadIndex(newShard); err != nil { |
| 204 | return err |
| 205 | } |
| 206 | |
| 207 | if err := renameio.Symlink(newShard, s.IndexPath); err != nil { |
| 208 | return err |
| 209 | } |
| 210 | return nil |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return fmt.Errorf("No such shard.") |
| 215 | } |
| 216 | |
| 217 | // TODO: fix the indexing code path to ensure valid UTF8 |
| 218 | // (currently it seems to only reject *some* invalid UTF8) |
| 219 | func ensureValidUTF8(s string) string { |
| 220 | if utf8.ValidString(s) { |
| 221 | return s |
| 222 | } |
| 223 | return strings.ToValidUTF8(s, "\uFFFD") |
| 224 | } |
| 225 | |
| 226 | type fileMatches struct { |
| 227 | fn string |
| 228 | matches []index.Match |
| 229 | } |
| 230 | |
| 231 | func (s *Server) queryPositional(ix *index.Index, hidden map[string]bool, literal string) ([]fileMatches, error) { |
| 232 | matches, err := ix.QueryPositional(literal) |
| 233 | if err != nil { |
| 234 | return nil, fmt.Errorf("ix.QueryPositional(%q): %v", literal, err) |
| 235 | } |
| 236 | |
| 237 | // Split matches into smaller slices (= create new slice headers), |
| 238 | // which are kept in memory alongside the file name (fileMatches). |
| 239 | var possible []fileMatches |
| 240 | for len(matches) > 0 { |
| 241 | docid := matches[0].Docid |
| 242 | n := 1 |
| 243 | // Find all matches within this document (source file). |
| 244 | for n < len(matches) && matches[n].Docid == docid { |
| 245 | n++ |
| 246 | } |
| 247 | fn, err := ix.DocidMap.Lookup(docid) |
| 248 | if err != nil { |
| 249 | return nil, fmt.Errorf("DocidMap.Lookup(%v): %v", docid, err) |
| 250 | } |
| 251 | if pkg, _, _ := strings.Cut(fn, "/"); !hidden[pkg] { |
| 252 | possible = append(possible, fileMatches{ |
| 253 | fn: fn, |
| 254 | matches: matches[:n], |