Esempio n. 1
0
    def _check_cache_results(self, indexpath, cachepath, cacheid, expected_results, num_results=10):
        # set cache manager
        cm = XapianMultipleCachesManager(cachepath)
        cm.add_cache(cacheid)
        cm.select_cache(cacheid)

        search_conn = SearchConnection(indexpath)
        search_conn.set_cache_manager(cm)

        query_id, query_term = (0, "term_a")
        cache_query_id = cm.get_queryid(query_term)  # obtain query_id from the cache
        self.assertEqual(query_id, cache_query_id)

        non_cached, cached = expected_results

        query = search_conn.query_field("field", query_term)
        base_result = [r.id for r in query.search(0, num_results)]
        # see if the results without merging the query are ok
        self.assertEqual(non_cached, base_result)

        cached_query = query.merge_with_cached(query_id)
        cached_result = [r.id for r in cached_query.search(0, num_results)]
        # test the merged query result
        self.assertEqual(cached, cached_result)

        search_conn.close()
        cm.close()
Esempio n. 2
0
    def test_multiple_cache(self):
        with tempdir() as basepath:
            # create an index
            indexpath = os.path.join(basepath, "test_index")
            self._create_index(indexpath)

            base_cachepath = os.path.join(basepath, "cache")
            os.makedirs(base_cachepath)

            # create and apply cache 1
            cache_manager = XapianMultipleCachesManager(base_cachepath)
            cache_manager.add_cache("1")
            cache_manager.select_cache("1")
            self._create_and_apply_cache(indexpath, cache_manager)

            # create and apply cache 2
            cache_manager.add_cache("cache2")
            cache_manager.select_cache("cache2")
            self._create_and_apply_cache(indexpath, cache_manager)

            # test cache 1
            self._check_cache_results(
                indexpath, base_cachepath, "1", [["5", "4", "3", "2", "1"], ["4", "2", "5", "3", "1"]]
            )
            # test cache 2
            self._check_cache_results(
                indexpath, base_cachepath, "cache2", [["5", "4", "3", "2", "1"], ["3", "4", "1", "5", "2"]]
            )

            # the document whose docid is 4 is in both caches, we're
            # testing here if replacing it with one cache manager set
            # will change the result in the other cache. It must change.

            # replace document
            iconn = IndexerConnection(indexpath)
            cache_manager = XapianMultipleCachesManager(base_cachepath)
            cache_manager.add_cache("1")
            cache_manager.add_cache("cache2")
            iconn.set_cache_manager(cache_manager)
            docid, terms = ("4", [("term_a", 4), ("term_b", 2)])
            pdoc = self._create_processed_doc(iconn, docid, terms)
            iconn.replace(pdoc, xapid=int(docid))
            iconn.flush()
            iconn.close()
            cache_manager.close()

            # check if the results in both caches are ok
            self._check_cache_results(
                indexpath, base_cachepath, "1", [["5", "4", "3", "2", "1"], ["4", "2", "5", "3", "1"]]
            )
            self._check_cache_results(
                indexpath, base_cachepath, "cache2", [["5", "4", "3", "2", "1"], ["3", "4", "1", "5", "2"]]
            )

            # there are 2 code pathes when we deal with caches:
            # 1. the cache has not enough results
            # 2. the cache has enough results
            # in the first case, the result will come from a mixed query
            # against the index. In the second, the results will come from
            # the cache_manger. So, all the cache managers must be updated.

            # remove document
            cache_manager = XapianMultipleCachesManager(base_cachepath)
            cache_manager.add_cache("1")
            cache_manager.add_cache("cache2")

            iconn = IndexerConnection(indexpath)
            iconn.set_cache_manager(cache_manager)
            iconn.delete(xapid=4)
            cache_manager.close()
            iconn.flush()
            iconn.close()

            # cache has not enough results
            self._check_cache_results(indexpath, base_cachepath, "1", [["5", "3", "2", "1"], ["2", "5", "3", "1"]])
            self._check_cache_results(indexpath, base_cachepath, "cache2", [["5", "3", "2", "1"], ["3", "1", "5", "2"]])

            # cache has enough results
            self._check_cache_results(indexpath, base_cachepath, "1", [["5"], ["2"]], num_results=1)
            self._check_cache_results(indexpath, base_cachepath, "cache2", [["5", "3"], ["3", "1"]], num_results=2)