From 64adf49706c2cd8d52e4da7ab18b37999f64c2b1 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Thu, 2 Nov 2023 16:55:50 +0530 Subject: [PATCH] refactor: use rpop count to pop in batches MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - fix: dedupe items in a batch - fix: reassign `query` cause it does not hold state _fkn ORMs man 🙄_ --- frappe/utils/global_search.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/frappe/utils/global_search.py b/frappe/utils/global_search.py index 9626d4aa15..08d93fb501 100644 --- a/frappe/utils/global_search.py +++ b/frappe/utils/global_search.py @@ -373,18 +373,23 @@ def sync_global_search(): :param flags: :return: """ - from itertools import islice + from collections import OrderedDict - def get_search_queue_items_generator(): - while frappe.cache.llen("global_search_queue") > 0: - item_json = frappe.cache.rpop("global_search_queue").decode("utf-8") - item_values = json.loads(item_json).values() - yield tuple(item_values) + while frappe.cache.llen("global_search_queue") > 0: + values_dict = OrderedDict() - search_queue_items = get_search_queue_items_generator() + for item in frappe.cache.rpop("global_search_queue", 10_000): + item_json = item.decode("utf-8") + item_dict = json.loads(item_json) + key = (item_dict["doctype"], item_dict["name"]) - while values := tuple(islice(search_queue_items, 10_000)): - sync_values(values) + if key in values_dict: + del values_dict[key] + + values_dict[key] = tuple(item_dict.values()) + + + sync_values(values_dict.values()) def sync_values(values): @@ -398,13 +403,13 @@ def sync_values(values): ) if frappe.db.db_type == "postgres": - query.on_conflict(GlobalSearch.doctype, GlobalSearch.name) + query = query.on_conflict(GlobalSearch.doctype, GlobalSearch.name) for field in conflict_fields: if frappe.db.db_type == "mariadb": - query.on_duplicate_key_update(GlobalSearch[field], Values(field)) + query = query.on_duplicate_key_update(GlobalSearch[field], Values(field)) elif frappe.db.db_type == "postgres": - query.do_update(GlobalSearch[field], Values(field)) + query = query.do_update(GlobalSearch[field], Values(field)) else: raise NotImplementedError