#include "ehs/db/Database.h" #include "ehs/io/Directory_LNX.h" #include "ehs/io/File.h" namespace ehs { Database::Database() : hashId(0) { } Database::Database(Str_8 id, const Version& version) : hashId(id.Hash_64()), id((Str_8&&)id), version(version) { } Database::Database(Str_8 filePath) { filePath = filePath.ReplaceAll("\\", "/"); UInt_64 i = 0; if (filePath.Find("/", &i, SearchPattern::RIGHT_LEFT)) dir = filePath.Sub(0, i); else dir = "./"; File file(filePath, Mode::READ, Disposition::OPEN); Log log = Log::GetLastLog(); if (log.GetType() == LogType::ERR && log.GetCode() == 0) { EHS_LOG_INT(LogType::ERR, 0, "Database file not found at, \"" + filePath + "\"."); return; } id = file.GetName(); hashId = id.Hash_64(); Serializer data = file.ReadSerializer_64(Endianness::LE, file.Size()); file.Release(); version = data.ReadVersion(); tables.Resize(data.Read()); for (UInt_64 i = 0; i < tables.Size(); ++i) { tables[i].parent = this; tables[i].Deserialize(dir, data); } EHS_LOG_SUCCESS(); } Database::Database(Database&& db) noexcept : hashId(db.hashId), id((Str_8&&)db.id), version(db.version), tables((Array&&)db.tables), dir((Str_8&&)db.dir) { for (UInt_64 i = 0; i < tables.Size(); ++i) tables[i].parent = this; db.hashId = 0; db.version = {0, 0, 0}; } Database::Database(const Database& db) : hashId(db.hashId), id(db.id), version(db.version), tables(db.tables), dir(db.dir) { for (UInt_64 i = 0; i < tables.Size(); ++i) tables[i].parent = this; } Database& Database::operator=(Database&& db) noexcept { if (this == &db) return *this; hashId = db.hashId; id = (Str_8&&)db.id; version = db.version; tables = (Array&&)db.tables; for (UInt_64 i = 0; i < tables.Size(); ++i) tables[i].parent = this; dir = (Str_8&&)db.dir; db.hashId = 0; db.version = {0, 0, 0}; return *this; } Database& Database::operator=(const Database& db) { if (this == &db) return *this; hashId = db.hashId; id = db.id; version = db.version; tables = db.tables; for (UInt_64 i = 0; i < tables.Size(); ++i) tables[i].parent = this; dir = db.dir; return *this; } UInt_64 Database::GetHashId() const { return hashId; } void Database::SetId(Str_8 newId) { hashId = newId.Hash_64(); id = (Str_8&&)newId; } Str_8 Database::GetId() const { return id; } void Database::SetVersion(const Version& newVersion) { version = newVersion; } Version Database::GetVersion() const { return version; } bool Database::HasTable(UInt_64 hashId) const { for (UInt_64 i = 0; i < tables.Size(); ++i) if (tables[i].GetHashId() == hashId) return true; return false; } bool Database::HasTable(const Str_8& id) const { return HasTable(id.Hash_64()); } DbTable* Database::CreateTable(Str_8 id) { if (HasTable(id)) return nullptr; tables.Push(DbTable((Str_8&&)id)); DbTable *tbl = &tables[tables.End()]; tbl->parent = this; return tbl; } DbTable* Database::GetTable(UInt_64 hashId) const { for (UInt_64 i = 0; i < tables.Size(); ++i) if (tables[i].GetHashId() == hashId) return &tables[i]; return nullptr; } DbTable* Database::GetTable(const Str_8& id) const { return GetTable(id.Hash_64()); } Str_8 Database::GetDirectory() const { return dir; } void Database::Save(Str_8 directory) { dir = (Str_8&&)directory; Serializer data(Endianness::LE); data.WriteVersion(version); data.Write(tables.Size()); Directory::CreateRecursive(dir); for (UInt_64 i = 0; i < tables.Size(); ++i) tables[i].Serialize(dir, data); File file(dir + "/" + id + ".ehd", Mode::WRITE, Disposition::CREATE_PERSISTENT); file.WriteSerializer_64(data); } }