您好,登錄后才能下訂單哦!
本節(jié)簡單介紹了PostgreSQL中的HTAB如何動態(tài)擴展,這是第1部分.
/*
* Top control structure for a hashtable --- in a shared table, each backend
* has its own copy (OK since no fields change at runtime)
* 哈希表的頂層控制結(jié)構(gòu).
* 在這個共享哈希表中,每一個后臺進程都有自己的拷貝
* (之所以沒有問題是因為fork出來后,在運行期沒有字段會變化)
*/
struct HTAB
{
//指向共享的控制信息
HASHHDR *hctl; /* => shared control information */
//段目錄
HASHSEGMENT *dir; /* directory of segment starts */
//哈希函數(shù)
HashValueFunc hash; /* hash function */
//哈希鍵比較函數(shù)
HashCompareFunc match; /* key comparison function */
//哈希鍵拷貝函數(shù)
HashCopyFunc keycopy; /* key copying function */
//內(nèi)存分配器
HashAllocFunc alloc; /* memory allocator */
//內(nèi)存上下文
MemoryContext hcxt; /* memory context if default allocator used */
//表名(用于錯誤信息)
char *tabname; /* table name (for error messages) */
//如在共享內(nèi)存中,則為T
bool isshared; /* true if table is in shared memory */
//如為T,則固定大小不能擴展
bool isfixed; /* if true, don't enlarge */
/* freezing a shared table isn't allowed, so we can keep state here */
//不允許凍結(jié)共享表,因此這里會保存相關(guān)狀態(tài)
bool frozen; /* true = no more inserts allowed */
/* We keep local copies of these fixed values to reduce contention */
//保存這些固定值的本地拷貝,以減少沖突
//哈希鍵長度(以字節(jié)為單位)
Size keysize; /* hash key length in bytes */
//段大小,必須為2的冪
long ssize; /* segment size --- must be power of 2 */
//段偏移,ssize的對數(shù)
int sshift; /* segment shift = log2(ssize) */
};
/*
* Header structure for a hash table --- contains all changeable info
* 哈希表的頭部結(jié)構(gòu) -- 存儲所有可變信息
*
* In a shared-memory hash table, the HASHHDR is in shared memory, while
* each backend has a local HTAB struct. For a non-shared table, there isn't
* any functional difference between HASHHDR and HTAB, but we separate them
* anyway to share code between shared and non-shared tables.
* 在共享內(nèi)存哈希表中,HASHHDR位于共享內(nèi)存中,每一個后臺進程都有一個本地HTAB結(jié)構(gòu).
* 對于非共享哈希表,HASHHDR和HTAB沒有任何功能性的不同,
* 但無論如何,我們還是把它們區(qū)分為共享和非共享表.
*/
struct HASHHDR
{
/*
* The freelist can become a point of contention in high-concurrency hash
* tables, so we use an array of freelists, each with its own mutex and
* nentries count, instead of just a single one. Although the freelists
* normally operate independently, we will scavenge entries from freelists
* other than a hashcode's default freelist when necessary.
* 在高并發(fā)的哈希表中,空閑鏈表會成為競爭熱點,因此我們使用空閑鏈表數(shù)組,
* 數(shù)組中的每一個元素都有自己的mutex和條目統(tǒng)計,而不是使用一個.
*
* If the hash table is not partitioned, only freeList[0] is used and its
* spinlock is not used at all; callers' locking is assumed sufficient.
* 如果哈希表沒有分區(qū),那么只有freelist[0]元素是有用的,自旋鎖沒有任何用處;
* 調(diào)用者鎖定被認為已足夠OK.
*/
/* Number of freelists to be used for a partitioned hash table. */
//#define NUM_FREELISTS 32
FreeListData freeList[NUM_FREELISTS];
/* These fields can change, but not in a partitioned table */
//這些域字段可以改變,但不適用于分區(qū)表
/* Also, dsize can't change in a shared table, even if unpartitioned */
//同時,就算是非分區(qū)表,共享表的dsize也不能改變
//目錄大小
long dsize; /* directory size */
//已分配的段大小(<= dsize)
long nsegs; /* number of allocated segments (<= dsize) */
//正在使用的最大桶ID
uint32 max_bucket; /* ID of maximum bucket in use */
//進入整個哈希表的模掩碼
uint32 high_mask; /* mask to modulo into entire table */
//進入低位哈希表的模掩碼
uint32 low_mask; /* mask to modulo into lower half of table */
/* These fields are fixed at hashtable creation */
//下面這些字段在哈希表創(chuàng)建時已固定
//哈希鍵大小(以字節(jié)為單位)
Size keysize; /* hash key length in bytes */
//所有用戶元素大小(以字節(jié)為單位)
Size entrysize; /* total user element size in bytes */
//分區(qū)個數(shù)(2的冪),或者為0
long num_partitions; /* # partitions (must be power of 2), or 0 */
//目標(biāo)的填充因子
long ffactor; /* target fill factor */
//如目錄是固定大小,則該值為dsize的上限值
long max_dsize; /* 'dsize' limit if directory is fixed size */
//段大小,必須是2的冪
long ssize; /* segment size --- must be power of 2 */
//段偏移,ssize的對數(shù)
int sshift; /* segment shift = log2(ssize) */
//一次性分配的條目個數(shù)
int nelem_alloc; /* number of entries to allocate at once */
#ifdef HASH_STATISTICS
/*
* Count statistics here. NB: stats code doesn't bother with mutex, so
* counts could be corrupted a bit in a partitioned table.
* 統(tǒng)計信息.
* 注意:統(tǒng)計相關(guān)的代碼不會影響mutex,因此對于分區(qū)表,統(tǒng)計可能有一點點問題
*/
long accesses;
long collisions;
#endif
};
/*
* Per-freelist data.
* 空閑鏈表數(shù)據(jù).
*
* In a partitioned hash table, each freelist is associated with a specific
* set of hashcodes, as determined by the FREELIST_IDX() macro below.
* nentries tracks the number of live hashtable entries having those hashcodes
* (NOT the number of entries in the freelist, as you might expect).
* 在一個分區(qū)哈希表中,每一個空閑鏈表與特定的hashcodes集合相關(guān),通過下面的FREELIST_IDX()宏進行定義.
* nentries跟蹤有這些hashcodes的仍存活的hashtable條目個數(shù).
* (注意不要搞錯,不是空閑的條目個數(shù))
*
* The coverage of a freelist might be more or less than one partition, so it
* needs its own lock rather than relying on caller locking. Relying on that
* wouldn't work even if the coverage was the same, because of the occasional
* need to "borrow" entries from another freelist; see get_hash_entry().
* 空閑鏈表的覆蓋范圍可能比一個分區(qū)多或少,因此需要自己的鎖而不能僅僅依賴調(diào)用者的鎖.
* 依賴調(diào)用者鎖在覆蓋面一樣的情況下也不會起效,因為偶爾需要從另一個自由列表“借用”條目,詳細參見get_hash_entry()
*
* Using an array of FreeListData instead of separate arrays of mutexes,
* nentries and freeLists helps to reduce sharing of cache lines between
* different mutexes.
* 使用FreeListData數(shù)組而不是一個獨立的mutexes,nentries和freelists數(shù)組有助于減少不同mutexes之間的緩存線共享.
*/
typedef struct
{
//該空閑鏈表的自旋鎖
slock_t mutex; /* spinlock for this freelist */
//相關(guān)桶中的條目個數(shù)
long nentries; /* number of entries in associated buckets */
//空閑元素鏈
HASHELEMENT *freeList; /* chain of free elements */
} FreeListData;
/*
* HASHELEMENT is the private part of a hashtable entry. The caller's data
* follows the HASHELEMENT structure (on a MAXALIGN'd boundary). The hash key
* is expected to be at the start of the caller's hash entry data structure.
* HASHELEMENT是哈希表條目的私有部分.
* 調(diào)用者的數(shù)據(jù)按照HASHELEMENT結(jié)構(gòu)組織(位于MAXALIGN的邊界).
* 哈希鍵應(yīng)位于調(diào)用者hash條目數(shù)據(jù)結(jié)構(gòu)的開始位置.
*/
typedef struct HASHELEMENT
{
//鏈接到相同桶中的下一個條目
struct HASHELEMENT *link; /* link to next entry in same bucket */
//該條目的哈希函數(shù)結(jié)果
uint32 hashvalue; /* hash function result for this entry */
} HASHELEMENT;
/* Hash table header struct is an opaque type known only within dynahash.c */
//哈希表頭部結(jié)構(gòu),非透明類型,用于dynahash.c
typedef struct HASHHDR HASHHDR;
/* Hash table control struct is an opaque type known only within dynahash.c */
//哈希表控制結(jié)構(gòu),非透明類型,用于dynahash.c
typedef struct HTAB HTAB;
/* Parameter data structure for hash_create */
//hash_create使用的參數(shù)數(shù)據(jù)結(jié)構(gòu)
/* Only those fields indicated by hash_flags need be set */
//根據(jù)hash_flags標(biāo)記設(shè)置相應(yīng)的字段
typedef struct HASHCTL
{
//分區(qū)個數(shù)(必須是2的冪)
long num_partitions; /* # partitions (must be power of 2) */
//段大小
long ssize; /* segment size */
//初始化目錄大小
long dsize; /* (initial) directory size */
//dsize上限
long max_dsize; /* limit to dsize if dir size is limited */
//填充因子
long ffactor; /* fill factor */
//哈希鍵大小(字節(jié)為單位)
Size keysize; /* hash key length in bytes */
//參見上述數(shù)據(jù)結(jié)構(gòu)注釋
Size entrysize; /* total user element size in bytes */
//
HashValueFunc hash; /* hash function */
HashCompareFunc match; /* key comparison function */
HashCopyFunc keycopy; /* key copying function */
HashAllocFunc alloc; /* memory allocator */
MemoryContext hcxt; /* memory context to use for allocations */
//共享內(nèi)存中的哈希頭部結(jié)構(gòu)地址
HASHHDR *hctl; /* location of header in shared mem */
} HASHCTL;
/* A hash bucket is a linked list of HASHELEMENTs */
//哈希桶是HASHELEMENTs鏈表
typedef HASHELEMENT *HASHBUCKET;
/* A hash segment is an array of bucket headers */
//hash segment是桶數(shù)組
typedef HASHBUCKET *HASHSEGMENT;
/*
* Hash functions must have this signature.
* Hash函數(shù)必須有它自己的標(biāo)識
*/
typedef uint32 (*HashValueFunc) (const void *key, Size keysize);
/*
* Key comparison functions must have this signature. Comparison functions
* return zero for match, nonzero for no match. (The comparison function
* definition is designed to allow memcmp() and strncmp() to be used directly
* as key comparison functions.)
* 哈希鍵對比函數(shù)必須有自己的標(biāo)識.
* 如匹配則對比函數(shù)返回0,不匹配返回非0.
* (對比函數(shù)定義被設(shè)計為允許在對比鍵值時可直接使用memcmp()和strncmp())
*/
typedef int (*HashCompareFunc) (const void *key1, const void *key2,
Size keysize);
/*
* Key copying functions must have this signature. The return value is not
* used. (The definition is set up to allow memcpy() and strlcpy() to be
* used directly.)
* 鍵拷貝函數(shù)必須有自己的標(biāo)識.
* 返回值無用.
*/
typedef void *(*HashCopyFunc) (void *dest, const void *src, Size keysize);
/*
* Space allocation function for a hashtable --- designed to match malloc().
* Note: there is no free function API; can't destroy a hashtable unless you
* use the default allocator.
* 哈希表的恐懼分配函數(shù) -- 被設(shè)計為與malloc()函數(shù)匹配.
* 注意:這里沒有釋放函數(shù)API;不能銷毀哈希表,除非使用默認的分配器.
*/
typedef void *(*HashAllocFunc) (Size request);
其結(jié)構(gòu)如下圖所示:
擴展后的結(jié)構(gòu)如下圖所示:
N/A
N/A
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。