@@ -39,31 +39,44 @@ class FileAccessorCache
3939 void SetCacheSize (uint64_t size) { m_cacheSize = size; };
4040};
4141
42+ // Write log to be used in conjunction with `WeakFileAccessor` to re-apply written data to a "revived" file.
43+ struct FileAccessorWriteLog
44+ {
45+ // To persist writes to a file accessor being revived (within the lock() function)
46+ // we keep a list of writes that will be re-applied in the lock function.
47+ std::shared_mutex m_persistedMutex;
48+ std::unordered_map<size_t , uint64_t > m_persistedPointers;
49+
50+ FileAccessorWriteLog () = default ;
51+
52+ // Add the pointer to the persisted pointers.
53+ void AddPointer (size_t address, size_t pointer);
54+
55+ // Apply all logged writes to the given accessor.
56+ void ApplyWrites (MappedFileAccessor& accessor);
57+ };
58+
4259class WeakFileAccessor
4360{
4461 // Weak pointer to the mapped file accessor, once this is expired we will re-open.
4562 std::weak_ptr<MappedFileAccessor> m_weakPtr;
4663 // File path for re-opening if needed
4764 std::string m_filePath;
65+
66+ std::shared_ptr<FileAccessorWriteLog> m_writeLog;
67+
4868 // TODO: Store a weak_ptr/shared_ptr to FileAccessorCache? That way we dont access Global()
4969 // TODO: Only need to do the above if we want multiple caches.
5070public:
5171 explicit WeakFileAccessor (std::weak_ptr<MappedFileAccessor> weakPtr, std::string filePath)
5272 : m_weakPtr(std::move(weakPtr))
5373 , m_filePath(std::move(filePath))
74+ , m_writeLog(std::make_shared<FileAccessorWriteLog>())
5475 {}
5576
56- std::shared_ptr<MappedFileAccessor> lock ()
57- {
58- auto sharedPtr = m_weakPtr.lock ();
59- if (!sharedPtr)
60- {
61- // This will revive other weak pointers to the same shared ptr.
62- // Update the weak pointer to the newly created shared instance
63- m_weakPtr = FileAccessorCache::Global ().Open (m_filePath).m_weakPtr ;
64- sharedPtr = m_weakPtr.lock ();
65- }
66-
67- return sharedPtr;
68- }
77+ std::shared_ptr<MappedFileAccessor> lock ();
78+
79+ // Persists the written pointer within this weak file accessor.
80+ // This works as we expect the weak file accessor to be stored per virtual memory region.
81+ void WritePointer (size_t address, size_t pointer);
6982};
0 commit comments