|
| 1 | +import asyncio |
| 2 | +import copy |
| 3 | +import typing |
| 4 | +import uuid |
| 5 | +from typing import Any, Dict, List, Optional, Tuple |
| 6 | + |
| 7 | +from hazelcast.protocol.codec import ( |
| 8 | + vector_collection_set_codec, |
| 9 | + vector_collection_get_codec, |
| 10 | + vector_collection_search_near_vector_codec, |
| 11 | + vector_collection_delete_codec, |
| 12 | + vector_collection_put_codec, |
| 13 | + vector_collection_put_if_absent_codec, |
| 14 | + vector_collection_remove_codec, |
| 15 | + vector_collection_put_all_codec, |
| 16 | + vector_collection_clear_codec, |
| 17 | + vector_collection_optimize_codec, |
| 18 | + vector_collection_size_codec, |
| 19 | +) |
| 20 | +from hazelcast.internal.asyncio_proxy.base import Proxy |
| 21 | +from hazelcast.serialization.compact import SchemaNotReplicatedError |
| 22 | +from hazelcast.serialization.data import Data |
| 23 | +from hazelcast.types import KeyType, ValueType |
| 24 | +from hazelcast.util import check_not_none |
| 25 | +from hazelcast.vector import ( |
| 26 | + Document, |
| 27 | + SearchResult, |
| 28 | + Vector, |
| 29 | + VectorType, |
| 30 | + VectorSearchOptions, |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +class VectorCollection(Proxy, typing.Generic[KeyType, ValueType]): |
| 35 | + def __init__(self, service_name, name, context): |
| 36 | + super(VectorCollection, self).__init__(service_name, name, context) |
| 37 | + |
| 38 | + async def get(self, key: Any) -> Document | None: |
| 39 | + check_not_none(key, "key can't be None") |
| 40 | + return await self._get_internal(key) |
| 41 | + |
| 42 | + async def set(self, key: Any, document: Document) -> None: |
| 43 | + check_not_none(key, "key can't be None") |
| 44 | + check_not_none(document, "document can't be None") |
| 45 | + check_not_none(document.value, "document value can't be None") |
| 46 | + return await self._set_internal(key, document) |
| 47 | + |
| 48 | + async def put(self, key: Any, document: Document) -> Document | None: |
| 49 | + check_not_none(key, "key can't be None") |
| 50 | + check_not_none(document, "document can't be None") |
| 51 | + check_not_none(document.value, "document value can't be None") |
| 52 | + return await self._put_internal(key, document) |
| 53 | + |
| 54 | + async def put_all(self, map: Dict[Any, Document]) -> None: |
| 55 | + check_not_none(map, "map can't be None") |
| 56 | + if not map: |
| 57 | + return None |
| 58 | + partition_service = self._context.partition_service |
| 59 | + partition_map: Dict[int, List[Tuple[Data, Document]]] = {} |
| 60 | + for key, doc in map.items(): |
| 61 | + check_not_none(key, "key can't be None") |
| 62 | + check_not_none(doc, "value can't be None") |
| 63 | + doc = copy.copy(doc) |
| 64 | + try: |
| 65 | + entry = (self._to_data(key), doc) |
| 66 | + doc.value = self._to_data(doc.value) |
| 67 | + except SchemaNotReplicatedError as e: |
| 68 | + return await self._send_schema_and_retry(e, self.put_all, map) |
| 69 | + |
| 70 | + partition_id = partition_service.get_partition_id(entry[0]) |
| 71 | + partition_map.setdefault(partition_id, []).append(entry) |
| 72 | + |
| 73 | + async with asyncio.TaskGroup() as tg: # type: ignore[attr-defined] |
| 74 | + for partition_id, entry_list in partition_map.items(): |
| 75 | + request = vector_collection_put_all_codec.encode_request(self.name, entry_list) |
| 76 | + tg.create_task(self._ainvoke_on_partition(request, partition_id)) |
| 77 | + |
| 78 | + return None |
| 79 | + |
| 80 | + async def put_if_absent(self, key: Any, document: Document) -> Document | None: |
| 81 | + check_not_none(key, "key can't be None") |
| 82 | + check_not_none(document, "document can't be None") |
| 83 | + check_not_none(document.value, "document value can't be None") |
| 84 | + return await self._put_if_absent_internal(key, document) |
| 85 | + |
| 86 | + async def search_near_vector( |
| 87 | + self, |
| 88 | + vector: Vector, |
| 89 | + *, |
| 90 | + include_value: bool = False, |
| 91 | + include_vectors: bool = False, |
| 92 | + limit: int = 10, |
| 93 | + hints: Dict[str, str] = None |
| 94 | + ) -> List[SearchResult]: |
| 95 | + check_not_none(vector, "vector can't be None") |
| 96 | + if limit <= 0: |
| 97 | + raise AssertionError("limit must be positive") |
| 98 | + return await self._search_near_vector_internal( |
| 99 | + vector, |
| 100 | + include_value=include_value, |
| 101 | + include_vectors=include_vectors, |
| 102 | + limit=limit, |
| 103 | + hints=hints, |
| 104 | + ) |
| 105 | + |
| 106 | + async def remove(self, key: Any) -> Document | None: |
| 107 | + check_not_none(key, "key can't be None") |
| 108 | + return await self._remove_internal(key) |
| 109 | + |
| 110 | + async def delete(self, key: Any) -> None: |
| 111 | + check_not_none(key, "key can't be None") |
| 112 | + return await self._delete_internal(key) |
| 113 | + |
| 114 | + async def optimize(self, index_name: str = None) -> None: |
| 115 | + request = vector_collection_optimize_codec.encode_request( |
| 116 | + self.name, index_name, uuid.uuid4() |
| 117 | + ) |
| 118 | + return await self._invoke(request) |
| 119 | + |
| 120 | + async def clear(self) -> None: |
| 121 | + request = vector_collection_clear_codec.encode_request(self.name) |
| 122 | + return await self._invoke(request) |
| 123 | + |
| 124 | + async def size(self) -> int: |
| 125 | + request = vector_collection_size_codec.encode_request(self.name) |
| 126 | + return await self._invoke(request, vector_collection_size_codec.decode_response) |
| 127 | + |
| 128 | + def _set_internal(self, key: Any, document: Document) -> asyncio.Future[None]: |
| 129 | + try: |
| 130 | + key_data = self._to_data(key) |
| 131 | + value_data = self._to_data(document.value) |
| 132 | + except SchemaNotReplicatedError as e: |
| 133 | + return self._send_schema_and_retry(e, self.set, key, document) |
| 134 | + document = copy.copy(document) |
| 135 | + document.value = value_data |
| 136 | + request = vector_collection_set_codec.encode_request( |
| 137 | + self.name, |
| 138 | + key_data, |
| 139 | + document, |
| 140 | + ) |
| 141 | + return self._invoke_on_key(request, key_data) |
| 142 | + |
| 143 | + def _get_internal(self, key: Any) -> asyncio.Future[Any]: |
| 144 | + def handler(message): |
| 145 | + doc = vector_collection_get_codec.decode_response(message) |
| 146 | + return self._transform_document(doc) |
| 147 | + |
| 148 | + try: |
| 149 | + key_data = self._to_data(key) |
| 150 | + except SchemaNotReplicatedError as e: |
| 151 | + return self._send_schema_and_retry(e, self.get, key) |
| 152 | + request = vector_collection_get_codec.encode_request( |
| 153 | + self.name, |
| 154 | + key_data, |
| 155 | + ) |
| 156 | + return self._invoke_on_key(request, key_data, response_handler=handler) |
| 157 | + |
| 158 | + def _search_near_vector_internal( |
| 159 | + self, |
| 160 | + vector: Vector, |
| 161 | + *, |
| 162 | + include_value: bool = False, |
| 163 | + include_vectors: bool = False, |
| 164 | + limit: int = 10, |
| 165 | + hints: Dict[str, str] = None |
| 166 | + ) -> asyncio.Future[List[SearchResult]]: |
| 167 | + def handler(message): |
| 168 | + results: List[ |
| 169 | + SearchResult |
| 170 | + ] = vector_collection_search_near_vector_codec.decode_response(message) |
| 171 | + for result in results: |
| 172 | + if result.key is not None: |
| 173 | + result.key = self._to_object(result.key) |
| 174 | + if result.value is not None: |
| 175 | + result.value = self._to_object(result.value) |
| 176 | + if result.vectors: |
| 177 | + for vec in result.vectors: |
| 178 | + vec.type = VectorType(vec.type) |
| 179 | + return results |
| 180 | + |
| 181 | + options = VectorSearchOptions( |
| 182 | + include_value=include_value, |
| 183 | + include_vectors=include_vectors, |
| 184 | + limit=limit, |
| 185 | + hints=hints or {}, |
| 186 | + ) |
| 187 | + request = vector_collection_search_near_vector_codec.encode_request( |
| 188 | + self.name, |
| 189 | + [vector], |
| 190 | + options, |
| 191 | + ) |
| 192 | + return self._invoke(request, response_handler=handler) |
| 193 | + |
| 194 | + def _delete_internal(self, key: Any) -> asyncio.Future[None]: |
| 195 | + key_data = self._to_data(key) |
| 196 | + request = vector_collection_delete_codec.encode_request(self.name, key_data) |
| 197 | + return self._invoke_on_key(request, key_data) |
| 198 | + |
| 199 | + def _remove_internal(self, key: Any) -> asyncio.Future[Document | None]: |
| 200 | + def handler(message): |
| 201 | + doc = vector_collection_remove_codec.decode_response(message) |
| 202 | + return self._transform_document(doc) |
| 203 | + |
| 204 | + key_data = self._to_data(key) |
| 205 | + request = vector_collection_remove_codec.encode_request(self.name, key_data) |
| 206 | + return self._invoke_on_key(request, key_data, response_handler=handler) |
| 207 | + |
| 208 | + def _put_internal(self, key: Any, document: Document) -> asyncio.Future[Document | None]: |
| 209 | + def handler(message): |
| 210 | + doc = vector_collection_put_codec.decode_response(message) |
| 211 | + return self._transform_document(doc) |
| 212 | + |
| 213 | + try: |
| 214 | + key_data = self._to_data(key) |
| 215 | + value_data = self._to_data(document.value) |
| 216 | + except SchemaNotReplicatedError as e: |
| 217 | + return self._send_schema_and_retry(e, self.set, key, document) |
| 218 | + document = copy.copy(document) |
| 219 | + document.value = value_data |
| 220 | + request = vector_collection_put_codec.encode_request( |
| 221 | + self.name, |
| 222 | + key_data, |
| 223 | + document, |
| 224 | + ) |
| 225 | + return self._invoke_on_key(request, key_data, response_handler=handler) |
| 226 | + |
| 227 | + def _put_if_absent_internal( |
| 228 | + self, key: Any, document: Document |
| 229 | + ) -> asyncio.Future[Document | None]: |
| 230 | + def handler(message): |
| 231 | + doc = vector_collection_put_if_absent_codec.decode_response(message) |
| 232 | + return self._transform_document(doc) |
| 233 | + |
| 234 | + try: |
| 235 | + key_data = self._to_data(key) |
| 236 | + value_data = self._to_data(document.value) |
| 237 | + except SchemaNotReplicatedError as e: |
| 238 | + return self._send_schema_and_retry(e, self.set, key, document) |
| 239 | + document.value = value_data |
| 240 | + request = vector_collection_put_if_absent_codec.encode_request( |
| 241 | + self.name, |
| 242 | + key_data, |
| 243 | + document, |
| 244 | + ) |
| 245 | + return self._invoke_on_key(request, key_data, response_handler=handler) |
| 246 | + |
| 247 | + def _transform_document(self, doc: Optional[Document]) -> Optional[Document]: |
| 248 | + if doc is not None: |
| 249 | + if doc.value is not None: |
| 250 | + doc.value = self._to_object(doc.value) |
| 251 | + for vec in doc.vectors: |
| 252 | + vec.type = VectorType(vec.type) |
| 253 | + return doc |
| 254 | + |
| 255 | + |
| 256 | +async def create_vector_collection_proxy(service_name, name, context): |
| 257 | + return VectorCollection(service_name, name, context) |
0 commit comments