Hi, here are the classes I have ...
File chedCache.h
#ifndef CHED_CACHE_INCLUDE
#define CHED_CACHE_INCLUDE
#include <map>
#include "chedView.h"
#define CHED_CACHE_MAP map <chedView, chedView>
class chedCache
{
public:
//--------------------------------------------------------------
// Constructor and Destructor
//--------------------------------------------------------------
chedCache() { };
~chedCache() { };
//--------------------------------------------------------------
// Accessor Methods
//--------------------------------------------------------------
const char *find(const char *key, const int keySize);
//--------------------------------------------------------------
// Mutator Methods
//--------------------------------------------------------------
void insert(const char *key, const int keySize, const
char *value, const int valueSize);
private :
CHED_CACHE_MAP _myCache;
CHED_CACHE_MAP::const_iterator _myIter;
};
file chedView.h
#ifndef CHED_VIEW_INCLUDE
#define CHED_VIEW_INCLUDE
#define MINIMUM(A,B) ((A) < (B) ? (A) : (B))
class chedView
{
public:
//--------------------------------------------------------------
// Constructor and Destructor
//--------------------------------------------------------------
chedView() { _mySize = 0; _myView = NULL; }
chedView(const char *myView, const int mySize) :
_mySize(mySize)
{
_myView = new char[mySize];
memcpy(_myView, myView, mySize);
cout << "constrcted " << _myView << " of size
: " << _mySize << endl;
}
~chedView() { if (_myView != NULL) delete [] _myView; }
//--------------------------------------------------------------
// Accessor Methods
//--------------------------------------------------------------
const char *getView() const { return _myView; }
const int getSize() const { return _mySize; }
//--------------------------------------------------------------
// Operators
//--------------------------------------------------------------
bool operator==(const chedView &a) const
{
cout << "operator == comparing the following"
<< endl;
cout << "my View = " << _myView << endl;
cout << "input View = " << a.getView() << endl;
return _mySize==a._mySize && memcmp(_myView,
a.getView(), _mySize);
}
bool operator<(const chedView &a) const
{
cout << "operator < comparing the following" <<
endl;
cout << "my View = " << _myView << endl;
cout << "input View = " << a.getView() << endl;
cout << "memcmp = " << memcmp(_myView,
a.getView(), MINIMUM(_mySize, a._mySize)) << endl;
return memcmp(_myView, a.getView(),
MINIMUM(_mySize, a._mySize))<0;
}
bool operator>(const chedView &a) const
{
return a < *this;
}
//--------------------------------------------------------------
// Mutator Methods
//--------------------------------------------------------------
private :
char *_myView;
int _mySize;
};
#endif
file chedCache.c
#include "chedCache.h"
/************************************************** *****************************
*
* Method Name: chedCache::insert
*
* Input Parameters: const char *key, const char *value
*
* Summary: insert the key, value pair in the cache
*
* Return Values: none
*
************************************************** *****************************/
void chedCache::insert(const char *key, const int keySize, const char
*value, const int valueSize)
{
chedView myKey(key, keySize), myValue(value, valueSize);
_myCache[myKey] = myValue;
_myIter = _myCache.begin();
while (_myIter != _myCache.end())
{
printf("insert -> found %s and %s in the cache\n",
(_myIter->first).getView(), (_myIter->second).getView());
++_myIter;
}
}
/************************************************** *****************************
*
* Method Name: chedCache::find
*
* Input Parameters: const char *key
*
* Summary: insert the key, value pair in the cache
*
* Return Values: NULL if no cache hit and cache stored object if
cache hit
*
************************************************** *****************************/
const char *chedCache::find(const char *key, const int keySize)
{
_myIter = _myCache.begin();
while (_myIter != _myCache.end())
{
printf("find first -> found %s and %s in the cache\n",
(_myIter->first).getView(), (_myIter->second).getView());
++_myIter;
}
cout << "constructing key to be searched " << endl;
chedView myView(key, keySize);
printf("chedView myView in find is %s \n", myView.getView());
cout << "constructed key to be searched " << endl;
_myIter = _myCache.begin();
while (_myIter != _myCache.end())
{
printf("find second -> found %s and %s in the cache\n",
(_myIter->first).getView(), (_myIter->second).getView());
++_myIter;
}
_myIter = _myCache.find(myView);
if (_myIter == _myCache.end()) printf("no dice\n"); else
printf("found\n");
return
((_myIter = _myCache.find(chedView(key, keySize)))
== _myCache.end()) ? NULL :
(_myIter->second).getView();
}
and finally the main program
#include "chedCache.h"
#define APP_SUCCESS 0
#define APP_FAILURE 1
void printTime(const int iter, const bool start);
typedef struct mySendView {
char racn_bsln_cd[2];
mySendView() {
initview();
}
void initview() {
memset(this, 0, sizeof(mySendView));
}
const string getName() const { return "chra_sel_RACNBaselineS";
}
} mySendView;
typedef struct myRetnView {
char racn_base_dt[31];
myRetnView() {
initview();
}
void initview() {
memset(this, 0, sizeof(myRetnView));
}
const string getName() const { return "chra_sel_RACNBaselineR";
}
} myRetnView;
int getData(mySendView &, myRetnView &);
/************************************************** *******************
* Function: eaAppInterface::bus
*
* Description: <description of function>
*
* Parameters:
*
* Error Messages:
************************************************** *******************/
int eaAppInterface::bus( int argc, const char* argv[], const char*
envp[] )
{
//-----------------------------------
// did we get proper input parameters
//-----------------------------------
if (argc < 2)
{
eaLogMsg << setid("INVALID_FUNCTION_USAGE")
<< "edCache::bus" << "$BIN/edCache
totalIter" << endl;
return APP_FAILURE;
}
//----------------------------
// declare necessary variables
//----------------------------
mySendView selRACNBslnS;
myRetnView selRACNBslnR;
int totalIter = atoi(argv[1]);
chedCache myCache;
const char *myEntry;
//------------------------
// now comes the real work
//------------------------
for (int i=1; i<=totalIter; i++)
{
//-----------------------
// populate the send view
//-----------------------
if (i%2 == 0)
strncpy(selRACNBslnS.racn_bsln_cd, "D",
sizeof(selRACNBslnS.racn_bsln_cd) - 1 );
else
strncpy(selRACNBslnS.racn_bsln_cd, "I",
sizeof(selRACNBslnS.racn_bsln_cd) - 1 );
//-----------------------
// is this data in cache?
//-----------------------
cout << "searching for " << selRACNBslnS.racn_bsln_cd
<< " in cache for iteration " << i <<
endl;
myEntry = myCache.find((const char *)&selRACNBslnS,
sizeof(selRACNBslnS));
//-----------
// yes, it is
//-----------
if (myEntry != NULL)
{
cout << " ... found " << endl;
memcpy(&selRACNBslnR, myEntry,
sizeof(selRACNBslnR));
}
//-------------------
// not found in cache
//-------------------
else
{
cout << " ... not found " << endl;
//-------------------------------------------------
// hopefully we can retrieve data from the
database
//-------------------------------------------------
if (getData(selRACNBslnS, selRACNBslnR) !=
APP_SUCCESS)
{
eaLogMsg << setid("WHATEVER") <<
"edCache::bus failed" << endl;
return APP_FAILURE;
}
//---------------------------------------
// store this in the cache for future use
//---------------------------------------
cout << "inserting " <<
selRACNBslnR.racn_base_dt
<< " in cache for iteration "
<< i << endl;
myCache.insert(
(const char *)&selRACNBslnS,
sizeof(selRACNBslnS),
(const char *)&selRACNBslnR,
sizeof(selRACNBslnR));
}
//------------------------------------
// let's make sure everything was okay
//------------------------------------
cout << "Iteration #" << i
<< " yields " <<
selRACNBslnR.racn_base_dt << endl;
}
//----------------
// declare victory
//----------------
return APP_SUCCESS;
}
int getData(mySendView &selRACNBslnS, myRetnView &selRACNBslnR)
{
//-------------
// do something
//-------------
if (strcmp(selRACNBslnS.racn_bsln_cd, "D"))
strncpy(selRACNBslnR.racn_base_dt, "02/06/1999
06:00:00", 30);
else
strncpy(selRACNBslnR.racn_base_dt, "02/09/1999
06:00:00", 30);
//-----------------------
// everything is okay now
//-----------------------
return APP_SUCCESS;
}
When I run this with an input parameter of 2, here is the output I get.
searching for I in cache for iteration 1
constructing key to be searched
constrcted I of size : 2
chedView myView in find is I
constructed key to be searched
no dice
constrcted I of size : 2
... not found
inserting 02/06/1999 06:00:00 in cache for iteration 1
constrcted I of size : 2
constrcted 02/06/1999 06:00:00 of size : 31
insert -> found I and 02/06/1999 06:00:00 in the cache
Iteration #1 yields 02/06/1999 06:00:00
searching for D in cache for iteration 2
find first -> found I and 02/06/1999 06:00:00 in the cache
constructing key to be searched
constrcted D of size : 2
chedView myView in find is D
constructed key to be searched
find second -> found D and in the cache
operator < comparing the following
my View = D
input View = D
memcmp = 0
operator < comparing the following
my View = D
input View = D
memcmp = 0
found
constrcted D of size : 2
operator < comparing the following
my View = D
input View = D
memcmp = 0
operator < comparing the following
my View = D
input View = D
memcmp = 0
... found
Iteration #2 yields
My problem is when I am going for the 2nd iteration through the main
program, after calling the find function for the chedCache class, it's
member variable gets garbled and I get unexpected results ie for the
2nd iteration, I am not supposed to find that particular entry in the
cache but I do and get incorrect results.
PLEASE HELP.