]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/tid.c
Update textin() and textout() to new fmgr style. This is just phase
[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.21 2000/07/05 23:11:35 tgl 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 /*
130  *      Functions to get latest tid of a specified tuple.
131  *
132  *      Maybe these implementations should be moved to another place
133  */
134 Datum
135 currtid_byreloid(PG_FUNCTION_ARGS)
136 {
137         Oid                             reloid = PG_GETARG_OID(0);
138         ItemPointer             tid = (ItemPointer) PG_GETARG_POINTER(1);
139         ItemPointer             result,
140                                         ret;
141         Relation                rel;
142
143         result = (ItemPointer) palloc(sizeof(ItemPointerData));
144         ItemPointerSetInvalid(result);
145         if ((rel = heap_open(reloid, AccessShareLock)) != NULL)
146         {
147                 ret = heap_get_latest_tid(rel, SnapshotNow, tid);
148                 if (ret)
149                         ItemPointerCopy(ret, result);
150                 heap_close(rel, AccessShareLock);
151         }
152         else
153                 elog(ERROR, "Relation %u not found", reloid);
154
155         PG_RETURN_POINTER(result);
156 }
157
158 Datum
159 currtid_byrelname(PG_FUNCTION_ARGS)
160 {
161         text               *relname = PG_GETARG_TEXT_P(0);
162         ItemPointer             tid = (ItemPointer) PG_GETARG_POINTER(1);
163         ItemPointer             result,
164                                         ret;
165         char               *str;
166         Relation                rel;
167
168         str = DatumGetCString(DirectFunctionCall1(textout,
169                                                                                           PointerGetDatum(relname)));
170
171         result = (ItemPointer) palloc(sizeof(ItemPointerData));
172         ItemPointerSetInvalid(result);
173         if ((rel = heap_openr(str, AccessShareLock)) != NULL)
174         {
175                 ret = heap_get_latest_tid(rel, SnapshotNow, tid);
176                 if (ret)
177                         ItemPointerCopy(ret, result);
178                 heap_close(rel, AccessShareLock);
179         }
180         else
181                 elog(ERROR, "Relation %s not found", str);
182
183         pfree(str);
184
185         PG_RETURN_POINTER(result);
186 }