|
| 1 | +/* |
| 2 | + * Copyright 2019 GridGain Systems, Inc. and Contributors. |
| 3 | + * |
| 4 | + * Licensed under the GridGain Community Edition License (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <Python.h> |
| 18 | + |
| 19 | +#ifdef _MSC_VER |
| 20 | + |
| 21 | +typedef __int32 int32_t; |
| 22 | +typedef unsigned __int32 uint32_t; |
| 23 | +typedef __int64 int64_t; |
| 24 | +typedef unsigned __int64 uint64_t; |
| 25 | + |
| 26 | +#else |
| 27 | +#include <stdint.h> |
| 28 | +#endif |
| 29 | + |
| 30 | +static int32_t FNV1_OFFSET_BASIS = 0x811c9dc5; |
| 31 | +static int32_t FNV1_PRIME = 0x01000193; |
| 32 | + |
| 33 | + |
| 34 | +PyObject* hashcode(PyObject* self, PyObject *args); |
| 35 | +PyObject* schema_id(PyObject* self, PyObject *args); |
| 36 | + |
| 37 | +PyObject* str_hashcode(PyObject* data); |
| 38 | +int32_t str_hashcode_(PyObject* data, int lower); |
| 39 | +PyObject* b_hashcode(PyObject* data); |
| 40 | + |
| 41 | +static PyMethodDef methods[] = { |
| 42 | + {"hashcode", (PyCFunction) hashcode, METH_VARARGS, ""}, |
| 43 | + {"schema_id", (PyCFunction) schema_id, METH_VARARGS, ""}, |
| 44 | + {NULL, NULL, 0, NULL} /* Sentinel */ |
| 45 | +}; |
| 46 | + |
| 47 | +static struct PyModuleDef moduledef = { |
| 48 | + PyModuleDef_HEAD_INIT, |
| 49 | + "_cutils", |
| 50 | + 0, /* m_doc */ |
| 51 | + -1, /* m_size */ |
| 52 | + methods, /* m_methods */ |
| 53 | + NULL, /* m_slots */ |
| 54 | + NULL, /* m_traverse */ |
| 55 | + NULL, /* m_clear */ |
| 56 | + NULL, /* m_free */ |
| 57 | +}; |
| 58 | + |
| 59 | +static char* hashcode_input_err = "supported only strings, bytearrays, bytes and memoryview"; |
| 60 | +static char* schema_id_input_err = "input argument must be dict or int"; |
| 61 | +static char* schema_field_type_err = "schema keys must be strings"; |
| 62 | + |
| 63 | +PyMODINIT_FUNC PyInit__cutils(void) { |
| 64 | + return PyModule_Create(&moduledef); |
| 65 | +} |
| 66 | + |
| 67 | +PyObject* hashcode(PyObject* self, PyObject *args) { |
| 68 | + PyObject* data; |
| 69 | + |
| 70 | + if (!PyArg_ParseTuple(args, "O", &data)) { |
| 71 | + return NULL; |
| 72 | + } |
| 73 | + |
| 74 | + if (data == Py_None) { |
| 75 | + return PyLong_FromLong(0); |
| 76 | + } |
| 77 | + else if (PyUnicode_CheckExact(data)) { |
| 78 | + return str_hashcode(data); |
| 79 | + } |
| 80 | + else { |
| 81 | + return b_hashcode(data); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +PyObject* str_hashcode(PyObject* data) { |
| 86 | + return PyLong_FromLong(str_hashcode_(data, 0)); |
| 87 | +} |
| 88 | + |
| 89 | +int32_t str_hashcode_(PyObject *str, int lower) { |
| 90 | + int32_t res = 0; |
| 91 | + |
| 92 | + Py_ssize_t sz = PyUnicode_GET_LENGTH(str); |
| 93 | + if (!sz) { |
| 94 | + return res; |
| 95 | + } |
| 96 | + |
| 97 | + int kind = PyUnicode_KIND(str); |
| 98 | + void* buf = PyUnicode_DATA(str); |
| 99 | + |
| 100 | + Py_ssize_t i; |
| 101 | + for (i = 0; i < sz; i++) { |
| 102 | + Py_UCS4 ch = PyUnicode_READ(kind, buf, i); |
| 103 | + |
| 104 | + if (lower) { |
| 105 | + ch = Py_UNICODE_TOLOWER(ch); |
| 106 | + } |
| 107 | + |
| 108 | + res = 31 * res + ch; |
| 109 | + } |
| 110 | + |
| 111 | + return res; |
| 112 | +} |
| 113 | + |
| 114 | +PyObject* b_hashcode(PyObject* data) { |
| 115 | + int32_t res = 1; |
| 116 | + Py_ssize_t sz; char* buf; |
| 117 | + |
| 118 | + if (PyBytes_CheckExact(data)) { |
| 119 | + sz = PyBytes_GET_SIZE(data); |
| 120 | + buf = PyBytes_AS_STRING(data); |
| 121 | + } |
| 122 | + else if (PyByteArray_CheckExact(data)) { |
| 123 | + sz = PyByteArray_GET_SIZE(data); |
| 124 | + buf = PyByteArray_AS_STRING(data); |
| 125 | + } |
| 126 | + else if (PyMemoryView_Check(data)) { |
| 127 | + Py_buffer* pyBuf = PyMemoryView_GET_BUFFER(data); |
| 128 | + sz = pyBuf->len; |
| 129 | + buf = (char*)pyBuf->buf; |
| 130 | + } |
| 131 | + else { |
| 132 | + PyErr_SetString(PyExc_ValueError, hashcode_input_err); |
| 133 | + return NULL; |
| 134 | + } |
| 135 | + |
| 136 | + Py_ssize_t i; |
| 137 | + for (i = 0; i < sz; i++) { |
| 138 | + res = 31 * res + (signed char)buf[i]; |
| 139 | + } |
| 140 | + |
| 141 | + return PyLong_FromLong(res); |
| 142 | +} |
| 143 | + |
| 144 | +PyObject* schema_id(PyObject* self, PyObject *args) { |
| 145 | + PyObject* data; |
| 146 | + |
| 147 | + if (!PyArg_ParseTuple(args, "O", &data)) { |
| 148 | + return NULL; |
| 149 | + } |
| 150 | + |
| 151 | + if (PyLong_CheckExact(data)) { |
| 152 | + return PyNumber_Long(data); |
| 153 | + } |
| 154 | + else if (data == Py_None) { |
| 155 | + return PyLong_FromLong(0); |
| 156 | + } |
| 157 | + else if (PyDict_Check(data)) { |
| 158 | + Py_ssize_t sz = PyDict_Size(data); |
| 159 | + |
| 160 | + if (sz == 0) { |
| 161 | + return PyLong_FromLong(0); |
| 162 | + } |
| 163 | + |
| 164 | + int32_t s_id = FNV1_OFFSET_BASIS; |
| 165 | + |
| 166 | + PyObject *key, *value; |
| 167 | + Py_ssize_t pos = 0; |
| 168 | + |
| 169 | + while (PyDict_Next(data, &pos, &key, &value)) { |
| 170 | + if (!PyUnicode_CheckExact(key)) { |
| 171 | + PyErr_SetString(PyExc_ValueError, schema_field_type_err); |
| 172 | + return NULL; |
| 173 | + } |
| 174 | + |
| 175 | + int32_t field_id = str_hashcode_(key, 1); |
| 176 | + s_id ^= field_id & 0xff; |
| 177 | + s_id *= FNV1_PRIME; |
| 178 | + s_id ^= (field_id >> 8) & 0xff; |
| 179 | + s_id *= FNV1_PRIME; |
| 180 | + s_id ^= (field_id >> 16) & 0xff; |
| 181 | + s_id *= FNV1_PRIME; |
| 182 | + s_id ^= (field_id >> 24) & 0xff; |
| 183 | + s_id *= FNV1_PRIME; |
| 184 | + } |
| 185 | + |
| 186 | + return PyLong_FromLong(s_id); |
| 187 | + } |
| 188 | + else { |
| 189 | + PyErr_SetString(PyExc_ValueError, schema_id_input_err); |
| 190 | + return NULL; |
| 191 | + } |
| 192 | +} |
0 commit comments