filehash/ffdb_page.c
static pgno_t
_ffdb_move_pages (ffdb_htab_t* hashp, unsigned short type,
		  pgno_t oldfirst, pgno_t oldlast,
		  pgno_t newfirst, pgno_t newlast)
{
  pgno_t dlast, page, rpage, tp;
  pgno_t prevp, nextp;
  unsigned int numpages, i;
  void *pagep, *prevpagep, *nextpagep;

  dlast = INVALID_PGNO;
  /* Create page header array to keep track old page information */
  numpages = oldlast - oldfirst + 1;

  /* Now we can move pages */
  page = oldfirst;
  rpage = newfirst;
  for (i = 0; i < numpages; i++) {
    pagep = ffdb_get_page (hashp, page, HASH_RAW_PAGE, 0, &tp);
    if (!pagep) {
      fprintf (stderr, "Cannot grab old page %d to move to new page %d.\n",
	       page, rpage);
      return dlast;
    }

    if (TYPE(pagep) != type) {
      page++;
      rpage++;
      ffdb_put_page (hashp, pagep, HASH_RAW_PAGE, 0);
      continue;
    }

    /* Get its original previous and next page number */
    nextp = NEXT_PGNO(pagep);
    prevp = PREV_PGNO(pagep);

    if (nextp != INVALID_PGNO) {
      if (nextp < oldfirst || nextp > oldlast) {
	/* The next page is not going to be moved, its previous link 
	 * Should be updated
	 */
	nextpagep = ffdb_get_page (hashp, nextp, HASH_RAW_PAGE, 0, &tp);
	if (!nextpagep) {
	  fprintf (stderr, "Cannot get next page %d not in the moving list for page %d\n", nextp, page);
	  ffdb_put_page (hashp, pagep, TYPE(pagep), 0);
	  return dlast;
	}

	PREV_PGNO(nextpagep) = rpage;
	ffdb_put_page (hashp, nextpagep, TYPE(nextpagep), 1);
      }
      else {
	/* I have to update this page next link to a new page number */
	NEXT_PGNO(pagep) = newfirst + (nextp - oldfirst);
      }
    }
    else {
      /* This is last data page in these moved page.
       * update last new location of this page
       */
      if (type == HASH_DATA_PAGE)
	dlast = rpage;
    }


    if (prevp != INVALID_PGNO) {
      if (prevp < oldfirst || prevp > oldlast) {
	/* The previous page is not going to be moved, its previous link 
	 * Should be updated
	 */
	prevpagep = ffdb_get_page (hashp, prevp, HASH_RAW_PAGE, 0, &tp);
	if (!prevpagep) {
	  fprintf (stderr, "Cannot get previous page %d not in the moving list for page %d\n", prevp, page);
	  ffdb_put_page (hashp, pagep, TYPE(pagep), 0);
	  return dlast;
	}

	NEXT_PGNO(prevpagep) = rpage;
	ffdb_put_page (hashp, prevpagep, TYPE(prevpagep), 1);
      }
      else {
	/* The previous link of this page has to be updated */
	PREV_PGNO(pagep) = newfirst + (prevp - oldfirst);
      }
    }

    /* Now update current page number */
    CURR_PGNO(pagep) = rpage;

#ifdef _FFDB_DEBUG
    fprintf (stderr, "Move old page type 0x%x %d to new page %d\n", type, page, rpage);
#endif

    /* Now update the hash data content on the primary pages
     * pointed by key field of data on this page
     */
    if (type == HASH_DATA_PAGE) {
      if (_ffdb_update_key_pages_content (hashp, pagep, page, rpage, 
					  oldfirst, oldlast,
					  newfirst, newlast) != 0) {
	/* Release this page and update page number */
	ffdb_put_page (hashp, pagep, HASH_DATA_PAGE, 0);
	return dlast;
      }
#ifdef _FFDB_DEBUG
      fprintf (stderr, "Update primary page for data page %d %d page = %d page sign = 0x%x\n", page, rpage, CURR_PGNO(pagep), PAGE_SIGN(pagep));
#endif
    }
    else if (type == HASH_BUCKET_PAGE) {
      /* Update the key page entry inside each data page pointed 
       * by the overflow page. The data pages that have been moved
       *will not be updated since they are updated by the above routine
       */
      if (_ffdb_update_data_pages_content (hashp, pagep, page, rpage,
					   oldfirst, oldlast,
					   newfirst, newlast) != 0) {
	/* Release this page and update page number */
	ffdb_put_page (hashp, pagep, HASH_DATA_PAGE, 0);
	return dlast;
      }
#ifdef _FFDB_DEBUG
      fprintf (stderr, "Update data page for primary page %d %d page = %d page sign = 0x%x\n", page, rpage, CURR_PGNO(pagep), PAGE_SIGN(pagep));
#endif
    }
      

    /* Release this page and update page number */
    ffdb_pagepool_change_page (hashp->mp, pagep, CURR_PGNO(pagep));

    /* update local variables */
    rpage++;
    page++;    
  }
    
  return dlast;
}
/**
 * Move pages when the file closes
 *
 * @param hashp hash table pointer
 * @param type this type of pages to be moved
 * @param oldfirst first page of old pages to be moved
 * @param oldlast the last page of old page to be moved
 * @param newfirst first page of new pages to move into
 * @param newlast last page of new pages to move into
 *
 * @return last page number on the linked list after successful move. 
 * If there is no data pages being moved, INVALID_PGNO is returned
 */


@return last page number on the linked list after successful move.
If there is no data pages being moved, INVALID_PGNO is returned
@return last page number on the linked list after successful move . If there is no data pages being moved INVALID_PGNO is returned
static pgno_t
_ffdb_move_pages (ffdb_htab_t* hashp, unsigned short type,
		  pgno_t oldfirst, pgno_t oldlast,
		  pgno_t newfirst, pgno_t newlast)
{
  pgno_t dlast, page, rpage, tp;
  pgno_t prevp, nextp;
  unsigned int numpages, i;
  void *pagep, *prevpagep, *nextpagep;
  dlast = INVALID_PGNO;
  numpages = oldlast - oldfirst + 1;
  page = oldfirst;
  rpage = newfirst;
  for (i = 0; i < numpages; i++) {
    pagep = ffdb_get_page (hashp, page, HASH_RAW_PAGE, 0, &tp);
    if (!pagep) {
      fprintf (stderr, "Cannot grab old page %d to move to new page %d.\n",
	       page, rpage);
      return dlast;
    }
    if (TYPE(pagep) != type) {
      page++;
      rpage++;
      ffdb_put_page (hashp, pagep, HASH_RAW_PAGE, 0);
      continue;
    }
    nextp = NEXT_PGNO(pagep);
    prevp = PREV_PGNO(pagep);
    if (nextp != INVALID_PGNO) {
      if (nextp < oldfirst || nextp > oldlast) {
	nextpagep = ffdb_get_page (hashp, nextp, HASH_RAW_PAGE, 0, &tp);
	if (!nextpagep) {
	  fprintf (stderr, "Cannot get next page %d not in the moving list for page %d\n", nextp, page);
	  ffdb_put_page (hashp, pagep, TYPE(pagep), 0);
	  return dlast;
	}
	PREV_PGNO(nextpagep) = rpage;
	ffdb_put_page (hashp, nextpagep, TYPE(nextpagep), 1);
      }
      else {
	NEXT_PGNO(pagep) = newfirst + (nextp - oldfirst);
      }
    }
    else {
      if (type == HASH_DATA_PAGE)
	dlast = rpage;
    }
    if (prevp != INVALID_PGNO) {
      if (prevp < oldfirst || prevp > oldlast) {
	prevpagep = ffdb_get_page (hashp, prevp, HASH_RAW_PAGE, 0, &tp);
	if (!prevpagep) {
	  fprintf (stderr, "Cannot get previous page %d not in the moving list for page %d\n", prevp, page);
	  ffdb_put_page (hashp, pagep, TYPE(pagep), 0);
	  return dlast;
	}
	NEXT_PGNO(prevpagep) = rpage;
	ffdb_put_page (hashp, prevpagep, TYPE(prevpagep), 1);
      }
      else {
	PREV_PGNO(pagep) = newfirst + (prevp - oldfirst);
      }
    }
    CURR_PGNO(pagep) = rpage;
#ifdef _FFDB_DEBUG
    fprintf (stderr, "Move old page type 0x%x %d to new page %d\n", type, page, rpage);
#endif
    if (type == HASH_DATA_PAGE) {
      if (_ffdb_update_key_pages_content (hashp, pagep, page, rpage, 
					  oldfirst, oldlast,
					  newfirst, newlast) != 0) {
	ffdb_put_page (hashp, pagep, HASH_DATA_PAGE, 0);
	return dlast;
      }
#ifdef _FFDB_DEBUG
      fprintf (stderr, "Update primary page for data page %d %d page = %d page sign = 0x%x\n", page, rpage, CURR_PGNO(pagep), PAGE_SIGN(pagep));
#endif
    }
    else if (type == HASH_BUCKET_PAGE) {
      if (_ffdb_update_data_pages_content (hashp, pagep, page, rpage,
					   oldfirst, oldlast,
					   newfirst, newlast) != 0) {
	ffdb_put_page (hashp, pagep, HASH_DATA_PAGE, 0);
	return dlast;
      }
#ifdef _FFDB_DEBUG
      fprintf (stderr, "Update data page for primary page %d %d page = %d page sign = 0x%x\n", page, rpage, CURR_PGNO(pagep), PAGE_SIGN(pagep));
#endif
    }
    ffdb_pagepool_change_page (hashp->mp, pagep, CURR_PGNO(pagep));
    rpage++;
    page++;    
  }
  return dlast;
}
static pgno_t _ffdb_move_pages ( ffdb_htab_t * hashp , unsigned short type , pgno_t oldfirst , pgno_t oldlast , pgno_t newfirst , pgno_t newlast ) { pgno_t dlast , page , rpage , tp ; pgno_t prevp , nextp ; unsigned int numpages , i ; void * pagep , * prevpagep , * nextpagep ; dlast = INVALID_PGNO ; numpages = oldlast - oldfirst + 1 ; page = oldfirst ; rpage = newfirst ; for ( i = 0 ; i < numpages ; i ++ ) { pagep = ffdb_get_page ( hashp , page , HASH_RAW_PAGE , 0 , & tp ) ; if ( ! pagep ) { fprintf ( stderr , " \n " , page , rpage ) ; return dlast ; } if ( TYPE ( pagep ) != type ) { page ++ ; rpage ++ ; ffdb_put_page ( hashp , pagep , HASH_RAW_PAGE , 0 ) ; continue ; } nextp = NEXT_PGNO ( pagep ) ; prevp = PREV_PGNO ( pagep ) ; if ( nextp != INVALID_PGNO ) { if ( nextp < oldfirst || nextp > oldlast ) { nextpagep = ffdb_get_page ( hashp , nextp , HASH_RAW_PAGE , 0 , & tp ) ; if ( ! nextpagep ) { fprintf ( stderr , " \n " , nextp , page ) ; ffdb_put_page ( hashp , pagep , TYPE ( pagep ) , 0 ) ; return dlast ; } PREV_PGNO ( nextpagep ) = rpage ; ffdb_put_page ( hashp , nextpagep , TYPE ( nextpagep ) , 1 ) ; } else { NEXT_PGNO ( pagep ) = newfirst + ( nextp - oldfirst ) ; } } else { if ( type == HASH_DATA_PAGE ) dlast = rpage ; } if ( prevp != INVALID_PGNO ) { if ( prevp < oldfirst || prevp > oldlast ) { prevpagep = ffdb_get_page ( hashp , prevp , HASH_RAW_PAGE , 0 , & tp ) ; if ( ! prevpagep ) { fprintf ( stderr , " \n " , prevp , page ) ; ffdb_put_page ( hashp , pagep , TYPE ( pagep ) , 0 ) ; return dlast ; } NEXT_PGNO ( prevpagep ) = rpage ; ffdb_put_page ( hashp , prevpagep , TYPE ( prevpagep ) , 1 ) ; } else { PREV_PGNO ( pagep ) = newfirst + ( prevp - oldfirst ) ; } } CURR_PGNO ( pagep ) = rpage ; #ifdef _FFDB_DEBUG fprintf ( stderr , " \n " , type , page , rpage ) ; #endif if ( type == HASH_DATA_PAGE ) { if ( _ffdb_update_key_pages_content ( hashp , pagep , page , rpage , oldfirst , oldlast , newfirst , newlast ) != 0 ) { ffdb_put_page ( hashp , pagep , HASH_DATA_PAGE , 0 ) ; return dlast ; } #ifdef _FFDB_DEBUG fprintf ( stderr , " \n " , page , rpage , CURR_PGNO ( pagep ) , PAGE_SIGN ( pagep ) ) ; #endif } else if ( type == HASH_BUCKET_PAGE ) { if ( _ffdb_update_data_pages_content ( hashp , pagep , page , rpage , oldfirst , oldlast , newfirst , newlast ) != 0 ) { ffdb_put_page ( hashp , pagep , HASH_DATA_PAGE , 0 ) ; return dlast ; } #ifdef _FFDB_DEBUG fprintf ( stderr , " \n " , page , rpage , CURR_PGNO ( pagep ) , PAGE_SIGN ( pagep ) ) ; #endif } ffdb_pagepool_change_page ( hashp -> mp , pagep , CURR_PGNO ( pagep ) ) ; rpage ++ ; page ++ ; } return dlast ; }
Move pages when the file closes
 @param hashp hash table pointer
 @param type this type of pages to be moved
 @param oldfirst first page of old pages to be moved
 @param oldlast the last page of old page to be moved
 @param newfirst first page of new pages to move into
 @param newlast last page of new pages to move into
Move pages when the file closes @param hashp hash table pointer @param type this type of pages to be moved @param oldfirst first page of old pages to be moved @param oldlast the last page of old page to be moved @param newfirst first page of new pages to move into @param newlast last page of new pages to move into
/* Create page header array to keep track old page information */ /* Now we can move pages */ /* Get its original previous and next page number */ /* The next page is not going to be moved, its previous link 
	 * Should be updated
	 */ /* I have to update this page next link to a new page number */ /* This is last data page in these moved page.
       * update last new location of this page
       */ /* The previous page is not going to be moved, its previous link 
	 * Should be updated
	 */ /* The previous link of this page has to be updated */ /* Now update current page number */ /* Now update the hash data content on the primary pages
     * pointed by key field of data on this page
     */ /* Release this page and update page number */ /* Update the key page entry inside each data page pointed 
       * by the overflow page. The data pages that have been moved
       *will not be updated since they are updated by the above routine
       */ /* Release this page and update page number */ /* Release this page and update page number */ /* update local variables */