Fixed the Logging system to actually be able to handle errors. Database is also fixed to use directories.
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
#include "ehs/db/Database.h"
|
||||
|
||||
#include "ehs/io/Directory_LNX.h"
|
||||
#include "ehs/io/File.h"
|
||||
|
||||
namespace ehs
|
||||
@@ -13,9 +15,25 @@ namespace ehs
|
||||
{
|
||||
}
|
||||
|
||||
Database::Database(const Str_8& filePath)
|
||||
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<UInt_64> data = file.ReadSerializer_64(Endianness::LE, file.Size());
|
||||
@@ -25,19 +43,30 @@ namespace ehs
|
||||
tables.Resize(data.Read<UInt_64>());
|
||||
|
||||
for (UInt_64 i = 0; i < tables.Size(); ++i)
|
||||
tables[i].Deserialize(data);
|
||||
{
|
||||
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<DbTable>&&)db.tables)
|
||||
: hashId(db.hashId), id((Str_8&&)db.id), version(db.version), tables((Array<DbTable>&&)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)
|
||||
: 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
|
||||
@@ -48,7 +77,12 @@ namespace ehs
|
||||
hashId = db.hashId;
|
||||
id = (Str_8&&)db.id;
|
||||
version = db.version;
|
||||
|
||||
tables = (Array<DbTable>&&)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};
|
||||
@@ -64,7 +98,12 @@ namespace ehs
|
||||
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;
|
||||
}
|
||||
@@ -116,7 +155,9 @@ namespace ehs
|
||||
|
||||
tables.Push(DbTable((Str_8&&)id));
|
||||
|
||||
return &tables[tables.End()];
|
||||
DbTable *tbl = &tables[tables.End()];
|
||||
tbl->parent = this;
|
||||
return tbl;
|
||||
}
|
||||
|
||||
DbTable* Database::GetTable(UInt_64 hashId) const
|
||||
@@ -133,16 +174,25 @@ namespace ehs
|
||||
return GetTable(id.Hash_64());
|
||||
}
|
||||
|
||||
void Database::Save(const Str_8& directory) const
|
||||
Str_8 Database::GetDirectory() const
|
||||
{
|
||||
return dir;
|
||||
}
|
||||
|
||||
void Database::Save(Str_8 directory)
|
||||
{
|
||||
dir = (Str_8&&)directory;
|
||||
|
||||
Serializer<UInt_64> data(Endianness::LE);
|
||||
data.WriteVersion(version);
|
||||
data.Write(tables.Size());
|
||||
|
||||
for (UInt_64 i = 0; i < tables.Size(); ++i)
|
||||
tables[i].Serialize(data);
|
||||
Directory::CreateRecursive(dir);
|
||||
|
||||
File file(directory + "/" + id + ".ehd", Mode::WRITE, Disposition::CREATE_PERSISTENT);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user