]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/tid.c
Mark functions as static and ifdef NOT_USED as appropriate.
[postgresql] / src / backend / utils / adt / tid.c
1 /*-------------------------------------------------------------------------
2  *
3  * tid.c
4  *        Functions for the built-in type tuple id
5  *
6  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/utils/adt/tid.c,v 1.19 2000/06/08 22:37:28 momjian Exp $
12  *
13  * NOTES
14  *        input routine largely stolen from boxin().
15  *
16  *-------------------------------------------------------------------------
17  */
18
19 #include "postgres.h"
20
21 #include "access/heapam.h"
22 #include "utils/builtins.h"
23
24 #define LDELIM                  '('
25 #define RDELIM                  ')'
26 #define DELIM                   ','
27 #define NTIDARGS                2
28
29 /* ----------------------------------------------------------------
30  *              tidin
31  * ----------------------------------------------------------------
32  */
33 ItemPointer
34 tidin(const char *str)
35 {
36         const char *p,
37                            *coord[NTIDARGS];
38         int                     i;
39         ItemPointer result;
40
41         BlockNumber blockNumber;
42         OffsetNumber offsetNumber;
43
44         if (str == NULL)
45                 return NULL;
46
47         for (i = 0, p = str; *p && i < NTIDARGS && *p != RDELIM; p++)
48                 if (*p == DELIM || (*p == LDELIM && !i))
49                         coord[i++] = p + 1;
50
51         /* if (i < NTIDARGS - 1) */
52         if (i < NTIDARGS)
53         {
54                 elog(ERROR, "%s invalid tid format", str);
55                 return NULL;
56         }
57
58         blockNumber = (BlockNumber) atoi(coord[0]);
59         offsetNumber = (OffsetNumber) atoi(coord[1]);
60
61         result = (ItemPointer) palloc(sizeof(ItemPointerData));
62
63         ItemPointerSet(result, blockNumber, offsetNumber);
64
65         return result;
66 }
67
68 /* ----------------------------------------------------------------
69  *              tidout
70  * ----------------------------------------------------------------
71  */
72 char *
73 tidout(ItemPointer itemPtr)
74 {
75         BlockNumber blockNumber;
76         OffsetNumber offsetNumber;
77         BlockId         blockId;
78         char            buf[32];
79         char       *str;
80         static char *invalidTid = "()";
81
82         if (!itemPtr || !ItemPointerIsValid(itemPtr))
83         {
84                 str = palloc(strlen(invalidTid));
85                 strcpy(str, invalidTid);
86                 return str;
87         }
88
89         blockId = &(itemPtr->ip_blkid);
90
91         blockNumber = BlockIdGetBlockNumber(blockId);
92         offsetNumber = itemPtr->ip_posid;
93
94         sprintf(buf, "(%d,%d)", blockNumber, offsetNumber);
95
96         str = (char *) palloc(strlen(buf) + 1);
97         strcpy(str, buf);
98
99         return str;
100 }
101
102 /*****************************************************************************
103  *       PUBLIC ROUTINES                                                                                                                 *
104  *****************************************************************************/
105
106 bool
107 tideq(ItemPointer arg1, ItemPointer arg2)
108 {
109         if ((!arg1) || (!arg2))
110                 return false;
111
112         return (BlockIdGetBlockNumber(&(arg1->ip_blkid)) ==
113                         BlockIdGetBlockNumber(&(arg2->ip_blkid)) &&
114                         arg1->ip_posid == arg2->ip_posid);
115 }
116
117 #ifdef NOT_USED
118 bool
119 tidne(ItemPointer arg1, ItemPointer arg2)
120 {
121         if ((!arg1) || (!arg2))
122                 return false;
123         return (BlockIdGetBlockNumber(&(arg1->ip_blkid)) !=
124                         BlockIdGetBlockNumber(&(arg2->ip_blkid)) ||
125                         arg1->ip_posid != arg2->ip_posid);
126 }
127 #endif
128
129 #ifdef NOT_USED
130 text *
131 tid_text(ItemPointer tid)
132 {
133         char       *str;
134
135         if (!tid)
136                 return (text *) NULL;
137         str = tidout(tid);
138
139         return textin(str);
140 }       /* tid_text() */
141 #endif
142
143 #ifdef NOT_USED
144 ItemPointer
145 text_tid(const text *string)
146 {
147         ItemPointer result;
148         char       *str;
149
150         if (!string)
151                 return (ItemPointer) 0;
152
153         str = textout((text *) string);
154         result = tidin(str);
155         pfree(str);
156
157         return result;
158 }       /* text_tid() */
159 #endif
160
161 /*
162  *      Functions to get latest tid of a specified tuple.
163  *      Maybe these implementations is moved
164  *      to another place
165 */
166 ItemPointer
167 currtid_byreloid(Oid reloid, ItemPointer tid)
168 {
169         ItemPointer result = NULL,
170                                 ret;
171         Relation        rel;
172
173         result = (ItemPointer) palloc(sizeof(ItemPointerData));
174         ItemPointerSetInvalid(result);
175         if (rel = heap_open(reloid, AccessShareLock), rel)
176         {
177                 ret = heap_get_latest_tid(rel, SnapshotNow, tid);
178                 if (ret)
179                         ItemPointerCopy(ret, result);
180                 heap_close(rel, AccessShareLock);
181         }
182         else
183                 elog(ERROR, "Relation %u not found", reloid);
184
185         return result;
186 }       /* currtid_byreloid() */
187
188 ItemPointer
189 currtid_byrelname(const text *relname, ItemPointer tid)
190 {
191         ItemPointer result = NULL,
192                                 ret;
193         char       *str;
194         Relation        rel;
195
196         if (!relname)
197                 return result;
198
199         str = textout((text *) relname);
200
201         result = (ItemPointer) palloc(sizeof(ItemPointerData));
202         ItemPointerSetInvalid(result);
203         if (rel = heap_openr(str, AccessShareLock), rel)
204         {
205                 ret = heap_get_latest_tid(rel, SnapshotNow, tid);
206                 if (ret)
207                         ItemPointerCopy(ret, result);
208                 heap_close(rel, AccessShareLock);
209         }
210         else
211                 elog(ERROR, "Relation %s not found", textout((text *) relname));
212         pfree(str);
213
214         return result;
215 }       /* currtid_byrelname() */