~vcs-imports/mammoth-replicator/trunk

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
#include "postgres.h"

#include "postgres.h"
#include "fmgr.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_namespace.h"
#include "utils/syscache.h"

#include "ts_cfg.h"
#include "dict.h"
#include "wparser.h"
#include "snmap.h"
#include "common.h"
#include "tsvector.h"



#include "common.h"
#include "wparser.h"
#include "ts_cfg.h"
#include "dict.h"


Oid			TSNSP_FunctionOid = InvalidOid;


text *
char2text(char *in)
{
	return charl2text(in, strlen(in));
}

text *
charl2text(char *in, int len)
{
	text	   *out = (text *) palloc(len + VARHDRSZ);

	memcpy(VARDATA(out), in, len);
	VARATT_SIZEP(out) = len + VARHDRSZ;
	return out;
}

char
		   *
text2char(text *in)
{
	char	   *out = palloc(VARSIZE(in));

	memcpy(out, VARDATA(in), VARSIZE(in) - VARHDRSZ);
	out[VARSIZE(in) - VARHDRSZ] = '\0';
	return out;
}

char
		   *
pnstrdup(char *in, int len)
{
	char	   *out = palloc(len + 1);

	memcpy(out, in, len);
	out[len] = '\0';
	return out;
}

text
		   *
ptextdup(text *in)
{
	text	   *out = (text *) palloc(VARSIZE(in));

	memcpy(out, in, VARSIZE(in));
	return out;
}

text
		   *
mtextdup(text *in)
{
	text	   *out = (text *) malloc(VARSIZE(in));

	if (!out)
		ts_error(ERROR, "No memory");
	memcpy(out, in, VARSIZE(in));
	return out;
}

void
ts_error(int state, const char *format,...)
{
	va_list		args;
	int			tlen = 128,
				len = 0;
	char	   *buf;

	reset_cfg();
	reset_dict();
	reset_prs();

	va_start(args, format);
	buf = palloc(tlen);
	len = vsnprintf(buf, tlen - 1, format, args);
	if (len >= tlen)
	{
		tlen = len + 1;
		buf = repalloc(buf, tlen);
		vsnprintf(buf, tlen - 1, format, args);
	}
	va_end(args);

	/* ?? internal error ?? */
	elog(state, "%s", buf);
	pfree(buf);
}

int
text_cmp(text *a, text *b)
{
	if (VARSIZE(a) == VARSIZE(b))
		return strncmp(VARDATA(a), VARDATA(b), VARSIZE(a) - VARHDRSZ);
	return (int) VARSIZE(a) - (int) VARSIZE(b);

}

char *
get_namespace(Oid funcoid)
{
	HeapTuple	tuple;
	Form_pg_proc proc;
	Form_pg_namespace nsp;
	Oid			nspoid;
	char	   *txt;

	tuple = SearchSysCache(PROCOID, ObjectIdGetDatum(funcoid), 0, 0, 0);
	if (!HeapTupleIsValid(tuple))
		elog(ERROR, "cache lookup failed for proc oid %u", funcoid);
	proc = (Form_pg_proc) GETSTRUCT(tuple);
	nspoid = proc->pronamespace;
	ReleaseSysCache(tuple);

	tuple = SearchSysCache(NAMESPACEOID, ObjectIdGetDatum(nspoid), 0, 0, 0);
	if (!HeapTupleIsValid(tuple))
		elog(ERROR, "cache lookup failed for namespace oid %u", nspoid);
	nsp = (Form_pg_namespace) GETSTRUCT(tuple);
	txt = pstrdup(NameStr((nsp->nspname)));
	ReleaseSysCache(tuple);

	return txt;
}

Oid
get_oidnamespace(Oid funcoid)
{
	HeapTuple	tuple;
	Form_pg_proc proc;
	Oid			nspoid;

	tuple = SearchSysCache(PROCOID, ObjectIdGetDatum(funcoid), 0, 0, 0);
	if (!HeapTupleIsValid(tuple))
		elog(ERROR, "cache lookup failed for proc oid %u", funcoid);
	proc = (Form_pg_proc) GETSTRUCT(tuple);
	nspoid = proc->pronamespace;
	ReleaseSysCache(tuple);

	return nspoid;
}