* Allocate a new page (either by recycling, or by extending the index file) * The returned buffer is already pinned and exclusive-locked * Caller is responsible for initializing the page by calling BloomInitPage */
| 350 | * Caller is responsible for initializing the page by calling BloomInitPage |
| 351 | */ |
| 352 | Buffer |
| 353 | BloomNewBuffer(Relation index) |
| 354 | { |
| 355 | Buffer buffer; |
| 356 | bool needLock; |
| 357 | |
| 358 | /* First, try to get a page from FSM */ |
| 359 | for (;;) |
| 360 | { |
| 361 | BlockNumber blkno = GetFreeIndexPage(index); |
| 362 | |
| 363 | if (blkno == InvalidBlockNumber) |
| 364 | break; |
| 365 | |
| 366 | buffer = ReadBuffer(index, blkno); |
| 367 | |
| 368 | /* |
| 369 | * We have to guard against the possibility that someone else already |
| 370 | * recycled this page; the buffer may be locked if so. |
| 371 | */ |
| 372 | if (ConditionalLockBuffer(buffer)) |
| 373 | { |
| 374 | Page page = BufferGetPage(buffer); |
| 375 | |
| 376 | if (PageIsNew(page)) |
| 377 | return buffer; /* OK to use, if never initialized */ |
| 378 | |
| 379 | if (BloomPageIsDeleted(page)) |
| 380 | return buffer; /* OK to use */ |
| 381 | |
| 382 | LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 383 | } |
| 384 | |
| 385 | /* Can't use it, so release buffer and try again */ |
| 386 | ReleaseBuffer(buffer); |
| 387 | } |
| 388 | |
| 389 | /* Must extend the file */ |
| 390 | needLock = !RELATION_IS_LOCAL(index); |
| 391 | if (needLock) |
| 392 | LockRelationForExtension(index, ExclusiveLock); |
| 393 | |
| 394 | buffer = ReadBuffer(index, P_NEW); |
| 395 | LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); |
| 396 | |
| 397 | if (needLock) |
| 398 | UnlockRelationForExtension(index, ExclusiveLock); |
| 399 | |
| 400 | return buffer; |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Initialize any page of a bloom index. |
no test coverage detected