Skip to content

Commit e44a3e8

Browse files
aMahannaapetenchea
andauthored
Make add_index public (#321)
* make `add_index` public | initial commit * fix `conftest` * Changed maintainer email * Fixing up after the merge * Using persistent index instead of hash * Fixing docs * Fixing typos * Fixing tests * Removing unused argument * Adding the formatter parameter * Added note explaining the formatter parameter. * Update arango/collection.py Co-authored-by: Anthony Mahanna <[email protected]> * Update arango/collection.py Co-authored-by: Anthony Mahanna <[email protected]> * Changed warning --------- Co-authored-by: Alex Petenchea <[email protected]> Co-authored-by: Alex Petenchea <[email protected]>
1 parent a26e06a commit e44a3e8

11 files changed

+202
-183
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ db = client.db("test", username="root", password="passwd")
5353
students = db.create_collection("students")
5454

5555
# Add a persistent index to the collection.
56-
students.add_persistent_index(fields=["name"], unique=True)
56+
students.add_index({'type': 'persistent', 'fields': ['name'], 'unique': True})
5757

5858
# Insert new documents into the collection.
5959
students.insert({"name": "jane", "age": 39})

arango/client.py

-3
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ def db(
199199
auth_method: str = "basic",
200200
user_token: Optional[str] = None,
201201
superuser_token: Optional[str] = None,
202-
verify_certificate: bool = True,
203202
) -> StandardDatabase:
204203
"""Connect to an ArangoDB database and return the database API wrapper.
205204
@@ -228,8 +227,6 @@ def db(
228227
are ignored. This token is not refreshed automatically. Token
229228
expiry will not be checked.
230229
:type superuser_token: str
231-
:param verify_certificate: Verify TLS certificates.
232-
:type verify_certificate: bool
233230
:return: Standard database API wrapper.
234231
:rtype: arango.database.StandardDatabase
235232
:raise arango.exceptions.ServerConnectionError: If **verify** was set

arango/collection.py

+60-105
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from numbers import Number
44
from typing import List, Optional, Sequence, Tuple, Union
5+
from warnings import warn
56

67
from arango.api import ApiGroup
78
from arango.connection import Connection
@@ -1074,7 +1075,7 @@ def build_coord_str_from_index(index: Json) -> str:
10741075
FILTER GEO_CONTAINS(rect, {coord_str})
10751076
LIMIT {skip_val}, {limit_val}
10761077
RETURN doc
1077-
"""
1078+
""" # noqa: E201 E202
10781079

10791080
bind_vars = {"@collection": self.name}
10801081

@@ -1259,11 +1260,27 @@ def response_handler(resp: Response) -> Json:
12591260

12601261
return self._execute(request, response_handler)
12611262

1262-
def _add_index(self, data: Json) -> Result[Json]:
1263-
"""Helper method for creating a new index.
1263+
def add_index(self, data: Json, formatter: bool = False) -> Result[Json]:
1264+
"""Create an index.
1265+
1266+
.. note::
12641267
1265-
:param data: Index data.
1268+
As the `add_index` method was made available starting with driver
1269+
version 8, we have decided to deprecate the other `add_*_index`
1270+
methods, making this the official way to create indexes. While
1271+
the other methods still work, we recommend using this one instead.
1272+
Note that the other methods would use a formatter by default,
1273+
processing the index attributes returned by the server (for the
1274+
most part, it does a snake case conversion). This method skips that,
1275+
returning the raw index, except for the `id` attribute. However,
1276+
if you want the formatter to be applied for backwards compatibility,
1277+
you can set the `formatter` parameter to `True`.
1278+
1279+
:param data: Index data. Must contain a "type" and "fields" attribute.
12661280
:type data: dict
1281+
:param formatter: If set to True, apply formatting to the returned result.
1282+
Should only be used for backwards compatibility.
1283+
:type formatter: bool
12671284
:return: New index details.
12681285
:rtype: dict
12691286
:raise arango.exceptions.IndexCreateError: If create fails.
@@ -1278,7 +1295,7 @@ def _add_index(self, data: Json) -> Result[Json]:
12781295
def response_handler(resp: Response) -> Json:
12791296
if not resp.is_success:
12801297
raise IndexCreateError(resp, request)
1281-
return format_index(resp.body)
1298+
return format_index(resp.body, formatter)
12821299

12831300
return self._execute(request, response_handler)
12841301

@@ -1297,8 +1314,7 @@ def add_hash_index(
12971314
12981315
The index types `hash` and `skiplist` are aliases for the persistent
12991316
index type and should no longer be used to create new indexes. The
1300-
aliases will be removed in a future version. Use
1301-
:func:`arango.collection.Collection.add_persistent_index` instead.
1317+
aliases will be removed in a future version.
13021318
13031319
:param fields: Document fields to index.
13041320
:type fields: [str]
@@ -1318,6 +1334,9 @@ def add_hash_index(
13181334
:rtype: dict
13191335
:raise arango.exceptions.IndexCreateError: If create fails.
13201336
"""
1337+
m = "add_hash_index is deprecated. Using add_index with {'type': 'hash'} instead." # noqa: E501
1338+
warn(m, DeprecationWarning, stacklevel=2)
1339+
13211340
data: Json = {"type": "hash", "fields": fields}
13221341

13231342
if unique is not None:
@@ -1331,7 +1350,7 @@ def add_hash_index(
13311350
if in_background is not None:
13321351
data["inBackground"] = in_background
13331352

1334-
return self._add_index(data)
1353+
return self.add_index(data, formatter=True)
13351354

13361355
def add_skiplist_index(
13371356
self,
@@ -1348,8 +1367,7 @@ def add_skiplist_index(
13481367
13491368
The index types `hash` and `skiplist` are aliases for the persistent
13501369
index type and should no longer be used to create new indexes. The
1351-
aliases will be removed in a future version. Use
1352-
:func:`arango.collection.Collection.add_persistent_index` instead.
1370+
aliases will be removed in a future version.
13531371
13541372
:param fields: Document fields to index.
13551373
:type fields: [str]
@@ -1369,6 +1387,9 @@ def add_skiplist_index(
13691387
:rtype: dict
13701388
:raise arango.exceptions.IndexCreateError: If create fails.
13711389
"""
1390+
m = "add_skiplist_index is deprecated. Using add_index with {'type': 'skiplist'} instead." # noqa: E501
1391+
warn(m, DeprecationWarning, stacklevel=2)
1392+
13721393
data: Json = {"type": "skiplist", "fields": fields}
13731394

13741395
if unique is not None:
@@ -1382,7 +1403,7 @@ def add_skiplist_index(
13821403
if in_background is not None:
13831404
data["inBackground"] = in_background
13841405

1385-
return self._add_index(data)
1406+
return self.add_index(data, formatter=True)
13861407

13871408
def add_geo_index(
13881409
self,
@@ -1414,6 +1435,9 @@ def add_geo_index(
14141435
:rtype: dict
14151436
:raise arango.exceptions.IndexCreateError: If create fails.
14161437
"""
1438+
m = "add_geo_index is deprecated. Using add_index with {'type': 'geo'} instead." # noqa: E501
1439+
warn(m, DeprecationWarning, stacklevel=2)
1440+
14171441
data: Json = {"type": "geo", "fields": fields}
14181442

14191443
if geo_json is not None:
@@ -1425,7 +1449,7 @@ def add_geo_index(
14251449
if legacyPolygons is not None:
14261450
data["legacyPolygons"] = legacyPolygons
14271451

1428-
return self._add_index(data)
1452+
return self.add_index(data, formatter=True)
14291453

14301454
def add_fulltext_index(
14311455
self,
@@ -1434,9 +1458,11 @@ def add_fulltext_index(
14341458
name: Optional[str] = None,
14351459
in_background: Optional[bool] = None,
14361460
) -> Result[Json]:
1437-
"""Create a new fulltext index. This method is deprecated
1438-
in ArangoDB 3.10 and will be removed in a future version
1439-
of the driver.
1461+
"""Create a new fulltext index.
1462+
1463+
.. warning::
1464+
This method is deprecated since ArangoDB 3.10 and will be removed
1465+
in a future version of the driver.
14401466
14411467
:param fields: Document fields to index.
14421468
:type fields: [str]
@@ -1450,6 +1476,9 @@ def add_fulltext_index(
14501476
:rtype: dict
14511477
:raise arango.exceptions.IndexCreateError: If create fails.
14521478
"""
1479+
m = "add_fulltext_index is deprecated. Using add_index with {'type': 'fulltext'} instead." # noqa: E501
1480+
warn(m, DeprecationWarning, stacklevel=2)
1481+
14531482
data: Json = {"type": "fulltext", "fields": fields}
14541483

14551484
if min_length is not None:
@@ -1459,7 +1488,7 @@ def add_fulltext_index(
14591488
if in_background is not None:
14601489
data["inBackground"] = in_background
14611490

1462-
return self._add_index(data)
1491+
return self.add_index(data, formatter=True)
14631492

14641493
def add_persistent_index(
14651494
self,
@@ -1502,6 +1531,9 @@ def add_persistent_index(
15021531
:rtype: dict
15031532
:raise arango.exceptions.IndexCreateError: If create fails.
15041533
"""
1534+
m = "add_persistent_index is deprecated. Using add_index with {'type': 'persistent'} instead." # noqa: E501
1535+
warn(m, DeprecationWarning, stacklevel=2)
1536+
15051537
data: Json = {"type": "persistent", "fields": fields}
15061538

15071539
if unique is not None:
@@ -1517,7 +1549,7 @@ def add_persistent_index(
15171549
if cacheEnabled is not None:
15181550
data["cacheEnabled"] = cacheEnabled
15191551

1520-
return self._add_index(data)
1552+
return self.add_index(data, formatter=True)
15211553

15221554
def add_ttl_index(
15231555
self,
@@ -1540,14 +1572,17 @@ def add_ttl_index(
15401572
:rtype: dict
15411573
:raise arango.exceptions.IndexCreateError: If create fails.
15421574
"""
1575+
m = "add_ttl_index is deprecated. Using add_index with {'type': 'ttl'} instead." # noqa: E501
1576+
warn(m, DeprecationWarning, stacklevel=2)
1577+
15431578
data: Json = {"type": "ttl", "fields": fields, "expireAfter": expiry_time}
15441579

15451580
if name is not None:
15461581
data["name"] = name
15471582
if in_background is not None:
15481583
data["inBackground"] = in_background
15491584

1550-
return self._add_index(data)
1585+
return self.add_index(data, formatter=True)
15511586

15521587
def add_inverted_index(
15531588
self,
@@ -1602,6 +1637,9 @@ def add_inverted_index(
16021637
:rtype: dict
16031638
:raise arango.exceptions.IndexCreateError: If create fails.
16041639
"""
1640+
m = "add_inverted_index is deprecated. Using add_index with {'type': 'inverted'} instead." # noqa: E501
1641+
warn(m, DeprecationWarning, stacklevel=2)
1642+
16051643
data: Json = {"type": "inverted", "fields": fields}
16061644

16071645
if name is not None:
@@ -1631,90 +1669,7 @@ def add_inverted_index(
16311669
if cache is not None:
16321670
data["cache"] = cache
16331671

1634-
return self._add_index(data)
1635-
1636-
def add_zkd_index(
1637-
self,
1638-
fields: Sequence[str],
1639-
field_value_types: str = "double",
1640-
name: Optional[str] = None,
1641-
unique: Optional[bool] = None,
1642-
in_background: Optional[bool] = None,
1643-
) -> Result[Json]:
1644-
"""Create a new ZKD Index.
1645-
1646-
:param fields: Document fields to index. Unlike for other indexes the
1647-
order of the fields does not matter.
1648-
:type fields: Sequence[str]
1649-
:param field_value_types: The type of the field values. The only allowed
1650-
value is "double" at the moment. Defaults to "double".
1651-
:type field_value_types: str
1652-
:param name: Optional name for the index.
1653-
:type name: str | None
1654-
:param unique: Whether the index is unique.
1655-
:type unique: bool | None
1656-
:param in_background: Do not hold the collection lock.
1657-
:type in_background: bool | None
1658-
:return: New index details.
1659-
:rtype: dict
1660-
:raise arango.exceptions.IndexCreateError: If create fails.
1661-
"""
1662-
data: Json = {
1663-
"type": "zkd",
1664-
"fields": fields,
1665-
"fieldValueTypes": field_value_types,
1666-
}
1667-
1668-
if unique is not None:
1669-
data["unique"] = unique
1670-
if name is not None:
1671-
data["name"] = name
1672-
if in_background is not None:
1673-
data["inBackground"] = in_background
1674-
1675-
return self._add_index(data)
1676-
1677-
def add_mdi_index(
1678-
self,
1679-
fields: Sequence[str],
1680-
field_value_types: str = "double",
1681-
name: Optional[str] = None,
1682-
unique: Optional[bool] = None,
1683-
in_background: Optional[bool] = None,
1684-
) -> Result[Json]:
1685-
"""Create a new MDI index, previously known as ZKD index. This method
1686-
is only usable with ArangoDB 3.12 and later.
1687-
1688-
:param fields: Document fields to index. Unlike for other indexes the
1689-
order of the fields does not matter.
1690-
:type fields: Sequence[str]
1691-
:param field_value_types: The type of the field values. The only allowed
1692-
value is "double" at the moment. Defaults to "double".
1693-
:type field_value_types: str
1694-
:param name: Optional name for the index.
1695-
:type name: str | None
1696-
:param unique: Whether the index is unique.
1697-
:type unique: bool | None
1698-
:param in_background: Do not hold the collection lock.
1699-
:type in_background: bool | None
1700-
:return: New index details.
1701-
:rtype: dict
1702-
:raise arango.exceptions.IndexCreateError: If create fails.
1703-
"""
1704-
data: Json = {
1705-
"type": "mdi",
1706-
"fields": fields,
1707-
"fieldValueTypes": field_value_types,
1708-
}
1709-
1710-
if unique is not None:
1711-
data["unique"] = unique
1712-
if name is not None:
1713-
data["name"] = name
1714-
if in_background is not None:
1715-
data["inBackground"] = in_background
1716-
1717-
return self._add_index(data)
1672+
return self.add_index(data, formatter=True)
17181673

17191674
def delete_index(self, index_id: str, ignore_missing: bool = False) -> Result[bool]:
17201675
"""Delete an index.
@@ -2076,7 +2031,7 @@ def update_match(
20762031
{f"LIMIT {limit}" if limit is not None else ""}
20772032
UPDATE doc WITH @body IN @@collection
20782033
OPTIONS {{ keepNull: @keep_none, mergeObjects: @merge {sync_val} }}
2079-
"""
2034+
""" # noqa: E201 E202
20802035

20812036
bind_vars = {
20822037
"@collection": self.name,
@@ -2261,7 +2216,7 @@ def replace_match(
22612216
{f"LIMIT {limit}" if limit is not None else ""}
22622217
REPLACE doc WITH @body IN @@collection
22632218
{f"OPTIONS {{ {sync_val} }}" if sync_val else ""}
2264-
"""
2219+
""" # noqa: E201 E202
22652220

22662221
bind_vars = {"@collection": self.name, "body": body}
22672222

@@ -2426,7 +2381,7 @@ def delete_match(
24262381
{f"LIMIT {limit}" if limit is not None else ""}
24272382
REMOVE doc IN @@collection
24282383
{f"OPTIONS {{ {sync_val} }}" if sync_val else ""}
2429-
"""
2384+
""" # noqa: E201 E202
24302385

24312386
bind_vars = {"@collection": self.name}
24322387

arango/formatter.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,22 @@ def format_body(body: Json) -> Json:
2020
return body
2121

2222

23-
def format_index(body: Json) -> Json:
23+
def format_index(body: Json, formatter: bool = True) -> Json:
2424
"""Format index data.
2525
2626
:param body: Input body.
2727
:type body: dict
28+
:param formatter: Convert (most) keys to snake_case.
29+
:type formatter: bool
2830
:return: Formatted body.
2931
:rtype: dict
3032
"""
33+
if not formatter:
34+
body.pop("code")
35+
body.pop("error")
36+
body["id"] = body["id"].split("/", 1)[-1]
37+
return body
38+
3139
result = {"id": body["id"].split("/", 1)[-1], "fields": body["fields"]}
3240
if "type" in body:
3341
result["type"] = body["type"]

0 commit comments

Comments
 (0)