Insert multiple documents into the table. :param documents: an Iterable of documents to insert :returns: a list containing the inserted documents' IDs
(self, documents: Iterable[Mapping])
| 179 | return doc_id |
| 180 | |
| 181 | def insert_multiple(self, documents: Iterable[Mapping]) -> List[int]: |
| 182 | """ |
| 183 | Insert multiple documents into the table. |
| 184 | |
| 185 | :param documents: an Iterable of documents to insert |
| 186 | :returns: a list containing the inserted documents' IDs |
| 187 | """ |
| 188 | doc_ids = [] |
| 189 | |
| 190 | def updater(table: dict): |
| 191 | for document in documents: |
| 192 | |
| 193 | # Make sure the document implements the ``Mapping`` interface |
| 194 | if not isinstance(document, Mapping): |
| 195 | raise ValueError('Document is not a Mapping') |
| 196 | |
| 197 | if isinstance(document, self.document_class): |
| 198 | # Check if document does not override an existing document |
| 199 | if document.doc_id in table: |
| 200 | raise ValueError( |
| 201 | f'Document with ID {str(document.doc_id)} ' |
| 202 | f'already exists' |
| 203 | ) |
| 204 | |
| 205 | # Store the doc_id, so we can return all document IDs |
| 206 | # later. Then save the document with its doc_id and |
| 207 | # skip the rest of the current loop |
| 208 | doc_id = document.doc_id |
| 209 | doc_ids.append(doc_id) |
| 210 | table[doc_id] = dict(document) |
| 211 | continue |
| 212 | |
| 213 | # Generate new document ID for this document |
| 214 | # Store the doc_id, so we can return all document IDs |
| 215 | # later, then save the document with the new doc_id |
| 216 | doc_id = self._get_next_id() |
| 217 | doc_ids.append(doc_id) |
| 218 | table[doc_id] = dict(document) |
| 219 | |
| 220 | # See below for details on ``Table._update`` |
| 221 | self._update_table(updater) |
| 222 | |
| 223 | return doc_ids |
| 224 | |
| 225 | def all(self) -> List[Document]: |
| 226 | """ |