42
42
from arango .response import Response
43
43
from arango .result import Result
44
44
from arango .typings import Fields , Headers , Json , Params
45
- from arango .utils import get_doc_id , is_none_or_int , is_none_or_str
45
+ from arango .utils import get_batches , get_doc_id , is_none_or_int , is_none_or_str
46
46
47
47
48
48
class Collection (ApiGroup ):
@@ -1934,7 +1934,8 @@ def import_bulk(
1934
1934
overwrite : Optional [bool ] = None ,
1935
1935
on_duplicate : Optional [str ] = None ,
1936
1936
sync : Optional [bool ] = None ,
1937
- ) -> Result [Json ]:
1937
+ batch_size : Optional [int ] = None ,
1938
+ ) -> Union [Result [Json ], List [Result [Json ]]]:
1938
1939
"""Insert multiple documents into the collection.
1939
1940
1940
1941
.. note::
@@ -1984,8 +1985,17 @@ def import_bulk(
1984
1985
:type on_duplicate: str
1985
1986
:param sync: Block until operation is synchronized to disk.
1986
1987
:type sync: bool | None
1988
+ :param batch_size: Split up **documents** into batches of max length
1989
+ **batch_size** and import them in a loop on the client side. If
1990
+ **batch_size** is specified, the return type of this method
1991
+ changes from a result object to a list of result objects.
1992
+ IMPORTANT NOTE: this parameter may go through breaking changes
1993
+ in the future where the return type may not be a list of result
1994
+ objects anymore. Use it at your own risk, and avoid
1995
+ depending on the return value if possible.
1996
+ :type batch_size: int
1987
1997
:return: Result of the bulk import.
1988
- :rtype: dict
1998
+ :rtype: dict | list[dict]
1989
1999
:raise arango.exceptions.DocumentInsertError: If import fails.
1990
2000
"""
1991
2001
documents = [self ._ensure_key_from_id (doc ) for doc in documents ]
@@ -2006,21 +2016,35 @@ def import_bulk(
2006
2016
if sync is not None :
2007
2017
params ["waitForSync" ] = sync
2008
2018
2009
- request = Request (
2010
- method = "post" ,
2011
- endpoint = "/_api/import" ,
2012
- data = documents ,
2013
- params = params ,
2014
- write = self .name ,
2015
- )
2016
-
2017
2019
def response_handler (resp : Response ) -> Json :
2018
2020
if resp .is_success :
2019
2021
result : Json = resp .body
2020
2022
return result
2021
2023
raise DocumentInsertError (resp , request )
2022
2024
2023
- return self ._execute (request , response_handler )
2025
+ if batch_size is None :
2026
+ request = Request (
2027
+ method = "post" ,
2028
+ endpoint = "/_api/import" ,
2029
+ data = documents ,
2030
+ params = params ,
2031
+ write = self .name ,
2032
+ )
2033
+
2034
+ return self ._execute (request , response_handler )
2035
+ else :
2036
+ results = []
2037
+ for batch in get_batches (documents , batch_size ):
2038
+ request = Request (
2039
+ method = "post" ,
2040
+ endpoint = "/_api/import" ,
2041
+ data = batch ,
2042
+ params = params ,
2043
+ write = self .name ,
2044
+ )
2045
+ results .append (self ._execute (request , response_handler ))
2046
+
2047
+ return results
2024
2048
2025
2049
2026
2050
class StandardCollection (Collection ):
0 commit comments