代码改变世界

今天做了一个很糟糕的storage and buffer manager

2012-11-08 21:02  Rudrj2  阅读(481)  评论(1编辑  收藏  举报

 

 

初步分析是LRE-2算法的问题,

while(pt->less_recent != NULL && b > pt->less_recent->b2dist)
这步判断耗时较多。。必须要修改。。。暂时无解。。
void BMgr::LRU2(BCB * ptr, int frid)
{
    if(mru == NULL)
  {
    mru = new LRUEle();
    mru->fid = frid;
    mru->less_recent = NULL;
    mru->more_recent = NULL;
    if(ptr->stime != -1)
    {
      mru->b2dist = (ptr->stime-ptr->ftime);

    }
    else
    {
      mru->b2dist = -1;
    }
    lru = mru;  
  }

  else
  {

    double b = 0.0;
    if(ptr->stime != -1)
    {
      b = ptr->stime - ptr->ftime;
    }

    else
    {
      b = -1;
    }
    LRUEle * pt = mru;

    if(b == -1)
    {

      while(pt->less_recent != NULL && pt->less_recent->b2dist != -1)
      {
        pt = pt->less_recent;
      }

      if(pt->less_recent == NULL)
      {

        pt->less_recent = new LRUEle();
        pt->less_recent->more_recent = pt;
        pt = pt->less_recent;
        pt->fid = frid;
        pt->less_recent = NULL;
        pt->b2dist = b;

        lru = pt;
      }

      else
      {

        LRUEle * temp = new LRUEle();
        temp->fid = frid;
        temp->b2dist = b;
        temp->less_recent = pt->less_recent;
        temp->more_recent = pt;

        pt->less_recent->more_recent = temp;
        pt->less_recent = temp;
      }
    }

    else
    {

      while(pt->less_recent != NULL && b > pt->less_recent->b2dist)
      {
        pt = pt->less_recent;
      }

      if(pt->less_recent == NULL)
      {

        pt->less_recent = new LRUEle();
        pt->less_recent->more_recent = pt;
        pt = pt->less_recent;
        pt->b2dist = b;
        pt->fid = frid;
        pt->less_recent = NULL;

        lru = pt;
      }

      else
      {

        LRUEle * temp = new LRUEle();
        temp->b2dist = b;
        temp->fid = frid;
        temp->less_recent = pt->less_recent;
        temp->more_recent = pt;

        pt->less_recent->more_recent = temp;
        pt->less_recent = temp;
      }
    }
  }
}