| 1 |
/** |
| 2 |
* Copyright (c) 2016-present, Gregory Szorc |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* This software may be modified and distributed under the terms |
| 6 |
* of the BSD license. See the LICENSE file for details. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "python-zstandard.h" |
| 10 |
|
| 11 |
extern PyObject *ZstdError; |
| 12 |
|
| 13 |
ZstdCompressionDict *train_dictionary(PyObject *self, PyObject *args, |
| 14 |
PyObject *kwargs) { |
| 15 |
static char *kwlist[] = { |
| 16 |
"dict_size", "samples", "k", "d", |
| 17 |
"f", "split_point", "accel", "notifications", |
| 18 |
"dict_id", "level", "steps", "threads", |
| 19 |
NULL}; |
| 20 |
|
| 21 |
size_t capacity; |
| 22 |
PyObject *samples; |
| 23 |
unsigned k = 0; |
| 24 |
unsigned d = 0; |
| 25 |
unsigned f = 0; |
| 26 |
double splitPoint = 0.0; |
| 27 |
unsigned accel = 0; |
| 28 |
unsigned notifications = 0; |
| 29 |
unsigned dictID = 0; |
| 30 |
int level = 0; |
| 31 |
unsigned steps = 0; |
| 32 |
int threads = 0; |
| 33 |
ZDICT_fastCover_params_t params; |
| 34 |
Py_ssize_t samplesLen; |
| 35 |
Py_ssize_t i; |
| 36 |
size_t samplesSize = 0; |
| 37 |
void *sampleBuffer = NULL; |
| 38 |
size_t *sampleSizes = NULL; |
| 39 |
void *sampleOffset; |
| 40 |
Py_ssize_t sampleSize; |
| 41 |
void *dict = NULL; |
| 42 |
size_t zresult; |
| 43 |
ZstdCompressionDict *result = NULL; |
| 44 |
|
| 45 |
if (!PyArg_ParseTupleAndKeywords( |
| 46 |
args, kwargs, "nO!|IIIdIIIiIi:train_dictionary", kwlist, &capacity, |
| 47 |
&PyList_Type, &samples, &k, &d, &f, &splitPoint, &accel, |
| 48 |
¬ifications, &dictID, &level, &steps, &threads)) { |
| 49 |
return NULL; |
| 50 |
} |
| 51 |
|
| 52 |
if (threads < 0) { |
| 53 |
threads = cpu_count(); |
| 54 |
} |
| 55 |
|
| 56 |
if (!steps && !threads) { |
| 57 |
/* Defaults from ZDICT_trainFromBuffer() */ |
| 58 |
d = d ? d : 8; |
| 59 |
steps = steps ? steps : 4; |
| 60 |
level = level ? level : 3; |
| 61 |
} |
| 62 |
|
| 63 |
memset(¶ms, 0, sizeof(params)); |
| 64 |
params.k = k; |
| 65 |
params.d = d; |
| 66 |
params.f = f; |
| 67 |
params.steps = steps; |
| 68 |
params.nbThreads = threads; |
| 69 |
params.splitPoint = splitPoint; |
| 70 |
params.accel = accel; |
| 71 |
|
| 72 |
params.zParams.compressionLevel = level; |
| 73 |
params.zParams.dictID = dictID; |
| 74 |
params.zParams.notificationLevel = notifications; |
| 75 |
|
| 76 |
/* Figure out total size of input samples. */ |
| 77 |
samplesLen = PyList_Size(samples); |
| 78 |
for (i = 0; i < samplesLen; i++) { |
| 79 |
PyObject *sampleItem = PyList_GET_ITEM(samples, i); |
| 80 |
|
| 81 |
if (!PyBytes_Check(sampleItem)) { |
| 82 |
PyErr_SetString(PyExc_ValueError, "samples must be bytes"); |
| 83 |
return NULL; |
| 84 |
} |
| 85 |
samplesSize += PyBytes_GET_SIZE(sampleItem); |
| 86 |
} |
| 87 |
|
| 88 |
sampleBuffer = PyMem_Malloc(samplesSize); |
| 89 |
if (!sampleBuffer) { |
| 90 |
PyErr_NoMemory(); |
| 91 |
goto finally; |
| 92 |
} |
| 93 |
|
| 94 |
sampleSizes = PyMem_Malloc(samplesLen * sizeof(size_t)); |
| 95 |
if (!sampleSizes) { |
| 96 |
PyErr_NoMemory(); |
| 97 |
goto finally; |
| 98 |
} |
| 99 |
|
| 100 |
sampleOffset = sampleBuffer; |
| 101 |
for (i = 0; i < samplesLen; i++) { |
| 102 |
PyObject *sampleItem = PyList_GET_ITEM(samples, i); |
| 103 |
sampleSize = PyBytes_GET_SIZE(sampleItem); |
| 104 |
sampleSizes[i] = sampleSize; |
| 105 |
memcpy(sampleOffset, PyBytes_AS_STRING(sampleItem), sampleSize); |
| 106 |
sampleOffset = (char *)sampleOffset + sampleSize; |
| 107 |
} |
| 108 |
|
| 109 |
dict = PyMem_Malloc(capacity); |
| 110 |
if (!dict) { |
| 111 |
PyErr_NoMemory(); |
| 112 |
goto finally; |
| 113 |
} |
| 114 |
|
| 115 |
Py_BEGIN_ALLOW_THREADS zresult = ZDICT_optimizeTrainFromBuffer_fastCover( |
| 116 |
dict, capacity, sampleBuffer, sampleSizes, (unsigned)samplesLen, |
| 117 |
¶ms); |
| 118 |
Py_END_ALLOW_THREADS |
| 119 |
|
| 120 |
if (ZDICT_isError(zresult)) { |
| 121 |
PyMem_Free(dict); |
| 122 |
PyErr_Format(ZstdError, "cannot train dict: %s", |
| 123 |
ZDICT_getErrorName(zresult)); |
| 124 |
goto finally; |
| 125 |
} |
| 126 |
|
| 127 |
result = PyObject_New(ZstdCompressionDict, ZstdCompressionDictType); |
| 128 |
if (!result) { |
| 129 |
PyMem_Free(dict); |
| 130 |
goto finally; |
| 131 |
} |
| 132 |
|
| 133 |
result->dictData = dict; |
| 134 |
result->dictSize = zresult; |
| 135 |
result->dictType = ZSTD_dct_fullDict; |
| 136 |
result->d = params.d; |
| 137 |
result->k = params.k; |
| 138 |
result->cdict = NULL; |
| 139 |
result->ddict = NULL; |
| 140 |
|
| 141 |
finally: |
| 142 |
PyMem_Free(sampleBuffer); |
| 143 |
PyMem_Free(sampleSizes); |
| 144 |
|
| 145 |
return result; |
| 146 |
} |
| 147 |
|
| 148 |
int ensure_ddict(ZstdCompressionDict *dict) { |
| 149 |
if (dict->ddict) { |
| 150 |
return 0; |
| 151 |
} |
| 152 |
|
| 153 |
Py_BEGIN_ALLOW_THREADS dict->ddict = ZSTD_createDDict_advanced( |
| 154 |
dict->dictData, dict->dictSize, ZSTD_dlm_byRef, dict->dictType, |
| 155 |
ZSTD_defaultCMem); |
| 156 |
Py_END_ALLOW_THREADS if (!dict->ddict) { |
| 157 |
PyErr_SetString(ZstdError, "could not create decompression dict"); |
| 158 |
return 1; |
| 159 |
} |
| 160 |
|
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
static int ZstdCompressionDict_init(ZstdCompressionDict *self, PyObject *args, |
| 165 |
PyObject *kwargs) { |
| 166 |
static char *kwlist[] = {"data", "dict_type", NULL}; |
| 167 |
|
| 168 |
int result = -1; |
| 169 |
Py_buffer source; |
| 170 |
unsigned dictType = ZSTD_dct_auto; |
| 171 |
|
| 172 |
self->dictData = NULL; |
| 173 |
self->dictSize = 0; |
| 174 |
self->cdict = NULL; |
| 175 |
self->ddict = NULL; |
| 176 |
|
| 177 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|I:ZstdCompressionDict", |
| 178 |
kwlist, &source, &dictType)) { |
| 179 |
return -1; |
| 180 |
} |
| 181 |
|
| 182 |
if (dictType != ZSTD_dct_auto && dictType != ZSTD_dct_rawContent && |
| 183 |
dictType != ZSTD_dct_fullDict) { |
| 184 |
PyErr_Format( |
| 185 |
PyExc_ValueError, |
| 186 |
"invalid dictionary load mode: %d; must use DICT_TYPE_* constants", |
| 187 |
dictType); |
| 188 |
goto finally; |
| 189 |
} |
| 190 |
|
| 191 |
self->dictType = dictType; |
| 192 |
|
| 193 |
self->dictData = PyMem_Malloc(source.len); |
| 194 |
if (!self->dictData) { |
| 195 |
PyErr_NoMemory(); |
| 196 |
goto finally; |
| 197 |
} |
| 198 |
|
| 199 |
memcpy(self->dictData, source.buf, source.len); |
| 200 |
self->dictSize = source.len; |
| 201 |
|
| 202 |
result = 0; |
| 203 |
|
| 204 |
finally: |
| 205 |
PyBuffer_Release(&source); |
| 206 |
return result; |
| 207 |
} |
| 208 |
|
| 209 |
static void ZstdCompressionDict_dealloc(ZstdCompressionDict *self) { |
| 210 |
if (self->cdict) { |
| 211 |
ZSTD_freeCDict(self->cdict); |
| 212 |
self->cdict = NULL; |
| 213 |
} |
| 214 |
|
| 215 |
if (self->ddict) { |
| 216 |
ZSTD_freeDDict(self->ddict); |
| 217 |
self->ddict = NULL; |
| 218 |
} |
| 219 |
|
| 220 |
if (self->dictData) { |
| 221 |
PyMem_Free(self->dictData); |
| 222 |
self->dictData = NULL; |
| 223 |
} |
| 224 |
|
| 225 |
PyObject_Del(self); |
| 226 |
} |
| 227 |
|
| 228 |
static PyObject * |
| 229 |
ZstdCompressionDict_precompute_compress(ZstdCompressionDict *self, |
| 230 |
PyObject *args, PyObject *kwargs) { |
| 231 |
static char *kwlist[] = {"level", "compression_params", NULL}; |
| 232 |
|
| 233 |
int level = 0; |
| 234 |
ZstdCompressionParametersObject *compressionParams = NULL; |
| 235 |
ZSTD_compressionParameters cParams; |
| 236 |
size_t zresult; |
| 237 |
|
| 238 |
if (!PyArg_ParseTupleAndKeywords( |
| 239 |
args, kwargs, "|iO!:precompute_compress", kwlist, &level, |
| 240 |
ZstdCompressionParametersType, &compressionParams)) { |
| 241 |
return NULL; |
| 242 |
} |
| 243 |
|
| 244 |
if (level && compressionParams) { |
| 245 |
PyErr_SetString(PyExc_ValueError, |
| 246 |
"must only specify one of level or compression_params"); |
| 247 |
return NULL; |
| 248 |
} |
| 249 |
|
| 250 |
if (!level && !compressionParams) { |
| 251 |
PyErr_SetString(PyExc_ValueError, |
| 252 |
"must specify one of level or compression_params"); |
| 253 |
return NULL; |
| 254 |
} |
| 255 |
|
| 256 |
if (self->cdict) { |
| 257 |
zresult = ZSTD_freeCDict(self->cdict); |
| 258 |
self->cdict = NULL; |
| 259 |
if (ZSTD_isError(zresult)) { |
| 260 |
PyErr_Format(ZstdError, "unable to free CDict: %s", |
| 261 |
ZSTD_getErrorName(zresult)); |
| 262 |
return NULL; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
if (level) { |
| 267 |
cParams = ZSTD_getCParams(level, 0, self->dictSize); |
| 268 |
} |
| 269 |
else { |
| 270 |
if (to_cparams(compressionParams, &cParams)) { |
| 271 |
return NULL; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
assert(!self->cdict); |
| 276 |
self->cdict = ZSTD_createCDict_advanced(self->dictData, self->dictSize, |
| 277 |
ZSTD_dlm_byRef, self->dictType, |
| 278 |
cParams, ZSTD_defaultCMem); |
| 279 |
|
| 280 |
if (!self->cdict) { |
| 281 |
PyErr_SetString(ZstdError, "unable to precompute dictionary"); |
| 282 |
return NULL; |
| 283 |
} |
| 284 |
|
| 285 |
Py_RETURN_NONE; |
| 286 |
} |
| 287 |
|
| 288 |
static PyObject *ZstdCompressionDict_dict_id(ZstdCompressionDict *self) { |
| 289 |
unsigned dictID = ZDICT_getDictID(self->dictData, self->dictSize); |
| 290 |
|
| 291 |
return PyLong_FromLong(dictID); |
| 292 |
} |
| 293 |
|
| 294 |
static PyObject *ZstdCompressionDict_as_bytes(ZstdCompressionDict *self) { |
| 295 |
return PyBytes_FromStringAndSize(self->dictData, self->dictSize); |
| 296 |
} |
| 297 |
|
| 298 |
static PyMethodDef ZstdCompressionDict_methods[] = { |
| 299 |
{"dict_id", (PyCFunction)ZstdCompressionDict_dict_id, METH_NOARGS, |
| 300 |
PyDoc_STR("dict_id() -- obtain the numeric dictionary ID")}, |
| 301 |
{"as_bytes", (PyCFunction)ZstdCompressionDict_as_bytes, METH_NOARGS, |
| 302 |
PyDoc_STR("as_bytes() -- obtain the raw bytes constituting the dictionary " |
| 303 |
"data")}, |
| 304 |
{"precompute_compress", |
| 305 |
(PyCFunction)ZstdCompressionDict_precompute_compress, |
| 306 |
METH_VARARGS | METH_KEYWORDS, NULL}, |
| 307 |
{NULL, NULL}}; |
| 308 |
|
| 309 |
static PyMemberDef ZstdCompressionDict_members[] = { |
| 310 |
{"k", T_UINT, offsetof(ZstdCompressionDict, k), READONLY, "segment size"}, |
| 311 |
{"d", T_UINT, offsetof(ZstdCompressionDict, d), READONLY, "dmer size"}, |
| 312 |
{NULL}}; |
| 313 |
|
| 314 |
static Py_ssize_t ZstdCompressionDict_length(ZstdCompressionDict *self) { |
| 315 |
return self->dictSize; |
| 316 |
} |
| 317 |
|
| 318 |
PyType_Slot ZstdCompressionDictSlots[] = { |
| 319 |
{Py_tp_dealloc, ZstdCompressionDict_dealloc}, |
| 320 |
{Py_sq_length, ZstdCompressionDict_length}, |
| 321 |
{Py_tp_methods, ZstdCompressionDict_methods}, |
| 322 |
{Py_tp_members, ZstdCompressionDict_members}, |
| 323 |
{Py_tp_init, ZstdCompressionDict_init}, |
| 324 |
{Py_tp_new, PyType_GenericNew}, |
| 325 |
{0, NULL}, |
| 326 |
}; |
| 327 |
|
| 328 |
PyType_Spec ZstdCompressionDictSpec = { |
| 329 |
"zstandard.backend_c.ZstdCompressionDict", |
| 330 |
sizeof(ZstdCompressionDict), |
| 331 |
0, |
| 332 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 333 |
ZstdCompressionDictSlots, |
| 334 |
}; |
| 335 |
|
| 336 |
PyTypeObject *ZstdCompressionDictType; |
| 337 |
|
| 338 |
void compressiondict_module_init(PyObject *mod) { |
| 339 |
ZstdCompressionDictType = |
| 340 |
(PyTypeObject *)PyType_FromSpec(&ZstdCompressionDictSpec); |
| 341 |
if (PyType_Ready(ZstdCompressionDictType) < 0) { |
| 342 |
return; |
| 343 |
} |
| 344 |
|
| 345 |
Py_INCREF((PyObject *)ZstdCompressionDictType); |
| 346 |
PyModule_AddObject(mod, "ZstdCompressionDict", |
| 347 |
(PyObject *)ZstdCompressionDictType); |
| 348 |
} |