Branch data Line data Source code
1 : : #include "storage/RowStore.hpp" 2 : : 3 : : #include "backend/StackMachine.hpp" 4 : : #include <algorithm> 5 : : #include <exception> 6 : : #include <fstream> 7 : : #include <iomanip> 8 : : #include <mutable/catalog/Catalog.hpp> 9 : : #include <mutable/catalog/Type.hpp> 10 : : #include <mutable/util/fn.hpp> 11 : : #include <typeinfo> 12 : : 13 : : 14 : : using namespace m; 15 : : 16 : : 17 [ + - + - : 40 : RowStore::RowStore(const Table &table) + - ] 18 : 20 : : Store(table) 19 [ + - + - ]: 20 : , offsets_(new uint32_t[table.num_attrs() + 1]) // add one slot for the offset of the meta data 20 : 20 : { 21 [ + - ]: 20 : compute_offsets(); 22 : 40 : capacity_ = ALLOCATION_SIZE / (row_size_ / 8); 23 [ + - - + ]: 20 : data_ = allocator_.allocate(ALLOCATION_SIZE); 24 : 20 : } 25 : : 26 : 36 : RowStore::~RowStore() 27 : 36 : { 28 [ + - ]: 20 : delete[] offsets_; 29 : 36 : } 30 : : 31 : 20 : void RowStore::compute_offsets() 32 : : { 33 : : /* TODO: use `PhysicalSchema` with additional bitmap-type to compute offsets. */ 34 : : using std::max; 35 : : 36 : 20 : const auto num_attrs = table().num_attrs(); 37 : 20 : const Attribute **attrs = new const Attribute*[num_attrs]; 38 : : 39 [ + + ]: 130 : for (uint32_t pos = 0; pos != num_attrs; ++pos) 40 : 110 : attrs[pos] = &table()[pos]; 41 : : 42 : : /* Sort attributes by their alignment requirement in descending order. */ 43 : 234 : std::stable_sort(attrs, attrs + num_attrs, [](const Attribute *first, const Attribute *second) { 44 : 214 : return first->type->alignment() > second->type->alignment(); 45 : : }); 46 : : 47 : : /* Compute offsets. */ 48 : 20 : uint64_t off = 0; 49 : 20 : uint64_t alignment = 8; 50 [ + + ]: 130 : for (uint32_t pos = 0; pos != num_attrs; ++pos) { 51 : 110 : const Attribute &attr = *attrs[pos]; 52 : 110 : offsets_[attr.id] = off; 53 : 110 : off += attr.type->size(); 54 : 110 : alignment = max(alignment, attr.type->alignment()); 55 : 110 : } 56 : : /* Add space for meta data. */ 57 : 20 : offsets_[num_attrs] = off; 58 : 20 : off += num_attrs; // reserve space for the NULL bitmap 59 [ + - ]: 20 : if (off % alignment) 60 : 20 : off += (alignment - off % alignment); // the offset is padded to fulfill the alignment requirements 61 : 20 : row_size_ = off; 62 : : 63 [ + - ]: 20 : delete[] attrs; 64 : 20 : } 65 : : 66 : : M_LCOV_EXCL_START 67 : : void RowStore::dump(std::ostream &out) const 68 : : { 69 : : out << "RowStore at " << data_.addr() << " for table \"" << table().name() << "\": " << num_rows_ << '/' << capacity_ 70 : : << " rows, " << row_size_ << " bits per row, offsets ["; 71 : : for (uint32_t i = 0, end = table().num_attrs(); i != end; ++i) { 72 : : if (i != 0) out << ", "; 73 : : out << offsets_[i]; 74 : : } 75 : : out << ']' << std::endl; 76 : : } 77 : : M_LCOV_EXCL_STOP 78 : : 79 : : __attribute__((constructor(202))) 80 : 1 : static void register_store() 81 : : { 82 : 1 : Catalog &C = Catalog::Get(); 83 [ + - ]: 1 : C.register_store<RowStore>(C.pool("RowStore"), "stores attributes in row-major order"); 84 : 1 : }