Database is now working and functional.

This commit is contained in:
Arron David Nelson 2024-04-09 02:31:10 -07:00
parent d13fe81ac5
commit 72347ea9a2
8 changed files with 55 additions and 10 deletions

View File

@ -36,6 +36,8 @@ namespace ehs
DbVar* GetVariable(UInt_64 hashId) const;
DbVar* GetVariable(const Str_8& id) const;
void Save() const;
void Load();

View File

@ -46,8 +46,12 @@ namespace ehs
bool CreateVariable(Str_8 id, DbType type, UInt_64 size, const Byte* defaultValue);
bool CreateVariable(Str_8 id, DbType type);
DbObject *CreateObject();
DbObject *GetObject(UInt_64 id) const;
private:
DbVarTmpl *GetVariableTemplate(UInt_64 hashId) const;

View File

@ -26,7 +26,7 @@ namespace ehs
DbVar();
DbVar(UInt_64 hashId, DbVarTmpl *master, UInt_64 size, const Byte *data);
DbVar(UInt_64 hashId, DbVarTmpl *master);
DbVar(DbVar &&var) noexcept;

View File

@ -32,6 +32,8 @@ namespace ehs
DbVarTmpl(Str_8 id, DbType type, const Byte* def);
DbVarTmpl(Str_8 id, DbType type);
DbVarTmpl(DbVarTmpl&& varTmpl) noexcept;
DbVarTmpl(const DbVarTmpl& varTmpl);

View File

@ -83,6 +83,11 @@ namespace ehs
return nullptr;
}
DbVar* DbObject::GetVariable(const Str_8& id) const
{
return GetVariable(id.Hash_64());
}
void DbObject::Save() const
{
if (!IsLoaded())
@ -95,7 +100,7 @@ namespace ehs
for (UInt_64 i = 0; i < vars.Size(); ++i)
vars[i].Serialize(data);
File file(parent->GetId() + "/" + Str_8::FromNum(id), Mode::WRITE, Disposition::CREATE_PERSISTENT);
File file(parent->GetId() + "/" + Str_8::FromNum(id) + ".eho", Mode::WRITE, Disposition::CREATE_PERSISTENT);
file.WriteSerializer_64(data);
}
@ -104,15 +109,18 @@ namespace ehs
if (IsLoaded())
return;
File file(parent->GetId() + "/" + Str_8::FromNum(id), Mode::READ, Disposition::OPEN);
File file(parent->GetId() + "/" + Str_8::FromNum(id) + ".eho", Mode::READ, Disposition::OPEN);
Serializer<UInt_64> data = file.ReadSerializer_64(Endianness::LE, file.Size());
file.Release();
vars.Resize(data.Read<UInt_64>());
for (UInt_64 i = 0; i < vars.Size(); ++i)
{
vars[i].parent = this;
vars[i].Deserialize(data);
}
}
bool DbObject::IsLoaded() const
{
@ -126,7 +134,7 @@ namespace ehs
void DbObject::CreateVariable(DbVarTmpl* master)
{
vars.Push(DbVar(master->GetHashId(), master, master->GetSize(), master->GetDefault()));
vars.Push(DbVar(master->GetHashId(), master));
vars[vars.End()].parent = this;
}
}

View File

@ -120,6 +120,21 @@ namespace ehs
return true;
}
bool DbTable::CreateVariable(Str_8 id, const DbType type)
{
if (HasVariable(id))
return false;
varTmpls.Push(DbVarTmpl((Str_8&&)id, type));
DbVarTmpl* var = &varTmpls[varTmpls.End()];
for (UInt_64 i = 0; i < objects.Size(); ++i)
objects[i].CreateVariable(var);
return true;
}
DbObject *DbTable::CreateObject()
{
objects.Push(DbObject(objects.Size()));
@ -134,7 +149,16 @@ namespace ehs
return obj;
}
DbVarTmpl* DbTable::GetVariableTemplate(UInt_64 hashId) const
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)

View File

@ -16,11 +16,11 @@ namespace ehs
{
}
DbVar::DbVar(const UInt_64 hashId, DbVarTmpl *master, const UInt_64 size, const Byte *const data)
: hashId(hashId), parent(nullptr), master(master), size(size), data(new Byte[DbTypeToSize(master->GetType()) * size])
DbVar::DbVar(const UInt_64 hashId, DbVarTmpl *master)
: hashId(hashId), parent(nullptr), master(master), size(master->GetSize()), data(new Byte[DbTypeToSize(master->GetType()) * size])
{
master->slaves.Push(this);
Util::Copy(this->data, data, DbTypeToSize(master->GetType()) * size);
Util::Copy(data, master->GetDefault(), DbTypeToSize(master->GetType()) * size);
}
DbVar::DbVar(DbVar &&var) noexcept

View File

@ -26,6 +26,11 @@ namespace ehs
Util::Copy(this->def, def, DbTypeToSize(type));
}
DbVarTmpl::DbVarTmpl(Str_8 id, DbType type)
: hashId(id.Hash_64()), id((Str_8&&)id), type(type), array(true), size(0), def(nullptr)
{
}
DbVarTmpl::DbVarTmpl(DbVarTmpl &&varTmpl) noexcept
: hashId(varTmpl.hashId), id((Str_8&&)varTmpl.id), type(varTmpl.type), array(varTmpl.array), size(varTmpl.size),
def(varTmpl.def)