diff options
| author | alex <[email protected]> | 2026-07-28 20:01:28 +0200 |
|---|---|---|
| committer | alex <[email protected]> | 2026-07-28 20:01:28 +0200 |
| commit | 95ccea8d0244e515947c335ace620838f01be60a (patch) | |
| tree | 1bce68640f72ba4c010f2abecc89d3e61460ab29 /core/index | |
| parent | c91c3afd8700138f8eb8a25a4ae422c2c9e46cea (diff) | |
| download | search-engine-95ccea8d0244e515947c335ace620838f01be60a.tar.xz search-engine-95ccea8d0244e515947c335ace620838f01be60a.zip | |
forgor :skull:
Diffstat (limited to 'core/index')
| -rw-r--r-- | core/index/index.go | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/core/index/index.go b/core/index/index.go index 59b315c..2e669fc 100644 --- a/core/index/index.go +++ b/core/index/index.go @@ -1,20 +1,20 @@ package index import ( + "encoding/gob" + "os" "searchEngine/core/analyzer" "sync" ) type Posting struct { DocId int - BodyCount int - TitleCount int BodyPositions []int TitlePositions []int } type InvertedIndex struct { - Mu sync.RWMutex + Mu sync.RWMutex `gob:"-"` Data map[string][]Posting DocNames map[int]string } @@ -30,23 +30,19 @@ func (idx *InvertedIndex) Add(docID int, title string, bodyTokens []string) { titleTokens := analyzer.ProcessText(title) type fieldStats struct { - body int bodyPositions []int - title int titlePositions []int } docStats := make(map[string]fieldStats) for position, token := range titleTokens { stats := docStats[token] - stats.title++ stats.titlePositions = append(stats.titlePositions, position) docStats[token] = stats } for position, token := range bodyTokens { stats := docStats[token] - stats.body++ stats.bodyPositions = append(stats.bodyPositions, position) docStats[token] = stats } @@ -59,10 +55,38 @@ func (idx *InvertedIndex) Add(docID int, title string, bodyTokens []string) { for token, stats := range docStats { idx.Data[token] = append(idx.Data[token], Posting{ DocId: docID, - BodyCount: stats.body, - TitleCount: stats.title, BodyPositions: stats.bodyPositions, TitlePositions: stats.titlePositions, }) } } + +func (idx *InvertedIndex) Save(filepath string) error { + idx.Mu.RLock() + defer idx.Mu.RUnlock() + + file, err := os.Create(filepath) + if err != nil { + return err + } + defer file.Close() + + encoder := gob.NewEncoder(file) + return encoder.Encode(idx) +} + +func Load(filepath string) (*InvertedIndex, error) { + file, err := os.Open(filepath) + if err != nil { + return nil, err + } + defer file.Close() + + idx := &InvertedIndex{} + decoder := gob.NewDecoder(file) + err = decoder.Decode(idx) + if err != nil { + return nil, err + } + return idx, nil +} |
