@@ -53,18 +53,18 @@ class DummyParent : public CollectionParent {
5353 {
5454 return m_obj;
5555 }
56+ uint32_t parent_version () const noexcept final
57+ {
58+ return 0 ;
59+ }
5660
5761protected:
5862 Obj m_obj;
5963 ref_type m_ref;
60- UpdateStatus update_if_needed_with_status () const final
64+ UpdateStatus update_if_needed () const final
6165 {
6266 return UpdateStatus::Updated;
6367 }
64- bool update_if_needed () const final
65- {
66- return true ;
67- }
6868 ref_type get_collection_ref (Index, CollectionType) const final
6969 {
7070 return m_ref;
@@ -255,6 +255,7 @@ class CollectionBase : public Collection {
255255 CollectionBase& operator =(CollectionBase&&) noexcept = default ;
256256
257257 void validate_index (const char * msg, size_t index, size_t size) const ;
258+ static UpdateStatus do_init_from_parent (BPlusTreeBase* tree, ref_type ref, bool allow_create);
258259};
259260
260261inline std::string_view collection_type_name (CollectionType col_type, bool uppercase = false )
@@ -492,7 +493,7 @@ class CollectionBaseImpl : public Interface, protected ArrayParent {
492493 if (m_parent) {
493494 try {
494495 // Update the parent. Will throw if parent is not existing.
495- switch (m_parent->update_if_needed_with_status ()) {
496+ switch (m_parent->update_if_needed ()) {
496497 case UpdateStatus::Updated:
497498 // Make sure to update next time around
498499 m_content_version = 0 ;
@@ -524,7 +525,7 @@ class CollectionBaseImpl : public Interface, protected ArrayParent {
524525 {
525526 try {
526527 // `has_changed()` sneakily modifies internal state.
527- update_if_needed_with_status ();
528+ update_if_needed ();
528529 if (m_last_content_version != m_content_version) {
529530 m_last_content_version = m_content_version;
530531 return true ;
@@ -573,14 +574,12 @@ class CollectionBaseImpl : public Interface, protected ArrayParent {
573574 Obj m_obj_mem;
574575 std::shared_ptr<CollectionParent> m_col_parent;
575576 CollectionParent::Index m_index;
576- mutable size_t m_my_version = 0 ;
577577 ColKey m_col_key;
578- bool m_nullable = false ;
579-
580578 mutable uint_fast64_t m_content_version = 0 ;
581-
582579 // Content version used by `has_changed()`.
583580 mutable uint_fast64_t m_last_content_version = 0 ;
581+ mutable uint32_t m_parent_version = 0 ;
582+ bool m_nullable = false ;
584583
585584 CollectionBaseImpl () = default ;
586585 CollectionBaseImpl (const CollectionBaseImpl& other)
@@ -650,13 +649,14 @@ class CollectionBaseImpl : public Interface, protected ArrayParent {
650649
651650 UpdateStatus get_update_status () const
652651 {
653- UpdateStatus status = m_parent ? m_parent->update_if_needed_with_status () : UpdateStatus::Detached;
652+ UpdateStatus status = m_parent ? m_parent->update_if_needed () : UpdateStatus::Detached;
654653
655654 if (status != UpdateStatus::Detached) {
656655 auto content_version = m_alloc->get_content_version ();
657- if (content_version != m_content_version || m_my_version != m_parent->m_parent_version ) {
656+ auto parent_version = m_parent->parent_version ();
657+ if (content_version != m_content_version || m_parent_version != parent_version) {
658658 m_content_version = content_version;
659- m_my_version = m_parent-> m_parent_version ;
659+ m_parent_version = parent_version ;
660660 status = UpdateStatus::Updated;
661661 }
662662 }
@@ -667,18 +667,14 @@ class CollectionBaseImpl : public Interface, protected ArrayParent {
667667 // / Refresh the parent object (if needed) and compare version numbers.
668668 // / Return true if the collection should initialize from parent
669669 // / Throws if the owning object no longer exists.
670- bool should_update ()
670+ bool should_update () const
671671 {
672672 check_parent ();
673- bool changed = m_parent->update_if_needed (); // Throws if the object does not exist.
674- auto content_version = m_alloc->get_content_version ();
675-
676- if (changed || content_version != m_content_version || m_my_version != m_parent->m_parent_version ) {
677- m_content_version = content_version;
678- m_my_version = m_parent->m_parent_version ;
679- return true ;
673+ auto status = get_update_status ();
674+ if (status == UpdateStatus::Detached) {
675+ throw StaleAccessor (" Parent no longer exists" );
680676 }
681- return false ;
677+ return status == UpdateStatus::Updated ;
682678 }
683679
684680 void bump_content_version ()
@@ -796,7 +792,7 @@ class CollectionBaseImpl : public Interface, protected ArrayParent {
796792 // /
797793 // / If no change has happened to the data, this function returns
798794 // / `UpdateStatus::NoChange`, and the caller is allowed to not do anything.
799- virtual UpdateStatus update_if_needed_with_status () const = 0;
795+ virtual UpdateStatus update_if_needed () const = 0;
800796};
801797
802798namespace _impl {
@@ -884,7 +880,7 @@ class ObjCollectionBase : public Interface, public _impl::ObjListProxy {
884880 // / `BPlusTree<T>`.
885881 virtual BPlusTree<ObjKey>* get_mutable_tree () const = 0;
886882
887- // / Implements update_if_needed() in a way that ensures the consistency of
883+ // / Implements ` update_if_needed()` in a way that ensures the consistency of
888884 // / the unresolved list. Derived classes should call this instead of calling
889885 // / `update_if_needed()` on their inner accessor.
890886 UpdateStatus update_if_needed () const
0 commit comments