EHS/src/db/DbTable.cpp

194 lines
3.8 KiB
C++
Raw Normal View History

#include "ehs/db/DbTable.h"
#include "ehs/io/Directory.h"
#include "ehs/io/File_UNX.h"
namespace ehs
{
DbTable::DbTable()
: hashId(0)
{
}
DbTable::DbTable(Str_8 id)
: hashId(id.Hash_64()), id((Str_8&&)id)
{
}
DbTable::DbTable(DbTable&& table) noexcept
: hashId(table.hashId), id((Str_8&&)table.id), varTmpls((Array<DbVarTmpl>&&)table.varTmpls),
objects((Array<DbObject>&&)table.objects)
{
table.hashId = 0;
}
DbTable::DbTable(const DbTable& table)
: hashId(table.hashId), id(table.id), varTmpls(table.varTmpls), objects(table.objects)
{
}
DbTable& DbTable::operator=(DbTable&& table) noexcept
{
if (this == &table)
return *this;
hashId = table.hashId;
id = (Str_8&&)table.id;
varTmpls = (Array<DbVarTmpl>&&)table.varTmpls;
objects = (Array<DbObject>&&)table.objects;
for (UInt_64 i = 0; i < objects.Size(); ++i)
objects[i].parent = this;
table.hashId = 0;
return *this;
}
DbTable& DbTable::operator=(const DbTable& table)
{
if (this == &table)
return *this;
hashId = table.hashId;
id = table.id;
varTmpls = table.varTmpls;
objects = table.objects;
for (UInt_64 i = 0; i < objects.Size(); ++i)
objects[i].parent = this;
return *this;
}
UInt_64 DbTable::GetHashId() const
{
return hashId;
}
void DbTable::SetId(Str_8 newId)
{
hashId = newId.Hash_64();
id = (Str_8&&)newId;
}
Str_8 DbTable::GetId() const
{
return id;
}
bool DbTable::HasVariable(const UInt_64 hashId) const
{
for (UInt_64 i = 0; i < varTmpls.Size(); ++i)
if (varTmpls[i].GetHashId() == hashId)
return true;
return false;
}
bool DbTable::HasVariable(const Str_8& id) const
{
return HasVariable(id.Hash_64());
}
2024-04-10 18:26:20 -07:00
bool DbTable::CreateVariable(DbVarTmpl var)
{
2024-04-10 18:26:20 -07:00
if (HasVariable(var.GetHashId()))
return false;
2024-04-10 18:26:20 -07:00
varTmpls.Push((DbVarTmpl&&)var);
2024-04-10 18:26:20 -07:00
DbVarTmpl* result = &varTmpls[varTmpls.End()];
for (UInt_64 i = 0; i < objects.Size(); ++i)
2024-04-10 18:26:20 -07:00
objects[i].CreateVariable(result);
return true;
}
DbObject *DbTable::CreateObject()
{
objects.Push(DbObject(objects.Size()));
DbObject* obj = &objects[objects.End()];
obj->parent = this;
for (UInt_64 i = 0; i < varTmpls.Size(); ++i)
obj->CreateVariable(&varTmpls[i]);
return obj;
}
2024-04-10 22:28:45 -07:00
DbObject* DbTable::GetObject(UInt_64 variableHashId, const Str_8& value) const
{
for (UInt_64 i = 0; i < objects.Size(); ++i)
{
objects[i].Load();
const DbVar * const var = objects[i].GetVariable(variableHashId);
if (var->GetValueStr() == value)
return &objects[i];
objects[i].Free();
}
return nullptr;
}
DbObject* DbTable::GetObject(const Str_8& variable, const Str_8& value) const
{
return GetObject(variable.Hash_64(), value);
}
DbObject* DbTable::GetObject(const UInt_64 id) const
{
for (UInt_64 i = 0; i < objects.Size(); ++i)
if (objects[i].GetId() == id)
return &objects[i];
return nullptr;
}
DbVarTmpl *DbTable::GetVariableTemplate(const UInt_64 hashId) const
{
for (UInt_64 i = 0; i < varTmpls.Size(); ++i)
if (varTmpls[i].GetHashId() == hashId)
return &varTmpls[i];
return nullptr;
}
void DbTable::Serialize(Serializer<UInt_64>& data) const
{
data.WriteStr(id);
data.Write(varTmpls.Size());
for (UInt_64 i = 0; i < varTmpls.Size(); ++i)
varTmpls[i].Serialize(data);
for (UInt_64 i = 0; i < objects.Size(); ++i)
objects[i].Save();
}
void DbTable::Deserialize(Serializer<UInt_64>& data)
{
id = data.ReadStr<Char_8, UInt_64>();
hashId = id.Hash_64();
varTmpls.Resize(data.Read<UInt_64>());
for (UInt_64 i = 0; i < varTmpls.Size(); ++i)
varTmpls[i].Deserialize(data);
Array<Str_8> files = Directory::GetAllFiles(id);
for (UInt_64 i = 0; i < files.Size(); ++i)
{
if (File::ParseExt_8(files[i]) != "eho")
continue;
objects.Push(DbObject(File::ParseName_8(files[i]).ToDecimal<UInt_64>()));
objects[objects.End()].parent = this;
}
}
}