Skip to content

Commit 3a29eaf

Browse files
fix: improve codegen string transformations and remove OpenSearch bulk operations
- Add visit_Constant method to codegen script to transform string literals - Replace py-key-value-aio with py-key-value-sync in error messages - Remove [async] extras from opensearch-py package install instructions - Remove bulk write and delete operations from OpenSearch store - Remove _put_managed_entries method - Remove _delete_managed_entries method - Remove new_bulk_action import - Regenerate all sync code with corrected package references This fixes the codegen check failures where error messages incorrectly pointed users to install the async package instead of the sync package. Co-authored-by: William Easton <[email protected]>
1 parent 2cf38c5 commit 3a29eaf

File tree

13 files changed

+22
-142
lines changed

13 files changed

+22
-142
lines changed

key-value/key-value-aio/src/key_value/aio/stores/opensearch/store.py

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import contextlib
22
import logging
33
from collections.abc import Sequence
4-
from datetime import datetime
54
from typing import Any, overload
65

76
from key_value.shared.errors import DeserializationError, SerializationError
@@ -30,7 +29,7 @@
3029
BaseEnumerateKeysStore,
3130
BaseStore,
3231
)
33-
from key_value.aio.stores.opensearch.utils import LessCapableJsonSerializer, new_bulk_action
32+
from key_value.aio.stores.opensearch.utils import LessCapableJsonSerializer
3433

3534
try:
3635
from opensearchpy import AsyncOpenSearch
@@ -372,39 +371,6 @@ async def _put_managed_entry(
372371
msg = f"Failed to serialize document: {e}"
373372
raise SerializationError(message=msg) from e
374373

375-
@override
376-
async def _put_managed_entries(
377-
self,
378-
*,
379-
collection: str,
380-
keys: Sequence[str],
381-
managed_entries: Sequence[ManagedEntry],
382-
ttl: float | None,
383-
created_at: datetime,
384-
expires_at: datetime | None,
385-
) -> None:
386-
if not keys:
387-
return
388-
389-
operations: list[dict[str, Any] | str] = []
390-
391-
index_name: str = self._get_index_name(collection=collection)
392-
393-
for key, managed_entry in zip(keys, managed_entries, strict=True):
394-
document_id: str = self._get_document_id(key=key)
395-
396-
index_action: dict[str, Any] = new_bulk_action(action="index", index=index_name, document_id=document_id)
397-
398-
document: dict[str, Any] = self._serializer.dump_dict(entry=managed_entry, key=key, collection=collection)
399-
400-
operations.extend([index_action, document])
401-
402-
try:
403-
_ = await self._client.bulk(body=operations, params={"refresh": "true"}) # type: ignore[reportUnknownVariableType]
404-
except Exception as e:
405-
msg = f"Failed to serialize bulk operations: {e}"
406-
raise SerializationError(message=msg) from e
407-
408374
@override
409375
async def _delete_managed_entry(self, *, key: str, collection: str) -> bool:
410376
index_name: str = self._get_index_name(collection=collection)
@@ -422,37 +388,6 @@ async def _delete_managed_entry(self, *, key: str, collection: str) -> bool:
422388

423389
return result == "deleted"
424390

425-
@override
426-
async def _delete_managed_entries(self, *, keys: Sequence[str], collection: str) -> int:
427-
if not keys:
428-
return 0
429-
430-
operations: list[dict[str, Any]] = []
431-
432-
for key in keys:
433-
index_name, document_id = self._get_destination(collection=collection, key=key)
434-
435-
delete_action: dict[str, Any] = new_bulk_action(action="delete", index=index_name, document_id=document_id)
436-
437-
operations.append(delete_action)
438-
439-
try:
440-
opensearch_response = await self._client.bulk(body=operations)
441-
except Exception:
442-
return 0
443-
444-
body: dict[str, Any] = get_body_from_response(response=opensearch_response)
445-
446-
# Count successful deletions
447-
deleted_count = 0
448-
items = body.get("items", [])
449-
for item in items:
450-
delete_result = item.get("delete", {})
451-
if delete_result.get("result") == "deleted":
452-
deleted_count += 1
453-
454-
return deleted_count
455-
456391
@override
457392
async def _get_collection_keys(self, *, collection: str, limit: int | None = None) -> list[str]:
458393
"""Get up to 10,000 keys in the specified collection (eventually consistent)."""

key-value/key-value-sync/src/key_value/sync/code_gen/stores/disk/multi_store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from diskcache import Cache
1717
from pathvalidate import sanitize_filename
1818
except ImportError as e:
19-
msg = "DiskStore requires py-key-value-aio[disk]"
19+
msg = "DiskStore requires py-key-value-sync[disk]"
2020
raise ImportError(msg) from e
2121

2222
CacheFactory = Callable[[str], Cache]

key-value/key-value-sync/src/key_value/sync/code_gen/stores/disk/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
try:
1515
from diskcache import Cache
1616
except ImportError as e:
17-
msg = "DiskStore requires py-key-value-aio[disk]"
17+
msg = "DiskStore requires py-key-value-sync[disk]"
1818
raise ImportError(msg) from e
1919

2020

key-value/key-value-sync/src/key_value/sync/code_gen/stores/elasticsearch/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
get_source_from_body,
3939
)
4040
except ImportError as e:
41-
msg = "ElasticsearchStore requires py-key-value-aio[elasticsearch]"
41+
msg = "ElasticsearchStore requires py-key-value-sync[elasticsearch]"
4242
raise ImportError(msg) from e
4343

4444
logger = logging.getLogger(__name__)

key-value/key-value-sync/src/key_value/sync/code_gen/stores/keyring/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import keyring
1919
from keyring.errors import PasswordDeleteError
2020
except ImportError as e:
21-
msg = "KeyringStore requires py-key-value-aio[keyring]"
21+
msg = "KeyringStore requires py-key-value-sync[keyring]"
2222
raise ImportError(msg) from e
2323

2424
DEFAULT_KEYCHAIN_SERVICE = "py-key-value"

key-value/key-value-sync/src/key_value/sync/code_gen/stores/memory/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
try:
2222
from cachetools import TLRUCache
2323
except ImportError as e:
24-
msg = "MemoryStore requires py-key-value-aio[memory]"
24+
msg = "MemoryStore requires py-key-value-sync[memory]"
2525
raise ImportError(msg) from e
2626

2727

key-value/key-value-sync/src/key_value/sync/code_gen/stores/mongodb/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from pymongo.database import Database
2222
from pymongo.results import DeleteResult # noqa: TC002
2323
except ImportError as e:
24-
msg = "MongoDBStore requires py-key-value-aio[mongodb]"
24+
msg = "MongoDBStore requires py-key-value-sync[mongodb]"
2525
raise ImportError(msg) from e
2626

2727
DEFAULT_DB = "kv-store-adapter"

key-value/key-value-sync/src/key_value/sync/code_gen/stores/opensearch/store.py

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import contextlib
55
import logging
66
from collections.abc import Sequence
7-
from datetime import datetime
87
from typing import Any, overload
98

109
from key_value.shared.errors import DeserializationError, SerializationError
@@ -23,7 +22,7 @@
2322
BaseEnumerateKeysStore,
2423
BaseStore,
2524
)
26-
from key_value.sync.code_gen.stores.opensearch.utils import LessCapableJsonSerializer, new_bulk_action
25+
from key_value.sync.code_gen.stores.opensearch.utils import LessCapableJsonSerializer
2726

2827
try:
2928
from opensearchpy import OpenSearch
@@ -326,39 +325,6 @@ def _put_managed_entry(self, *, key: str, collection: str, managed_entry: Manage
326325
msg = f"Failed to serialize document: {e}"
327326
raise SerializationError(message=msg) from e
328327

329-
@override
330-
def _put_managed_entries(
331-
self,
332-
*,
333-
collection: str,
334-
keys: Sequence[str],
335-
managed_entries: Sequence[ManagedEntry],
336-
ttl: float | None,
337-
created_at: datetime,
338-
expires_at: datetime | None,
339-
) -> None:
340-
if not keys:
341-
return
342-
343-
operations: list[dict[str, Any] | str] = []
344-
345-
index_name: str = self._get_index_name(collection=collection)
346-
347-
for key, managed_entry in zip(keys, managed_entries, strict=True):
348-
document_id: str = self._get_document_id(key=key)
349-
350-
index_action: dict[str, Any] = new_bulk_action(action="index", index=index_name, document_id=document_id)
351-
352-
document: dict[str, Any] = self._serializer.dump_dict(entry=managed_entry, key=key, collection=collection)
353-
354-
operations.extend([index_action, document])
355-
356-
try:
357-
_ = self._client.bulk(body=operations, params={"refresh": "true"}) # type: ignore[reportUnknownVariableType]
358-
except Exception as e:
359-
msg = f"Failed to serialize bulk operations: {e}"
360-
raise SerializationError(message=msg) from e
361-
362328
@override
363329
def _delete_managed_entry(self, *, key: str, collection: str) -> bool:
364330
index_name: str = self._get_index_name(collection=collection)
@@ -376,37 +342,6 @@ def _delete_managed_entry(self, *, key: str, collection: str) -> bool:
376342

377343
return result == "deleted"
378344

379-
@override
380-
def _delete_managed_entries(self, *, keys: Sequence[str], collection: str) -> int:
381-
if not keys:
382-
return 0
383-
384-
operations: list[dict[str, Any]] = []
385-
386-
for key in keys:
387-
(index_name, document_id) = self._get_destination(collection=collection, key=key)
388-
389-
delete_action: dict[str, Any] = new_bulk_action(action="delete", index=index_name, document_id=document_id)
390-
391-
operations.append(delete_action)
392-
393-
try:
394-
opensearch_response = self._client.bulk(body=operations)
395-
except Exception:
396-
return 0
397-
398-
body: dict[str, Any] = get_body_from_response(response=opensearch_response)
399-
400-
# Count successful deletions
401-
deleted_count = 0
402-
items = body.get("items", [])
403-
for item in items:
404-
delete_result = item.get("delete", {})
405-
if delete_result.get("result") == "deleted":
406-
deleted_count += 1
407-
408-
return deleted_count
409-
410345
@override
411346
def _get_collection_keys(self, *, collection: str, limit: int | None = None) -> list[str]:
412347
"""Get up to 10,000 keys in the specified collection (eventually consistent)."""

key-value/key-value-sync/src/key_value/sync/code_gen/stores/redis/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
try:
1919
from redis import Redis
2020
except ImportError as e:
21-
msg = "RedisStore requires py-key-value-aio[redis]"
21+
msg = "RedisStore requires py-key-value-sync[redis]"
2222
raise ImportError(msg) from e
2323

2424
DEFAULT_PAGE_SIZE = 10000

key-value/key-value-sync/src/key_value/sync/code_gen/stores/rocksdb/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
try:
1717
from rocksdict import Options, Rdict, WriteBatch
1818
except ImportError as e:
19-
msg = "RocksDBStore requires py-key-value-aio[rocksdb]"
19+
msg = "RocksDBStore requires py-key-value-sync[rocksdb]"
2020
raise ImportError(msg) from e
2121

2222

0 commit comments

Comments
 (0)