Files
2026-06-11 16:39:47 +05:30

528 lines
15 KiB
C++

/*
File: DataStore.cpp
Description: Implements the DataStore class which provides a centralized singleton repository
for managing system data in the Vehicle Service Management System.
Includes accessors for users, services, combo packages, service bookings,
job cards, inventory items, invoices, and payments.
Author: Trenser
Date: 19-May-2026
*/
#include "DataStore.h"
#include "Config.h"
#include "SerializedRecords.h"
#include "FileHelper.h"
DataStore::DataStore() :
m_globalMutex(NULL) {}
/*
Function: initialize
Description: Initializes the shared-memory datastore.
Creates or opens the global datastore mutex,
configures all mapping metadata, and creates
or opens the file mappings used to persist
application data. After successful completion,
all datastore files are mapped into memory and
ready for use by the application.
Parameter: None
Return type: bool
- true : Initialization completed successfully.
- false : Failed to create the mutex or open
one or more file mappings.
*/
bool DataStore::initialize()
{
m_globalMutex = CreateMutexA(NULL, FALSE, "VehicleServiceSystemMutex");
if (m_globalMutex == NULL)
{
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;
m_notifications.recordSize = sizeof(SerializedNotification);
m_services.fileName = config::file::SERVICE_FILE;
m_services.recordSize = sizeof(SerializedService);
m_comboPackages.fileName = config::file::COMBOPACKAGE_FILE;
m_comboPackages.recordSize = sizeof(SerializedComboPackage);
m_inventoryItems.fileName = config::file::INVENTORYITEM_FILE;
m_inventoryItems.recordSize = sizeof(SerializedInventoryItem);
m_serviceBookings.fileName = config::file::SERVICEBOOKING_FILE;
m_serviceBookings.recordSize = sizeof(SerializedServiceBooking);
m_jobCards.fileName = config::file::JOBCARD_FILE;
m_jobCards.recordSize = sizeof(SerializedJobCard);
m_invoices.fileName = config::file::INVOICE_FILE;
m_invoices.recordSize = sizeof(SerializedInvoice);
m_serviceManagementObservers.fileName = config::file::SERVICEMANAGEMENTOBSERVERS;
m_serviceManagementObservers.recordSize = sizeof(SerializedObserver);
m_paymentManagementObservers.fileName = config::file::PAYMENTMANAGEMENTOBSERVERS;
m_paymentManagementObservers.recordSize = sizeof(SerializedObserver);
m_inventoryManagementObservers.fileName = config::file::INVENTORYMANAGEMENTOBSERVERS;
m_inventoryManagementObservers.recordSize = sizeof(SerializedObserver);
if (!SharedMemory::createOrOpenMapping(m_users))
{
return false;
}
if (!SharedMemory::createOrOpenMapping(m_notifications))
{
return false;
}
if (!SharedMemory::createOrOpenMapping(m_services))
{
return false;
}
if (!SharedMemory::createOrOpenMapping(m_comboPackages))
{
return false;
}
if (!SharedMemory::createOrOpenMapping(m_inventoryItems))
{
return false;
}
if (!SharedMemory::createOrOpenMapping(m_serviceBookings))
{
return false;
}
if (!SharedMemory::createOrOpenMapping(m_jobCards))
{
return false;
}
if (!SharedMemory::createOrOpenMapping(m_invoices))
{
return false;
}
if (!SharedMemory::createOrOpenMapping(m_payments))
{
return false;
}
if (!SharedMemory::createOrOpenMapping(m_serviceManagementObservers))
{
return false;
}
if (!SharedMemory::createOrOpenMapping(m_paymentManagementObservers))
{
return false;
}
if (!SharedMemory::createOrOpenMapping(m_inventoryManagementObservers))
{
return false;
}
return true;
}
/*
Function: shutdown
Description: Releases all shared-memory resources owned by the
datastore. Closes every file mapping, unmaps all
mapped views, and releases the datastore mutex.
After this call, the datastore must be initialized
again before use.
Parameter: None
Return type: void
*/
void DataStore::shutdown()
{
SharedMemory::closeMapping(m_users);
SharedMemory::closeMapping(m_notifications);
SharedMemory::closeMapping(m_services);
SharedMemory::closeMapping(m_comboPackages);
SharedMemory::closeMapping(m_inventoryItems);
SharedMemory::closeMapping(m_serviceBookings);
SharedMemory::closeMapping(m_jobCards);
SharedMemory::closeMapping(m_invoices);
SharedMemory::closeMapping(m_payments);
SharedMemory::closeMapping(m_serviceManagementObservers);
SharedMemory::closeMapping(m_paymentManagementObservers);
SharedMemory::closeMapping(m_inventoryManagementObservers);
if (m_globalMutex != NULL)
{
CloseHandle(m_globalMutex);
m_globalMutex = NULL;
}
}
/*
Function: getInstance
Description: Provides a singleton instance of the DataStore class.
Parameters:
- None
Returns:
- Reference to the single DataStore instance.
*/
DataStore& DataStore::getInstance()
{
static DataStore dataStore;
return dataStore;
}
/*
Function: getUsers
Description: Retrieves all user records from the datastore.
Parameters:
- None
Returns:
- util::Map<std::string, TrackedRecord<User>>: Collection of user records
*/
util::Map<std::string, TrackedRecord<User>> DataStore::getUsers()
{
return util::Map<std::string, TrackedRecord<User>>();
}
/*
Function: getNotifications
Description: Retrieves all notification records from the datastore.
Parameters:
- None
Returns:
- util::Map<std::string, TrackedRecord<Notification>>: Collection of notification records
*/
util::Map<std::string, TrackedRecord<Notification>> DataStore::getNotifications()
{
return util::Map<std::string, TrackedRecord<Notification>>();
}
/*
Function: getServices
Description: Retrieves all service records from the datastore.
Parameters:
- None
Returns:
- util::Map<std::string, TrackedRecord<Service>>: Collection of service records
*/
util::Map<std::string, TrackedRecord<Service>> DataStore::getServices()
{
return util::Map<std::string, TrackedRecord<Service>>();
}
/*
Function: getComboPackages
Description: Retrieves all combo package records from the datastore.
Parameters:
- None
Returns:
- util::Map<std::string, TrackedRecord<ComboPackage>>: Collection of combo package records
*/
util::Map<std::string, TrackedRecord<ComboPackage>> DataStore::getComboPackages()
{
return util::Map<std::string, TrackedRecord<ComboPackage>>();
}
/*
Function: getInventoryItems
Description: Retrieves all inventory item records from the datastore.
Parameters:
- None
Returns:
- util::Map<std::string, TrackedRecord<InventoryItem>>: Collection of inventory item records
*/
util::Map<std::string, TrackedRecord<InventoryItem>> DataStore::getInventoryItems()
{
return util::Map<std::string, TrackedRecord<InventoryItem>>();
}
/*
Function: getServiceBookings
Description: Retrieves all service booking records from the datastore.
Parameters:
- None
Returns:
- util::Map<std::string, TrackedRecord<ServiceBooking>>: Collection of service booking records
*/
util::Map<std::string, TrackedRecord<ServiceBooking>> DataStore::getServiceBookings()
{
return util::Map<std::string, TrackedRecord<ServiceBooking>>();
}
/*
Function: getJobCards
Description: Retrieves all job card records from the datastore.
Parameters:
- None
Returns:
- util::Map<std::string, TrackedRecord<JobCard>>: Collection of job card records
*/
util::Map<std::string, TrackedRecord<JobCard>> DataStore::getJobCards()
{
return util::Map<std::string, TrackedRecord<JobCard>>();
}
/*
Function: getInvoices
Description: Retrieves all invoice records from the datastore.
Parameters:
- None
Returns:
- util::Map<std::string, TrackedRecord<Invoice>>: Collection of invoice records
*/
util::Map<std::string, TrackedRecord<Invoice>> DataStore::getInvoices()
{
return util::Map<std::string, TrackedRecord<Invoice>>();
}
/*
Function: getPayments
Description: Retrieves all payment records from the datastore.
Parameters:
- None
Returns:
- util::Map<std::string, TrackedRecord<Payment>>: Collection of payment records
*/
util::Map<std::string, TrackedRecord<Payment>> DataStore::getPayments()
{
return util::Map<std::string, TrackedRecord<Payment>>();
}
/*
Function: getServiceManagementObservers
Description: Retrieves all service management observer records from the datastore.
Parameters:
- None
Returns:
- util::Map<std::string, TrackedRecord<std::string>>: Collection of observer records
*/
util::Map<std::string, TrackedRecord<std::string>> DataStore::getServiceManagementObservers()
{
return util::Map<std::string, TrackedRecord<std::string>>();
}
/*
Function: getPaymentManagementObservers
Description: Retrieves all payment management observer records from the datastore.
Parameters:
- None
Returns:
- util::Map<std::string, TrackedRecord<std::string>>: Collection of observer records
*/
util::Map<std::string, TrackedRecord<std::string>> DataStore::getPaymentManagementObservers()
{
return util::Map<std::string, TrackedRecord<std::string>>();
}
/*
Function: getInventoryManagementObservers
Description: Retrieves all inventory management observer records from the datastore.
Parameters:
- None
Returns:
- util::Map<std::string, TrackedRecord<std::string>>: Collection of observer records
*/
util::Map<std::string, TrackedRecord<std::string>> DataStore::getInventoryManagementObservers()
{
return util::Map<std::string, TrackedRecord<std::string>>();
}
/*
Function: saveUsers
Description: Persists all user records to the datastore.
Parameters:
- users: util::Map<std::string, TrackedRecord<User>>&, collection of user records
Returns:
- None
*/
void DataStore::saveUsers(util::Map<std::string, TrackedRecord<User>>& users)
{
}
/*
Function: saveNotifications
Description: Persists all notification records to the datastore.
Parameters:
- notifications: util::Map<std::string, TrackedRecord<Notification>>&, collection of notification records
Returns:
- None
*/
void DataStore::saveNotifications(util::Map<std::string, TrackedRecord<Notification>>& notifications)
{
}
/*
Function: saveServices
Description: Persists all service records to the datastore.
Parameters:
- services: util::Map<std::string, TrackedRecord<Service>>&, collection of service records
Returns:
- None
*/
void DataStore::saveServices(util::Map<std::string, TrackedRecord<Service>>& services)
{
}
/*
Function: saveComboPackages
Description: Persists all combo package records to the datastore.
Parameters:
- comboPackages: util::Map<std::string, TrackedRecord<ComboPackage>>&, collection of combo package records
Returns:
- None
*/
void DataStore::saveComboPackages(util::Map<std::string, TrackedRecord<ComboPackage>>& comboPackages)
{
}
/*
Function: saveInventoryItems
Description: Persists all inventory item records to the datastore.
Parameters:
- inventoryItems: util::Map<std::string, TrackedRecord<InventoryItem>>&, collection of inventory item records
Returns:
- None
*/
void DataStore::saveInventoryItems(util::Map<std::string, TrackedRecord<InventoryItem>>& inventoryItems)
{
}
/*
Function: saveServiceBookings
Description: Persists all service booking records to the datastore.
Parameters:
- bookings: util::Map<std::string, TrackedRecord<ServiceBooking>>&, collection of service booking records
Returns:
- None
*/
void DataStore::saveServiceBookings(util::Map<std::string, TrackedRecord<ServiceBooking>>& bookings)
{
}
/*
Function: saveJobCards
Description: Persists all job card records to the datastore.
Parameters:
- jobCards: util::Map<std::string, TrackedRecord<JobCard>>&, collection of job card records
Returns:
- None
*/
void DataStore::saveJobCards(util::Map<std::string, TrackedRecord<JobCard>>& jobCards)
{
}
/*
Function: saveInvoices
Description: Persists all invoice records to the datastore.
Parameters:
- invoices: util::Map<std::string, TrackedRecord<Invoice>>&, collection of invoice records
Returns:
- None
*/
void DataStore::saveInvoices(util::Map<std::string, TrackedRecord<Invoice>>& invoices)
{
}
/*
Function: savePayments
Description: Persists all payment records to the datastore.
Parameters:
- payments: util::Map<std::string, TrackedRecord<Payment>>&, collection of payment records
Returns:
- None
*/
void DataStore::savePayments(util::Map<std::string, TrackedRecord<Payment>>& payments)
{
}
/*
Function: saveServiceManagementObservers
Description: Persists all service management observer records to the datastore.
Parameters:
- observers: util::Map<std::string, TrackedRecord<std::string>>&, collection of observer records
Returns:
- None
*/
void DataStore::saveServiceManagementObservers(util::Map<std::string, TrackedRecord<std::string>>& observers)
{
}
/*
Function: savePaymentManagementObservers
Description: Persists all payment management observer records to the datastore.
Parameters:
- observers: util::Map<std::string, TrackedRecord<std::string>>&, collection of observer records
Returns:
- None
*/
void DataStore::savePaymentManagementObservers(util::Map<std::string, TrackedRecord<std::string>>& observers)
{
}
/*
Function: saveInventoryManagementObservers
Description: Persists all inventory management observer records to the datastore.
Parameters:
- observers: util::Map<std::string, TrackedRecord<std::string>>&, collection of observer records
Returns:
- None
*/
void DataStore::saveInventoryManagementObservers(util::Map<std::string, TrackedRecord<std::string>>& observers)
{
}
/*
Function: lockDataStore
Description: Acquires exclusive access to the datastore.
Parameters:
- None
Returns:
- bool: True if the datastore was successfully locked, otherwise false
*/
bool DataStore::lockDataStore()
{
return false;
}
/*
Function: unlockDataStore
Description: Releases exclusive access to the datastore.
Parameters:
- None
Returns:
- bool: True if the datastore was successfully unlocked, otherwise false
*/
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;
}