Database implementation mostly complete.

This commit is contained in:
2024-04-09 01:44:15 -07:00
parent beba947c69
commit d13fe81ac5
20 changed files with 768 additions and 106 deletions

148
src/db/Database.cpp Normal file
View File

@@ -0,0 +1,148 @@
#include "ehs/db/Database.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(const Str_8& filePath)
{
File file(filePath, Mode::READ, Disposition::OPEN);
id = file.GetName();
hashId = id.Hash_64();
Serializer<UInt_64> data = file.ReadSerializer_64(Endianness::LE, file.Size());
file.Release();
version = data.ReadVersion();
tables.Resize(data.Read<UInt_64>());
for (UInt_64 i = 0; i < tables.Size(); ++i)
tables[i].Deserialize(data);
}
Database::Database(Database&& db) noexcept
: hashId(db.hashId), id((Str_8&&)db.id), version(db.version), tables((Array<DbTable>&&)db.tables)
{
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)
{
}
Database& Database::operator=(Database&& db) noexcept
{
if (this == &db)
return *this;
hashId = db.hashId;
id = (Str_8&&)db.id;
version = db.version;
tables = (Array<DbTable>&&)db.tables;
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;
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));
return &tables[tables.End()];
}
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());
}
void Database::Save(const Str_8& directory) const
{
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);
File file(directory + "/" + id + ".ehd", Mode::WRITE, Disposition::CREATE_PERSISTENT);
file.WriteSerializer_64(data);
}
}