home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / db02_src.zip / cache.cc < prev    next >
C/C++ Source or Header  |  1993-11-05  |  6KB  |  270 lines

  1. /*************************************************************************
  2.  * Source Id :
  3.  *
  4.  * $Id: cache.cc,v 1.13 1993/06/23 05:21:22 kevinl Exp $
  5.  *-------------------------------------------------------------------------
  6.  * Project Notes :
  7.  *
  8.  *  Diamond Base
  9.  *  ============
  10.  *      A solid database implementation, spurred on by the continuing
  11.  *  Metal (Lead) Base saga.
  12.  *
  13.  *  Project Team :
  14.  *        A. Davison
  15.  *        K. Lentin
  16.  *        D. Platt
  17.  *
  18.  *    Project Commenced : 05-02-1993
  19.  *
  20.  *-------------------------------------------------------------------------
  21.  *  Module Notes :
  22.  *
  23.  *  Record caching for dbobj
  24.  *
  25.  *
  26.  *  Original Author : Kev
  27.  *
  28.  *-------------------------------------------------------------------------
  29.  * Revision History:
  30.  *
  31.  * $Log: cache.cc,v $
  32. // Revision 1.13  1993/06/23  05:21:22  kevinl
  33. // Mallocs are now in angular brackets
  34. //
  35. // Revision 1.12  1993/05/26  01:01:39  kevinl
  36. // MALLOC_H_MISSING
  37. //
  38. // Revision 1.11  1993/05/11  14:44:50  kevinl
  39. // Added version number output
  40. //
  41. // Revision 1.10  1993/05/06  04:00:19  kevinl
  42. // SASC define for malloc.h
  43. //
  44. // Revision 1.9  1993/05/01  14:30:47  kevinl
  45. // added a missing bool cast
  46. //
  47. // Revision 1.8  1993/04/25  10:11:47  kevinl
  48. // Added freeCache to allow destructing/reconstructing properly (in case)
  49. // Comments!
  50. //
  51. // Revision 1.7  1993/04/15  04:21:52  kevinl
  52. // Moved malloc.h
  53. //
  54. // Revision 1.6  1993/04/09  13:00:29  kevinl
  55. // Stats can be called from diaRel now.
  56. //
  57. // Revision 1.5  1993/04/08  11:23:11  kevinl
  58. // Fixed NaN% in destructor
  59. //
  60. // Revision 1.4  1993/04/08  05:26:40  kevinl
  61. // FGixed some memory leaks
  62. //
  63. // Revision 1.3  1993/04/08  00:36:24  kevinl
  64. // removed default arguments
  65. //
  66. // Revision 1.2  1993/04/07  06:38:57  kevinl
  67. // Made cache stats part of DEBUG block
  68. //
  69. // Revision 1.1  1993/04/05  00:00:08  kevinl
  70. // Initial revision
  71. //
  72.  **************************************************************************/
  73.  
  74. #ifndef MALLOC_H_MISSING
  75. #include <malloc.h>
  76. #endif
  77. #include <iostream.h>
  78. #include <cache.h>
  79.  
  80. char* cache::verStr(void)
  81. {
  82.     return "$Id: cache.cc,v 1.13 1993/06/23 05:21:22 kevinl Exp $";
  83. }
  84.  
  85. // Create constructor
  86. // Creates a cahce with cSize entries for bSize entities
  87.  
  88. cache::cache(long bSize, long cSize)
  89. {
  90.     createCache(bSize, cSize);
  91. }
  92.  
  93. // Nothing constructor
  94.  
  95. cache::cache(void)
  96. {
  97.     data = 0;
  98. }
  99.  
  100. // Destructor. Make sure the memory is gone
  101.  
  102. cache::~cache()
  103. {
  104. #ifdef DEBUG
  105.     cout << "Cache dieing...";
  106.     stats();
  107. #endif
  108.     freeCache();
  109. }
  110.  
  111. // Frees all the cache memory
  112.  
  113. void
  114. cache::freeCache(void)
  115. {
  116.     // Free the blocks allocated
  117.     for (int i=0; i < cacheSize; i++)
  118.         if (data[i].block)
  119.             delete data[i].block;
  120.  
  121.     // And the array itself
  122.     delete data;
  123.     data = 0;
  124. }
  125.  
  126. // Output some stats for the concerned user
  127.  
  128. void
  129. cache::stats(void)
  130. {
  131.     cout << attempts << " attempts, " << hits << " hits = " << (attempts?(hits / (double)attempts * 100.00):0) << "%" << endl;
  132.     cout << "        " << writes << " writes and " << dispose << " disposals" << endl;
  133. }
  134.  
  135. // Let's make a cache
  136.  
  137. void
  138. cache::createCache(long bSize, long cSize)
  139. {
  140.     // Are we replacing an old one?
  141.     if (data)
  142.         freeCache();
  143.  
  144.     // Make space for the array, setup the cache variables and init the stats
  145.     data = new cacheBlock[cacheSize = cSize];
  146.     blockSize = bSize;
  147.     curSize = 0;
  148.     attempts = 0;
  149.     hits = 0;
  150.     writes = 0;
  151.     dispose = 0;
  152.     for (int i=0; i < cacheSize; i++)
  153.         data[i].block = 0;
  154. }
  155.  
  156. // whereCache:
  157. // Find an entry in the cache
  158.  
  159. long
  160. cache::whereCache(long id)
  161. {
  162.     //cerr << "Searching for " << id << " in " << curSize << " entries" << endl;
  163.     for (int i=0; i < curSize; i++)
  164.         if (data[i].id == id)
  165.             return i;
  166.     return -1;
  167. }
  168.  
  169. // inCache:
  170. // A boolean front end to whereCache
  171.  
  172. bool
  173. cache::inCache(long id)
  174. {
  175.     return (bool)(whereCache(id) != -1);
  176. }
  177.  
  178. // getCache:
  179. // Find an entry in the cache and if it is found, move it to the front
  180.  
  181. bool
  182. cache::getCache(char* theData, long id)
  183. {
  184.     // Where is it?
  185.     long where = whereCache(id);
  186.  
  187.     // Keep the stats
  188.     attempts++;
  189.  
  190.     if (where == -1) 
  191.     {
  192.         //cerr << "Nup, not there!" << endl;
  193.         return false;
  194.     }
  195.  
  196.     //cerr << "Found at entry " << where << endl;
  197.  
  198.     // Found, so copy it into the data area.
  199.     bcopy(data[where].block, theData, blockSize);
  200.  
  201.     // If the block was not at the front, make it so.
  202.     if (where != 0)
  203.     {
  204.         // Save the block pointer
  205.         cacheBlock temp = data[where];
  206.  
  207.         // Move everyone else down, thus taking up the where spot
  208.         bcopy((char*)data, (char*)(data+1), where * sizeof(cacheBlock));
  209.  
  210.         // Put the stored one up front
  211.         data[0] = temp;
  212.     }
  213.  
  214.     // Stats
  215.     hits++;
  216.     return true;
  217. }
  218.  
  219. // putCache:
  220. // Write an entry into the cache. If it is already there move it to the
  221. // front, otherwise insert it at the front.
  222.  
  223. void
  224. cache::putCache(char* theData, long id)
  225. {
  226.     // We got this one?
  227.     long where = whereCache(id);
  228.  
  229.     // Stats
  230.     writes++;
  231.  
  232.     if (where == -1) // We don't have it.
  233.     {
  234.         // If the cache currently has space, make a new spot.
  235.         if (curSize < cacheSize)
  236.         {
  237.             // Create a new spot on the end and put us in there.
  238.             // where will be used to push it up later
  239.             where = curSize++;
  240.             data[where].block = new char[blockSize];
  241.             data[where].id = id;
  242.             //cerr << "Created block " << where << " with id " << id << endl;
  243.         }
  244.         else
  245.         {
  246.             // Nuke the last spot
  247.             where = cacheSize - 1;
  248.             data[where].id = id;
  249.             dispose++;
  250.             //cerr << "Putting it in block " << where << " with id " << id << endl;
  251.         }
  252.     }
  253.  
  254.     // where now points to the spot to put it in
  255.  
  256.     if (where != 0)
  257.     {
  258.         // If it's in the middle, move everybody down one and push us to 
  259.         // the front of the cache.
  260.         //cerr << "Moving " << where << " blocks up 1" << endl;
  261.         cacheBlock temp = data[where];
  262.         bcopy((char*)data, (char*)(data+1), where * sizeof(cacheBlock));
  263.         data[where = 0] = temp;
  264.     }
  265.  
  266.     // where will equal 0 now. copy the data in
  267.  
  268.     bcopy(theData, data[where].block, blockSize);
  269. }
  270.