Blame source/uds/cachedChapterIndex.c

Packit Service 310c69
/*
Packit Service 310c69
 * Copyright (c) 2020 Red Hat, Inc.
Packit Service 310c69
 *
Packit Service 310c69
 * This program is free software; you can redistribute it and/or
Packit Service 310c69
 * modify it under the terms of the GNU General Public License
Packit Service 310c69
 * as published by the Free Software Foundation; either version 2
Packit Service 310c69
 * of the License, or (at your option) any later version.
Packit Service 310c69
 * 
Packit Service 310c69
 * This program is distributed in the hope that it will be useful,
Packit Service 310c69
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 310c69
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 310c69
 * GNU General Public License for more details.
Packit Service 310c69
 * 
Packit Service 310c69
 * You should have received a copy of the GNU General Public License
Packit Service 310c69
 * along with this program; if not, write to the Free Software
Packit Service 310c69
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit Service 310c69
 * 02110-1301, USA. 
Packit Service 310c69
 *
Packit Service 310c69
 * $Id: //eng/uds-releases/jasper/src/uds/cachedChapterIndex.c#3 $
Packit Service 310c69
 */
Packit Service 310c69
Packit Service 310c69
#include "cachedChapterIndex.h"
Packit Service 310c69
Packit Service 310c69
#include "memoryAlloc.h"
Packit Service 310c69
Packit Service 310c69
/**********************************************************************/
Packit Service 310c69
int initializeCachedChapterIndex(CachedChapterIndex *chapter,
Packit Service 310c69
                                 const Geometry     *geometry)
Packit Service 310c69
{
Packit Service 310c69
  chapter->virtualChapter  = UINT64_MAX;
Packit Service 310c69
  chapter->indexPagesCount = geometry->indexPagesPerChapter;
Packit Service 310c69
Packit Service 310c69
  int result = ALLOCATE(chapter->indexPagesCount, DeltaIndexPage, __func__,
Packit Service 310c69
                        &chapter->indexPages);
Packit Service 310c69
  if (result != UDS_SUCCESS) {
Packit Service 310c69
    return result;
Packit Service 310c69
  }
Packit Service 310c69
Packit Service 310c69
  result = ALLOCATE(chapter->indexPagesCount, struct volume_page,
Packit Service 310c69
                    "sparse index VolumePages", &chapter->volumePages);
Packit Service 310c69
  if (result != UDS_SUCCESS) {
Packit Service 310c69
    return result;
Packit Service 310c69
  }
Packit Service 310c69
  
Packit Service 310c69
  unsigned int i;
Packit Service 310c69
  for (i = 0; i < chapter->indexPagesCount; i++) {
Packit Service 310c69
    result = initializeVolumePage(geometry, &chapter->volumePages[i]);
Packit Service 310c69
    if (result != UDS_SUCCESS) {
Packit Service 310c69
      return result;
Packit Service 310c69
    }
Packit Service 310c69
  }
Packit Service 310c69
  return UDS_SUCCESS;
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**********************************************************************/
Packit Service 310c69
void destroyCachedChapterIndex(CachedChapterIndex *chapter)
Packit Service 310c69
{
Packit Service 310c69
  if (chapter->volumePages != NULL) {
Packit Service 310c69
    unsigned int i;
Packit Service 310c69
    for (i = 0; i < chapter->indexPagesCount; i++) {
Packit Service 310c69
      destroyVolumePage(&chapter->volumePages[i]);
Packit Service 310c69
    }
Packit Service 310c69
  }
Packit Service 310c69
  FREE(chapter->indexPages);
Packit Service 310c69
  FREE(chapter->volumePages);
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**********************************************************************/
Packit Service 310c69
int cacheChapterIndex(CachedChapterIndex *chapter,
Packit Service 310c69
                      uint64_t            virtualChapter,
Packit Service 310c69
                      const Volume       *volume)
Packit Service 310c69
{
Packit Service 310c69
  // Mark the cached chapter as unused in case the update fails midway.
Packit Service 310c69
  chapter->virtualChapter = UINT64_MAX;
Packit Service 310c69
Packit Service 310c69
  // Read all the page data and initialize the entire DeltaIndexPage array.
Packit Service 310c69
  // (It's not safe for the zone threads to do it lazily--they'll race.)
Packit Service 310c69
  int result = readChapterIndexFromVolume(volume, virtualChapter,
Packit Service 310c69
                                          chapter->volumePages,
Packit Service 310c69
                                          chapter->indexPages);
Packit Service 310c69
  if (result != UDS_SUCCESS) {
Packit Service 310c69
    return result;
Packit Service 310c69
  }
Packit Service 310c69
Packit Service 310c69
  // Reset all chapter counter values to zero.
Packit Service 310c69
  chapter->counters.searchHits        = 0;
Packit Service 310c69
  chapter->counters.searchMisses      = 0;
Packit Service 310c69
  chapter->counters.consecutiveMisses = 0;
Packit Service 310c69
Packit Service 310c69
  // Mark the entry as valid--it's now in the cache.
Packit Service 310c69
  chapter->virtualChapter = virtualChapter;
Packit Service 310c69
  chapter->skipSearch     = false;
Packit Service 310c69
Packit Service 310c69
  return UDS_SUCCESS;
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**********************************************************************/
Packit Service 310c69
int searchCachedChapterIndex(CachedChapterIndex *chapter,
Packit Service 310c69
                             const Geometry     *geometry,
Packit Service 310c69
                             const IndexPageMap *indexPageMap,
Packit Service 310c69
                             const UdsChunkName *name,
Packit Service 310c69
                             int                *recordPagePtr)
Packit Service 310c69
{
Packit Service 310c69
  // Find the indexPageNumber in the chapter that would have the chunk name.
Packit Service 310c69
  unsigned int physicalChapter
Packit Service 310c69
    = mapToPhysicalChapter(geometry, chapter->virtualChapter);
Packit Service 310c69
  unsigned int indexPageNumber;
Packit Service 310c69
  int result = findIndexPageNumber(indexPageMap, name, physicalChapter,
Packit Service 310c69
                                   &indexPageNumber);
Packit Service 310c69
  if (result != UDS_SUCCESS) {
Packit Service 310c69
    return result;
Packit Service 310c69
  }
Packit Service 310c69
Packit Service 310c69
  return searchChapterIndexPage(&chapter->indexPages[indexPageNumber],
Packit Service 310c69
                                geometry, name, recordPagePtr);
Packit Service 310c69
}