summaryrefslogtreecommitdiff
path: root/index/index.go
blob: 74562133123823f09526efab7d51b4eae5739688 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package index

import "sync"

type InvertedIndex struct {
	Mu       sync.RWMutex
	Data     map[string][]int
	DocNames map[int]string
}

func New() *InvertedIndex {
	return &InvertedIndex{
		Data:     make(map[string][]int),
		DocNames: make(map[int]string),
	}
}

func (idx *InvertedIndex) Add(docID int, title string, tokens []string) {
	idx.Mu.Lock()
	defer idx.Mu.Unlock()

	idx.DocNames[docID] = title
	seen := make(map[string]struct{})

	for _, token := range tokens {
		if _, exists := seen[token]; !exists {
			idx.Data[token] = append(idx.Data[token], docID)

			seen[token] = struct{}{}
		}
	}
}