From 82c739daa66215607e251d4f353d917037c321e9 Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Mon, 6 May 2019 17:15:25 -0400 Subject: [PATCH] abstract _apply_constraints_for_array_attributes for wallet server --- lbrynet/wallet/server/db.py | 73 +++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/lbrynet/wallet/server/db.py b/lbrynet/wallet/server/db.py index 08cd9b2dc..40a305bcf 100644 --- a/lbrynet/wallet/server/db.py +++ b/lbrynet/wallet/server/db.py @@ -12,6 +12,49 @@ from lbrynet.schema.url import URL, normalize_name from lbrynet.wallet.transaction import Transaction, Output +ATTRIBUTE_ARRAY_MAX_LENGTH = 100 + + +def _apply_constraints_for_array_attributes(constraints, attr): + any_items = constraints.pop(f'any_{attr}s', [])[:ATTRIBUTE_ARRAY_MAX_LENGTH] + if any_items: + constraints.update({ + f'$any_{attr}{i}': item for i, item in enumerate(any_items) + }) + values = ', '.join( + f':$any_{attr}{i}' for i in range(len(any_items)) + ) + constraints[f'claim.txo_hash__in#_any_{attr}'] = f""" + SELECT DISTINCT txo_hash FROM {attr} WHERE {attr} IN ({values}) + """ + + all_items = constraints.pop(f'all_{attr}s', [])[:ATTRIBUTE_ARRAY_MAX_LENGTH] + if all_items: + constraints[f'$all_{attr}_count'] = len(all_items) + constraints.update({ + f'$all_{attr}{i}': item for i, item in enumerate(all_items) + }) + values = ', '.join( + f':$all_{attr}{i}' for i in range(len(all_items)) + ) + constraints[f'claim.txo_hash__in#_all_{attr}'] = f""" + SELECT txo_hash FROM {attr} WHERE {attr} IN ({values}) + GROUP BY txo_hash HAVING COUNT({attr}) = :$all_{attr}_count + """ + + not_items = constraints.pop(f'not_{attr}s', [])[:ATTRIBUTE_ARRAY_MAX_LENGTH] + if not_items: + constraints.update({ + f'$not_{attr}{i}': item for i, item in enumerate(not_items) + }) + values = ', '.join( + f':$not_{attr}{i}' for i in range(len(not_items)) + ) + constraints[f'claim.txo_hash__not_in#_not_{attr}'] = f""" + SELECT DISTINCT txo_hash FROM {attr} WHERE {attr} IN ({values}) + """ + + class SQLDB: TRENDING_BLOCKS = 300 # number of blocks over which to calculate trending @@ -359,33 +402,9 @@ class SQLDB: tx_hash + struct.pack('