MCPcopy
hub / github.com/msiemens/tinydb / _get_next_id

Method _get_next_id

tinydb/table.py:663–696  ·  view source on GitHub ↗

Return the ID for a newly inserted document.

(self)

Source from the content-addressed store, hash-verified

661 yield self.document_class(doc, self.document_id_class(doc_id))
662
663 def _get_next_id(self):
664 """
665 Return the ID for a newly inserted document.
666 """
667
668 # If we already know the next ID
669 if self._next_id is not None:
670 next_id = self._next_id
671 self._next_id = next_id + 1
672
673 return next_id
674
675 # Determine the next document ID by finding out the max ID value
676 # of the current table documents
677
678 # Read the table documents
679 table = self._read_table()
680
681 # If the table is empty, set the initial ID
682 if not table:
683 next_id = 1
684 self._next_id = next_id + 1
685
686 return next_id
687
688 # Determine the next ID based on the maximum ID that's currently in use
689 max_id = max(self.document_id_class(i) for i in table.keys())
690 next_id = max_id + 1
691
692 # The next ID we will return AFTER this call needs to be larger than
693 # the current next ID we calculated
694 self._next_id = next_id + 1
695
696 return next_id
697
698 def _read_table(self) -> Dict[str, Mapping]:
699 """

Callers 5

insertMethod · 0.95
updaterMethod · 0.95
test_next_idFunction · 0.80
test_truncate_tableFunction · 0.80
test_lastid_after_openFunction · 0.80

Calls 1

_read_tableMethod · 0.95

Tested by 3

test_next_idFunction · 0.64
test_truncate_tableFunction · 0.64
test_lastid_after_openFunction · 0.64