1 #include "pgroonga.h"
2 
3 #include "pgrn-ctid.h"
4 
5 #include <access/heapam.h>
6 #include <access/htup.h>
7 #include <storage/buf.h>
8 #include <storage/bufmgr.h>
9 #include <utils/snapmgr.h>
10 #include <utils/snapshot.h>
11 
12 bool
PGrnCtidIsAlive(Relation table,ItemPointer ctid)13 PGrnCtidIsAlive(Relation table, ItemPointer ctid)
14 {
15 	Snapshot snapshot;
16 	Buffer buffer;
17 	HeapTupleData heapTuple;
18 	bool found;
19 
20 	snapshot = GetActiveSnapshot();
21 	buffer = ReadBuffer(table, ItemPointerGetBlockNumber(ctid));
22 	LockBuffer(buffer, BUFFER_LOCK_SHARE);
23 	/* table_index_fetch_tuple_check() may be better in the future. */
24 	found = heap_hot_search_buffer(ctid,
25 								   table,
26 								   buffer,
27 								   snapshot,
28 								   &heapTuple,
29 								   NULL,
30 								   true);
31 	LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
32 	ReleaseBuffer(buffer);
33 
34 	return found;
35 }
36 
37 uint64
PGrnCtidPack(ItemPointer ctid)38 PGrnCtidPack(ItemPointer ctid)
39 {
40 	BlockNumber blockNumber;
41 	OffsetNumber offsetNumber;
42 
43 	blockNumber = ItemPointerGetBlockNumber(ctid);
44 	offsetNumber = ItemPointerGetOffsetNumber(ctid);
45 	return (((uint64)blockNumber << 16) | ((uint64)offsetNumber));
46 }
47 
48 ItemPointerData
PGrnCtidUnpack(uint64 packedCtid)49 PGrnCtidUnpack(uint64 packedCtid)
50 {
51 	ItemPointerData	ctid;
52 	ItemPointerSet(&ctid,
53 				   (packedCtid >> 16) & 0xFFFFFFFF,
54 				   packedCtid & 0xFFFF);
55 	return ctid;
56 }
57 
58