diff options
| author | alex <[email protected]> | 2026-07-28 17:37:06 +0200 |
|---|---|---|
| committer | alex <[email protected]> | 2026-07-28 17:37:06 +0200 |
| commit | c91c3afd8700138f8eb8a25a4ae422c2c9e46cea (patch) | |
| tree | fd9b5ea4f3b821581b2ceb4e7fa8afc1a84c0eeb /core/engine.go | |
| parent | 5858d34b8ab573e18a4f6ddc5ee962b24ea269dc (diff) | |
| download | search-engine-c91c3afd8700138f8eb8a25a4ae422c2c9e46cea.tar.xz search-engine-c91c3afd8700138f8eb8a25a4ae422c2c9e46cea.zip | |
refactored entire code base to be in the package core so i can later implement interfaces to interact with the engine
Diffstat (limited to 'core/engine.go')
| -rw-r--r-- | core/engine.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/core/engine.go b/core/engine.go new file mode 100644 index 0000000..4b6b150 --- /dev/null +++ b/core/engine.go @@ -0,0 +1,56 @@ +package core + +import ( + "fmt" + "searchEngine/core/analyzer" + "searchEngine/core/index" + "searchEngine/core/ingest" + "searchEngine/core/search" + "sync" +) + +type Engine struct { + idx *index.InvertedIndex +} + +func NewEngine() *Engine { + return &Engine{ + idx: index.New(), + } +} + +func (e *Engine) Index() *index.InvertedIndex { + return e.idx +} + +func (e *Engine) IndexDump(filePath string, numWorkers int) error { + if numWorkers <= 0 { + numWorkers = 4 + } + + jobQueue := make(chan ingest.Page, 100) + var wg sync.WaitGroup + + for i := 0; i < numWorkers; i++ { + wg.Go(func() { + for page := range jobQueue { + tokens := analyzer.ProcessText(page.Text) + e.idx.Add(page.Id, page.Title, tokens) + } + }) + } + + err := ingest.IngestWikiDump(filePath, jobQueue) + close(jobQueue) + wg.Wait() + + if err != nil { + return fmt.Errorf("failed to ingest wiki dump: %w", err) + } + + return nil +} + +func (e *Engine) Search(query string) []string { + return search.Search(e.idx, query) +} |
