Skip to content

Commit

Permalink
tairvector add hincr hincbyfloat
Browse files Browse the repository at this point in the history
  • Loading branch information
hanwen.thw authored and yangbodong22011 committed Jul 26, 2023
1 parent 4c5fd34 commit 0e6de33
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
3 changes: 3 additions & 0 deletions tair/commands.py
Expand Up @@ -33,6 +33,7 @@
parse_tvs_hmget_result,
parse_tvs_msearch_result,
parse_tvs_search_result,
parse_tvs_hincrbyfloat_result,
)
from tair.tairzset import TairZsetCommands, parse_tair_zset_items

Expand Down Expand Up @@ -155,6 +156,8 @@ def bool_ok(resp) -> bool:
"TVS.MKNNSEARCH": parse_tvs_msearch_result,
"TVS.MINDEXKNNSEARCH": parse_tvs_search_result,
"TVS.MINDEXMKNNSEARCH": parse_tvs_msearch_result,
"TVS.HINCRBY": int_or_none,
"TVS.HINCRBYFLOAT": parse_tvs_hincrbyfloat_result,
}


Expand Down
20 changes: 20 additions & 0 deletions tair/tairvector.py
Expand Up @@ -579,7 +579,21 @@ def process_batch(batch):
)
queue = itertools.islice(queue, k)
return [(key, score) for score, key in queue]

HINCRBY_CMD = "TVS.HINCRBY"
HINCRBYFLOAT_CMD = "TVS.HINCRBYFLOAT"

def tvs_hincrby(self, index: str, key: str, field: str, num: int):
"""
increment the long value of a tairvector field by the given amount, not support field VECTOR
"""
return self.execute_command(self.HINCRBY_CMD, index, key, field, num)

def tvs_hincrbyfloat(self, index: str, key: str, field: str, num: float):
"""
increment the float value of a tairvector field by the given amount, not support field VECTOR
"""
return self.execute_command(self.HINCRBYFLOAT_CMD, index, key, field, num)

def parse_tvs_get_index_result(resp) -> Union[Dict, None]:
if len(resp) == 0:
Expand Down Expand Up @@ -610,3 +624,9 @@ def parse_tvs_search_result(resp) -> List[Tuple]:

def parse_tvs_msearch_result(resp) -> List[List[Tuple]]:
return [parse_tvs_search_result(r) for r in resp]


def parse_tvs_hincrbyfloat_result(resp) -> Union[float, None]:
if resp is None:
return resp
return float(resp.decode())
20 changes: 16 additions & 4 deletions tests/test_tairvector.py
Expand Up @@ -4,6 +4,7 @@
import sys
import unittest
import uuid
import pytest
from random import choice, randint, random

import redis
Expand Down Expand Up @@ -173,9 +174,10 @@ def test_3_hset(self):
self.assertTrue(vectorEqual(v, obj[Constants.VECTOR_KEY]))
del obj[Constants.VECTOR_KEY]
self.assertDictEqual(obj, test_attributes[i])


def test_hmget(self):
self.assertTrue(client.tvs_create_index("test", dim, **self.index_params))
# self.assertTrue(client.tvs_create_index("test", dim, **self.index_params))
vector = [randint(1, 100) for _ in range(dim)]
key = "key_" + str(uuid.uuid4())
value1 = "value_" + str(uuid.uuid4())
Expand Down Expand Up @@ -219,9 +221,19 @@ def test_7_delete(self):
# delete inserted entries
for i in range(len(test_vectors)):
self.assertEqual(client.tvs_del("test", str(i)), 1)

def test_9_delete(self):
self.assertEqual(client.tvs_del_index("test"), 1)

def test_8_hincry(self):
key_tmp = "key_tmp1"
client.tvs_hset("test", key_tmp, field1=1)
self.assertEqual(client.tvs_hincrby("test", key_tmp, "field1", 1), 2)

def test_9_hincrbyfloat(self):
key_tmp = "key_tmp1"
client.tvs_hset("test", key_tmp, field2=1.1)
assert client.tvs_hincrbyfloat("test", key_tmp, "field2", 2.2) == pytest.approx(3.3)

def test_10_delete(self):
client.tvs_del_index("test")


class SearchCommandsTest(unittest.TestCase):
Expand Down

0 comments on commit 0e6de33

Please sign in to comment.