Skip to content

Commit 01d2b96

Browse files
committed
Adding support for /responsibleShard
1 parent 1dc4dc7 commit 01d2b96

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

arangoasync/collection.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
)
1818
from arangoasync.exceptions import (
1919
CollectionPropertiesError,
20+
CollectionResponsibleShardError,
2021
CollectionStatisticsError,
2122
CollectionTruncateError,
2223
DocumentCountError,
@@ -594,6 +595,38 @@ def response_handler(resp: Response) -> CollectionStatistics:
594595

595596
return await self._executor.execute(request, response_handler)
596597

598+
async def responsible_shard(self, document: Json) -> Result[str]:
599+
"""Return the ID of the shard responsible for given document.
600+
601+
If the document does not exist, return the shard that would be
602+
responsible.
603+
604+
Args:
605+
document (dict): Document body with "_key" field.
606+
607+
Returns:
608+
str: Shard ID.
609+
610+
Raises:
611+
CollectionResponsibleShardError: If retrieval fails.
612+
613+
References:
614+
- `get-the-responsible-shard-for-a-document <https://docs.arangodb.com/stable/develop/http-api/collections/#get-the-responsible-shard-for-a-document>`__
615+
""" # noqa: E501
616+
request = Request(
617+
method=Method.PUT,
618+
endpoint=f"/_api/collection/{self.name}/responsibleShard",
619+
data=self.serializer.dumps(document),
620+
)
621+
622+
def response_handler(resp: Response) -> str:
623+
if resp.is_success:
624+
body = self.deserializer.loads(resp.raw_body)
625+
return cast(str, body["shardId"])
626+
raise CollectionResponsibleShardError(resp, request)
627+
628+
return await self._executor.execute(request, response_handler)
629+
597630
async def has(
598631
self,
599632
document: str | Json,

arangoasync/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ class CollectionPropertiesError(ArangoServerError):
195195
"""Failed to retrieve collection properties."""
196196

197197

198+
class CollectionResponsibleShardError(ArangoServerError):
199+
"""Failed to retrieve responsible shard."""
200+
201+
198202
class CollectionStatisticsError(ArangoServerError):
199203
"""Failed to retrieve collection statistics."""
200204

tests/test_collection.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from arangoasync.errno import DATA_SOURCE_NOT_FOUND, INDEX_NOT_FOUND
66
from arangoasync.exceptions import (
77
CollectionPropertiesError,
8+
CollectionResponsibleShardError,
89
CollectionStatisticsError,
910
CollectionTruncateError,
1011
DocumentCountError,
@@ -23,20 +24,29 @@ def test_collection_attributes(db, doc_col):
2324

2425

2526
@pytest.mark.asyncio
26-
async def test_collection_misc_methods(doc_col, bad_col):
27+
async def test_collection_misc_methods(doc_col, bad_col, docs):
2728
# Properties
2829
properties = await doc_col.properties()
2930
assert properties.name == doc_col.name
3031
assert properties.is_system is False
3132
assert len(properties.format()) > 1
3233
with pytest.raises(CollectionPropertiesError):
3334
await bad_col.properties()
35+
36+
# Statistics
3437
statistics = await doc_col.statistics()
3538
assert statistics.name == doc_col.name
3639
assert "figures" in statistics
3740
with pytest.raises(CollectionStatisticsError):
3841
await bad_col.statistics()
3942

43+
# Shards
44+
doc = await doc_col.insert(docs[0])
45+
shard = await doc_col.responsible_shard(doc)
46+
assert isinstance(shard, str)
47+
with pytest.raises(CollectionResponsibleShardError):
48+
await bad_col.responsible_shard(doc)
49+
4050

4151
@pytest.mark.asyncio
4252
async def test_collection_index(doc_col, bad_col, cluster):

0 commit comments

Comments
 (0)