mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
RowStore.cpp
Go to the documentation of this file.
2
4#include <algorithm>
5#include <exception>
6#include <fstream>
7#include <iomanip>
10#include <mutable/util/fn.hpp>
11#include <typeinfo>
12
13
14using namespace m;
15
16
18 : Store(table)
19 , offsets_(new uint32_t[table.num_attrs() + 1]) // add one slot for the offset of the meta data
20{
24}
25
27{
28 delete[] offsets_;
29}
30
32{
33 /* TODO: use `PhysicalSchema` with additional bitmap-type to compute offsets. */
34 using std::max;
35
36 const auto num_attrs = table().num_attrs();
37 const Attribute **attrs = new const Attribute*[num_attrs];
38
39 for (uint32_t pos = 0; pos != num_attrs; ++pos)
40 attrs[pos] = &table()[pos];
41
42 /* Sort attributes by their alignment requirement in descending order. */
43 std::stable_sort(attrs, attrs + num_attrs, [](const Attribute *first, const Attribute *second) {
44 return first->type->alignment() > second->type->alignment();
45 });
46
47 /* Compute offsets. */
48 uint64_t off = 0;
49 uint64_t alignment = 8;
50 for (uint32_t pos = 0; pos != num_attrs; ++pos) {
51 const Attribute &attr = *attrs[pos];
52 offsets_[attr.id] = off;
53 off += attr.type->size();
54 alignment = max(alignment, attr.type->alignment());
55 }
56 /* Add space for meta data. */
57 offsets_[num_attrs] = off;
58 off += num_attrs; // reserve space for the NULL bitmap
59 if (off % alignment)
60 off += (alignment - off % alignment); // the offset is padded to fulfill the alignment requirements
61 row_size_ = off;
62
63 delete[] attrs;
64}
65
67void 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}
78
79__attribute__((constructor(202)))
80static void register_store()
81{
82 Catalog &C = Catalog::Get();
83 C.register_store<RowStore>(C.pool("RowStore"), "stores attributes in row-major order");
84}
__attribute__((constructor(202))) static void register_interpreter()
‍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 row store.
Definition: RowStore.hpp:12
memory::Memory data_
the underlying memory containing the data
Definition: RowStore.hpp:21
void dump() const
Definition: Store.cpp:14
std::size_t num_rows_
the number of rows in use
Definition: RowStore.hpp:22
uint32_t * offsets_
the offsets from the first column, in bits, of all columns
Definition: RowStore.hpp:24
memory::LinearAllocator allocator_
the memory allocator
Definition: RowStore.hpp:20
std::size_t capacity_
the number of available rows
Definition: RowStore.hpp:23
uint32_t row_size_
the size of a row, in bits; includes NULL bitmap and other meta data
Definition: RowStore.hpp:25
static constexpr std::size_t ALLOCATION_SIZE
1 GiB
Definition: RowStore.hpp:14
void compute_offsets()
Computes the offsets of the attributes within a row.
Definition: RowStore.cpp:31
RowStore(const Table &table)
Definition: RowStore.cpp:17
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