Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Refactor and get pages in the right format

Daoud Clarkebc27518596ba
diff --git a/mwmbl/indexer/index_batches.py b/mwmbl/indexer/index_batches.py
index 710af66..b3838ac 100644
--- a/mwmbl/indexer/index_batches.py
+++ b/mwmbl/indexer/index_batches.py
@@ -41,7 +41,7 @@ def index_batches(batch_data: Collection[HashedBatch], index_path: str):
index_pages(index_path, page_documents)
-def index_pages(index_path, page_documents):
+def index_pages(index_path: str, page_documents: dict[int, list[Document]]):
with TinyIndex(Document, index_path, 'w') as indexer:
for page, documents in page_documents.items():
new_documents = []
diff --git a/mwmbl/tinysearchengine/copy_index.py b/mwmbl/tinysearchengine/copy_index.py
index 33d251c..1317cab 100644
--- a/mwmbl/tinysearchengine/copy_index.py
+++ b/mwmbl/tinysearchengine/copy_index.py
@@ -1,10 +1,14 @@
"""
Copy an old index into a new one
"""
+from collections import defaultdict
+from logging import getLogger
from pathlib import Path
+from time import sleep
from django.conf import settings
+from mwmbl.indexer.index_batches import index_pages
from mwmbl.indexer.paths import INDEX_NAME
from mwmbl.models import OldIndex
from mwmbl.tinysearchengine.indexer import TinyIndex, Document
@@ -12,20 +16,39 @@ from mwmbl.tinysearchengine.indexer import TinyIndex, Document
NUM_PAGES_TO_COPY = 10
+logger = getLogger(__name__)
-def copy_pages():
- new_index_path = Path(settings.DATA_PATH) / INDEX_NAME
+def copy_pages(old_index_path: str, new_index_path: str, start_page: int):
+ logger.info(f"Copying pages from {old_index_path} to {new_index_path} starting at page {start_page}")
+
+ # Get all old indexes:
+ page_documents = defaultdict(list)
with TinyIndex(item_factory=Document, index_path=new_index_path) as new_index:
- # Get all old indexes:
- old_indexes = OldIndex.objects.all()
- for old_index_info in old_indexes:
- old_index_path = Path(old_index.index_path)
- with TinyIndex(item_factory=Document, index_path=old_index_path) as old_index:
- # Copy each page in the old index into the new one
- start_page = old_index_info.start_page
- for i in range(start_page, start_page + NUM_PAGES_TO_COPY):
-
- # TODO: merge the items in the new and old indexes
- page = old_index.get_page(i)
- new_index.add_page(page)
+ with TinyIndex(item_factory=Document, index_path=old_index_path) as old_index:
+ # Copy each page in the old index into the new one
+ for i in range(start_page, start_page + NUM_PAGES_TO_COPY):
+ documents = old_index.get_page(i)
+ assert all(document.term is not None for document in documents)
+ for document in documents:
+ new_page = new_index.get_key_page_index(document.term)
+ page_documents[new_page].append(document)
+
+ logger.info(f"Copying {len(page_documents)} pages to {new_index_path}")
+ index_pages(new_index_path, page_documents)
+
+
+def copy_all_indexes(new_index_path):
+ old_indexes = OldIndex.objects.all()
+ for old_index_info in old_indexes:
+ copy_pages(old_index_info.index_path, new_index_path, old_index_info.start_page)
+
+
+def run_continuously():
+ new_index_path = Path(settings.DATA_PATH) / INDEX_NAME
+ while True:
+ try:
+ copy_all_indexes(new_index_path)
+ except Exception as e:
+ logger.exception("Error copying pages")
+ sleep(10)