2024-04-09 01:44:15 -07:00
|
|
|
#include "ehs/db/DbTable.h"
|
2024-04-23 22:29:49 -07:00
|
|
|
#include "ehs/db/Database.h"
|
2024-04-09 01:44:15 -07:00
|
|
|
#include "ehs/io/Directory.h"
|
2024-07-24 01:36:20 -07:00
|
|
|
#include "ehs/io/File.h"
|
2024-04-09 01:44:15 -07:00
|
|
|
|
|
|
|
namespace ehs
|
|
|
|
{
|
|
|
|
DbTable::DbTable()
|
2024-04-23 22:29:49 -07:00
|
|
|
: parent(nullptr), hashId(0)
|
2024-04-09 01:44:15 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DbTable::DbTable(Str_8 id)
|
2024-04-23 22:29:49 -07:00
|
|
|
: parent(nullptr), hashId(id.Hash_64()), id((Str_8&&)id)
|
2024-04-09 01:44:15 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DbTable::DbTable(DbTable&& table) noexcept
|
2024-04-23 22:29:49 -07:00
|
|
|
: parent(table.parent), hashId(table.hashId), id((Str_8&&)table.id), varTmpls((Array<DbVarTmpl>&&)table.varTmpls),
|
2024-04-09 01:44:15 -07:00
|
|
|
objects((Array<DbObject>&&)table.objects)
|
|
|
|
{
|
2024-04-23 22:29:49 -07:00
|
|
|
table.parent = nullptr;
|
2024-04-09 01:44:15 -07:00
|
|
|
table.hashId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DbTable::DbTable(const DbTable& table)
|
2024-04-23 22:29:49 -07:00
|
|
|
: parent(nullptr), hashId(table.hashId), id(table.id), varTmpls(table.varTmpls), objects(table.objects)
|
2024-04-09 01:44:15 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DbTable& DbTable::operator=(DbTable&& table) noexcept
|
|
|
|
{
|
|
|
|
if (this == &table)
|
|
|
|
return *this;
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
parent = table.parent;
|
2024-04-09 01:44:15 -07:00
|
|
|
hashId = table.hashId;
|
|
|
|
id = (Str_8&&)table.id;
|
|
|
|
varTmpls = (Array<DbVarTmpl>&&)table.varTmpls;
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
objects = (Array<DbObject>&&)table.objects;
|
2024-04-09 01:44:15 -07:00
|
|
|
for (UInt_64 i = 0; i < objects.Size(); ++i)
|
|
|
|
objects[i].parent = this;
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
table.parent = nullptr;
|
2024-04-09 01:44:15 -07:00
|
|
|
table.hashId = 0;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
DbTable& DbTable::operator=(const DbTable& table)
|
|
|
|
{
|
|
|
|
if (this == &table)
|
|
|
|
return *this;
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
parent = nullptr;
|
2024-04-09 01:44:15 -07:00
|
|
|
hashId = table.hashId;
|
|
|
|
id = table.id;
|
|
|
|
varTmpls = table.varTmpls;
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
objects = table.objects;
|
2024-04-09 01:44:15 -07:00
|
|
|
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-09 01:44:15 -07:00
|
|
|
{
|
2024-04-10 18:26:20 -07:00
|
|
|
if (HasVariable(var.GetHashId()))
|
2024-04-09 01:44:15 -07:00
|
|
|
return false;
|
|
|
|
|
2024-04-10 18:26:20 -07:00
|
|
|
varTmpls.Push((DbVarTmpl&&)var);
|
2024-04-09 01:44:15 -07:00
|
|
|
|
2024-04-10 18:26:20 -07:00
|
|
|
DbVarTmpl* result = &varTmpls[varTmpls.End()];
|
2024-04-09 01:44:15 -07:00
|
|
|
|
|
|
|
for (UInt_64 i = 0; i < objects.Size(); ++i)
|
2024-04-10 18:26:20 -07:00
|
|
|
objects[i].CreateVariable(result);
|
2024-04-09 02:31:10 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DbObject *DbTable::CreateObject()
|
2024-04-09 01:44:15 -07:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-04-09 02:31:10 -07:00
|
|
|
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
|
2024-04-09 01:44:15 -07:00
|
|
|
{
|
|
|
|
for (UInt_64 i = 0; i < varTmpls.Size(); ++i)
|
|
|
|
if (varTmpls[i].GetHashId() == hashId)
|
|
|
|
return &varTmpls[i];
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
void DbTable::Serialize(const Str_8 &dir, Serializer<UInt_64>& data) const
|
2024-04-09 01:44:15 -07:00
|
|
|
{
|
|
|
|
data.WriteStr(id);
|
|
|
|
data.Write(varTmpls.Size());
|
|
|
|
|
|
|
|
for (UInt_64 i = 0; i < varTmpls.Size(); ++i)
|
|
|
|
varTmpls[i].Serialize(data);
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
if (objects.Size())
|
|
|
|
Directory::Create(dir + "/" + id);
|
|
|
|
|
2024-04-09 01:44:15 -07:00
|
|
|
for (UInt_64 i = 0; i < objects.Size(); ++i)
|
|
|
|
objects[i].Save();
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
void DbTable::Deserialize(const Str_8 &dir, Serializer<UInt_64>& data)
|
2024-04-09 01:44:15 -07:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
Array<Str_8> files = Directory::GetAllFiles(dir + "/" + id);
|
2024-04-09 01:44:15 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|