File: wirestate.c

package info (click to toggle)
py-postgresql 1.2.1%2Bgit20180803.ef7b9a9-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,620 kB
  • sloc: python: 18,317; ansic: 2,024; sql: 282; sh: 26; makefile: 22
file content (289 lines) | stat: -rw-r--r-- 6,746 bytes parent folder | download | duplicates (4)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * .port.optimized.WireState - PQ wire state for COPY.
 */
#define include_wirestate_types \
	mTYPE(WireState)

struct wirestate
{
	PyObject_HEAD
	char size_fragment[4];				/* the header fragment; continuation specifies bytes read so far. */
	PyObject *final_view;				/* Py_None unless we reach an unknown message */
	Py_ssize_t remaining_bytes;		/* Bytes remaining in message */
	short continuation;					/* >= 0 when continuing a fragment */
};

static void
ws_dealloc(PyObject *self)
{
	struct wirestate *ws = ((struct wirestate *) self);
	Py_XDECREF(ws->final_view);
	Py_TYPE(self)->tp_free(self);
}

static PyObject *
ws_new(PyTypeObject *subtype, PyObject *args, PyObject *kw)
{
	static char *kwlist[] = {"condition", NULL};
	struct wirestate *ws;
	PyObject *rob;

	if (!PyArg_ParseTupleAndKeywords(args, kw, "|O", kwlist, &rob))
		return(NULL);

	rob = subtype->tp_alloc(subtype, 0);
	ws = ((struct wirestate *) rob);

	ws->continuation = -1;
	ws->remaining_bytes = 0;
	ws->final_view = NULL;

	return(rob);
}

#define CONDITION(MSGTYPE) (MSGTYPE != 'd')

static PyObject *
ws_update(PyObject *self, PyObject *view)
{
	struct wirestate *ws;
	uint32_t remaining_bytes, nmessages = 0;
	unsigned char *buf, msgtype;
	char size_fragment[4];
	short continuation;
	Py_ssize_t position = 0, len;
	PyObject *rob, *final_view = NULL;

	if (PyObject_AsReadBuffer(view, (const void **) &buf, &len))
		return(NULL);

	if (len == 0)
	{
		/*
		 * Nothing changed.
		 */
		return(PyLong_FromUnsignedLong(0));
	}

	ws = (struct wirestate *) self;

	if (ws->final_view)
	{
		PyErr_SetString(PyExc_RuntimeError, "wire state has been terminated");
		return(NULL);
	}

	remaining_bytes = ws->remaining_bytes;
	continuation = ws->continuation;

	if (continuation >= 0)
	{
		short sf_len = continuation, added;
		/*
		 * Continuation of message header.
		 */
		added = 4 - sf_len;
		/*
		 * If the buffer's length does not provide, limit to len.
		 */
		if (len < added)
			added = len;

		Py_MEMCPY(size_fragment, ws->size_fragment, 4);
		Py_MEMCPY(size_fragment + sf_len, buf, added);

		continuation = continuation + added;
		if (continuation == 4)
		{
			/*
			 * Completed the size part of the header.
			 */
			Py_MEMCPY(&remaining_bytes, size_fragment, 4);
			remaining_bytes = (local_ntohl((int32_t) remaining_bytes));
			if (remaining_bytes < 4)
				goto invalid_message_header;

			remaining_bytes = remaining_bytes - sf_len;
			if (remaining_bytes == 0)
				++nmessages;
			continuation = -1;
		}
		else
		{
			/*
			 * Consumed more of the header, but more is still needed.
			 * Jump past the main loop.
			 */
			goto return_nmessages;
		}
	}

	do
	{
		if (remaining_bytes > 0)
		{
			position = position + remaining_bytes;
			if (position > len)
			{
				remaining_bytes = position - len;
				position = len;
			}
			else
			{
				remaining_bytes = 0;
				++nmessages;
			}
		}

		/*
		 * Done with view.
		 */
		if (position >= len)
			break;

		/*
		 * Validate message type.
		 */
		msgtype = *(buf + position);
		if (CONDITION(msgtype))
		{
			final_view = PySequence_GetSlice(view, position, len);
			break;
		}

		/*
		 * Have enough for a complete header?
		 */
		if (len - position < 5)
		{
			/*
			 * Start a continuation. Message type has been verified.
			 */
			continuation = (len - position) - 1;
			Py_MEMCPY(size_fragment, buf + position + 1, (Py_ssize_t) continuation);
			break;
		}

		/*
		 * +1 to include the message type.
		 */
		Py_MEMCPY(&remaining_bytes, buf + position + 1, 4);
		remaining_bytes = local_ntohl((int32_t) remaining_bytes) + 1;
		if (remaining_bytes < 5)
			goto invalid_message_header;
	} while(1);

return_nmessages:
	rob = PyLong_FromUnsignedLong(nmessages);
	if (rob == NULL)
	{
		Py_XDECREF(final_view);
		return(NULL);
	}

	/* Commit new state */
	ws->remaining_bytes = remaining_bytes;
	ws->final_view = final_view;
	ws->continuation = continuation;
	Py_MEMCPY(ws->size_fragment, size_fragment, 4);
	return(rob);

invalid_message_header:
	PyErr_SetString(PyExc_ValueError, "message header contained an invalid size");
	return(NULL);
}

static PyMethodDef ws_methods[] = {
	{"update", ws_update, METH_O,
		PyDoc_STR("update the state of the wire using the given buffer object"),},
	{NULL}
};

PyObject *
ws_size_fragment(PyObject *self, void *closure)
{
	struct wirestate *ws;
	ws = (struct wirestate *) self;

	return(PyBytes_FromStringAndSize(ws->size_fragment,
		ws->continuation <= 0 ? 0 : ws->continuation));
}

PyObject *
ws_remaining_bytes(PyObject *self, void *closure)
{
	struct wirestate *ws;
	ws = (struct wirestate *) self;
	return(PyLong_FromLong(
		ws->continuation == -1 ? ws->remaining_bytes : -1
	));
}

PyObject *
ws_final_view(PyObject *self, void *closure)
{
	struct wirestate *ws;
	PyObject *rob;

	ws = (struct wirestate *) self;
	rob = ws->final_view ? ws->final_view : Py_None;

	Py_INCREF(rob);
	return(rob);
}

static PyGetSetDef ws_getset[] = {
	{"size_fragment", ws_size_fragment, NULL,
		PyDoc_STR("The data acculumated for the continuation."), NULL,},
	{"remaining_bytes", ws_remaining_bytes, NULL,
		PyDoc_STR("Number bytes necessary to complete the current message."), NULL,},
	{"final_view", ws_final_view, NULL,
		PyDoc_STR("A memoryview of the data that triggered the CONDITION()."), NULL,},
	{NULL}
};

PyTypeObject WireState_Type = {
	PyVarObject_HEAD_INIT(NULL, 0)
	"postgresql.port.optimized.WireState",	/* tp_name */
	sizeof(struct wirestate),				/* tp_basicsize */
	0,												/* tp_itemsize */
	ws_dealloc,									/* tp_dealloc */
	NULL,											/* tp_print */
	NULL,											/* tp_getattr */
	NULL,											/* tp_setattr */
	NULL,											/* tp_compare */
	NULL,											/* tp_repr */
	NULL,											/* tp_as_number */
	NULL,											/* tp_as_sequence */
	NULL,											/* tp_as_mapping */
	NULL,											/* tp_hash */
	NULL,											/* tp_call */
	NULL,											/* tp_str */
	NULL,											/* tp_getattro */
	NULL,											/* tp_setattro */
	NULL,											/* tp_as_buffer */
	Py_TPFLAGS_DEFAULT,						/* tp_flags */
	PyDoc_STR("Track the state of the wire."),
		/* tp_doc */
	NULL,											/* tp_traverse */
	NULL,											/* tp_clear */
	NULL,											/* tp_richcompare */
	0,												/* tp_weaklistoffset */
	NULL,											/* tp_iter */
	NULL,											/* tp_iternext */
	ws_methods,									/* tp_methods */
	NULL,											/* tp_members */
	ws_getset,									/* tp_getset */
	NULL,											/* tp_base */
	NULL,											/* tp_dict */
	NULL,											/* tp_descr_get */
	NULL,											/* tp_descr_set */
	0,												/* tp_dictoffset */
	NULL,											/* tp_init */
	NULL,											/* tp_alloc */
	ws_new,										/* tp_new */
	NULL,											/* tp_free */
};
/*
 * vim: ts=3:sw=3:noet:
 */