#include "ehs/db/DbTable.h" #include "ehs/db/Database.h" #include "ehs/io/Directory.h" #include "ehs/io/File.h" namespace ehs { DbTable::DbTable() : parent(nullptr), hashId(0) { } DbTable::DbTable(Str_8 id) : parent(nullptr), hashId(id.Hash_64()), id((Str_8&&)id) { } DbTable::DbTable(DbTable&& table) noexcept : parent(table.parent), hashId(table.hashId), id((Str_8&&)table.id), varTmpls((Array&&)table.varTmpls), objects((Array&&)table.objects) { table.parent = nullptr; table.hashId = 0; } DbTable::DbTable(const DbTable& table) : parent(nullptr), hashId(table.hashId), id(table.id), varTmpls(table.varTmpls), objects(table.objects) { } DbTable& DbTable::operator=(DbTable&& table) noexcept { if (this == &table) return *this; parent = table.parent; hashId = table.hashId; id = (Str_8&&)table.id; varTmpls = (Array&&)table.varTmpls; objects = (Array&&)table.objects; for (UInt_64 i = 0; i < objects.Size(); ++i) objects[i].parent = this; table.parent = nullptr; table.hashId = 0; return *this; } DbTable& DbTable::operator=(const DbTable& table) { if (this == &table) return *this; parent = nullptr; 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()); } bool DbTable::CreateVariable(DbVarTmpl var) { if (HasVariable(var.GetHashId())) return false; varTmpls.Push((DbVarTmpl&&)var); DbVarTmpl* result = &varTmpls[varTmpls.End()]; for (UInt_64 i = 0; i < objects.Size(); ++i) 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; } 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(const Str_8 &dir, Serializer& data) const { data.WriteStr(id); data.Write(varTmpls.Size()); for (UInt_64 i = 0; i < varTmpls.Size(); ++i) varTmpls[i].Serialize(data); if (objects.Size()) Directory::Create(dir + "/" + id); for (UInt_64 i = 0; i < objects.Size(); ++i) objects[i].Save(); } void DbTable::Deserialize(const Str_8 &dir, Serializer& data) { id = data.ReadStr(); hashId = id.Hash_64(); varTmpls.Resize(data.Read()); for (UInt_64 i = 0; i < varTmpls.Size(); ++i) varTmpls[i].Deserialize(data); Array files = Directory::GetAllFiles(dir + "/" + 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())); objects[objects.End()].parent = this; } } }