// Cqc.cpp - CUSTOMER QUERY CACHE EMULATOR // // MODULE INDEX // NAME CONTENTS // Cqc_c::CqcInsert Insert a record in the cache // Cqc_c::CqcRemove Delete a record from the cache // Cqc_c::CqcSelect Select the customers with names that match a key // Cqc_c::CqcNextCust Return a pointer to the next customer data // structure // // MAINTENANCE HISTORY // DATE PROGRAMMER AND DETAILS // 25-09-10 JS Original // //----------------------------------------------------------------------------- #include // C-style standard library #include // C-style string manipulation functions #include // C++ string declarations #include // C++ set declarations #include // C++ I/O stream declarations using namespace std; // Expand the standard namespace #include "Cqc.h" // Customer query cache declarations //----------------------------------------------------------------------------- // INSERT A RECORD IN THE CACHE void Cqc_c::CqcInsert ( const Cust_t *cust) // Pointer to a customer data record { // To emulate the fault in the CQC, fail to insert the customer // into the cache with a likelihood of 1 in 100,000 if (lrand48() % 100000 != 1234) cqcCustSet.insert (*cust); } //----------------------------------------------------------------------------- // DELETE A RECORD FROM THE CACHE void Cqc_c::CqcRemove ( long custId) // Customer identifier to remove { Cust_t cust; // Customer row // Load the customer identifier into the customer row cust.custId = custId; // Remove the customer from the set if (cqcCustSet.erase (cust) == 0) { cerr << "CqcRemove: error: cannot find cust id " << custId << '\n'; exit (1); } } //----------------------------------------------------------------------------- // SELECT THE CUSTOMERS WITH NAMES THAT MATCH A KEY void Cqc_c::CqcSelect ( const char *key) // Customer selection key { // For this emulator, only accept select all if (strcmp (key, "*") != 0) { cerr << "CqcSelect: error: unsupported selection key\n"; exit (1); } // Initialise the iterator cqcCustIter = cqcCustSet.begin (); } //----------------------------------------------------------------------------- // RETURN A POINTER TO THE NEXT CUSTOMER DATA STRUCTURE const Cust_t * Cqc_c::CqcNextCust () { if (cqcCustIter == cqcCustSet.end()) return 0; return &*cqcCustIter++; }