From 7f097e78a2ab7d88dda5d03b86477f6c23be78cf Mon Sep 17 00:00:00 2001 From: Alan Cleary Date: Thu, 14 Jul 2022 13:19:32 -0600 Subject: [PATCH] Updated the chromosome service to use the redis package in place of the aioredis and redisearch packages. --- chromosome/chromosome/aioredisearch.py | 58 ------------------------ chromosome/chromosome/database.py | 4 +- chromosome/chromosome/request_handler.py | 4 +- chromosome/setup.cfg | 5 +- genes/setup.cfg | 2 +- 5 files changed, 6 insertions(+), 67 deletions(-) delete mode 100644 chromosome/chromosome/aioredisearch.py diff --git a/chromosome/chromosome/aioredisearch.py b/chromosome/chromosome/aioredisearch.py deleted file mode 100644 index 10abb20d..00000000 --- a/chromosome/chromosome/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/chromosome/chromosome/database.py b/chromosome/chromosome/database.py index 112b76cc..c2bd2fb7 100644 --- a/chromosome/chromosome/database.py +++ b/chromosome/chromosome/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/chromosome/chromosome/request_handler.py b/chromosome/chromosome/request_handler.py index a19abd18..29fc8ea4 100644 --- a/chromosome/chromosome/request_handler.py +++ b/chromosome/chromosome/request_handler.py @@ -1,5 +1,5 @@ # module -from chromosome.aioredisearch import Client +from redis.commands.search import AsyncSearch class RequestHandler: @@ -9,7 +9,7 @@ def __init__(self, redis_connection): async def process(self, name): # connect to the index - chromosome_index = Client('chromosomeIdx', conn=self.redis_connection) + chromosome_index = AsyncSearch(self.redis_connection, index_name='chromosomeIdx') # get the chromosome chromosome_doc = await chromosome_index.load_document(f'chromosome:{name}') if not hasattr(chromosome_doc, 'name'): diff --git a/chromosome/setup.cfg b/chromosome/setup.cfg index 067b0590..252c2cd1 100644 --- a/chromosome/setup.cfg +++ b/chromosome/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] diff --git a/genes/setup.cfg b/genes/setup.cfg index 83fedc95..8f0fbcd7 100644 --- a/genes/setup.cfg +++ b/genes/setup.cfg @@ -29,11 +29,11 @@ project_urls = packages = find: python_requires = >=3.5,<4 install_requires = - redis >=4.3.4,<5 aiohttp >=3.7,<4 aiohttp-cors grpcio >=1.39,<2 grpcio-tools >=1.39,<2 + redis >=4.3.4,<5 uvloop >=0.16,<1 [options.entry_points]