Skip to content

Commit 1e79243

Browse files
committed
Adding configure method
1 parent c4024c5 commit 1e79243

File tree

3 files changed

+83
-6
lines changed

3 files changed

+83
-6
lines changed

arangoasync/collection.py

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
)
1818
from arangoasync.exceptions import (
1919
CollectionChecksumError,
20+
CollectionConfigureError,
2021
CollectionPropertiesError,
2122
CollectionResponsibleShardError,
2223
CollectionRevisionError,
@@ -507,7 +508,71 @@ async def properties(self) -> Result[CollectionProperties]:
507508
def response_handler(resp: Response) -> CollectionProperties:
508509
if not resp.is_success:
509510
raise CollectionPropertiesError(resp, request)
510-
return CollectionProperties(self._executor.deserialize(resp.raw_body))
511+
return CollectionProperties(self.deserializer.loads(resp.raw_body))
512+
513+
return await self._executor.execute(request, response_handler)
514+
515+
async def configure(
516+
self,
517+
cache_enabled: Optional[bool] = None,
518+
computed_values: Optional[Jsons] = None,
519+
replication_factor: Optional[int | str] = None,
520+
schema: Optional[Json] = None,
521+
wait_for_sync: Optional[bool] = None,
522+
write_concern: Optional[int] = None,
523+
) -> Result[CollectionProperties]:
524+
"""Changes the properties of a collection.
525+
526+
Only the provided attributes are updated.
527+
528+
Args:
529+
cache_enabled (bool | None): Whether the in-memory hash cache
530+
for documents should be enabled for this collection.
531+
computed_values (list | None): An optional list of objects, each
532+
representing a computed value.
533+
replication_factor (int | None): In a cluster, this attribute determines
534+
how many copies of each shard are kept on different DB-Servers.
535+
For SatelliteCollections, it needs to be the string "satellite".
536+
schema (dict | None): The configuration of the collection-level schema
537+
validation for documents.
538+
wait_for_sync (bool | None): If set to `True`, the data is synchronized
539+
to disk before returning from a document create, update, replace or
540+
removal operation.
541+
write_concern (int | None): Determines how many copies of each shard are
542+
required to be in sync on the different DB-Servers.
543+
544+
Returns:
545+
CollectionProperties: Properties.
546+
547+
Raises:
548+
CollectionConfigureError: If configuration fails.
549+
550+
References:
551+
- `change-the-properties-of-a-collection <https://docs.arangodb.com/stable/develop/http-api/collections/#change-the-properties-of-a-collection>`__
552+
""" # noqa: E501
553+
data: Json = {}
554+
if cache_enabled is not None:
555+
data["cacheEnabled"] = cache_enabled
556+
if computed_values is not None:
557+
data["computedValues"] = computed_values
558+
if replication_factor is not None:
559+
data["replicationFactor"] = replication_factor
560+
if schema is not None:
561+
data["schema"] = schema
562+
if wait_for_sync is not None:
563+
data["waitForSync"] = wait_for_sync
564+
if write_concern is not None:
565+
data["writeConcern"] = write_concern
566+
request = Request(
567+
method=Method.PUT,
568+
endpoint=f"/_api/collection/{self.name}/properties",
569+
data=self.serializer.dumps(data),
570+
)
571+
572+
def response_handler(resp: Response) -> CollectionProperties:
573+
if not resp.is_success:
574+
raise CollectionConfigureError(resp, request)
575+
return CollectionProperties(self.deserializer.loads(resp.raw_body))
511576

512577
return await self._executor.execute(request, response_handler)
513578

@@ -1605,9 +1670,9 @@ async def insert(
16051670

16061671
def response_handler(resp: Response) -> bool | Json:
16071672
if resp.is_success:
1608-
if silent is True:
1673+
if silent:
16091674
return True
1610-
return self._executor.deserialize(resp.raw_body)
1675+
return self.deserializer.loads(resp.raw_body)
16111676
msg: Optional[str] = None
16121677
if resp.status_code == HTTP_BAD_PARAMETER:
16131678
msg = (
@@ -1712,7 +1777,7 @@ def response_handler(resp: Response) -> bool | Json:
17121777
if resp.is_success:
17131778
if silent is True:
17141779
return True
1715-
return self._executor.deserialize(resp.raw_body)
1780+
return self.deserializer.loads(resp.raw_body)
17161781
msg: Optional[str] = None
17171782
if resp.status_code == HTTP_PRECONDITION_FAILED:
17181783
raise DocumentRevisionError(resp, request)
@@ -1802,7 +1867,7 @@ def response_handler(resp: Response) -> bool | Json:
18021867
if resp.is_success:
18031868
if silent is True:
18041869
return True
1805-
return self._executor.deserialize(resp.raw_body)
1870+
return self.deserializer.loads(resp.raw_body)
18061871
msg: Optional[str] = None
18071872
if resp.status_code == HTTP_PRECONDITION_FAILED:
18081873
raise DocumentRevisionError(resp, request)
@@ -1887,7 +1952,7 @@ def response_handler(resp: Response) -> bool | Json:
18871952
if resp.is_success:
18881953
if silent is True:
18891954
return True
1890-
return self._executor.deserialize(resp.raw_body)
1955+
return self.deserializer.loads(resp.raw_body)
18911956
msg: Optional[str] = None
18921957
if resp.status_code == HTTP_PRECONDITION_FAILED:
18931958
raise DocumentRevisionError(resp, request)

arangoasync/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ class CollectionChecksumError(ArangoServerError):
187187
"""Failed to retrieve collection checksum."""
188188

189189

190+
class CollectionConfigureError(ArangoServerError):
191+
"""Failed to configure collection properties."""
192+
193+
190194
class CollectionDeleteError(ArangoServerError):
191195
"""Failed to delete collection."""
192196

tests/test_collection.py

Lines changed: 8 additions & 0 deletions
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
CollectionChecksumError,
8+
CollectionConfigureError,
89
CollectionPropertiesError,
910
CollectionResponsibleShardError,
1011
CollectionRevisionError,
@@ -38,6 +39,13 @@ async def test_collection_misc_methods(doc_col, bad_col, docs, cluster):
3839
with pytest.raises(CollectionPropertiesError):
3940
await bad_col.properties()
4041

42+
# Configure
43+
wfs = not properties.wait_for_sync
44+
new_properties = await doc_col.configure(wait_for_sync=wfs)
45+
assert new_properties.wait_for_sync == wfs
46+
with pytest.raises(CollectionConfigureError):
47+
await bad_col.configure(wait_for_sync=wfs)
48+
4149
# Statistics
4250
statistics = await doc_col.statistics()
4351
assert statistics.name == doc_col.name

0 commit comments

Comments
 (0)