-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpg_xid.c
71 lines (57 loc) · 1.18 KB
/
pg_xid.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
#include "postgres.h"
#include "fmgr.h"
#include "funcapi.h"
#include "catalog/pg_type.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "xid.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
extern void _PG_init(void);
extern Datum xid( PG_FUNCTION_ARGS );
extern Datum xid_encoded( PG_FUNCTION_ARGS );
/*
* Module initialization function
*/
void
_PG_init(void) {
xid_init();
}
/*
* Generate ID
*/
PG_FUNCTION_INFO_V1( xid );
Datum
xid( PG_FUNCTION_ARGS )
{
// Declaration
bytea *result;
unsigned char *id;
id = xid_generate();
result = (bytea *)palloc(XID_RAW_LEN + VARHDRSZ);
SET_VARSIZE(result, XID_RAW_LEN + VARHDRSZ);
memcpy(VARDATA(result), id, XID_RAW_LEN);
free(id);
PG_RETURN_BYTEA_P(result);
}
/*
* Generate ID Encoded
*/
PG_FUNCTION_INFO_V1( xid_encoded );
Datum
xid_encoded( PG_FUNCTION_ARGS )
{
// Declaration
text *result;
unsigned char *id;
unsigned char *encoded;
id = xid_generate();
encoded = xid_encode(id);
result = (text *)palloc(XID_ENCODED_LEN + VARHDRSZ);
SET_VARSIZE(result, XID_ENCODED_LEN + VARHDRSZ);
memcpy(VARDATA(result), encoded, XID_ENCODED_LEN);
free(id);
free(encoded);
PG_RETURN_TEXT_P(result);
}