Skip to content

Commit

Permalink
Adapt to latest choices in API semantics (#248)
Browse files Browse the repository at this point in the history
* wip on docstrings

* remove 'acknowledge' attribute from Result classes

* upper_bound + raw-result lists in bulk write results and in some Results

Introduce upper_bound parameter to count_documents + exceptions flow
raw_results (plural and always a list) in deleteResult+insertManyResults
bulk_api_results always a map to list for BulkWriteResult

* add SortDocuments as singleton and likewise make ReturnDocument into a singleton

* SortType, FilterType for uniformity
  • Loading branch information
hemidactylus authored Mar 8, 2024
1 parent 91261e7 commit 856c075
Show file tree
Hide file tree
Showing 7 changed files with 344 additions and 184 deletions.
90 changes: 52 additions & 38 deletions astrapy/idiomatic/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
from astrapy.db import AstraDBCollection, AsyncAstraDBCollection
from astrapy.idiomatic.types import (
DocumentType,
FilterType,
ProjectionType,
ReturnDocument,
SortType,
normalize_optional_projection,
)
from astrapy.idiomatic.database import AsyncDatabase, Database
Expand Down Expand Up @@ -254,18 +256,18 @@ def insert_many(
]
return InsertManyResult(
# if we are here, cim_responses are all dicts (no exceptions)
raw_result=cim_responses, # type: ignore[arg-type]
raw_results=cim_responses, # type: ignore[arg-type]
inserted_ids=inserted_ids,
)

def find(
self,
filter: Optional[Dict[str, Any]] = None,
filter: Optional[FilterType] = None,
*,
projection: Optional[ProjectionType] = None,
skip: Optional[int] = None,
limit: Optional[int] = None,
sort: Optional[Dict[str, Any]] = None,
sort: Optional[SortType] = None,
) -> Cursor:
return (
Cursor(
Expand All @@ -280,12 +282,12 @@ def find(

def find_one(
self,
filter: Optional[Dict[str, Any]] = None,
filter: Optional[FilterType] = None,
*,
projection: Optional[ProjectionType] = None,
skip: Optional[int] = None,
limit: Optional[int] = None,
sort: Optional[Dict[str, Any]] = None,
sort: Optional[SortType] = None,
) -> Union[DocumentType, None]:
fo_cursor = self.find(
filter=filter,
Expand All @@ -304,7 +306,7 @@ def distinct(
self,
key: str,
*,
filter: Optional[Dict[str, Any]] = None,
filter: Optional[FilterType] = None,
) -> List[Any]:
return self.find(
filter=filter,
Expand All @@ -314,14 +316,20 @@ def distinct(
def count_documents(
self,
filter: Dict[str, Any],
upper_bound: int,
) -> int:
cd_response = self._astra_db_collection.count_documents(filter=filter)
if "count" in cd_response.get("status", {}):
count: int = cd_response["status"]["count"]
if cd_response["status"].get("moreData", False):
raise ValueError(f"Document count exceeds {count}")
raise ValueError(
f"Document count exceeds {count}, the maximum allowed by the server"
)
else:
return count
if count > upper_bound:
raise ValueError("Document count exceeds required upper bound")
else:
return count
else:
raise ValueError(
"Could not complete a count_documents operation. "
Expand All @@ -334,12 +342,12 @@ def find_one_and_replace(
replacement: DocumentType,
*,
projection: Optional[ProjectionType] = None,
sort: Optional[Dict[str, Any]] = None,
sort: Optional[SortType] = None,
upsert: bool = False,
return_document: ReturnDocument = ReturnDocument.BEFORE,
return_document: str = ReturnDocument.BEFORE,
) -> Union[DocumentType, None]:
options = {
"returnDocument": return_document.value,
"returnDocument": return_document,
"upsert": upsert,
}
fo_response = self._astra_db_collection.find_one_and_replace(
Expand Down Expand Up @@ -395,12 +403,12 @@ def find_one_and_update(
update: Dict[str, Any],
*,
projection: Optional[ProjectionType] = None,
sort: Optional[Dict[str, Any]] = None,
sort: Optional[SortType] = None,
upsert: bool = False,
return_document: ReturnDocument = ReturnDocument.BEFORE,
return_document: str = ReturnDocument.BEFORE,
) -> Union[DocumentType, None]:
options = {
"returnDocument": return_document.value,
"returnDocument": return_document,
"upsert": upsert,
}
fo_response = self._astra_db_collection.find_one_and_update(
Expand Down Expand Up @@ -477,7 +485,7 @@ def find_one_and_delete(
filter: Dict[str, Any],
*,
projection: Optional[ProjectionType] = None,
sort: Optional[Dict[str, Any]] = None,
sort: Optional[SortType] = None,
) -> Union[DocumentType, None]:
_projection = normalize_optional_projection(projection, ensure_fields={"_id"})
target_document = self.find_one(
Expand Down Expand Up @@ -505,13 +513,13 @@ def delete_one(
if deleted_count == -1:
return DeleteResult(
deleted_count=None,
raw_result=do_response,
raw_results=[do_response],
)
else:
# expected a non-negative integer:
return DeleteResult(
deleted_count=deleted_count,
raw_result=do_response,
raw_results=[do_response],
)
else:
raise ValueError(
Expand All @@ -535,13 +543,13 @@ def delete_many(
if deleted_count == -1:
return DeleteResult(
deleted_count=None,
raw_result=dm_responses,
raw_results=dm_responses,
)
else:
# per API specs, deleted_count has to be a non-negative integer.
return DeleteResult(
deleted_count=deleted_count,
raw_result=dm_responses,
raw_results=dm_responses,
)
else:
raise ValueError(
Expand Down Expand Up @@ -780,18 +788,18 @@ async def insert_many(
]
return InsertManyResult(
# if we are here, cim_responses are all dicts (no exceptions)
raw_result=cim_responses, # type: ignore[arg-type]
raw_results=cim_responses, # type: ignore[arg-type]
inserted_ids=inserted_ids,
)

def find(
self,
filter: Optional[Dict[str, Any]] = None,
filter: Optional[FilterType] = None,
*,
projection: Optional[ProjectionType] = None,
skip: Optional[int] = None,
limit: Optional[int] = None,
sort: Optional[Dict[str, Any]] = None,
sort: Optional[SortType] = None,
) -> AsyncCursor:
return (
AsyncCursor(
Expand All @@ -806,12 +814,12 @@ def find(

async def find_one(
self,
filter: Optional[Dict[str, Any]] = None,
filter: Optional[FilterType] = None,
*,
projection: Optional[ProjectionType] = None,
skip: Optional[int] = None,
limit: Optional[int] = None,
sort: Optional[Dict[str, Any]] = None,
sort: Optional[SortType] = None,
) -> Union[DocumentType, None]:
fo_cursor = self.find(
filter=filter,
Expand All @@ -830,7 +838,7 @@ async def distinct(
self,
key: str,
*,
filter: Optional[Dict[str, Any]] = None,
filter: Optional[FilterType] = None,
) -> List[Any]:
cursor = self.find(
filter=filter,
Expand All @@ -841,14 +849,20 @@ async def distinct(
async def count_documents(
self,
filter: Dict[str, Any],
upper_bound: int,
) -> int:
cd_response = await self._astra_db_collection.count_documents(filter=filter)
if "count" in cd_response.get("status", {}):
count: int = cd_response["status"]["count"]
if cd_response["status"].get("moreData", False):
raise ValueError(f"Document count exceeds {count}")
raise ValueError(
f"Document count exceeds {count}, the maximum allowed by the server"
)
else:
return count
if count > upper_bound:
raise ValueError("Document count exceeds required upper bound")
else:
return count
else:
raise ValueError(
"Could not complete a count_documents operation. "
Expand All @@ -861,12 +875,12 @@ async def find_one_and_replace(
replacement: DocumentType,
*,
projection: Optional[ProjectionType] = None,
sort: Optional[Dict[str, Any]] = None,
sort: Optional[SortType] = None,
upsert: bool = False,
return_document: ReturnDocument = ReturnDocument.BEFORE,
return_document: str = ReturnDocument.BEFORE,
) -> Union[DocumentType, None]:
options = {
"returnDocument": return_document.value,
"returnDocument": return_document,
"upsert": upsert,
}
fo_response = await self._astra_db_collection.find_one_and_replace(
Expand Down Expand Up @@ -922,12 +936,12 @@ async def find_one_and_update(
update: Dict[str, Any],
*,
projection: Optional[ProjectionType] = None,
sort: Optional[Dict[str, Any]] = None,
sort: Optional[SortType] = None,
upsert: bool = False,
return_document: ReturnDocument = ReturnDocument.BEFORE,
return_document: str = ReturnDocument.BEFORE,
) -> Union[DocumentType, None]:
options = {
"returnDocument": return_document.value,
"returnDocument": return_document,
"upsert": upsert,
}
fo_response = await self._astra_db_collection.find_one_and_update(
Expand Down Expand Up @@ -1004,7 +1018,7 @@ async def find_one_and_delete(
filter: Dict[str, Any],
*,
projection: Optional[ProjectionType] = None,
sort: Optional[Dict[str, Any]] = None,
sort: Optional[SortType] = None,
) -> Union[DocumentType, None]:
_projection = normalize_optional_projection(projection, ensure_fields={"_id"})
target_document = await self.find_one(
Expand Down Expand Up @@ -1034,13 +1048,13 @@ async def delete_one(
if deleted_count == -1:
return DeleteResult(
deleted_count=None,
raw_result=do_response,
raw_results=[do_response],
)
else:
# expected a non-negative integer:
return DeleteResult(
deleted_count=deleted_count,
raw_result=do_response,
raw_results=[do_response],
)
else:
raise ValueError(
Expand Down Expand Up @@ -1068,13 +1082,13 @@ async def delete_many(
if deleted_count == -1:
return DeleteResult(
deleted_count=None,
raw_result=dm_responses,
raw_results=dm_responses,
)
else:
# per API specs, deleted_count has to be a non-negative integer.
return DeleteResult(
deleted_count=deleted_count,
raw_result=dm_responses,
raw_results=dm_responses,
)
else:
raise ValueError(
Expand Down
Loading

0 comments on commit 856c075

Please sign in to comment.