| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "slices" |
| 5 | "sort" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // The search index's postings (§6.2): one partition for team pages, and |
| 10 | // one per person for their private pages. A query reads the team |
| 11 | // partition and the viewer's own, and no other, so the time Bo's query |
| 12 | // takes cannot depend on what Ana's private notes contain. Each |
| 13 | // partition numbers its own documents densely, so a query's working |
| 14 | // arrays are sized by those two partitions alone. |
| 15 | |
| 16 | // partition is one scope's postings. |
| 17 | type partition struct { |
| 18 | pages []int64 // document number → page id, 0 when free |
| 19 | numbers map[int64]int32 // page id → document number |
| 20 | free []int32 |
| 21 | lengths []int32 |
| 22 | terms map[string]*postingList |
| 23 | // dictionary is the terms, sorted, for prefix expansion. It is |
| 24 | // rebuilt under the write lock after a change, never at query time. |
| 25 | dictionary []string |
| 26 | dirty bool |
| 27 | documents int |
| 28 | words int64 |
| 29 | } |
| 30 | |
| 31 | // postingList is one term's documents and their term frequencies. |
| 32 | type postingList struct { |
| 33 | documents []int32 |
| 34 | counts []uint16 |
| 35 | } |
| 36 | |
| 37 | func newPartition() *partition { |
| 38 | return &partition{numbers: map[int64]int32{}, |
| 39 | terms: map[string]*postingList{}} |
| 40 | } |
| 41 | |
| 42 | // add indexes a page's term counts. The caller holds the write lock. |
| 43 | func (part *partition) add(pageID int64, counts map[string]uint16, |
| 44 | length int) { |
| 45 | number := part.number(pageID) |
| 46 | part.lengths[number] = int32(length) |
| 47 | part.documents++ |
| 48 | part.words += int64(length) |
| 49 | for term, count := range counts { |
| 50 | list := part.terms[term] |
| 51 | if list == nil { |
| 52 | list = &postingList{} |
| 53 | part.terms[term] = list |
| 54 | part.dirty = true |
| 55 | } |
| 56 | list.documents = append(list.documents, number) |
| 57 | list.counts = append(list.counts, count) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func (part *partition) number(pageID int64) int32 { |
| 62 | if count := len(part.free); count > 0 { |
| 63 | number := part.free[count-1] |
| 64 | part.free = part.free[:count-1] |
| 65 | part.pages[number], part.numbers[pageID] = pageID, number |
| 66 | return number |
| 67 | } |
| 68 | number := int32(len(part.pages)) |
| 69 | part.pages = append(part.pages, pageID) |
| 70 | part.lengths = append(part.lengths, 0) |
| 71 | part.numbers[pageID] = number |
| 72 | return number |
| 73 | } |
| 74 | |
| 75 | // remove unindexes a page, given the terms it was indexed with. |
| 76 | func (part *partition) remove(pageID int64, counts map[string]uint16) { |
| 77 | number, ok := part.numbers[pageID] |
| 78 | if !ok { |
| 79 | return |
| 80 | } |
| 81 | for term := range counts { |
| 82 | list := part.terms[term] |
| 83 | if list == nil { |
| 84 | continue |
| 85 | } |
| 86 | if at := slices.Index(list.documents, number); at >= 0 { |
| 87 | last := len(list.documents) - 1 |
| 88 | list.documents[at] = list.documents[last] |
| 89 | list.counts[at] = list.counts[last] |
| 90 | list.documents = list.documents[:last] |
| 91 | list.counts = list.counts[:last] |
| 92 | } |
| 93 | if len(list.documents) == 0 { |
| 94 | delete(part.terms, term) |
| 95 | part.dirty = true |
| 96 | } |
| 97 | } |
| 98 | part.documents-- |
| 99 | part.words -= int64(part.lengths[number]) |
| 100 | part.pages[number], part.lengths[number] = 0, 0 |
| 101 | delete(part.numbers, pageID) |
| 102 | part.free = append(part.free, number) |
| 103 | } |
| 104 | |
| 105 | // settle rebuilds the sorted dictionary after changes. |
| 106 | func (part *partition) settle() { |
| 107 | if !part.dirty { |
| 108 | return |
| 109 | } |
| 110 | part.dictionary = part.dictionary[:0] |
| 111 | for term := range part.terms { |
| 112 | part.dictionary = append(part.dictionary, term) |
| 113 | } |
| 114 | sort.Strings(part.dictionary) |
| 115 | part.dirty = false |
| 116 | } |
| 117 | |
| 118 | // expand is the partition's terms that start with prefix, at most limit. |
| 119 | func (part *partition) expand(prefix string, limit int) []string { |
| 120 | start := sort.SearchStrings(part.dictionary, prefix) |
| 121 | var out []string |
| 122 | for at := start; at < len(part.dictionary) && len(out) < limit; at++ { |
| 123 | if !strings.HasPrefix(part.dictionary[at], prefix) { |
| 124 | break |
| 125 | } |
| 126 | out = append(out, part.dictionary[at]) |
| 127 | } |
| 128 | return out |
| 129 | } |
| 130 | |
| 131 | // frequency is how many of the partition's documents hold term. |
| 132 | func (part *partition) frequency(term string) int { |
| 133 | if list := part.terms[term]; list != nil { |
| 134 | return len(list.documents) |
| 135 | } |
| 136 | return 0 |
| 137 | } |
| 138 | |
| 139 | // termCounts is a text's words and how often each occurs, and its length. |
| 140 | func termCounts(text string) (map[string]uint16, int) { |
| 141 | counts := map[string]uint16{} |
| 142 | words := folds(text) |
| 143 | for _, term := range words { |
| 144 | if counts[term] < 65535 { |
| 145 | counts[term]++ |
| 146 | } |
| 147 | } |
| 148 | return counts, len(words) |
| 149 | } |
| 150 | |
| 151 | // containing is the pages holding every one of words. Linear in the |
| 152 | // postings read (review L5): each list marks the documents that held |
| 153 | // every list before it, so no list is searched for another's entries. |
| 154 | func (part *partition) containing(words []string) []int64 { |
| 155 | if len(words) == 0 || len(words) > 255 { |
| 156 | return nil |
| 157 | } |
| 158 | held := make([]uint8, len(part.pages)) |
| 159 | for place, term := range words { |
| 160 | list := part.terms[term] |
| 161 | if list == nil { |
| 162 | return nil |
| 163 | } |
| 164 | for _, number := range list.documents { |
| 165 | if held[number] == uint8(place) { |
| 166 | held[number] = uint8(place + 1) |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | var out []int64 |
| 171 | for number, count := range held { |
| 172 | if int(count) == len(words) { |
| 173 | out = append(out, part.pages[number]) |
| 174 | } |
| 175 | } |
| 176 | return out |
| 177 | } |
| 178 | |