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//------------------------------------------------------------------------------
43class Table
44{
45 using ColumnBuffer = void*;
46
47private:
48 friend class Database;
49 Table() = default;
50 ~Table();
51
52public:
53 class Partition;
54
56 bool HasAttribute(AttributeId attribute) const;
57
59 AttributeId GetAttributeId(ColumnIndex columnIndex) const;
65 TableSignature const& GetSignature() const;
66
68 ColumnIndex AddAttribute(AttributeId attribute, bool updateSignature = true);
70 RowId AddRow();
72 void RemoveRow(RowId row);
74 SizeT GetNumRows() const;
76 void SetNumRows(SizeT value);
78 void SetToDefault(RowId row);
79
81 SizeT Defragment(std::function<void(Partition*, RowId, RowId)> const& moveCallback);
83 void Clean();
85 void Reset();
86
90 uint16_t GetNumActivePartitions() const;
92 uint16_t GetNumPartitions() const;
96 Partition* GetPartition(uint16_t partitionId);
98 void* GetValuePointer(ColumnIndex cid, RowId row);
100 void* GetBuffer(uint16_t partition, ColumnIndex cid);
101
105 void DeserializeInstance(Util::Blob const& data, RowId row);
106
108 static RowId MigrateInstance(
109 Table& src,
110 RowId srcRow,
111 Table& dst,
112 bool defragment = true,
113 std::function<void(Partition*, RowId, RowId)> const& moveCallback = nullptr
114 );
116 static RowId DuplicateInstance(Table const& src, RowId srcRow, Table& dst);
117
119 static void MigrateInstances(
120 Table& src,
121 Util::Array<RowId> const& srcRows,
122 Table& dst,
124 bool defragment = true,
125 std::function<void(Partition*, IndexT, IndexT)> const& moveCallback = nullptr
126 );
128 static void DuplicateInstances(Table& src, Util::Array<RowId> const& srcRows, Table& dst, Util::FixedArray<RowId>& dstRows);
129
131 static constexpr Memory::HeapType HEAP_MEMORY_TYPE = Memory::HeapType::DefaultHeap;
132
135
138
139private:
140
144 TableId tid = TableId::Invalid();
146 uint32_t totalNumRows = 0;
163};
164
165//------------------------------------------------------------------------------
169{
170private:
171 Partition() = default;
172 ~Partition();
173
174public:
175 // total capacity (in elements) that the partition can contain
176 static constexpr uint CAPACITY = 256;
177 // The table that this partition is part of
178 Table* table = nullptr;
179 // next active partition that has entities, or null if end of chain
180 Partition* next = nullptr;
181 // previous active partition that has entities, or null if first in chain
182 Partition* previous = nullptr;
183 // The id of the partition
184 uint16_t partitionId = 0xFFFF;
186 uint32_t numRows = 0;
187 // bump the version if you change anything about the partition
188 uint64_t version = 0;
189 // holds freed indices/rows to be reused in the partition.
199
200private:
201 friend Table;
203 uint16_t AllocateRowIndex();
205 void FreeIndex(uint16_t instance);
207 void EraseSwapIndex(uint16_t instance);
208};
209
210} // namespace MemDb
Definition database.h:28
Definition table.h:169
Partition * next
Definition table.h:180
static constexpr uint CAPACITY
Definition table.h:176
Util::Array< ColumnBuffer > columns
holds all the column buffers. This excludes non-typed attributes
Definition table.h:192
friend Table
Definition table.h:201
Table * table
Definition table.h:178
Util::BitField< CAPACITY > modifiedRows
check a bit if the row has been modified, and you need to track it.
Definition table.h:195
void FreeIndex(uint16_t instance)
Free an index.
Definition table.cc:758
uint16_t partitionId
Definition table.h:184
uint16_t AllocateRowIndex()
recycle free row or allocate new row
Definition table.cc:730
Partition * previous
Definition table.h:182
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:186
uint64_t version
Definition table.h:188
Util::Array< uint16_t > freeIds
Definition table.h:190
Util::BitField< CAPACITY > validRows
bits are set if the row is occupied.
Definition table.h:198
~Partition()
Definition table.cc:714
Definition table.h:44
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:158
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:45
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:146
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:152
Util::StringAtom name
name of the table
Definition table.h:134
Util::Array< Partition * > partitions
All partitions, even null partitions.
Definition table.h:150
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:148
Util::HashTable< AttributeId, IndexT, 32, 1 > columnRegistry
maps attr id -> index in columns array
Definition table.h:162
Partition * firstActivePartition
First partition that has entities. You can use this to iterate over all active partitions with entiti...
Definition table.h:156
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:144
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:154
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:160
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:142
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:131
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