-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazytuple.cpp
438 lines (389 loc) · 14 KB
/
lazytuple.cpp
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include "lazytuple.h"
#include "oocmap.h"
#include "db.h"
#include "errors.h"
//
// Methods that are not directly exposed to Python.
// These throw exceptions.
//
OOCLazyTupleObject* OOCLazyTuple_fastnew(OOCMapObject* const ooc, const uint64_t tupleId) {
PyObject* const pySelf = OOCLazyTupleType.tp_alloc(&OOCLazyTupleType, 0);
if(pySelf == nullptr) throw OocError(OocError::OutOfMemory);
OOCLazyTupleObject* self = reinterpret_cast<OOCLazyTupleObject*>(pySelf);
self->ooc = ooc;
Py_INCREF(ooc);
self->tupleId = tupleId;
self->eager = nullptr;
return self;
}
PyObject* OOCLazyTupleObject_eager(OOCLazyTupleObject* const self, OOCTransaction& txn) {
if(self->eager != nullptr) {
Py_INCREF(self->eager);
return self->eager;
}
MDB_val mdbKey = { .mv_size = sizeof(self->tupleId), .mv_data = &self->tupleId };
MDB_val mdbValue;
const bool found = get(txn.txn, self->ooc->tuplesDb, &mdbKey, &mdbValue);
if(!found) throw OocError(OocError::UnexpectedData);
const Py_ssize_t size = mdbValue.mv_size / sizeof(EncodedValue);
PyObject* const result = PyTuple_New(size);
if(result == nullptr) throw OocError(OocError::OutOfMemory);
EncodedValue* const encodedResults = static_cast<EncodedValue* const>(mdbValue.mv_data);
for(Py_ssize_t i = 0; i < size; ++i)
PyTuple_SET_ITEM(result, i, OOCMap_decode(self->ooc, encodedResults + i, txn));
txn.commit();
self->eager = result;
Py_INCREF(result);
return result;
}
//
// Methods that are directly exposed to Python
// These are not allowed to throw exceptions.
//
static PyObject* OOCLazyTuple_new(PyTypeObject* const type, PyObject* const args, PyObject* const kwds) {
PyObject* pySelf = type->tp_alloc(type, 0);
OOCLazyTupleObject* self = reinterpret_cast<OOCLazyTupleObject*>(pySelf);
if(self == nullptr) {
PyErr_NoMemory();
return nullptr;
}
self->ooc = nullptr;
self->tupleId = 0;
self->eager = nullptr;
return (PyObject*)self;
}
static int OOCLazyTuple_init(OOCLazyTupleObject* const self, PyObject* const args, PyObject* const kwds) {
// parse parameters
static const char *kwlist[] = {"oocmap", "tuple_id", nullptr};
PyObject* oocmapObject = nullptr;
const int parseSuccess = PyArg_ParseTupleAndKeywords(
args,
kwds,
"O!K",
const_cast<char**>(kwlist),
&OOCMapType, &oocmapObject, &self->tupleId);
if(!parseSuccess)
return -1;
// TODO: consider that __init__ might be called on an already initialized object
self->ooc = reinterpret_cast<OOCMapObject*>(oocmapObject);
Py_INCREF(oocmapObject);
self->eager = nullptr;
return 0;
}
static void OOCLazyTuple_dealloc(OOCLazyTupleObject* const self) {
Py_DECREF(self->ooc);
Py_XDECREF(self->eager);
Py_TYPE(self)->tp_free((PyObject*)self);
}
static Py_ssize_t OOCLazyTuple_length(PyObject* const pySelf) {
if(pySelf->ob_type != &OOCLazyTupleType) {
PyErr_BadArgument();
return -1;
}
OOCLazyTupleObject* const self = reinterpret_cast<OOCLazyTupleObject*>(pySelf);
if(self->eager != nullptr)
return PyTuple_Size(self->eager);
try {
OOCTransaction txn(self->ooc, true);
const Py_ssize_t result = OOCLazyTupleObject_length(self, txn);
txn.commit();
return result;
} catch(const OocError& error) {
error.pythonize();
return -1;
}
}
Py_ssize_t OOCLazyTupleObject_length(OOCLazyTupleObject* const self, OOCTransaction& txn) {
if(self->eager != nullptr)
return PyTuple_Size(self->eager);
MDB_val mdbKey = { .mv_size = sizeof(self->tupleId), .mv_data = &self->tupleId };
MDB_val mdbValue;
const bool found = get(txn.txn, self->ooc->tuplesDb, &mdbKey, &mdbValue);
if(!found) throw OocError(OocError::UnexpectedData);
Py_ssize_t result = mdbValue.mv_size / sizeof(EncodedValue);
return result;
}
static PyObject* OOCLazyTuple_item(PyObject* const pySelf, Py_ssize_t const index) {
if(pySelf->ob_type != &OOCLazyTupleType) {
PyErr_BadArgument();
return nullptr;
}
OOCLazyTupleObject* const self = reinterpret_cast<OOCLazyTupleObject*>(pySelf);
if(self->eager != nullptr)
return PyTuple_GET_ITEM(self->eager, index);
try {
OOCTransaction txn(self->ooc, true);
MDB_val mdbKey = { .mv_size = sizeof(self->tupleId), .mv_data = &self->tupleId };
MDB_val mdbValue;
const bool found = get(txn.txn, self->ooc->tuplesDb, &mdbKey, &mdbValue);
if(!found) throw OocError(OocError::UnexpectedData);
if(index < 0 || index >= static_cast<Py_ssize_t>(mdbValue.mv_size / sizeof(EncodedValue)))
throw OocError(OocError::IndexError);
EncodedValue* const encodedResult = static_cast<EncodedValue* const>(mdbValue.mv_data) + index;
PyObject* const result = OOCMap_decode(self->ooc, encodedResult, txn);
txn.commit();
return result;
} catch(const OocError& error) {
error.pythonize();
return nullptr;
}
}
PyObject* OOCLazyTuple_eager(PyObject* const pySelf) {
if(pySelf->ob_type != &OOCLazyTupleType) {
PyErr_BadArgument();
return nullptr;
}
OOCLazyTupleObject* const self = reinterpret_cast<OOCLazyTupleObject*>(pySelf);
if(self->eager != nullptr) {
Py_INCREF(self->eager);
return self->eager;
}
try {
OOCTransaction txn(self->ooc, true);
return OOCLazyTupleObject_eager(self, txn);
} catch(const OocError& error) {
error.pythonize();
return nullptr;
}
}
Py_hash_t OOCLazyTuple_hash(PyObject* const pySelf) {
// If we want LazyTuple to work as a key in a dict the same way as a normal tuple would, they have to hash
// to the same thing. The only way to achieve that is to call eager().
PyObject* const eager = OOCLazyTuple_eager(pySelf);
if(eager == nullptr) return -1;
const Py_hash_t result = PyObject_Hash(eager);
Py_DECREF(eager);
return result;
}
PyObject* OOCLazyTuple_richcompare(PyObject* const pySelf, PyObject* const other, int op) {
// TODO: optimize this (but only if it shows up on a profiler)
PyObject* const eager = OOCLazyTuple_eager(pySelf);
if(eager == nullptr) return nullptr;
PyObject* const result = PyObject_RichCompare(eager, other, op);
Py_DECREF(eager);
return result;
}
static PyObject* OOCLazyTuple_index(
PyObject* const pySelf,
PyObject *const *const args,
const Py_ssize_t nargs
) {
if(pySelf->ob_type != &OOCLazyTupleType) {
PyErr_BadArgument();
return nullptr;
}
OOCLazyTupleObject* const self = reinterpret_cast<OOCLazyTupleObject*>(pySelf);
// parse parameters
if(nargs < 1) {
PyErr_Format(PyExc_TypeError, "index expected at least 1 argument, got 0");
return nullptr;
}
PyObject* const value = args[0];
Py_ssize_t start = 0;
if(nargs > 1) {
start = PyLong_AsSsize_t(args[1]);
if(PyErr_Occurred()) return nullptr;
}
Py_ssize_t stop = 9223372036854775807;
if(nargs > 2) {
stop = PyLong_AsSsize_t(args[2]);
if(PyErr_Occurred()) return nullptr;
}
Py_ssize_t index;
try {
OOCTransaction txn(self->ooc, true);
index = OOCLazyTupleObject_index(self, txn, value, start, stop);
txn.commit();
} catch(const OocError& error) {
error.pythonize();
return nullptr;
}
if(index < 0) {
PyErr_Format(PyExc_ValueError, "%R is not in list", value);
return nullptr;
} else {
return PyLong_FromSsize_t(index);
}
}
Py_ssize_t OOCLazyTupleObject_index(
OOCLazyTupleObject* self,
OOCTransaction& txn,
PyObject* value,
Py_ssize_t start,
Py_ssize_t stop
) {
MDB_val mdbKey = { .mv_size = sizeof(self->tupleId), .mv_data = &self->tupleId };
MDB_val mdbValue;
const bool found = get(txn.txn, self->ooc->tuplesDb, &mdbKey, &mdbValue);
if(!found) throw OocError(OocError::UnexpectedData);
const Py_ssize_t length = mdbValue.mv_size / sizeof(EncodedValue);
EncodedValue* const encodedResults = static_cast<EncodedValue* const>(mdbValue.mv_data);
// unfuck start and stop
// That behavior in Python is seriously weird and we have to copy it here.
if(start < 0) {
start += length;
if(start < 0)
start = 0;
}
if(stop < 0) {
stop += length;
}
Id2EncodedMap insertedItemsInThisTransaction;
const EncodedValue* encodedValue = nullptr;
try {
encodedValue = OOCMap_encode(self->ooc, value, txn, true, true);
} catch(const OocError& e) {
switch(e.errorCode) {
case OocError::MutableValueNotAllowed:
// For mutable values, we have to search linearly through the list and compare them all.
break;
case OocError::ImmutableValueNotFound:
// The immutable value isn't in the map yet, so it can't possibly be in the tuple.
return -1;
default:
throw;
}
}
while(start < stop) {
if(start < 0 || start >= length)
return -1;
EncodedValue* const encodedItem = encodedResults + start;
if(encodedValue == nullptr) {
PyObject* const item = OOCMap_decode(self->ooc, encodedItem, txn);
if(PyObject_RichCompareBool(value, item, Py_EQ))
return start;
} else {
if(*encodedValue == *encodedItem)
return start;
}
start += 1;
}
return -1;
}
static PyObject* OOCLazyTuple_count(
PyObject* const pySelf,
PyObject* const value
) {
if(pySelf->ob_type != &OOCLazyTupleType) {
PyErr_BadArgument();
return nullptr;
}
OOCLazyTupleObject* const self = reinterpret_cast<OOCLazyTupleObject*>(pySelf);
Py_ssize_t count;
try {
OOCTransaction txn(self->ooc, true);
count = OOCLazyTupleObject_count(self, txn, value);
txn.commit();
} catch(const OocError& error) {
error.pythonize();
return nullptr;
}
return PyLong_FromSsize_t(count);
}
Py_ssize_t OOCLazyTupleObject_count(OOCLazyTupleObject* self, OOCTransaction& txn, PyObject* value) {
Id2EncodedMap insertedItemsInThisTransaction;
const EncodedValue* encodedValue = nullptr;
try {
encodedValue = OOCMap_encode(self->ooc, value, txn, true, true);
} catch(const OocError& e) {
switch(e.errorCode) {
case OocError::MutableValueNotAllowed:
// For mutable values, we have to search linearly through the list and compare them all.
break;
case OocError::ImmutableValueNotFound:
// The immutable value isn't in the map yet, so it can't possibly be in the tuple.
return 0;
default:
throw;
}
}
MDB_val mdbKey = { .mv_size = sizeof(self->tupleId), .mv_data = &self->tupleId };
MDB_val mdbValue;
const bool found = get(txn.txn, self->ooc->tuplesDb, &mdbKey, &mdbValue);
if(!found) throw OocError(OocError::UnexpectedData);
const Py_ssize_t length = mdbValue.mv_size / sizeof(EncodedValue);
EncodedValue* const encodedResults = static_cast<EncodedValue* const>(mdbValue.mv_data);
Py_ssize_t result = 0;
for(Py_ssize_t i = 0; i < length; ++i) {
EncodedValue* const encodedItem = encodedResults + i;
if(encodedValue == nullptr) {
PyObject* const item = OOCMap_decode(self->ooc, encodedItem, txn);
if(PyObject_RichCompareBool(value, item, Py_EQ))
result += 1;
} else {
if(*encodedValue == *encodedItem)
result += 1;
}
}
return result;
}
PyObject* OOCLazyTuple_concat(PyObject* pySelf, PyObject* pyOther) {
PyObject* selfEager = nullptr;
if(pySelf->ob_type == &OOCLazyTupleType) {
selfEager = OOCLazyTuple_eager(pySelf);
pySelf = selfEager;
}
PyObject* otherEager = nullptr;
if(pyOther->ob_type == &OOCLazyTupleType) {
otherEager = OOCLazyTuple_eager(pyOther);
pyOther = otherEager;
}
PyObject* result = nullptr;
if(pySelf != nullptr && pyOther != nullptr)
result = PySequence_Concat(pySelf, pyOther);
if(selfEager != nullptr)
Py_DECREF(selfEager);
if(otherEager != nullptr)
Py_DECREF(otherEager);
return result;
}
PyObject* OOCLazyTuple_repeat(PyObject* const pySelf, const Py_ssize_t count) {
PyObject* const eager = OOCLazyTuple_eager(pySelf);
if(eager == nullptr) return nullptr;
PyObject* const result = PySequence_Repeat(eager, count);
Py_DECREF(eager);
return result;
}
static PyMethodDef OOCLazyTuple_methods[] = {
{
"eager",
(PyCFunction)OOCLazyTuple_eager,
METH_NOARGS,
PyDoc_STR("returns the original tuple")
}, {
"index",
(PyCFunction)OOCLazyTuple_index,
METH_FASTCALL,
PyDoc_STR("returns the index of the given item in the tuple")
}, {
"count",
(PyCFunction)OOCLazyTuple_count,
METH_O,
PyDoc_STR("counts how often an item appears in the tuple")
}, {nullptr}, // sentinel
};
static PySequenceMethods OOCLazyTuple_sequence_methods = {
.sq_length = OOCLazyTuple_length,
.sq_concat = OOCLazyTuple_concat,
.sq_repeat = OOCLazyTuple_repeat,
.sq_item = OOCLazyTuple_item,
};
static PyNumberMethods OOCLazyTuple_number_methods = {
.nb_add = OOCLazyTuple_concat,
};
PyTypeObject OOCLazyTupleType = {
PyVarObject_HEAD_INIT(nullptr, 0)
.tp_name = "oocmap.LazyTuple",
.tp_basicsize = sizeof(OOCLazyTupleObject),
.tp_itemsize = 0,
.tp_dealloc = (destructor)OOCLazyTuple_dealloc,
.tp_as_number = &OOCLazyTuple_number_methods,
.tp_as_sequence = &OOCLazyTuple_sequence_methods,
.tp_hash = OOCLazyTuple_hash,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = "A tuple-like class that's backed by an OOCMap",
.tp_richcompare = OOCLazyTuple_richcompare,
.tp_methods = OOCLazyTuple_methods,
.tp_init = (initproc)OOCLazyTuple_init,
.tp_new = OOCLazyTuple_new,
};