buf_table.c
上传用户:blenddy
上传日期:2007-01-07
资源大小:6495k
文件大小:3k
源码类别:

数据库系统

开发平台:

Unix_Linux

  1. /*-------------------------------------------------------------------------
  2.  *
  3.  * buf_table.c
  4.  *   routines for finding buffers in the buffer pool.
  5.  *
  6.  * Copyright (c) 1994, Regents of the University of California
  7.  *
  8.  *
  9.  * IDENTIFICATION
  10.  *   $Header: /usr/local/cvsroot/pgsql/src/backend/storage/buffer/buf_table.c,v 1.14 1999/02/13 23:17:55 momjian Exp $
  11.  *
  12.  *-------------------------------------------------------------------------
  13.  */
  14. /*
  15.  * OLD COMMENTS
  16.  *
  17.  * Data Structures:
  18.  *
  19.  * Buffers are identified by their BufferTag (buf.h). This
  20.  * file contains routines for allocating a shmem hash table to
  21.  * map buffer tags to buffer descriptors.
  22.  *
  23.  * Synchronization:
  24.  *
  25.  * All routines in this file assume buffer manager spinlock is
  26.  * held by their caller.
  27.  */
  28. #include "postgres.h"
  29. #include "storage/bufmgr.h"
  30. #include "storage/buf_internals.h" /* where the declarations go */
  31. #include "storage/shmem.h"
  32. #include "storage/spin.h"
  33. #include "utils/hsearch.h"
  34. static HTAB *SharedBufHash;
  35. typedef struct lookup
  36. {
  37. BufferTag key;
  38. Buffer id;
  39. } LookupEnt;
  40. /*
  41.  * Initialize shmem hash table for mapping buffers
  42.  */
  43. void
  44. InitBufTable()
  45. {
  46. HASHCTL info;
  47. int hash_flags;
  48. /* assume lock is held */
  49. /* BufferTag maps to Buffer */
  50. info.keysize = sizeof(BufferTag);
  51. info.datasize = sizeof(Buffer);
  52. info.hash = tag_hash;
  53. hash_flags = (HASH_ELEM | HASH_FUNCTION);
  54. SharedBufHash = (HTAB *) ShmemInitHash("Shared Buffer Lookup Table",
  55.    NBuffers, NBuffers,
  56.    &info, hash_flags);
  57. if (!SharedBufHash)
  58. {
  59. elog(FATAL, "couldn't initialize shared buffer pool Hash Tbl");
  60. exit(1);
  61. }
  62. }
  63. BufferDesc *
  64. BufTableLookup(BufferTag *tagPtr)
  65. {
  66. LookupEnt  *result;
  67. bool found;
  68. if (tagPtr->blockNum == P_NEW)
  69. return NULL;
  70. result = (LookupEnt *)
  71. hash_search(SharedBufHash, (char *) tagPtr, HASH_FIND, &found);
  72. if (!result)
  73. {
  74. elog(ERROR, "BufTableLookup: BufferLookup table corrupted");
  75. return NULL;
  76. }
  77. if (!found)
  78. return NULL;
  79. return &(BufferDescriptors[result->id]);
  80. }
  81. /*
  82.  * BufTableDelete
  83.  */
  84. bool
  85. BufTableDelete(BufferDesc *buf)
  86. {
  87. LookupEnt  *result;
  88. bool found;
  89. /*
  90.  * buffer not initialized or has been removed from table already.
  91.  * BM_DELETED keeps us from removing buffer twice.
  92.  */
  93. if (buf->flags & BM_DELETED)
  94. return TRUE;
  95. buf->flags |= BM_DELETED;
  96. result = (LookupEnt *)
  97. hash_search(SharedBufHash, (char *) &(buf->tag), HASH_REMOVE, &found);
  98. if (!(result && found))
  99. {
  100. elog(ERROR, "BufTableDelete: BufferLookup table corrupted");
  101. return FALSE;
  102. }
  103. return TRUE;
  104. }
  105. bool
  106. BufTableInsert(BufferDesc *buf)
  107. {
  108. LookupEnt  *result;
  109. bool found;
  110. /* cannot insert it twice */
  111. Assert(buf->flags & BM_DELETED);
  112. buf->flags &= ~(BM_DELETED);
  113. result = (LookupEnt *)
  114. hash_search(SharedBufHash, (char *) &(buf->tag), HASH_ENTER, &found);
  115. if (!result)
  116. {
  117. Assert(0);
  118. elog(ERROR, "BufTableInsert: BufferLookup table corrupted");
  119. return FALSE;
  120. }
  121. /* found something else in the table ! */
  122. if (found)
  123. {
  124. Assert(0);
  125. elog(ERROR, "BufTableInsert: BufferLookup table corrupted");
  126. return FALSE;
  127. }
  128. result->id = buf->buf_id;
  129. return TRUE;
  130. }
  131. /* prints out collision stats for the buf table */
  132. #ifdef NOT_USED
  133. void
  134. DBG_LookupListCheck(int nlookup)
  135. {
  136. nlookup = 10;
  137. hash_stats("Shared", SharedBufHash);
  138. }
  139. #endif