mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
PaxStore.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <exception>
5#include <fstream>
6#include <iomanip>
8#include <numeric>
9
10
11using namespace m;
12
13
14PaxStore::PaxStore(const Table &table, uint32_t block_size_in_bytes)
15 : Store(table)
16 , offsets_(new uint32_t[table.num_attrs() + 1]) // add one slot for the offset of the meta data
17 , block_size_(block_size_in_bytes)
18{
20
22}
23
25{
26 delete[] offsets_;
27}
28
30{
31 using std::max;
32
33 const auto num_attrs = table().num_attrs();
34 const Attribute **attrs = new const Attribute*[num_attrs];
35
36 for (uint32_t pos = 0; pos != num_attrs; ++pos)
37 attrs[pos] = &table()[pos];
38
39 /* Sort attributes by their alignment requirement in descending order. */
40 std::stable_sort(attrs, attrs + num_attrs, [](const Attribute *first, const Attribute *second) {
41 return first->type->alignment() > second->type->alignment();
42 });
43
44 /* Compute offsets (for a single row). */
45 uint64_t off = 0;
46 uint64_t alignment = 8;
47 std::size_t num_not_byte_aligned = 0; // number of attribute columns which are not necessarily byte-aligned
48 for (uint32_t pos = 0; pos != num_attrs; ++pos) {
49 const Attribute &attr = *attrs[pos];
50 offsets_[attr.id] = off;
51 off += attr.type->size();
52 alignment = max(alignment, attr.type->alignment());
53 if (attr.type->size() % 8)
54 ++num_not_byte_aligned;
55 }
56 /* Add space for meta data. */
57 offsets_[num_attrs] = off;
58 off += num_attrs; // reserve space for the NULL bitmap
59 uint64_t row_size_ = off;
60
61#if 1
62 /* Compute number of rows within a PAX block. Consider worst case padding of 7 bits (because each column within
63 * a PAX block must be byte-aligned) for every possibly not byte-aligned attribute column. Null bitmap column is
64 * ignored since it is the last column. */
65 num_rows_per_block_ = (block_size_ * 8 - num_not_byte_aligned * 7) / row_size_;
66#else
67 /* Compute number of rows within a PAX block. Assert that the stride jumps to the next block are whole multiples
68 * of a byte by packing potentially less rows into one block. Therefore, padding can be ignored. */
69 auto max_num_rows_per_block = (block_size_ * 8) / row_size_;
70 num_rows_per_block_ = (max_num_rows_per_block / 8) * 8; // round down to next multiple of 8
71#endif
72
73 /* Compute offsets (for all rows in a PAX block) by multiplying the offset for a single row by the number of rows
74 * within a PAX block. Add padding to next byte if necessary. */
75 uint32_t running_bit_offset = 0;
76 for (uint32_t pos = 0; pos != num_attrs; ++pos) {
77 const Attribute &attr = *attrs[pos];
78 offsets_[attr.id] = offsets_[attr.id] * num_rows_per_block_ + running_bit_offset;
79 M_insist(offsets_[attr.id] % 8 == 0, "attribute column must be byte aligned");
80 if (auto bit_offset = (offsets_[attr.id] + attr.type->size() * num_rows_per_block_) % 8; bit_offset)
81 running_bit_offset += 8 - bit_offset;
82 }
83 offsets_[num_attrs] = offsets_[num_attrs] * num_rows_per_block_ + running_bit_offset;
84 M_insist(offsets_[num_attrs] % 8 == 0, "NULL bitmap column must be byte aligned");
85 M_insist(offsets_[num_attrs] + num_attrs * num_rows_per_block_ <= block_size_ * 8);
86
87 /* Compute capacity. */
89
90 delete[] attrs;
91}
92
94void PaxStore::dump(std::ostream &out) const
95{
96 out << "PaxStore at " << data_.addr() << " for table \"" << table().name() << "\": " << num_rows_ << '/' << capacity_
97 << " rows, " << block_size_ << " bytes per block, " << num_rows_per_block_ << " rows per block, offsets in bits [";
98 for (uint32_t i = 0, end = table().num_attrs(); i != end; ++i) {
99 if (i != 0) out << ", ";
100 out << offsets_[i];
101 }
102 out << ']' << std::endl;
103}
105
106__attribute__((constructor(202)))
107static void register_store()
108{
109 Catalog &C = Catalog::Get();
110 C.register_store<PaxStore>(C.pool("PaxStore"), "stores attributes using PAX layout");
111}
__attribute__((constructor(202))) static void register_interpreter()
#define M_insist(...)
Definition: macro.hpp:129
‍mutable namespace
Definition: Backend.hpp:10
An attribute of a table.
Definition: Schema.hpp:289
const PrimitiveType * type
the type of the attribute
Definition: Schema.hpp:294
std::size_t id
the internal identifier of the attribute, unique within its table
Definition: Schema.hpp:292
The catalog contains all Databases and keeps track of all meta information of the database system.
Definition: Catalog.hpp:215
ThreadSafePooledString pool(const char *str) const
Creates an internalized copy of the string str by adding it to the internal StringPool.
Definition: Catalog.hpp:274
static Catalog & Get()
Return a reference to the single Catalog instance.
void register_store(ThreadSafePooledString name, const char *description=nullptr)
Registers a new Store with the given name.
Definition: Catalog.hpp:343
This class implements a generic PAX store.
Definition: PaxStore.hpp:12
uint32_t block_size_
the size of a PAX block, in bytes; includes padding
Definition: PaxStore.hpp:27
void dump() const
Definition: Store.cpp:14
static constexpr std::size_t ALLOCATION_SIZE
1 GiB
Definition: PaxStore.hpp:14
void compute_block_offsets()
Computes the offsets of the columns within a PAX block, the rows size, the number of rows that fit in...
Definition: PaxStore.cpp:29
memory::Memory data_
the underlying memory containing the data
Definition: PaxStore.hpp:23
std::size_t num_rows_per_block_
the number of rows within a PAX block
Definition: PaxStore.hpp:28
std::size_t num_rows_
the number of rows in use
Definition: PaxStore.hpp:24
memory::LinearAllocator allocator_
the memory allocator
Definition: PaxStore.hpp:22
PaxStore(const Table &table, uint32_t block_size_in_bytes=BLOCK_SIZE)
Definition: PaxStore.cpp:14
std::size_t capacity_
the number of available rows
Definition: PaxStore.hpp:25
uint32_t * offsets_
the offsets of each column within a PAX block, in bits
Definition: PaxStore.hpp:26
Defines a generic store interface.
Definition: Store.hpp:22
const Table & table() const
Definition: Store.hpp:36
A table is a sorted set of attributes.
Definition: Schema.hpp:388
virtual const ThreadSafePooledString & name() const =0
Returns the name of the Table.
virtual std::size_t num_attrs() const =0
Returns the number of attributes in this table.
virtual uint64_t alignment() const
Compute the alignment requirement in bits of an instance of this type.
Definition: Type.hpp:95
virtual uint64_t size() const
Compute the size in bits of an instance of this type.
Definition: Type.hpp:92
Memory allocate(std::size_t size) override
Creates a new memory object with size bytes of freshly allocated memory.
Definition: memory.cpp:120
void * addr() const
Returns a pointer to the beginning of the virtual address space where this allocation is mapped to.
Definition: memory.hpp:109