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