From bd81a24f5dca24370dea64bfcbf93c80781a6b95 Mon Sep 17 00:00:00 2001 From: Alan Cleary Date: Thu, 14 Jul 2022 13:45:57 -0600 Subject: [PATCH] Updated the pairwise_macro_synteny_blocks service to use the redis package in place of the aioredis and redisearch packages. --- .../aioredisearch.py | 58 ------------------- .../pairwise_macro_synteny_blocks/database.py | 4 +- .../request_handler.py | 6 +- pairwise_macro_synteny_blocks/setup.cfg | 5 +- 4 files changed, 6 insertions(+), 67 deletions(-) delete mode 100644 pairwise_macro_synteny_blocks/pairwise_macro_synteny_blocks/aioredisearch.py diff --git a/pairwise_macro_synteny_blocks/pairwise_macro_synteny_blocks/aioredisearch.py b/pairwise_macro_synteny_blocks/pairwise_macro_synteny_blocks/aioredisearch.py deleted file mode 100644 index 10abb20d..00000000 --- a/pairwise_macro_synteny_blocks/pairwise_macro_synteny_blocks/aioredisearch.py +++ /dev/null @@ -1,58 +0,0 @@ -# Python -import time -# dependencies -import redisearch -import six - - -# copy of RediSearch's to_string function that's not exported -def to_string(s): - if isinstance(s, six.string_types): - return s - elif isinstance(s, six.binary_type): - return s.decode('utf-8') - else: - return s # Not a string we care about - - -# a class that overrides a subset of the RediSearch Client methods to be -# asynchronous -class Client(redisearch.Client): - - async def load_document(self, id): - fields = await self.redis.hgetall(id) - if six.PY3: - f2 = {to_string(k): to_string(v) for k, v in fields.items()} - fields = f2 - try: - del fields['id'] - except KeyError: - pass - return redisearch.Document(id=id, **fields) - - # unlike RediSearch get, this implementation returns a Document instance for - # each id that exists in the database and None for those that don't - async def get(self, *ids): - flat_fields = await self.redis.execute_command('FT.MGET', self.index_name, *ids) - docs = [] - for id, id_flat_fields in zip(ids, flat_fields): - if id_flat_fields is None: - docs.append(None) - else: - id_fields = dict( - dict(zip(map(to_string, id_flat_fields[::2]), - map(to_string, id_flat_fields[1::2]))) - ) - doc = redisearch.Document(id, payload=None, **id_fields) - docs.append(doc) - return docs - - async def search(self, query): - args, query = self._mk_query_args(query) - st = time.time() - res = await self.redis.execute_command(self.SEARCH_CMD, *args) - return redisearch.Result(res, - not query._no_content, - duration=(time.time() - st) * 1000.0, - has_payload=query._with_payloads, - with_scores=query._with_scores) diff --git a/pairwise_macro_synteny_blocks/pairwise_macro_synteny_blocks/database.py b/pairwise_macro_synteny_blocks/pairwise_macro_synteny_blocks/database.py index 112b76cc..c2bd2fb7 100644 --- a/pairwise_macro_synteny_blocks/pairwise_macro_synteny_blocks/database.py +++ b/pairwise_macro_synteny_blocks/pairwise_macro_synteny_blocks/database.py @@ -1,10 +1,10 @@ # dependencies -import aioredis +import redis.asyncio as redis async def connectToRedis(host='localhost', port=6379, db=0, password=None): # connect to database - connection = await aioredis.Redis(host=host, port=port, db=db, password=password, decode_responses=True) + connection = await redis.Redis(host=host, port=port, db=db, password=password, decode_responses=True) # ping to force connection, preventing errors downstream await connection.ping() return connection diff --git a/pairwise_macro_synteny_blocks/pairwise_macro_synteny_blocks/request_handler.py b/pairwise_macro_synteny_blocks/pairwise_macro_synteny_blocks/request_handler.py index 0e9375bc..f9ab1e64 100644 --- a/pairwise_macro_synteny_blocks/pairwise_macro_synteny_blocks/request_handler.py +++ b/pairwise_macro_synteny_blocks/pairwise_macro_synteny_blocks/request_handler.py @@ -1,8 +1,9 @@ # Python from collections import defaultdict from itertools import chain +# dependencies +from redis.commands.search import AsyncSearch # module -from pairwise_macro_synteny_blocks.aioredisearch import Client from pairwise_macro_synteny_blocks.metrics import METRICS @@ -146,8 +147,7 @@ def _indexPairsToIndexBlocks(self, pairs, intermediate, matched): async def process(self, query_chromosome, target, matched, intermediate, mask, metrics, chromosome_genes, chromosome_length): # connect to the indexes - chromosome_index = Client('chromosomeIdx', conn=self.redis_connection) - gene_index = Client('geneIdx', conn=self.redis_connection) + chromosome_index = AsyncSearch(self.redis_connection, index_name='chromosomeIdx') # check if the target chromosome exists target_doc_id = f'chromosome:{target}' diff --git a/pairwise_macro_synteny_blocks/setup.cfg b/pairwise_macro_synteny_blocks/setup.cfg index 3d5eb26e..3a55c03d 100644 --- a/pairwise_macro_synteny_blocks/setup.cfg +++ b/pairwise_macro_synteny_blocks/setup.cfg @@ -17,8 +17,6 @@ classifiers = Operating System :: OS Independent Programming Language :: Python :: 3 Programming Language :: Python :: 3 :: Only - Programming Language :: Python :: 3.5 - Programming Language :: Python :: 3.6 Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.8 Programming Language :: Python :: 3.9 @@ -31,12 +29,11 @@ project_urls = packages = find: python_requires = >=3.5,<4 install_requires = - aioredis >=2.0,<3 aiohttp >=3.7,<4 aiohttp-cors grpcio >=1.39,<2 grpcio-tools >=1.39,<2 - redisearch >=2,<3 + redis >=4.3.4,<5 uvloop >=0.16,<1 [options.entry_points]