Compare commits
No commits in common. "78e2568e2b9aee0cb1d5d66574212cf6f7789399" and "dd7f0c0a2884a2e67d4dc4fa576bcc84b0090727" have entirely different histories.
78e2568e2b
...
dd7f0c0a28
10
CHANGELOG.md
10
CHANGELOG.md
@ -2,16 +2,6 @@
|
|||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
## [unreleased]
|
|
||||||
|
|
||||||
### 🚀 Features
|
|
||||||
|
|
||||||
- *(index)* Add save and load functionality for Index using gob encoding/decoding
|
|
||||||
|
|
||||||
### 📚 Documentation
|
|
||||||
|
|
||||||
- Add first release in Changelog
|
|
||||||
|
|
||||||
## [0.1.0] - 2025-03-25
|
## [0.1.0] - 2025-03-25
|
||||||
|
|
||||||
### 🚀 Features
|
### 🚀 Features
|
||||||
|
2
main.go
2
main.go
@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Initialize Index
|
// Initialize Index
|
||||||
index.InitIndex("default")
|
index.CreateIndex()
|
||||||
|
|
||||||
// Start HTTP Server
|
// Start HTTP Server
|
||||||
http.StartService()
|
http.StartService()
|
||||||
|
@ -2,12 +2,6 @@ package index
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
|
||||||
"encoding/gob"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -19,40 +13,7 @@ var Index InvertedIndex
|
|||||||
var DocFreq DocumentFrequency
|
var DocFreq DocumentFrequency
|
||||||
var Documents []string
|
var Documents []string
|
||||||
|
|
||||||
type SavedIndex struct {
|
func CreateIndex() {
|
||||||
Index InvertedIndex
|
|
||||||
DocFreq DocumentFrequency
|
|
||||||
Documents []string
|
|
||||||
}
|
|
||||||
|
|
||||||
func InitIndex(name string) {
|
|
||||||
createIndex()
|
|
||||||
|
|
||||||
// check if index as file exists
|
|
||||||
// if not, create one and save it
|
|
||||||
indexFilePath := "/tmp/" + name + ".db"
|
|
||||||
if _, err := os.Stat(indexFilePath); errors.Is(err, os.ErrNotExist) {
|
|
||||||
f, err := os.Create(indexFilePath)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
} else {
|
|
||||||
var savedIndex SavedIndex
|
|
||||||
|
|
||||||
err = readStructFromFile(indexFilePath, &savedIndex)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Fehler beim Lesen:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
Index = savedIndex.Index
|
|
||||||
DocFreq = savedIndex.DocFreq
|
|
||||||
Documents = savedIndex.Documents
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func createIndex() {
|
|
||||||
index := make(InvertedIndex)
|
index := make(InvertedIndex)
|
||||||
docFreq := make(DocumentFrequency)
|
docFreq := make(DocumentFrequency)
|
||||||
var docs []string
|
var docs []string
|
||||||
@ -62,21 +23,6 @@ func createIndex() {
|
|||||||
DocFreq = docFreq
|
DocFreq = docFreq
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateIndex(name string) {
|
|
||||||
savedIndex := SavedIndex{
|
|
||||||
Index: Index,
|
|
||||||
DocFreq: DocFreq,
|
|
||||||
Documents: Documents,
|
|
||||||
}
|
|
||||||
|
|
||||||
indexFilePath := "/tmp/" + name + ".db"
|
|
||||||
err := writeStructToFile(indexFilePath, savedIndex)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Fehler beim Schreiben:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func AddDocToIndex(url string, content string) {
|
func AddDocToIndex(url string, content string) {
|
||||||
Documents = append(Documents, url)
|
Documents = append(Documents, url)
|
||||||
|
|
||||||
@ -100,35 +46,4 @@ func AddDocToIndex(url string, content string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
updateIndex("default")
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeStructToFile(filename string, data interface{}) error {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
enc := gob.NewEncoder(&buf)
|
|
||||||
err := enc.Encode(data)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error when encoding the structs: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = os.WriteFile(filename, buf.Bytes(), 0644)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error when writing to the file: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func readStructFromFile(filename string, data interface{}) error {
|
|
||||||
content, err := os.ReadFile(filename)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error reading the file: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
buf := bytes.NewBuffer(content)
|
|
||||||
dec := gob.NewDecoder(buf)
|
|
||||||
err = dec.Decode(data)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error when decoding the structs: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user