00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef HASH_H
00018 #define HASH_H
00019
00020 #include "access/genam.h"
00021 #include "access/itup.h"
00022 #include "access/sdir.h"
00023 #include "access/xlog.h"
00024 #include "fmgr.h"
00025 #include "storage/bufmgr.h"
00026 #include "storage/lock.h"
00027 #include "utils/relcache.h"
00028
00029
00030
00031
00032
00033 typedef uint32 Bucket;
00034
00035 #define BUCKET_TO_BLKNO(metap,B) \
00036 ((BlockNumber) ((B) + ((B) ? (metap)->hashm_spares[_hash_log2((B)+1)-1] : 0)) + 1)
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #define LH_UNUSED_PAGE (0)
00050 #define LH_OVERFLOW_PAGE (1 << 0)
00051 #define LH_BUCKET_PAGE (1 << 1)
00052 #define LH_BITMAP_PAGE (1 << 2)
00053 #define LH_META_PAGE (1 << 3)
00054
00055 typedef struct HashPageOpaqueData
00056 {
00057 BlockNumber hasho_prevblkno;
00058 BlockNumber hasho_nextblkno;
00059 Bucket hasho_bucket;
00060 uint16 hasho_flag;
00061 uint16 hasho_page_id;
00062 } HashPageOpaqueData;
00063
00064 typedef HashPageOpaqueData *HashPageOpaque;
00065
00066
00067
00068
00069
00070
00071
00072 #define HASHO_PAGE_ID 0xFF80
00073
00074
00075
00076
00077 typedef struct HashScanOpaqueData
00078 {
00079
00080 uint32 hashso_sk_hash;
00081
00082
00083
00084
00085
00086 Bucket hashso_bucket;
00087 bool hashso_bucket_valid;
00088
00089
00090
00091
00092
00093 BlockNumber hashso_bucket_blkno;
00094
00095
00096
00097
00098
00099
00100
00101 Buffer hashso_curbuf;
00102
00103
00104 ItemPointerData hashso_curpos;
00105
00106
00107 ItemPointerData hashso_heappos;
00108 } HashScanOpaqueData;
00109
00110 typedef HashScanOpaqueData *HashScanOpaque;
00111
00112
00113
00114
00115
00116 #define HASH_METAPAGE 0
00117
00118 #define HASH_MAGIC 0x6440640
00119 #define HASH_VERSION 2
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 #define HASH_MAX_SPLITPOINTS 32
00142 #define HASH_MAX_BITMAPS 128
00143
00144 typedef struct HashMetaPageData
00145 {
00146 uint32 hashm_magic;
00147 uint32 hashm_version;
00148 double hashm_ntuples;
00149 uint16 hashm_ffactor;
00150 uint16 hashm_bsize;
00151 uint16 hashm_bmsize;
00152
00153 uint16 hashm_bmshift;
00154 uint32 hashm_maxbucket;
00155 uint32 hashm_highmask;
00156 uint32 hashm_lowmask;
00157 uint32 hashm_ovflpoint;
00158
00159 uint32 hashm_firstfree;
00160 uint32 hashm_nmaps;
00161 RegProcedure hashm_procid;
00162 uint32 hashm_spares[HASH_MAX_SPLITPOINTS];
00163
00164 BlockNumber hashm_mapp[HASH_MAX_BITMAPS];
00165 } HashMetaPageData;
00166
00167 typedef HashMetaPageData *HashMetaPage;
00168
00169
00170
00171
00172 #define HashMaxItemSize(page) \
00173 MAXALIGN_DOWN(PageGetPageSize(page) - \
00174 SizeOfPageHeaderData - \
00175 sizeof(ItemIdData) - \
00176 MAXALIGN(sizeof(HashPageOpaqueData)))
00177
00178 #define HASH_MIN_FILLFACTOR 10
00179 #define HASH_DEFAULT_FILLFACTOR 75
00180
00181
00182
00183
00184 #define BYTE_TO_BIT 3
00185 #define ALL_SET ((uint32) ~0)
00186
00187
00188
00189
00190
00191
00192
00193
00194 #define BMPGSZ_BYTE(metap) ((metap)->hashm_bmsize)
00195 #define BMPGSZ_BIT(metap) ((metap)->hashm_bmsize << BYTE_TO_BIT)
00196 #define BMPG_SHIFT(metap) ((metap)->hashm_bmshift)
00197 #define BMPG_MASK(metap) (BMPGSZ_BIT(metap) - 1)
00198
00199 #define HashPageGetBitmap(page) \
00200 ((uint32 *) PageGetContents(page))
00201
00202 #define HashGetMaxBitmapSize(page) \
00203 (PageGetPageSize((Page) page) - \
00204 (MAXALIGN(SizeOfPageHeaderData) + MAXALIGN(sizeof(HashPageOpaqueData))))
00205
00206 #define HashPageGetMeta(page) \
00207 ((HashMetaPage) PageGetContents(page))
00208
00209
00210
00211
00212 #define BITS_PER_MAP 32
00213
00214
00215 #define CLRBIT(A, N) ((A)[(N)/BITS_PER_MAP] &= ~(1<<((N)%BITS_PER_MAP)))
00216 #define SETBIT(A, N) ((A)[(N)/BITS_PER_MAP] |= (1<<((N)%BITS_PER_MAP)))
00217 #define ISSET(A, N) ((A)[(N)/BITS_PER_MAP] & (1<<((N)%BITS_PER_MAP)))
00218
00219
00220
00221
00222 #define HASH_READ BUFFER_LOCK_SHARE
00223 #define HASH_WRITE BUFFER_LOCK_EXCLUSIVE
00224 #define HASH_NOLOCK (-1)
00225
00226 #define HASH_SHARE ShareLock
00227 #define HASH_EXCLUSIVE ExclusiveLock
00228
00229
00230
00231
00232 #define HTEqualStrategyNumber 1
00233 #define HTMaxStrategyNumber 1
00234
00235
00236
00237
00238
00239
00240 #define HASHPROC 1
00241
00242
00243
00244
00245 extern Datum hashbuild(PG_FUNCTION_ARGS);
00246 extern Datum hashbuildempty(PG_FUNCTION_ARGS);
00247 extern Datum hashinsert(PG_FUNCTION_ARGS);
00248 extern Datum hashbeginscan(PG_FUNCTION_ARGS);
00249 extern Datum hashgettuple(PG_FUNCTION_ARGS);
00250 extern Datum hashgetbitmap(PG_FUNCTION_ARGS);
00251 extern Datum hashrescan(PG_FUNCTION_ARGS);
00252 extern Datum hashendscan(PG_FUNCTION_ARGS);
00253 extern Datum hashmarkpos(PG_FUNCTION_ARGS);
00254 extern Datum hashrestrpos(PG_FUNCTION_ARGS);
00255 extern Datum hashbulkdelete(PG_FUNCTION_ARGS);
00256 extern Datum hashvacuumcleanup(PG_FUNCTION_ARGS);
00257 extern Datum hashoptions(PG_FUNCTION_ARGS);
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 extern Datum hashchar(PG_FUNCTION_ARGS);
00269 extern Datum hashint2(PG_FUNCTION_ARGS);
00270 extern Datum hashint4(PG_FUNCTION_ARGS);
00271 extern Datum hashint8(PG_FUNCTION_ARGS);
00272 extern Datum hashoid(PG_FUNCTION_ARGS);
00273 extern Datum hashenum(PG_FUNCTION_ARGS);
00274 extern Datum hashfloat4(PG_FUNCTION_ARGS);
00275 extern Datum hashfloat8(PG_FUNCTION_ARGS);
00276 extern Datum hashoidvector(PG_FUNCTION_ARGS);
00277 extern Datum hashint2vector(PG_FUNCTION_ARGS);
00278 extern Datum hashname(PG_FUNCTION_ARGS);
00279 extern Datum hashtext(PG_FUNCTION_ARGS);
00280 extern Datum hashvarlena(PG_FUNCTION_ARGS);
00281 extern Datum hash_any(register const unsigned char *k, register int keylen);
00282 extern Datum hash_uint32(uint32 k);
00283
00284
00285
00286
00287 extern void _hash_doinsert(Relation rel, IndexTuple itup);
00288 extern OffsetNumber _hash_pgaddtup(Relation rel, Buffer buf,
00289 Size itemsize, IndexTuple itup);
00290
00291
00292 extern Buffer _hash_addovflpage(Relation rel, Buffer metabuf, Buffer buf);
00293 extern BlockNumber _hash_freeovflpage(Relation rel, Buffer ovflbuf,
00294 BufferAccessStrategy bstrategy);
00295 extern void _hash_initbitmap(Relation rel, HashMetaPage metap,
00296 BlockNumber blkno, ForkNumber forkNum);
00297 extern void _hash_squeezebucket(Relation rel,
00298 Bucket bucket, BlockNumber bucket_blkno,
00299 BufferAccessStrategy bstrategy);
00300
00301
00302 extern void _hash_getlock(Relation rel, BlockNumber whichlock, int access);
00303 extern bool _hash_try_getlock(Relation rel, BlockNumber whichlock, int access);
00304 extern void _hash_droplock(Relation rel, BlockNumber whichlock, int access);
00305 extern Buffer _hash_getbuf(Relation rel, BlockNumber blkno,
00306 int access, int flags);
00307 extern Buffer _hash_getinitbuf(Relation rel, BlockNumber blkno);
00308 extern Buffer _hash_getnewbuf(Relation rel, BlockNumber blkno,
00309 ForkNumber forkNum);
00310 extern Buffer _hash_getbuf_with_strategy(Relation rel, BlockNumber blkno,
00311 int access, int flags,
00312 BufferAccessStrategy bstrategy);
00313 extern void _hash_relbuf(Relation rel, Buffer buf);
00314 extern void _hash_dropbuf(Relation rel, Buffer buf);
00315 extern void _hash_wrtbuf(Relation rel, Buffer buf);
00316 extern void _hash_chgbufaccess(Relation rel, Buffer buf, int from_access,
00317 int to_access);
00318 extern uint32 _hash_metapinit(Relation rel, double num_tuples,
00319 ForkNumber forkNum);
00320 extern void _hash_pageinit(Page page, Size size);
00321 extern void _hash_expandtable(Relation rel, Buffer metabuf);
00322
00323
00324 extern void _hash_regscan(IndexScanDesc scan);
00325 extern void _hash_dropscan(IndexScanDesc scan);
00326 extern bool _hash_has_active_scan(Relation rel, Bucket bucket);
00327 extern void ReleaseResources_hash(void);
00328
00329
00330 extern bool _hash_next(IndexScanDesc scan, ScanDirection dir);
00331 extern bool _hash_first(IndexScanDesc scan, ScanDirection dir);
00332 extern bool _hash_step(IndexScanDesc scan, Buffer *bufP, ScanDirection dir);
00333
00334
00335 typedef struct HSpool HSpool;
00336
00337 extern HSpool *_h_spoolinit(Relation heap, Relation index, uint32 num_buckets);
00338 extern void _h_spooldestroy(HSpool *hspool);
00339 extern void _h_spool(IndexTuple itup, HSpool *hspool);
00340 extern void _h_indexbuild(HSpool *hspool);
00341
00342
00343 extern bool _hash_checkqual(IndexScanDesc scan, IndexTuple itup);
00344 extern uint32 _hash_datum2hashkey(Relation rel, Datum key);
00345 extern uint32 _hash_datum2hashkey_type(Relation rel, Datum key, Oid keytype);
00346 extern Bucket _hash_hashkey2bucket(uint32 hashkey, uint32 maxbucket,
00347 uint32 highmask, uint32 lowmask);
00348 extern uint32 _hash_log2(uint32 num);
00349 extern void _hash_checkpage(Relation rel, Buffer buf, int flags);
00350 extern uint32 _hash_get_indextuple_hashkey(IndexTuple itup);
00351 extern IndexTuple _hash_form_tuple(Relation index,
00352 Datum *values, bool *isnull);
00353 extern OffsetNumber _hash_binsearch(Page page, uint32 hash_value);
00354 extern OffsetNumber _hash_binsearch_last(Page page, uint32 hash_value);
00355
00356
00357 extern void hash_redo(XLogRecPtr lsn, XLogRecord *record);
00358 extern void hash_desc(StringInfo buf, uint8 xl_info, char *rec);
00359
00360 #endif