Nebula
Loading...
Searching...
No Matches
table.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
11//------------------------------------------------------------------------------
12#include "ids/id.h"
13#include "util/arrayallocator.h"
14#include "util/fixedarray.h"
15#include "util/string.h"
16#include "util/stringatom.h"
17#include "util/hashtable.h"
18#include "attributeid.h"
19#include "tablesignature.h"
20#include "util/bitfield.h"
21#include "util/queue.h"
23#include "tableid.h"
24#include <functional>
25
26namespace MemDb
27{
28
39
40//------------------------------------------------------------------------------
63class Table
64{
65 using ColumnBuffer = void*;
66
67private:
68 friend class Database;
69 Table() = default;
70 ~Table();
71
72public:
73 class Partition;
74
76 bool HasAttribute(AttributeId attribute) const;
77
79 AttributeId GetAttributeId(ColumnIndex columnIndex) const;
85 TableSignature const& GetSignature() const;
86
88 ColumnIndex AddAttribute(AttributeId attribute, bool updateSignature = true);
90 RowId AddRow();
92 void RemoveRow(RowId row);
94 SizeT GetNumRows() const;
96 void SetNumRows(SizeT value);
98 void SetToDefault(RowId row);
99
101 SizeT Defragment(std::function<void(Partition*, RowId, RowId)> const& moveCallback);
103 void Clean();
105 void Reset();
106
110 uint16_t GetNumActivePartitions() const;
112 uint16_t GetNumPartitions() const;
116 Partition* GetPartition(uint16_t partitionId);
118 void* GetValuePointer(ColumnIndex cid, RowId row);
120 void* GetBuffer(uint16_t partition, ColumnIndex cid);
121
125 void DeserializeInstance(Util::Blob const& data, RowId row);
126
128 static RowId MigrateInstance(
129 Table& src,
130 RowId srcRow,
131 Table& dst,
132 bool defragment = true,
133 std::function<void(Partition*, RowId, RowId)> const& moveCallback = nullptr
134 );
136 static RowId DuplicateInstance(Table const& src, RowId srcRow, Table& dst);
137
139 static void MigrateInstances(
140 Table& src,
141 Util::Array<RowId> const& srcRows,
142 Table& dst,
144 bool defragment = true,
145 std::function<void(Partition*, IndexT, IndexT)> const& moveCallback = nullptr
146 );
148 static void DuplicateInstances(Table& src, Util::Array<RowId> const& srcRows, Table& dst, Util::FixedArray<RowId>& dstRows);
149
151 static constexpr Memory::HeapType HEAP_MEMORY_TYPE = Memory::HeapType::DefaultHeap;
152
155
158
159private:
160
164 TableId tid = TableId::Invalid();
166 uint32_t totalNumRows = 0;
183};
184
185//------------------------------------------------------------------------------
205{
206private:
207 Partition() = default;
208 ~Partition();
209
210public:
211 // total capacity (in elements) that the partition can contain
212 static constexpr uint CAPACITY = 256;
213 // The table that this partition is part of
214 Table* table = nullptr;
215 // next active partition that has entities, or null if end of chain
216 Partition* next = nullptr;
217 // previous active partition that has entities, or null if first in chain
218 Partition* previous = nullptr;
219 // The id of the partition
220 uint16_t partitionId = 0xFFFF;
222 uint32_t numRows = 0;
223 // bump the version if you change anything about the partition
224 uint64_t version = 0;
225 // holds freed indices/rows to be reused in the partition.
235
236private:
237 friend Table;
239 uint16_t AllocateRowIndex();
241 void FreeIndex(uint16_t instance);
243 void EraseSwapIndex(uint16_t instance);
244};
245
246} // namespace MemDb
Definition database.h:28
Represents a partition within a Table in MemDb.
Partition * next
Definition table.h:216
static constexpr uint CAPACITY
Definition table.h:212
Util::Array< ColumnBuffer > columns
holds all the column buffers. This excludes non-typed attributes
Definition table.h:228
friend Table
Definition table.h:237
Table * table
Definition table.h:214
Util::BitField< CAPACITY > modifiedRows
check a bit if the row has been modified, and you need to track it.
Definition table.h:231
void FreeIndex(uint16_t instance)
Free an index.
Definition table.cc:758
uint16_t partitionId
Definition table.h:220
uint16_t AllocateRowIndex()
recycle free row or allocate new row
Definition table.cc:730
Partition * previous
Definition table.h:218
void EraseSwapIndex(uint16_t instance)
erase row by swapping with last row and reducing number of rows in table
Definition table.cc:769
uint32_t numRows
number of rows
Definition table.h:222
uint64_t version
Definition table.h:224
Util::Array< uint16_t > freeIds
Definition table.h:226
Util::BitField< CAPACITY > validRows
bits are set if the row is occupied.
Definition table.h:234
~Partition()
Definition table.cc:714
A table of columns (attributes) and rows.
void Reset()
Reset table. Deallocate all data.
Definition table.cc:383
SizeT GetNumRows() const
Get total number of rows in a table.
Definition table.cc:239
uint16_t numActivePartitions
number of active partitions
Definition table.h:178
ColumnIndex GetAttributeIndex(AttributeId attribute) const
Returns the index of the attribute or invalid if attribute is missing from table.
Definition table.cc:123
void * ColumnBuffer
Definition table.h:65
void RemoveRow(RowId row)
Deallocate a row from a table. This only frees the row for recycling. See Defragment.
Definition table.cc:229
SizeT Defragment(std::function< void(Partition *, RowId, RowId)> const &moveCallback)
Defragment table.
Definition table.cc:277
uint32_t totalNumRows
sum of all rows in all partitions of this table,
Definition table.h:166
bool HasAttribute(AttributeId attribute) const
Check if a column exists in the table.
Definition table.cc:105
void SetToDefault(RowId row)
Set all row values to default.
Definition table.cc:257
uint16_t GetNumActivePartitions() const
Get number of partitions that contain entities.
Definition table.cc:414
Partition * GetPartition(uint16_t partitionId)
Definition table.cc:441
Util::Array< Partition * > freePartitions
free partitions for recycling allocated partitions
Definition table.h:172
Util::StringAtom name
name of the table
Definition table.h:154
Util::Array< Partition * > partitions
All partitions, even null partitions.
Definition table.h:170
uint16_t GetNumPartitions() const
Get number of partitions in table.
Definition table.cc:423
ColumnIndex AddAttribute(AttributeId attribute, bool updateSignature=true)
Add an attribute to the table.
Definition table.cc:154
void DeserializeInstance(Util::Blob const &data, RowId row)
deserialize a blob into a row
Definition table.cc:514
Partition * currentPartition
Current partition that we'll be using when allocating data.
Definition table.h:168
Util::HashTable< AttributeId, IndexT, 32, 1 > columnRegistry
maps attr id -> index in columns array
Definition table.h:182
Partition * firstActivePartition
First partition that has entities. You can use this to iterate over all active partitions with entiti...
Definition table.h:176
void SetNumRows(SizeT value)
set total number of rows in a table. This does not allocate any memory or create instaces,...
Definition table.cc:248
TableId tid
table identifier
Definition table.h:164
Partition * GetFirstActivePartition()
Get first active partition with entities.
Definition table.cc:405
Util::Blob SerializeInstance(RowId row) const
Serialize a row into a blob.
Definition table.cc:479
static void DuplicateInstances(Table &src, Util::Array< RowId > const &srcRows, Table &dst, Util::FixedArray< RowId > &dstRows)
duplicate instance from one row into destination table.
Definition table.cc:662
void * GetBuffer(uint16_t partition, ColumnIndex cid)
get a buffer. Might be invalidated if rows are allocated or deallocated
Definition table.cc:469
Table()=default
Partition * GetCurrentPartition()
Get current partition.
Definition table.cc:432
Util::Array< uint16_t > nullPartitions
indices to null partitions
Definition table.h:174
static RowId DuplicateInstance(Table const &src, RowId srcRow, Table &dst)
duplicate instance from one row into destination table.
Definition table.cc:580
Util::Array< AttributeId > attributes
all attributes that this table has
Definition table.h:180
static void MigrateInstances(Table &src, Util::Array< RowId > const &srcRows, Table &dst, Util::FixedArray< RowId > &dstRows, bool defragment=true, std::function< void(Partition *, IndexT, IndexT)> const &moveCallback=nullptr)
move n instances from one table to another.
Definition table.cc:625
void * GetValuePointer(ColumnIndex cid, RowId row)
get a buffer. Might be invalidated if rows are allocated or deallocated
Definition table.cc:451
TableSignature signature
the signature of this table. Contains one bit set to true for every attribute that exists in the tabl...
Definition table.h:162
Util::Array< AttributeId > const & GetAttributes() const
Get the all descriptors for a table.
Definition table.cc:136
~Table()
Definition table.cc:39
RowId AddRow()
Add/Get a free row from the table.
Definition table.cc:200
TableSignature const & GetSignature() const
Get the table signature.
Definition table.cc:145
static RowId MigrateInstance(Table &src, RowId srcRow, Table &dst, bool defragment=true, std::function< void(Partition *, RowId, RowId)> const &moveCallback=nullptr)
move instance from one table to another.
Definition table.cc:544
static constexpr Memory::HeapType HEAP_MEMORY_TYPE
allocation heap used for the column buffers
Definition table.h:151
void Clean()
Clean table. Does not deallocate anything; just sets the size of the table to zero.
Definition table.cc:365
Partition * NewPartition()
Create a new partition for this table. Adds it to the list of partitions and the vacancy list.
Definition table.cc:48
Basically a bitfield with packed ComponentIds.
Definition tablesignature.h:26
Nebula's dynamic array class.
Definition array.h:60
Implements large bit field.
Definition bitfield.h:24
The Util::Blob class encapsulates a chunk of raw memory into a C++ object which can be copied,...
Definition blob.h:22
Implements a fixed size one-dimensional array.
Definition fixedarray.h:20
Organizes key/value pairs by a hash code.
Definition hashtable.h:42
A StringAtom.
Definition stringatom.h:22
Attribute.
Definition attribute.h:26
AttributeId GetAttributeId()
Definition attributeregistry.h:67
HeapType
Heap types are defined here.
Definition osxmemoryconfig.h:25
Definition attributeid.h:19
column id
Definition tableid.h:38
row identifier
Definition tableid.h:18
information for creating a table
Definition table.h:31
SizeT numAttributes
number of columns
Definition table.h:37
AttributeId const * attributeIds
array of attributes the table should initially have
Definition table.h:35
Util::String name
name to be given to the table
Definition table.h:33
Table identifier.
Definition tableid.h:14
Nebula's universal string class.
Definition string.h:50
int SizeT
Definition types.h:49
unsigned int uint
Definition types.h:31
int IndexT
Definition types.h:48