|
1 |
| -__all__ = ["AQL"] |
| 1 | +__all__ = ["AQL", "AQLQueryCache"] |
2 | 2 |
|
3 | 3 |
|
4 | 4 | from typing import Optional
|
5 | 5 |
|
6 | 6 | from arangoasync.cursor import Cursor
|
7 | 7 | from arangoasync.errno import HTTP_NOT_FOUND
|
8 | 8 | from arangoasync.exceptions import (
|
| 9 | + AQLCacheClearError, |
| 10 | + AQLCacheConfigureError, |
| 11 | + AQLCacheEntriesError, |
| 12 | + AQLCachePropertiesError, |
9 | 13 | AQLQueryClearError,
|
10 | 14 | AQLQueryExecuteError,
|
11 | 15 | AQLQueryExplainError,
|
|
23 | 27 | from arangoasync.typings import (
|
24 | 28 | Json,
|
25 | 29 | Jsons,
|
| 30 | + QueryCacheProperties, |
26 | 31 | QueryExplainOptions,
|
27 | 32 | QueryProperties,
|
28 | 33 | QueryTrackingConfiguration,
|
29 | 34 | Result,
|
30 | 35 | )
|
31 | 36 |
|
32 | 37 |
|
| 38 | +class AQLQueryCache: |
| 39 | + """AQL Query Cache API wrapper. |
| 40 | +
|
| 41 | + Args: |
| 42 | + executor: API executor. Required to execute the API requests. |
| 43 | + """ |
| 44 | + |
| 45 | + def __init__(self, executor: ApiExecutor) -> None: |
| 46 | + self._executor = executor |
| 47 | + |
| 48 | + @property |
| 49 | + def name(self) -> str: |
| 50 | + """Return the name of the current database.""" |
| 51 | + return self._executor.db_name |
| 52 | + |
| 53 | + @property |
| 54 | + def serializer(self) -> Serializer[Json]: |
| 55 | + """Return the serializer.""" |
| 56 | + return self._executor.serializer |
| 57 | + |
| 58 | + @property |
| 59 | + def deserializer(self) -> Deserializer[Json, Jsons]: |
| 60 | + """Return the deserializer.""" |
| 61 | + return self._executor.deserializer |
| 62 | + |
| 63 | + def __repr__(self) -> str: |
| 64 | + return f"<AQLQueryCache in {self.name}>" |
| 65 | + |
| 66 | + async def entries(self) -> Result[Jsons]: |
| 67 | + """Return a list of all AQL query results cache entries. |
| 68 | +
|
| 69 | +
|
| 70 | + Returns: |
| 71 | + list: List of AQL query results cache entries. |
| 72 | +
|
| 73 | + Raises: |
| 74 | + AQLCacheEntriesError: If retrieval fails. |
| 75 | +
|
| 76 | + References: |
| 77 | + - `list-the-entries-of-the-aql-query-results-cache <https://docs.arangodb.com/stable/develop/http-api/queries/aql-query-results-cache/#list-the-entries-of-the-aql-query-results-cache>`__ |
| 78 | + """ # noqa: E501 |
| 79 | + request = Request(method=Method.GET, endpoint="/_api/query-cache/entries") |
| 80 | + |
| 81 | + def response_handler(resp: Response) -> Jsons: |
| 82 | + if not resp.is_success: |
| 83 | + raise AQLCacheEntriesError(resp, request) |
| 84 | + return self.deserializer.loads_many(resp.raw_body) |
| 85 | + |
| 86 | + return await self._executor.execute(request, response_handler) |
| 87 | + |
| 88 | + async def plan_entries(self) -> Result[Jsons]: |
| 89 | + """Return a list of all AQL query plan cache entries. |
| 90 | +
|
| 91 | + Returns: |
| 92 | + list: List of AQL query plan cache entries. |
| 93 | +
|
| 94 | + Raises: |
| 95 | + AQLCacheEntriesError: If retrieval fails. |
| 96 | +
|
| 97 | + References: |
| 98 | + - `list-the-entries-of-the-aql-query-plan-cache <https://docs.arangodb.com/stable/develop/http-api/queries/aql-query-plan-cache/#list-the-entries-of-the-aql-query-plan-cache>`__ |
| 99 | + """ # noqa: E501 |
| 100 | + request = Request(method=Method.GET, endpoint="/_api/query-plan-cache") |
| 101 | + |
| 102 | + def response_handler(resp: Response) -> Jsons: |
| 103 | + if not resp.is_success: |
| 104 | + raise AQLCacheEntriesError(resp, request) |
| 105 | + return self.deserializer.loads_many(resp.raw_body) |
| 106 | + |
| 107 | + return await self._executor.execute(request, response_handler) |
| 108 | + |
| 109 | + async def clear(self) -> Result[None]: |
| 110 | + """Clear the AQL query results cache. |
| 111 | +
|
| 112 | + Raises: |
| 113 | + AQLCacheClearError: If clearing the cache fails. |
| 114 | +
|
| 115 | + References: |
| 116 | + - `clear-the-aql-query-results-cache <https://docs.arangodb.com/stable/develop/http-api/queries/aql-query-results-cache/#clear-the-aql-query-results-cache>`__ |
| 117 | + """ # noqa: E501 |
| 118 | + request = Request(method=Method.DELETE, endpoint="/_api/query-cache") |
| 119 | + |
| 120 | + def response_handler(resp: Response) -> None: |
| 121 | + if not resp.is_success: |
| 122 | + raise AQLCacheClearError(resp, request) |
| 123 | + |
| 124 | + return await self._executor.execute(request, response_handler) |
| 125 | + |
| 126 | + async def clear_plan(self) -> Result[None]: |
| 127 | + """Clear the AQL query plan cache. |
| 128 | +
|
| 129 | + Raises: |
| 130 | + AQLCacheClearError: If clearing the cache fails. |
| 131 | +
|
| 132 | + References: |
| 133 | + - `clear-the-aql-query-plan-cache <https://docs.arangodb.com/stable/develop/http-api/queries/aql-query-plan-cache/#clear-the-aql-query-plan-cache>`__ |
| 134 | + """ # noqa: E501 |
| 135 | + request = Request(method=Method.DELETE, endpoint="/_api/query-plan-cache") |
| 136 | + |
| 137 | + def response_handler(resp: Response) -> None: |
| 138 | + if not resp.is_success: |
| 139 | + raise AQLCacheClearError(resp, request) |
| 140 | + |
| 141 | + return await self._executor.execute(request, response_handler) |
| 142 | + |
| 143 | + async def properties(self) -> Result[QueryCacheProperties]: |
| 144 | + """Return the current AQL query results cache configuration. |
| 145 | +
|
| 146 | + Returns: |
| 147 | + QueryCacheProperties: Current AQL query cache properties. |
| 148 | +
|
| 149 | + Raises: |
| 150 | + AQLCachePropertiesError: If retrieval fails. |
| 151 | +
|
| 152 | + References: |
| 153 | + - `get-the-aql-query-results-cache-configuration <https://docs.arangodb.com/stable/develop/http-api/queries/aql-query-results-cache/#get-the-aql-query-results-cache-configuration>`__ |
| 154 | + """ # noqa: E501 |
| 155 | + request = Request(method=Method.GET, endpoint="/_api/query-cache/properties") |
| 156 | + |
| 157 | + def response_handler(resp: Response) -> QueryCacheProperties: |
| 158 | + if not resp.is_success: |
| 159 | + raise AQLCachePropertiesError(resp, request) |
| 160 | + return QueryCacheProperties(self.deserializer.loads(resp.raw_body)) |
| 161 | + |
| 162 | + return await self._executor.execute(request, response_handler) |
| 163 | + |
| 164 | + async def configure( |
| 165 | + self, |
| 166 | + mode: Optional[str] = None, |
| 167 | + max_results: Optional[int] = None, |
| 168 | + max_results_size: Optional[int] = None, |
| 169 | + max_entry_size: Optional[int] = None, |
| 170 | + include_system: Optional[bool] = None, |
| 171 | + ) -> Result[QueryCacheProperties]: |
| 172 | + """Configure the AQL query results cache. |
| 173 | +
|
| 174 | + Args: |
| 175 | + mode (str | None): Cache mode. Allowed values are `"off"`, `"on"`, |
| 176 | + and `"demand"`. |
| 177 | + max_results (int | None): Max number of query results stored per |
| 178 | + database-specific cache. |
| 179 | + max_results_size (int | None): Max cumulative size of query results stored |
| 180 | + per database-specific cache. |
| 181 | + max_entry_size (int | None): Max entry size of each query result stored per |
| 182 | + database-specific cache. |
| 183 | + include_system (bool | None): Store results of queries in system collections. |
| 184 | +
|
| 185 | + Returns: |
| 186 | + QueryCacheProperties: Updated AQL query cache properties. |
| 187 | +
|
| 188 | + Raises: |
| 189 | + AQLCacheConfigureError: If setting the configuration fails. |
| 190 | +
|
| 191 | + References: |
| 192 | + - `set-the-aql-query-results-cache-configuration <https://docs.arangodb.com/stable/develop/http-api/queries/aql-query-results-cache/#set-the-aql-query-results-cache-configuration>`__ |
| 193 | + """ # noqa: E501 |
| 194 | + data: Json = dict() |
| 195 | + if mode is not None: |
| 196 | + data["mode"] = mode |
| 197 | + if max_results is not None: |
| 198 | + data["maxResults"] = max_results |
| 199 | + if max_results_size is not None: |
| 200 | + data["maxResultsSize"] = max_results_size |
| 201 | + if max_entry_size is not None: |
| 202 | + data["maxEntrySize"] = max_entry_size |
| 203 | + if include_system is not None: |
| 204 | + data["includeSystem"] = include_system |
| 205 | + |
| 206 | + request = Request( |
| 207 | + method=Method.PUT, |
| 208 | + endpoint="/_api/query-cache/properties", |
| 209 | + data=self.serializer.dumps(data), |
| 210 | + ) |
| 211 | + |
| 212 | + def response_handler(resp: Response) -> QueryCacheProperties: |
| 213 | + if not resp.is_success: |
| 214 | + raise AQLCacheConfigureError(resp, request) |
| 215 | + return QueryCacheProperties(self.deserializer.loads(resp.raw_body)) |
| 216 | + |
| 217 | + return await self._executor.execute(request, response_handler) |
| 218 | + |
| 219 | + |
33 | 220 | class AQL:
|
34 | 221 | """AQL (ArangoDB Query Language) API wrapper.
|
35 | 222 |
|
@@ -58,6 +245,11 @@ def deserializer(self) -> Deserializer[Json, Jsons]:
|
58 | 245 | """Return the deserializer."""
|
59 | 246 | return self._executor.deserializer
|
60 | 247 |
|
| 248 | + @property |
| 249 | + def cache(self) -> AQLQueryCache: |
| 250 | + """Return the AQL Query Cache API wrapper.""" |
| 251 | + return AQLQueryCache(self._executor) |
| 252 | + |
61 | 253 | def __repr__(self) -> str:
|
62 | 254 | return f"<AQL in {self.name}>"
|
63 | 255 |
|
|
0 commit comments