-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayAdapter.h
414 lines (323 loc) · 13.7 KB
/
ArrayAdapter.h
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
#ifndef EXPERIMENTS_ARRAYADAPTER_H
#define EXPERIMENTS_ARRAYADAPTER_H
#include "ArrayAdapterFwd.h"
#include <cstring>
#include "arrow/table.h"
#include <atomic>
template<class T, size_t SIZE>
struct ArrayAdapter {
private:
using Allocator = StdFixedSizeArrayAllocator<T, SIZE>;
using Deleter = DeleterForAllocator<T, StdFixedSizeArrayAllocator<T, SIZE>>;
inline static Allocator &alloc = Allocator::oneAndOnly();
public:
using ArrayPtr = std::unique_ptr<T[], Deleter>;
using ArrayCPtr = std::shared_ptr<const T>;
using ValueType = T;
using DeclaredType = std::variant<ArrayPtr, ArrayCPtr>;
static ArrayPtr createLeaf(void *context = nullptr) {
assert(context == nullptr);
return ArrayPtr(alloc.allocate(1), Deleter());
}
static const T *constArray(const DeclaredType &leaf) {
if (leaf.index() == 0) {
return &std::get<0>(leaf)[0];
} else {
return &(std::get<1>(leaf).get()[0]);
}
}
static const T &at(const DeclaredType &leaf, size_t pos) {
if (leaf.index() == 0) {
return std::get<0>(leaf)[pos];
} else {
return std::get<1>(leaf).get()[pos];
}
}
static void copy(DeclaredType &dest, size_t destOffset, const DeclaredType &src, size_t srcOffset, size_t length) {
if (dest.index() == 0) {
if (src.index() == 0) {
memcpy(&std::get<ArrayPtr>(dest)[destOffset], &std::get<ArrayPtr>(src)[srcOffset],
length * sizeof(T));
} else {
memcpy(&std::get<ArrayPtr>(dest)[destOffset], &std::get<ArrayCPtr>(src).get()[srcOffset],
length * sizeof(T));
}
} else {
throw std::logic_error("Cannot write to a const leaf");
}
}
static void getValues(T *destLeaf, const DeclaredType &src, size_t srcOffset, size_t length) {
const T *data = src.index() ? std::get<ArrayCPtr>(src).get() : static_cast<const T *>(std::get<ArrayPtr>(
src).get());
if constexpr (std::is_trivially_copyable<T>::value) {
memcpy(destLeaf, &data[srcOffset], length * sizeof(T));
} else {
for (size_t i = 0; i < length; i++) {
destLeaf[i] = data[srcOffset + i];
}
}
}
static void setAt(DeclaredType &leaf, size_t pos, const T &value) {
std::get<0>(leaf)[pos] = value;
}
static void setValues(DeclaredType &dest, size_t offset, const T *srcLeaf, size_t length) {
T *data = &std::get<ArrayPtr>(dest)[offset];
if constexpr (std::is_trivially_copyable<T>::value) {
std::memcpy(data, srcLeaf, length * sizeof(T));
} else {
for (size_t i = 0; i < length; i++) {
data[i] = srcLeaf[i];
}
}
}
static DeclaredType mutateCopy(const DeclaredType &src) {
T *resultPointer = alloc.allocate(1);
getValues(resultPointer, src, 0, SIZE);
return ArrayPtr(resultPointer, Deleter());
}
static void mutate(DeclaredType &leaf, void *context) {
assert(context == nullptr);
if (leaf.index() == 1) {
leaf = mutateCopy(leaf);
}
}
static void makeConst(DeclaredType &leaf) {
if (leaf.index() == 0) {
ArrayPtr &pointer = std::get<0>(leaf);
T *data = pointer.release();
leaf = DeclaredType(ArrayCPtr(data, Deleter(), alloc));
}
}
static DeclaredType copy(const DeclaredType &leaf) {
return leaf.index() == 1 ? DeclaredType(std::get<1>(leaf)) : mutateCopy(leaf);
}
static void shiftData(DeclaredType &buf, size_t from, size_t to, size_t length) {
memmove(&std::get<ArrayPtr>(buf)[to], &std::get<ArrayPtr>(buf)[from], length * sizeof(T));
}
static bool isMutable(const DeclaredType &buf) { return buf.index() == 0; }
static bool isNull(const DeclaredType &buf) {
return buf.index() == 0 ? std::get<0>(buf) == nullptr : std::get<1>(buf) == nullptr;
}
};
template<class T, size_t SIZE>
struct IndexAdapter;
template<size_t BlockSize>
struct SpaceProvider {
using Allocator = StdFixedSizeArrayAllocator<size_t, BlockSize>;
using Deleter = DeleterForAllocator<size_t, Allocator>;
using CopyList = std::unique_ptr<size_t[], Deleter>;
inline static constexpr size_t NULL_ENTRY = std::numeric_limits<size_t>::max();
struct TranslationUnit {
TranslationUnit(const std::shared_ptr<size_t> &targetPointer, CopyList &&sourceRows) :
targetPointer_(targetPointer), sourceRows_(std::move(sourceRows)) {}
//Pointer to the new block (where previous blocks need to be copied to)
const std::shared_ptr<size_t> targetPointer_;
/**
* Contains the original locations of row ranges to be copied onto the new block
*/
CopyList sourceRows_;
};
struct RefId;
struct RefIdWithTracking;
class AllocationSession {
std::deque<TranslationUnit> commitChanges_;
SpaceProvider &spaceProvider_;
public:
AllocationSession(SpaceProvider &spaceProvider) : spaceProvider_(spaceProvider) {}
auto makeConst(RefIdWithTracking &&refId) -> RefId;
auto newBlock() -> RefIdWithTracking;
std::deque<TranslationUnit> close() { return std::move(commitChanges_); }
};
struct RefId {
const size_t firstReference = 0;
const size_t size;
RefId(size_t firstReference, size_t size, std::shared_ptr<size_t> ownerPtr) :
firstReference(firstReference), size(size), ownerPtr_(ownerPtr) {}
RefId(RefId &&refId) = default;
RefId(const RefId &refId) = default;
size_t operator[](size_t pos) {
return firstReference + pos;
}
void getValues(size_t *destLeaf, size_t srcOffset, size_t length) {
assert(srcOffset <= BlockSize);
assert(srcOffset + length <= BlockSize);
for (size_t i = 0; i < length; i++) {
destLeaf[i] = i + srcOffset + firstReference;
}
}
size_t id() const {
return *ownerPtr_;
}
private:
friend class SpaceProvider;
std::shared_ptr<size_t> ownerPtr_;
};
struct RefIdWithTracking : public RefId {
AllocationSession &allocationSession_;
CopyList copyList;
RefIdWithTracking(size_t firstReference, size_t size, std::shared_ptr<size_t> ownerPtr,
AllocationSession &allocationSession) :
RefId(firstReference, size, ownerPtr), allocationSession_(allocationSession),
copyList(copyListAlloc.allocate(1), Deleter()) {
std::fill(copyList.get(), copyList.get() + BlockSize, NULL_ENTRY);
}
size_t &operator[](size_t pos) {
return copyList[pos];
}
void copyFrom(const RefId &src, size_t srcOffset, size_t destOffset, size_t len) {
assert(destOffset <= BlockSize);
assert(destOffset + len <= BlockSize);
assert(srcOffset + len <= src.size);
for (size_t i = 0; i < len; i++) {
copyList[i + destOffset] = src.firstReference + i + srcOffset;
}
}
void copyFrom(const RefIdWithTracking &src, size_t srcOffset, size_t destOffset, size_t len) {
assert(destOffset <= BlockSize);
assert(destOffset + len <= BlockSize);
for (size_t i = 0; i < len; i++) {
copyList[i + destOffset] = src.copyList[srcOffset + i];
}
}
void getValues(size_t *destLeaf, size_t srcOffset, size_t length) {
assert(srcOffset <= BlockSize);
assert(srcOffset + length <= BlockSize);
memcpy(destLeaf, ©List[srcOffset], length * sizeof(size_t));
}
void setValues(const size_t *destLeaf, size_t destOffset, size_t length) {
assert(destOffset <= BlockSize);
assert(destOffset + length <= BlockSize);
memcpy(©List[destOffset], destLeaf, length * sizeof(size_t));
}
void shiftData(size_t from, size_t to, size_t length) {
memmove(©List[to], ©List[from], length * sizeof(size_t));
}
};
private:
using BlockAllocator = StdFixedAllocator<size_t>;
using BlockDeleter = DeleterForFixedAllocator<size_t>;
struct DeleterForReference {
SpaceProvider &spaceProvider_;
DeleterForReference(SpaceProvider &spaceProvider) : spaceProvider_(spaceProvider) {}
void operator()(size_t *__ptr) {
spaceProvider_.removingReference(*__ptr);
auto &allocator = StdFixedAllocator<size_t>::oneAndOnly();
allocator.destroy(__ptr);
allocator.deallocate(__ptr, 1);
}
};
std::atomic<size_t> nextId = BlockSize;//Start one step off zero
inline static Allocator ©ListAlloc = Allocator::oneAndOnly();
public:
void removingReference(size_t refId) {
//TODO notify listeners
}
size_t nextAddress() {
auto result = (nextId += BlockSize) - BlockSize;//We want the previous value
return result;
}
RefId mapBlock(size_t size) {
static auto &blockAllocator = BlockAllocator::oneAndOnly();
size_t *p = blockAllocator.allocate(1);
*p = nextId;
nextId += (size / BlockSize) * BlockSize;
if (size % BlockSize) {
nextId += BlockSize;
}
return RefId(*p, size, std::shared_ptr<size_t>(p, DeleterForReference(*this)));
}
std::unique_ptr<AllocationSession> newAllocationSession() {
return std::make_unique<AllocationSession>(*this); //propagate and hold
}
//TODO - use this with the index adapter
//Define - ColumnSpace, BIndex, Column,
};
template<size_t BlockSize>
auto SpaceProvider<BlockSize>::AllocationSession::makeConst(RefIdWithTracking &&refId) -> RefId {
commitChanges_.emplace_back(refId.ownerPtr_, std::move(refId.copyList));
return RefId(std::move(refId));
}
template<size_t BlockSize>
auto SpaceProvider<BlockSize>::AllocationSession::newBlock() -> SpaceProvider::RefIdWithTracking {
static auto &blockAllocator = BlockAllocator::oneAndOnly();
size_t *p = blockAllocator.allocate(1);
*p = spaceProvider_.nextAddress();
return RefIdWithTracking(*p, BlockSize, std::shared_ptr<size_t>(p, DeleterForReference(spaceProvider_)), *this);
}
template<size_t SIZE>
struct IndexAdapter<size_t, SIZE> {
using ValueType = size_t;
private:
using Provider = SpaceProvider<SIZE>;
using ProviderSession = typename Provider::AllocationSession;
public:
using ArrayPtr = typename Provider::RefIdWithTracking;
using ArrayCPtr = typename Provider::RefId;
using DeclaredType = std::variant<ArrayPtr, ArrayCPtr>;
static ArrayPtr createLeaf(void *context = nullptr) {
assert(context != nullptr);
return static_cast<ProviderSession *>(context)->newBlock();
}
static ValueType at(const DeclaredType &leaf, size_t pos) {
if (leaf.index() == 0) {
return std::get<0>(leaf).id() + pos;
} else {
return std::get<1>(leaf).id() + pos;
}
}
static void copy(DeclaredType &dest, size_t destOffset, const DeclaredType &src, size_t srcOffset, size_t length) {
if (dest.index() == 0) {
if (src.index() == 0) {
std::get<ArrayPtr>(dest).copyFrom(std::get<ArrayPtr>(src), srcOffset, destOffset, length);
} else {
std::get<ArrayPtr>(dest).copyFrom(std::get<ArrayCPtr>(src), srcOffset, destOffset, length);
}
} else {
throw std::logic_error("Cannot write to a const leaf");
}
}
static void getValues(ValueType *destLeaf, const DeclaredType &src, size_t srcOffset, size_t length) {
std::visit([&](const auto &src) {
src.getValues(destLeaf, srcOffset, length);
}, src);
}
static void setAt(DeclaredType &leaf, size_t pos, const ValueType &value) {
std::get<0>(leaf)[pos] = value;
}
static void setValues(DeclaredType &dest, size_t offset, const ValueType *srcLeaf, size_t length) {
std::get<ArrayPtr>(dest).setValues(srcLeaf, offset, length);
}
static DeclaredType mutateCopy(const DeclaredType &src, ProviderSession *context) {
DeclaredType result = DeclaredType(context->newBlock());
copy(result, 0, src, 0, std::visit([](const auto &ref) {
return ref.size;
}, src));
return result;
}
static void mutate(DeclaredType &leaf, void *context) {
assert(context != nullptr);
if (leaf.index() == 1) {
//DeclaredType mutatedCopy = mutateCopy(leaf, static_cast<ProviderSession *>(context));
leaf.template emplace<ArrayPtr>(
std::get<ArrayPtr>(mutateCopy(leaf, static_cast<ProviderSession *>(context))));
}
}
static void makeConst(DeclaredType &leaf) {
if (leaf.index() == 0) {
leaf.template emplace<ArrayCPtr>(
std::get<0>(leaf).allocationSession_.makeConst(std::get<0>(std::move(leaf))));
}
}
static DeclaredType copy(const DeclaredType &leaf) {
return leaf.index() == 1 ? DeclaredType(ArrayCPtr(std::get<1>(leaf))) :
mutateCopy(leaf, &std::get<0>(leaf).allocationSession_);
}
static void shiftData(DeclaredType &buf, size_t from, size_t to, size_t length) {
std::get<ArrayPtr>(buf).shiftData(from, to, length);
}
static bool isMutable(const DeclaredType &buf) { return buf.index() == 0; }
static bool isNull(const DeclaredType &buf) {
return buf.index() == 0 ? !std::get<0>(buf).firstReference : !std::get<1>(buf).firstReference;
}
};
#endif //EXPERIMENTS_ARRAYADAPTER_H