-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg_dms_ref.c
180 lines (177 loc) · 4.54 KB
/
pg_dms_ref.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "postgres.h"
#include "fmgr.h"
#include "utils/fmgrprotos.h"
#include "utils/uuid.h"
#include "utils/timestamp.h"
#include "utils/lsyscache.h"
#include "utils/builtins.h"
#include "utils/array.h"
#include "access/xact.h"
#include "access/htup_details.h"
#include "miscadmin.h"
#include "funcapi.h"
#include "pg_dms.h"
//
//
// ref type
//
//
PG_FUNCTION_INFO_V1(pg_dms_ref_in);
PG_FUNCTION_INFO_V1(pg_dms_ref_out);
Datum
pg_dms_ref_in(PG_FUNCTION_ARGS)
{
char *str = PG_GETARG_CSTRING(0);
if(strlen(str)!=73)
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("key -%s is invalid", str)));
pg_dms_ref *result = palloc(sizeof(pg_dms_ref));
result->family = *DatumGetUUIDP(DirectFunctionCall1(uuid_in, CStringGetDatum(strtok (str, "_"))));
result->version = *DatumGetUUIDP(DirectFunctionCall1(uuid_in, CStringGetDatum(strtok (NULL, "_"))));
PG_RETURN_POINTER(result);
}
Datum
pg_dms_ref_out(PG_FUNCTION_ARGS)
{
pg_dms_ref *ref = PG_GETARG_PGDMSREF_P(0);
char *f = DatumGetCString(DirectFunctionCall1(uuid_out, UUIDPGetDatum(&ref->family)));
char *v = DatumGetCString(DirectFunctionCall1(uuid_out, UUIDPGetDatum(&ref->version)));
char *result = psprintf("%s_%s", f, v);
PG_RETURN_CSTRING(result);
};
//
//
// ref <-> ref
//
//
PG_FUNCTION_INFO_V1(pg_dms_ref_cmp);
PG_FUNCTION_INFO_V1(pg_dms_refgt);
PG_FUNCTION_INFO_V1(pg_dms_refge);
PG_FUNCTION_INFO_V1(pg_dms_refeq);
PG_FUNCTION_INFO_V1(pg_dms_reflt);
PG_FUNCTION_INFO_V1(pg_dms_refle);
//
static int ref_cmp (pg_dms_ref *a, pg_dms_ref *b)
{
return memcmp(&(a->family), &(b->family),UUID_LEN*2);
};
Datum
pg_dms_ref_cmp(PG_FUNCTION_ARGS)
{
PG_RETURN_INT32(ref_cmp(PG_GETARG_PGDMSREF_P(0), PG_GETARG_PGDMSREF_P(1)));
};
Datum
pg_dms_refgt(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(ref_cmp(PG_GETARG_PGDMSREF_P(0), PG_GETARG_PGDMSREF_P(1))>0);
};
Datum
pg_dms_refge(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(ref_cmp(PG_GETARG_PGDMSREF_P(0), PG_GETARG_PGDMSREF_P(1))>=0);
};
Datum
pg_dms_refeq(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(ref_cmp(PG_GETARG_PGDMSREF_P(0), PG_GETARG_PGDMSREF_P(1))==0);
};
Datum
pg_dms_refle(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(ref_cmp(PG_GETARG_PGDMSREF_P(0), PG_GETARG_PGDMSREF_P(1))<=0);
};
Datum
pg_dms_reflt(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(ref_cmp(PG_GETARG_PGDMSREF_P(0), PG_GETARG_PGDMSREF_P(1))<0);
};
//
//
// id <-> ref
//
//
PG_FUNCTION_INFO_V1(pg_dms_idref_cmp);
PG_FUNCTION_INFO_V1(pg_dms_idrefgt);
PG_FUNCTION_INFO_V1(pg_dms_idrefge);
PG_FUNCTION_INFO_V1(pg_dms_idrefeq);
PG_FUNCTION_INFO_V1(pg_dms_idreflt);
PG_FUNCTION_INFO_V1(pg_dms_idrefle);
//
static int idref_cmp (pg_dms_id *a, pg_dms_ref *b)
{
return memcmp(&(a->family), &(b->family), UUID_LEN*2);
}
Datum
pg_dms_idref_cmp(PG_FUNCTION_ARGS)
{
PG_RETURN_INT32(idref_cmp(PG_GETARG_PGDMSID_P(0), PG_GETARG_PGDMSREF_P(1)));
};
Datum
pg_dms_idrefgt(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(idref_cmp(PG_GETARG_PGDMSID_P(0), PG_GETARG_PGDMSREF_P(1))>0);
};
Datum
pg_dms_idrefge(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(idref_cmp(PG_GETARG_PGDMSID_P(0), PG_GETARG_PGDMSREF_P(1))>=0);
};
Datum
pg_dms_idrefeq(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(idref_cmp(PG_GETARG_PGDMSID_P(0), PG_GETARG_PGDMSREF_P(1))==0);
};
Datum
pg_dms_idrefle(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(idref_cmp(PG_GETARG_PGDMSID_P(0), PG_GETARG_PGDMSREF_P(1))<=0);
};
Datum
pg_dms_idreflt(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(idref_cmp(PG_GETARG_PGDMSID_P(0), PG_GETARG_PGDMSREF_P(1))<0);
};
//
//
// ref <-> id
//
//
PG_FUNCTION_INFO_V1(pg_dms_refid_cmp);
PG_FUNCTION_INFO_V1(pg_dms_refidgt);
PG_FUNCTION_INFO_V1(pg_dms_refidge);
PG_FUNCTION_INFO_V1(pg_dms_refideq);
PG_FUNCTION_INFO_V1(pg_dms_refidlt);
PG_FUNCTION_INFO_V1(pg_dms_refidle);
static int refid_cmp (pg_dms_ref *a, pg_dms_id *b)
{
return memcmp( &(a->family), &(b->family), UUID_LEN*2);
}
Datum
pg_dms_refid_cmp(PG_FUNCTION_ARGS)
{
PG_RETURN_INT32(refid_cmp(PG_GETARG_PGDMSREF_P(0),PG_GETARG_PGDMSID_P(1)));
};
Datum
pg_dms_refidgt(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(refid_cmp(PG_GETARG_PGDMSREF_P(0),PG_GETARG_PGDMSID_P(1))>0);
};
Datum
pg_dms_refidge(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(refid_cmp(PG_GETARG_PGDMSREF_P(0),PG_GETARG_PGDMSID_P(1))>=0);
};
Datum
pg_dms_refideq(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(refid_cmp(PG_GETARG_PGDMSREF_P(0),PG_GETARG_PGDMSID_P(1))==0);
};
Datum
pg_dms_refidle(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(refid_cmp(PG_GETARG_PGDMSREF_P(0),PG_GETARG_PGDMSID_P(1))<=0);
};
Datum
pg_dms_refidlt(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(refid_cmp(PG_GETARG_PGDMSREF_P(0),PG_GETARG_PGDMSID_P(1))<0);
};