home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / CONTRIB / MBASE / MBASE50.TAR / mbase / src / cache.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-02  |  2.9 KB  |  132 lines

  1. /*
  2.  * METALBASE 5.0
  3.  *
  4.  * Released October 1st, 1992 by Huan-Ti [ richid@owlnet.rice.edu ]
  5.  *                                       [ t-richj@microsoft.com ]
  6.  */
  7.  
  8. #define UTIL_C
  9. #include "mbase.h"
  10. #include "internal.h"
  11.  
  12. cache  mb_cache[MAX_CACHE];
  13. cache  mb_ctop;
  14. int    ncache = 0;
  15. cache *mb_clast = NULL;
  16. long   mb_nlast = 0L;
  17.  
  18. cache *mb_hash[MAX_CACHE];  /* Attempted */
  19.  
  20. /****************************************************************************/
  21.  
  22. cache *
  23. _read_cache (rel, rcd, idx)
  24. relation    *rel;
  25. long              rcd;
  26. int                    idx;
  27. {
  28.    cache *ptr, *end;
  29.    long   n;
  30.  
  31.    end = &mb_cache[ncache];  /* Can't check when ptr==end */
  32.  
  33.    if (mb_nlast == rcd && mb_clast != NULL)  /* Now in practice, we seem to */
  34.       {                                      /* query the same one twice    */
  35.       return mb_clast;                       /* in a row a lot, so here's   */
  36.       }                                      /* a hack to optimize for it.  */
  37.  
  38.    mb_nlast = rcd;
  39.  
  40.    if (rcd == 0L)
  41.       {
  42.       if (mb_ctop.num != 0L)
  43.          {
  44.          return (mb_clast = &mb_ctop);
  45.          }
  46.  
  47.       GO_TOP (rel, idx);
  48.       readx  (rel->relcode, &(mb_ctop.num), 4);
  49.       mb_ctop.changed = 0;
  50.       return (mb_clast = &mb_ctop);
  51.       }
  52.  
  53.    if ((ptr = mb_hash[ n=(rcd % MAX_CACHE) ]) != NULL && ptr->num == rcd)
  54.       {
  55.       return (mb_clast = ptr);
  56.       }
  57.  
  58.    for (ptr = mb_cache; ptr < end; ptr++)
  59.       if (ptr->num == rcd)
  60.          {
  61.          return (mb_clast = mb_hash[n] = ptr);
  62.          }
  63.  
  64.    ptr = _new_cache (rel, idx);
  65.  
  66.    GO_INDEX (rel, rcd, idx);
  67.    readx (rel->relcode, &(ptr->left),   4);
  68.    readx (rel->relcode, &(ptr->right),  4);
  69.    readx (rel->relcode, &(ptr->parent), 4);
  70.    readx (rel->relcode, &(ptr->parbal), 1);
  71.  
  72.    ptr->num     = rcd;
  73.    ptr->changed = 0;
  74.  
  75.    return (mb_clast = mb_hash[n] = ptr);
  76. }
  77.  
  78. void
  79. _flush_cache (rel, idx)
  80. relation     *rel;
  81. int                idx;
  82. {
  83.    cache *ptr, *end;
  84.  
  85.    if (mb_ctop.changed)
  86.       {
  87.       GO_TOP (rel, idx);
  88.       writx  (rel->relcode, &(mb_ctop.num), 4);
  89.       }
  90.  
  91.    end = &mb_cache[ncache];  /* Can't check when ptr==end */
  92.  
  93.    for (ptr = mb_cache; ptr < end; ptr++)
  94.       if (ptr->changed)
  95.          {
  96.          GO_INDEX (rel, ptr->num, idx);
  97.          writx (rel->relcode, &(ptr->left),   4);
  98.          writx (rel->relcode, &(ptr->right),  4);
  99.          writx (rel->relcode, &(ptr->parent), 4);
  100.          writx (rel->relcode, &(ptr->parbal), 1);
  101.          }
  102.  
  103.    _free_cache ();
  104. }
  105.  
  106. cache *
  107. _new_cache (rel, idx)
  108. relation   *rel;
  109. int              idx;
  110. {
  111.    if (ncache >= MAX_CACHE) /* If this happens, expand cache for performance */
  112.       {
  113.       _flush_cache (rel, idx);
  114.       }
  115.    ncache ++;
  116.  
  117.    return &mb_cache[ncache-1];
  118. }
  119.  
  120. void
  121. _free_cache ()
  122. {
  123.    register int  i;
  124.    mb_clast = NULL;  /* If we free the cache, it invalidates all cache*'s. */
  125.    ncache = 0;
  126.    for (i = 0; i < MAX_CACHE; i++)
  127.       mb_hash[i] = NULL;
  128.    mb_ctop.num = 0L;
  129.    mb_ctop.changed = 0;
  130. }
  131.  
  132.