From dc5da0a7b74864d1515aa111fd8265e50b1ad790 Mon Sep 17 00:00:00 2001 From: Joel Thomas Date: Thu, 11 Jun 2026 16:39:47 +0530 Subject: [PATCH] changes --- .../datastores/DataStore.cpp | 46 ++++++++++ .../datastores/DataStore.h | 90 +++++++++++++++++++ .../datastores/sharedmemory/RecordState.h | 3 +- .../utilities/Config.h | 1 + 4 files changed, 138 insertions(+), 2 deletions(-) diff --git a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/DataStore.cpp b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/DataStore.cpp index 0efc02d..c21173c 100644 --- a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/DataStore.cpp +++ b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/DataStore.cpp @@ -11,6 +11,7 @@ Date: 19-May-2026 #include "DataStore.h" #include "Config.h" #include "SerializedRecords.h" +#include "FileHelper.h" DataStore::DataStore() : m_globalMutex(NULL) {} @@ -37,6 +38,7 @@ bool DataStore::initialize() { return false; } + util::ensureDirectoryExists(config::file::DIRECTORY); m_users.fileName = config::file::USER_FILE; m_users.recordSize = sizeof(SerializedUser); m_notifications.fileName = config::file::NOTIFICATION_FILE; @@ -479,4 +481,48 @@ Returns: bool DataStore::unlockDataStore() { return false; +} + +/* +Function: lockDataStore +Description: Acquires the datastore mutex, providing + exclusive access to the shared-memory + datastore. This function blocks until + the mutex becomes available or an error + occurs. +Parameter: None +Return type: + bool + - true : Mutex successfully acquired. + - false : Failed to acquire the mutex. +*/ +bool DataStore::lockDataStore() +{ + if (m_globalMutex == NULL) + { + return false; + } + DWORD result = WaitForSingleObject(m_globalMutex, INFINITE); + return result == WAIT_OBJECT_0; +} + +/* +Function: unlockDataStore +Description: Releases the datastore mutex after a + successful call to lockDataStore(), + allowing other processes to access the + shared-memory datastore. +Parameter: None +Return type: + bool + - true : Mutex successfully released. + - false : Failed to release the mutex. +*/ +bool DataStore::unlockDataStore() +{ + if (m_globalMutex == NULL) + { + return false; + } + return ReleaseMutex(m_globalMutex) != 0; } \ No newline at end of file diff --git a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/DataStore.h b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/DataStore.h index a0d6ae0..f1dcd2e 100644 --- a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/DataStore.h +++ b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/DataStore.h @@ -79,3 +79,93 @@ private: template void saveRecords(MappingInfo& mapping, util::Map>& records); }; + +/* +Function: loadRecords +Description: Loads all serialized records from the specified + shared-memory mapping, deserializes them into + application objects, and returns them as a map + of tracked records. Each loaded record is marked + as CLEAN and assigned its corresponding slot index + within the mapping so that future modifications + can be written back efficiently. +Parameter: + - mapping: Reference to the mapping containing the + serialized records. +Return type: + util::Map> + A map containing all loaded records keyed by + their unique identifier. +*/ +template +util::Map> DataStore::loadRecords(MappingInfo& mapping) +{ + util::Map> records; + SharedMemory::ensureLatestMapping(mapping); + size_t recordCount = SharedMemory::getRecordCount(mapping); + for (size_t index = 0; index < recordCount; ++index) + { + TSerialized* serialized = static_cast(SharedMemory::getRecordAddress(mapping,index)); + TObject* object = TObject::deserialize(*serialized); + TrackedRecord trackedRecord; + trackedRecord.data = object; + trackedRecord.state = RecordState::CLEAN; + trackedRecord.slotIndex = index; + records.insert(object->getId(), trackedRecord); + } + return records; +} + +/* +Function: saveRecords +Description: Persists all modified and newly added records + contained in the provided map to the specified + shared-memory mapping. Modified records overwrite + their existing slots, while new records are + appended to the end of the mapping. Records marked + as CLEAN are ignored. After persistence, all + temporary objects owned by the tracked records are + destroyed to release memory. +Parameter: + - mapping: Reference to the mapping where records are + stored. + - records: Map containing the records to be persisted. +Return type: void +*/ +template void DataStore::saveRecords(MappingInfo& mapping, util::Map>& records) +{ + SharedMemory::ensureLatestMapping(mapping); + for (int index = 0; index < records.getSize(); ++index) + { + TrackedRecord& record = records.getValueAt(index); + if (record.state == RecordState::CLEAN) + { + continue; + } + TSerialized serialized = record.data->serialize(); + if (record.state == RecordState::MODIFIED) + { + TSerialized* destination = static_cast(SharedMemory::getRecordAddress(mapping, record.slotIndex)); + if (destination) + { + *destination = serialized; + } + } + else if (record.state == RecordState::NEW_RECORD) + { + if (!SharedMemory::ensureCapacityForInsert(mapping)) + { + continue; + } + size_t recordCount = SharedMemory::getRecordCount(mapping); + TSerialized* destination = static_cast(SharedMemory::getRecordAddress(mapping, recordCount)); + *destination = serialized; + SharedMemory::setRecordCount(mapping, recordCount + 1); + } + } + for (int index = 0; index < records.getSize(); ++index) + { + delete records.getValueAt(index).data; + records.getValueAt(index).data = nullptr; + } +} \ No newline at end of file diff --git a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/sharedmemory/RecordState.h b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/sharedmemory/RecordState.h index f6487cd..8e3aadc 100644 --- a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/sharedmemory/RecordState.h +++ b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/sharedmemory/RecordState.h @@ -4,6 +4,5 @@ enum class RecordState : int { CLEAN, NEW_RECORD, - MODIFIED, - DELETED + MODIFIED }; \ No newline at end of file diff --git a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/utilities/Config.h b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/utilities/Config.h index bddaf61..2eef19d 100644 --- a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/utilities/Config.h +++ b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/utilities/Config.h @@ -29,6 +29,7 @@ namespace config namespace file { const size_t INITIAL_CAPACITY = 100; + constexpr const char* DIRECTORY = "files/"; constexpr const char* INVENTORYITEM_FILE = "files/InventoryItem.dat"; constexpr const char* USER_FILE = "files/User.dat"; constexpr const char* NOTIFICATION_FILE = "files/Notification.dat";