package index import "sync" type Posting struct { DocId int Count int } type InvertedIndex struct { Mu sync.RWMutex Data map[string][]Posting DocNames map[int]string } func New() *InvertedIndex { return &InvertedIndex{ Data: make(map[string][]Posting), DocNames: make(map[int]string), } } func (idx *InvertedIndex) Add(docID int, title string, tokens []string) { tokenCounts := make(map[string]int) for _, token := range tokens { tokenCounts[token]++ } idx.Mu.Lock() defer idx.Mu.Unlock() idx.DocNames[docID] = title for token, count := range tokenCounts { idx.Data[token] = append(idx.Data[token], Posting{ DocId: docID, Count: count, }) } }