forked from couchbase/couchbase-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctranscoder.c
More file actions
240 lines (206 loc) · 5.64 KB
/
Copy pathctranscoder.c
File metadata and controls
240 lines (206 loc) · 5.64 KB
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
/**
* Copyright 2013 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
/**
* An optimized Trancoder class. Users may subclass this object and only use
* some of the transcoder methods
*/
#include "pycbc.h"
#include "structmember.h"
static PyObject *
encode_key(PyObject *self, PyObject *args)
{
int rv;
char *buf;
size_t nbuf;
PyObject *kobj;
rv = PyArg_ParseTuple(args, "O", &kobj);
if (!rv) {
return NULL;
}
rv = pycbc_tc_simple_encode(&kobj, &buf, &nbuf, PYCBC_FMT_UTF8);
if (rv < 0) {
return NULL;
}
(void)self;
return kobj;
}
static PyObject *
decode_key(PyObject *self, PyObject *args)
{
int rv;
char *buf;
PyObject *bobj;
Py_ssize_t plen;
rv = PyArg_ParseTuple(args, "O", &bobj);
if (!rv) {
return NULL;
}
rv = PyBytes_AsStringAndSize(bobj, &buf, &plen);
if (rv < 0) {
return NULL;
}
rv = pycbc_tc_simple_decode(&bobj, buf, plen, PYCBC_FMT_UTF8);
if (rv < 0) {
return NULL;
}
(void)self;
return bobj;
}
static PyObject *
encode_value(PyObject *self, PyObject *args)
{
unsigned long flags;
int rv;
PyObject *vobj;
PyObject *flagsobj;
char *buf;
PyObject *ret;
size_t nbuf;
rv = PyArg_ParseTuple(args, "OO", &vobj, &flagsobj);
if (!rv) {
return NULL;
}
rv = pycbc_get_u32(flagsobj, &flags);
if (rv < 0) {
return NULL;
}
rv = pycbc_tc_simple_encode(&vobj, &buf, &nbuf, flags);
if (rv < 0) {
return NULL;
}
ret = PyTuple_New(2);
PyTuple_SET_ITEM(ret, 0, vobj);
PyTuple_SET_ITEM(ret, 1, flagsobj);
/** INCREF flags because we got it as an argument */
Py_INCREF(flagsobj);
(void)self;
return ret;
}
static PyObject *
decode_value(PyObject *self, PyObject *args)
{
PyObject *flagsobj;
PyObject *vobj;
char *buf;
Py_ssize_t nbuf;
int rv;
unsigned long flags;
rv = PyArg_ParseTuple(args, "OO", &vobj, &flagsobj);
if (!rv) {
return NULL;
}
rv = PyBytes_AsStringAndSize(vobj, &buf, &nbuf);
if (rv < 0) {
return NULL;
}
rv = pycbc_get_u32(flagsobj, &flags);
if (rv < 0) {
return NULL;
}
rv = pycbc_tc_simple_decode(&vobj, buf, nbuf, flags);
if (rv < 0) {
return NULL;
}
(void)self;
return vobj;
}
static PyTypeObject TranscoderType = {
PYCBC_POBJ_HEAD_INIT(NULL)
0
};
typedef struct {
PyObject_HEAD
} TranscoderObject;
PyDoc_STRVAR(encode_key_doc,
"Encode the key as a bytes object.\n"
"\n"
":param key: This is an object passed as a string key.\n"
" There is no restriction on this type\n"
"\n"
":return: a bytes object\n"
" The default implementation encodes the key as UTF-8.\n"
" On Python 2.x, ``bytes`` is a synonym for ``str``. On Python 3.x,\n"
" ``bytes`` and ``str`` are distinct objects, in which one must first\n"
" *encode* a string to a specific encoding\n"
"\n");
PyDoc_STRVAR(decode_key_doc,
"Convert the key from bytes into something else.\n"
"\n"
":param bytes key: The key, in the form of a bytearray\n"
"\n"
":return: a string or other object your application will use\n"
" The returned key *must* be hashable\n"
"\n"
"The default implementation decodes the keys from UTF-8.\n"
"\n");
PyDoc_STRVAR(encode_value_doc,
"Encode the value into something meaningful\n"
"\n"
":param any value: A value. This may be a string or a complex python\n"
" object.\n"
":param any format: The `format` argument as passed to the mutator\n"
"\n"
":return: A tuple of ``(value, flags)``\n"
" ``value`` must be a ``bytes`` object. ``flags`` must be an integer type\n"
" whose value does not exceed 32 bits\n"
"\n");
PyDoc_STRVAR(decode_value_doc,
"Decode the value from the raw bytes representation into something\n"
"meaningful\n"
"\n"
":param bytes value: Raw bytes, as stored on the server\n"
":param int flags: The flags for the value\n"
"\n"
":return: Something meaningful to be used as a value within the\n"
" application\n"
"\n");
static PyMethodDef cTranscoder_methods[] = {
{ PYCBC_TCNAME_ENCODE_KEY, (PyCFunction)encode_key,
METH_VARARGS, encode_key_doc
},
{ PYCBC_TCNAME_DECODE_KEY, (PyCFunction)decode_key,
METH_VARARGS, decode_key_doc
},
{ PYCBC_TCNAME_ENCODE_VALUE, (PyCFunction)encode_value,
METH_VARARGS, encode_value_doc
},
{ PYCBC_TCNAME_DECODE_VALUE, (PyCFunction)decode_value,
METH_VARARGS, decode_value_doc
},
{ NULL }
};
static void
transcoder_dealloc(PyObject *o)
{
Py_TYPE(o)->tp_free(o);
}
int
pycbc_TranscoderType_init(PyObject **ptr)
{
PyTypeObject *p = &TranscoderType;
*ptr = (PyObject*)p;
if (p->tp_name) {
return 0;
}
p->tp_name = "Transcoder";
p->tp_doc = PyDoc_STR("Efficient, subclassable transcoder interface/class");
p->tp_dealloc = transcoder_dealloc;
p->tp_basicsize = sizeof(TranscoderObject);
p->tp_methods = cTranscoder_methods;
p->tp_new = PyType_GenericNew;
p->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
return PyType_Ready(p);
}