summaryrefslogtreecommitdiff
path: root/core/index/index.go
diff options
context:
space:
mode:
Diffstat (limited to 'core/index/index.go')
-rw-r--r--core/index/index.go42
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
+}