diff --git a/include/ehs/db/DbObject.h b/include/ehs/db/DbObject.h index 2a52825..6c5309c 100644 --- a/include/ehs/db/DbObject.h +++ b/include/ehs/db/DbObject.h @@ -36,6 +36,8 @@ namespace ehs DbVar* GetVariable(UInt_64 hashId) const; + DbVar* GetVariable(const Str_8& id) const; + void Save() const; void Load(); diff --git a/include/ehs/db/DbTable.h b/include/ehs/db/DbTable.h index 5a436f2..b86c307 100644 --- a/include/ehs/db/DbTable.h +++ b/include/ehs/db/DbTable.h @@ -46,10 +46,14 @@ 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; + DbVarTmpl *GetVariableTemplate(UInt_64 hashId) const; void Serialize(Serializer& data) const; diff --git a/include/ehs/db/DbVar.h b/include/ehs/db/DbVar.h index 5d3b8ae..74d1f6a 100644 --- a/include/ehs/db/DbVar.h +++ b/include/ehs/db/DbVar.h @@ -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; diff --git a/include/ehs/db/DbVarTmpl.h b/include/ehs/db/DbVarTmpl.h index 282b0bf..e41d5c7 100644 --- a/include/ehs/db/DbVarTmpl.h +++ b/include/ehs/db/DbVarTmpl.h @@ -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); diff --git a/src/db/DbObject.cpp b/src/db/DbObject.cpp index 1e3de63..a0410b7 100644 --- a/src/db/DbObject.cpp +++ b/src/db/DbObject.cpp @@ -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,14 +109,17 @@ 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 data = file.ReadSerializer_64(Endianness::LE, file.Size()); file.Release(); vars.Resize(data.Read()); 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; } } diff --git a/src/db/DbTable.cpp b/src/db/DbTable.cpp index fbcf108..dbf6b09 100644 --- a/src/db/DbTable.cpp +++ b/src/db/DbTable.cpp @@ -120,7 +120,22 @@ namespace ehs return true; } - DbObject* DbTable::CreateObject() + 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) diff --git a/src/db/DbVar.cpp b/src/db/DbVar.cpp index 30a5d38..f2843c8 100644 --- a/src/db/DbVar.cpp +++ b/src/db/DbVar.cpp @@ -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 diff --git a/src/db/DbVarTmpl.cpp b/src/db/DbVarTmpl.cpp index aee80c1..77b5bbc 100644 --- a/src/db/DbVarTmpl.cpp +++ b/src/db/DbVarTmpl.cpp @@ -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)