An open source, non-profit web search engine
Add a test for copying pages
Daoud Clarke11d388bfc648
diff --git a/mwmbl/background.py b/mwmbl/background.pyindex d51be4a..86aa05a 100644--- a/mwmbl/background.py+++ b/mwmbl/background.py@@ -7,9 +7,12 @@ from logging import getLogger, basicConfigfrom pathlib import Pathfrom time import sleep+from mwmbl import settingsfrom mwmbl.indexer import index_batches, historicalfrom mwmbl.indexer.batch_cache import BatchCachefrom mwmbl.indexer.paths import BATCH_DIR_NAME, INDEX_NAME+from mwmbl.models import OldIndex+from mwmbl.tinysearchengine.copy_index import copy_pagesbasicConfig(stream=sys.stdout, level=logging.INFO)logger = getLogger(__name__)@@ -32,3 +35,19 @@ def run(data_path: str):except Exception:logger.exception("Error indexing batches")sleep(10)+++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, 10)+++def copy_indexes_continuously():+ new_index_path = Path(settings.DATA_PATH) / INDEX_NAME+ while True:+ try:+ copy_all_indexes(new_index_path)+ except Exception:+ logger.exception("Error copying pages")+ sleep(10)diff --git a/mwmbl/tinysearchengine/copy_index.py b/mwmbl/tinysearchengine/copy_index.pyindex 1317cab..7afcbbe 100644--- a/mwmbl/tinysearchengine/copy_index.py+++ b/mwmbl/tinysearchengine/copy_index.py@@ -3,23 +3,14 @@ Copy an old index into a new one"""from collections import defaultdictfrom logging import getLogger-from pathlib import Path-from time import sleep--from django.conf import settingsfrom mwmbl.indexer.index_batches import index_pages-from mwmbl.indexer.paths import INDEX_NAME-from mwmbl.models import OldIndexfrom mwmbl.tinysearchengine.indexer import TinyIndex, Document--NUM_PAGES_TO_COPY = 10-logger = getLogger(__name__)-def copy_pages(old_index_path: str, new_index_path: str, start_page: int):+def copy_pages(old_index_path: str, new_index_path: str, start_page: int, num_pages_to_copy):logger.info(f"Copying pages from {old_index_path} to {new_index_path} starting at page {start_page}")# Get all old indexes:@@ -27,8 +18,11 @@ def copy_pages(old_index_path: str, new_index_path: str, start_page: int):with TinyIndex(item_factory=Document, index_path=new_index_path) as new_index: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)+ for page_index in range(start_page, start_page + num_pages_to_copy):+ if page_index >= old_index.num_pages:+ break++ documents = old_index.get_page(page_index)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)@@ -36,19 +30,3 @@ def copy_pages(old_index_path: str, new_index_path: str, start_page: int):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)diff --git a/mwmbl/tinysearchengine/indexer.py b/mwmbl/tinysearchengine/indexer.pyindex 121b353..160ca0c 100644--- a/mwmbl/tinysearchengine/indexer.py+++ b/mwmbl/tinysearchengine/indexer.py@@ -185,7 +185,8 @@ class TinyIndex(Generic[T]):def retrieve(self, key: str) -> List[T]:index = self.get_key_page_index(key)logger.debug(f"Retrieving index {index}")- return self.get_page(index)+ page = self.get_page(index)+ return [item for item in page if item.term is None or item.term == key]def get_key_page_index(self, key) -> int:key_hash = mmh3.hash(key, signed=False)diff --git a/test/test_copy_index.py b/test/test_copy_index.pynew file mode 100644index 0000000..e7b5152--- /dev/null+++ b/test/test_copy_index.py@@ -0,0 +1,27 @@+from pathlib import Path+from tempfile import TemporaryDirectory++from mwmbl.indexer.paths import INDEX_NAME+from mwmbl.tinysearchengine.copy_index import copy_pages+from mwmbl.tinysearchengine.indexer import TinyIndex, Document, PAGE_SIZE++NUM_PAGES = 10000+++def test_copy_pages():+ search_term = "apple"+ old_index_path = Path(__file__).parent.parent / "devdata" / INDEX_NAME++ # Make a temporary directory for the new index+ with TemporaryDirectory() as new_index_dir:+ new_index_path = str(Path(new_index_dir) / INDEX_NAME)+ TinyIndex.create(Document, new_index_path, NUM_PAGES, PAGE_SIZE)+ with TinyIndex(item_factory=Document, index_path=old_index_path) as old_index:+ start_index = old_index.get_key_page_index(search_term)+ copy_pages(old_index_path, new_index_path, start_index, 1)+ with TinyIndex(item_factory=Document, index_path=new_index_path) as new_index:+ old_items = old_index.retrieve(search_term)+ new_items = new_index.retrieve(search_term)++ assert len(old_items) > 0+ assert old_items == new_items