From 3196c5021e06f3338f847aa41551e8b4e68b2d70 Mon Sep 17 00:00:00 2001 From: Karutoh Date: Wed, 10 Apr 2024 18:26:20 -0700 Subject: [PATCH] Simplified the database. --- CMakeLists.txt | 1 - include/ehs/db/DbTable.h | 6 +-- include/ehs/db/DbType.h | 61 ---------------------- include/ehs/db/DbVar.h | 71 ++++++++++++++++++++++---- include/ehs/db/DbVarTmpl.h | 45 ++++++++++------- src/db/DbObject.cpp | 1 + src/db/DbTable.cpp | 40 ++------------- src/db/DbVar.cpp | 85 ++++++++----------------------- src/db/DbVarTmpl.cpp | 100 +++++++------------------------------ 9 files changed, 131 insertions(+), 279 deletions(-) delete mode 100644 include/ehs/db/DbType.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e0803db..9ebe0ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -166,7 +166,6 @@ set(EHS_SOURCES src/io/UsbBase.cpp include/ehs/io/Usb_LNX.h src/io/Usb_LNX.cpp - include/ehs/db/DbType.h include/ehs/db/DbTable.h include/ehs/db/DbObject.h include/ehs/db/DbVar.h diff --git a/include/ehs/db/DbTable.h b/include/ehs/db/DbTable.h index b86c307..4c92fce 100644 --- a/include/ehs/db/DbTable.h +++ b/include/ehs/db/DbTable.h @@ -42,11 +42,7 @@ namespace ehs bool HasVariable(const Str_8& id) const; - bool CreateVariable(Str_8 id, DbType type, const Byte* defaultValue); - - bool CreateVariable(Str_8 id, DbType type, UInt_64 size, const Byte* defaultValue); - - bool CreateVariable(Str_8 id, DbType type); + bool CreateVariable(DbVarTmpl var); DbObject *CreateObject(); diff --git a/include/ehs/db/DbType.h b/include/ehs/db/DbType.h deleted file mode 100644 index e871b8c..0000000 --- a/include/ehs/db/DbType.h +++ /dev/null @@ -1,61 +0,0 @@ -#pragma once - -#include "ehs/Types.h" - -namespace ehs -{ - enum class DbType : UInt_8 - { - SINT_64, - UINT_64, - SINT_32, - UINT_32, - SINT_16, - UINT_16, - SINT_8, - UINT_8, - FLOAT, - DOUBLE, - BOOLEAN, - CHAR_32, - CHAR_16, - CHAR_8 - }; - - constexpr UInt_8 DbTypeToSize(const DbType type) - { - switch (type) - { - case DbType::SINT_64: - return 8; - case DbType::UINT_64: - return 8; - case DbType::SINT_32: - return 4; - case DbType::UINT_32: - return 4; - case DbType::SINT_16: - return 2; - case DbType::UINT_16: - return 2; - case DbType::SINT_8: - return 1; - case DbType::UINT_8: - return 1; - case DbType::FLOAT: - return 4; - case DbType::DOUBLE: - return 8; - case DbType::BOOLEAN: - return 1; - case DbType::CHAR_32: - return 4; - case DbType::CHAR_16: - return 2; - case DbType::CHAR_8: - return 1; - default: - return 0; - } - } -} diff --git a/include/ehs/db/DbVar.h b/include/ehs/db/DbVar.h index 74d1f6a..9fdc1cd 100644 --- a/include/ehs/db/DbVar.h +++ b/include/ehs/db/DbVar.h @@ -1,7 +1,6 @@ #pragma once #include "ehs/Types.h" -#include "DbType.h" #include "ehs/Serializer.h" namespace ehs @@ -13,20 +12,18 @@ namespace ehs { private: friend class DbObject; - friend class DbVarTmpl; UInt_64 hashId; DbObject *parent; - DbVarTmpl *master; + Byte *value; UInt_64 size; - Byte *data; public: ~DbVar(); DbVar(); - DbVar(UInt_64 hashId, DbVarTmpl *master); + DbVar(UInt_64 hashId, const DbVarTmpl *master); DbVar(DbVar &&var) noexcept; @@ -40,14 +37,66 @@ namespace ehs UInt_64 GetHashId() const; + template + void SetValueArray(const T* const newValue, const UInt_64 newSize) + { + size = sizeof(T) * newSize; + + value = new Byte[size]; + + Util::Copy(value, newValue, size); + } + + template + void SetValueStr(const T * const newValue) + { + size = sizeof(T) * Str::Len(newValue); + + value = new Byte[size]; + + Util::Copy(value, newValue, size); + } + + template + void SetValueStr(const Str& newValue) + { + size = newValue.Size(true); + + value = new Byte[size]; + + Util::Copy(value, &newValue[0], size); + } + + template + void SetValue(const Byte* newValue) + { + size = sizeof(T); + + value = new Byte[size]; + + Util::Copy(value, newValue, size); + } + + template + T* GetValueArray() const + { + return (T*)value; + } + + template + Str GetValueStr() const + { + return {(T*)value, size / sizeof(T)}; + } + + template + T GetValue() const + { + return *(T*)value; + } + UInt_64 GetSize() const; - void SetData(UInt_64 newSize, const Byte* newData); - - void SetData(const Byte* newData); - - Byte* GetData() const; - private: void Serialize(Serializer &data) const; diff --git a/include/ehs/db/DbVarTmpl.h b/include/ehs/db/DbVarTmpl.h index e41d5c7..cc5ee76 100644 --- a/include/ehs/db/DbVarTmpl.h +++ b/include/ehs/db/DbVarTmpl.h @@ -1,6 +1,5 @@ #pragma once -#include "DbType.h" #include "ehs/EHS.h" #include "ehs/Serializer.h" #include "ehs/Str.h" @@ -13,26 +12,32 @@ namespace ehs { private: friend class DbTable; - friend class DbVar; UInt_64 hashId; Str_8 id; - DbType type; - bool array; - UInt_64 size; Byte* def; - Array slaves; + UInt_64 size; public: ~DbVarTmpl(); DbVarTmpl(); - DbVarTmpl(Str_8 id, DbType type, UInt_64 size, const Byte* def); + template + DbVarTmpl(Str_8 id, const T* const def, UInt_64 size) + : hashId(id.Hash_64()), id((Str_8&&)id), def(new Byte[sizeof(T) * size]), size(sizeof(T) * size) + { + Util::Copy(this->def, def, this->size); + } - DbVarTmpl(Str_8 id, DbType type, const Byte* def); + template + DbVarTmpl(Str_8 id, const T* const def) + : hashId(id.Hash_64()), id((Str_8&&)id), def(new Byte[sizeof(T)]), size(sizeof(T)) + { + Util::Copy(this->def, def, this->size); + } - DbVarTmpl(Str_8 id, DbType type); + DbVarTmpl(Str_8 id); DbVarTmpl(DbVarTmpl&& varTmpl) noexcept; @@ -42,27 +47,29 @@ namespace ehs DbVarTmpl& operator=(const DbVarTmpl& varTmpl); + operator Byte *() const; + UInt_64 GetHashId() const; void SetId(Str_8 newId); Str_8 GetId() const; - void SetType(DbType newType); + template + T* GetDefaultArray() const + { + return (T*)def; + } - DbType GetType() const; - - void SetIsArray(bool value); - - bool IsArray() const; + template + T GetDefault() const + { + return *(T*)def; + } UInt_64 GetSize() const; - Byte* GetDefault() const; - private: - void UpdateSlave(const DbVar *oldSlave, DbVar *newSlave) const; - void Serialize(Serializer &data) const; void Deserialize(Serializer &data); diff --git a/src/db/DbObject.cpp b/src/db/DbObject.cpp index a0410b7..95349f1 100644 --- a/src/db/DbObject.cpp +++ b/src/db/DbObject.cpp @@ -101,6 +101,7 @@ namespace ehs vars[i].Serialize(data); File file(parent->GetId() + "/" + Str_8::FromNum(id) + ".eho", Mode::WRITE, Disposition::CREATE_PERSISTENT); + file.SeekBeginning(); file.WriteSerializer_64(data); } diff --git a/src/db/DbTable.cpp b/src/db/DbTable.cpp index dbf6b09..0db8f80 100644 --- a/src/db/DbTable.cpp +++ b/src/db/DbTable.cpp @@ -90,47 +90,17 @@ namespace ehs return HasVariable(id.Hash_64()); } - bool DbTable::CreateVariable(Str_8 id, DbType type, const Byte* const defaultValue) + bool DbTable::CreateVariable(DbVarTmpl var) { - if (HasVariable(id)) + if (HasVariable(var.GetHashId())) return false; - varTmpls.Push(DbVarTmpl((Str_8&&)id, type, defaultValue)); + varTmpls.Push((DbVarTmpl&&)var); - DbVarTmpl* var = &varTmpls[varTmpls.End()]; + DbVarTmpl* result = &varTmpls[varTmpls.End()]; for (UInt_64 i = 0; i < objects.Size(); ++i) - objects[i].CreateVariable(var); - - return true; - } - - bool DbTable::CreateVariable(Str_8 id, const DbType type, const UInt_64 size, const Byte* const defaultValue) - { - if (HasVariable(id)) - return false; - - varTmpls.Push(DbVarTmpl((Str_8&&)id, type, size, defaultValue)); - - DbVarTmpl* var = &varTmpls[varTmpls.End()]; - - for (UInt_64 i = 0; i < objects.Size(); ++i) - objects[i].CreateVariable(var); - - 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); + objects[i].CreateVariable(result); return true; } diff --git a/src/db/DbVar.cpp b/src/db/DbVar.cpp index f2843c8..92387da 100644 --- a/src/db/DbVar.cpp +++ b/src/db/DbVar.cpp @@ -1,42 +1,37 @@ #include "ehs/db/DbVar.h" #include "ehs/Util.h" -#include "ehs/db/DbObject.h" -#include "ehs/db/DbTable.h" #include "ehs/db/DbVarTmpl.h" +#include "ehs/db/DbObject.h" namespace ehs { DbVar::~DbVar() { - delete[] data; + delete[] value; } DbVar::DbVar() - : hashId(0), parent(nullptr), master(nullptr), size(0), data(nullptr) + : hashId(0), parent(nullptr), value(nullptr), size(0) { } - DbVar::DbVar(const UInt_64 hashId, DbVarTmpl *master) - : hashId(hashId), parent(nullptr), master(master), size(master->GetSize()), data(new Byte[DbTypeToSize(master->GetType()) * size]) + DbVar::DbVar(const UInt_64 hashId, const DbVarTmpl * const master) + : hashId(hashId), parent(nullptr), value(new Byte[master->GetSize()]), size(master->GetSize()) { - master->slaves.Push(this); - Util::Copy(data, master->GetDefault(), DbTypeToSize(master->GetType()) * size); + Util::Copy(value, &(*master)[0], size); } DbVar::DbVar(DbVar &&var) noexcept - : hashId(var.hashId), parent(var.parent), master(var.master), size(var.size), data(var.data) + : hashId(var.hashId), parent(var.parent), value(var.value), size(var.size) { - master->UpdateSlave(&var, this); - var.hashId = 0; var.parent = nullptr; - var.master = nullptr; + var.value = nullptr; var.size = 0; - var.data = nullptr; } DbVar::DbVar(const DbVar &var) - : hashId(0), parent(nullptr), master(nullptr), size(0), data(nullptr) + : hashId(0), parent(nullptr), value(nullptr), size(0) { } @@ -45,20 +40,16 @@ namespace ehs if (this == &var) return *this; - delete[] data; + delete[] value; hashId = var.hashId; parent = var.parent; - master = var.master; + value = var.value; size = var.size; - data = var.data; - - master->UpdateSlave(&var, this); var.hashId = 0; - var.master = nullptr; + var.value = nullptr; var.size = 0; - var.data = nullptr; return *this; } @@ -68,20 +59,19 @@ namespace ehs if (this == &var) return *this; - delete[] data; + delete[] value; hashId = 0; parent = nullptr; - master = nullptr; + value = nullptr; size = 0; - data = nullptr; return *this; } DbVar::operator Byte *() const { - return data; + return value; } UInt_64 DbVar::GetHashId() const @@ -94,54 +84,19 @@ namespace ehs return size; } - void DbVar::SetData(const UInt_64 newSize, const Byte* const newData) - { - if (!master->IsArray()) - return; - - size = newSize; - - const UInt_64 byteSize = DbTypeToSize(master->GetType()) * newSize; - - data = new Byte[byteSize]; - - Util::Copy(data, newData, byteSize); - } - - void DbVar::SetData(const Byte* newData) - { - size = 1; - - const UInt_64 byteSize = DbTypeToSize(master->GetType()); - - data = new Byte[byteSize]; - - Util::Copy(data, newData, byteSize); - } - - Byte* DbVar::GetData() const - { - return data; - } - void DbVar::Serialize(Serializer &data) const { data.Write(hashId); - data.Write(size); - data.WriteArray(this->data, DbTypeToSize(master->GetType()) * size); + data.WriteArray(value, size); } void DbVar::Deserialize(Serializer &data) { hashId = data.Read(); - master = parent->parent->GetVariableTemplate(hashId); - - size = data.Read(); - - UInt_64 byteSize = 0; - data.ReadArray(this->data, &byteSize); - this->data = new Byte[byteSize]; - data.ReadArray(this->data, &byteSize); + size = 0; + data.ReadArray(value, &size); + value = new Byte[size]; + data.ReadArray(value, &size); } } diff --git a/src/db/DbVarTmpl.cpp b/src/db/DbVarTmpl.cpp index 77b5bbc..31f0bec 100644 --- a/src/db/DbVarTmpl.cpp +++ b/src/db/DbVarTmpl.cpp @@ -9,42 +9,25 @@ namespace ehs } DbVarTmpl::DbVarTmpl() - : hashId(0), type(DbType::SINT_64), array(false), size(0), def(nullptr) + : hashId(0), size(0), def(nullptr) { } - DbVarTmpl::DbVarTmpl(Str_8 id, const DbType type, const UInt_64 size, const Byte* const def) - : hashId(id.Hash_64()), id((Str_8&&)id), type(type), array(true), size(size), - def(new Byte[DbTypeToSize(type) * size]) - { - Util::Copy(this->def, def, DbTypeToSize(type) * size); - } - - DbVarTmpl::DbVarTmpl(Str_8 id, const DbType type, const Byte* const def) - : hashId(id.Hash_64()), id((Str_8&&)id), type(type), array(false), size(1), def(new Byte[DbTypeToSize(type)]) - { - 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(Str_8 id) + : hashId(id.Hash_64()), id((Str_8&&)id), def(nullptr), size(0) { } 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) + : hashId(varTmpl.hashId), id((Str_8&&)varTmpl.id), def(varTmpl.def), size(varTmpl.size) { varTmpl.hashId = 0; - varTmpl.type = DbType::SINT_64; - varTmpl.array = false; - varTmpl.size = 0; varTmpl.def = nullptr; + varTmpl.size = 0; } DbVarTmpl::DbVarTmpl(const DbVarTmpl &varTmpl) - : hashId(varTmpl.hashId), id(varTmpl.id), type(varTmpl.type), array(varTmpl.array), size(varTmpl.size), - def(varTmpl.def) + : hashId(varTmpl.hashId), id(varTmpl.id), def(varTmpl.def), size(varTmpl.size) { } @@ -57,18 +40,10 @@ namespace ehs hashId = varTmpl.hashId; id = (Str_8&&)varTmpl.id; - type = varTmpl.type; - array = varTmpl.array; size = varTmpl.size; def = varTmpl.def; - slaves = (Array&&)varTmpl.slaves; - - for (UInt_64 i = 0; i < slaves.Size(); ++i) - slaves[i]->master = this; varTmpl.hashId = 0; - varTmpl.type = DbType::SINT_64; - varTmpl.array = false; varTmpl.size = 0; varTmpl.def = nullptr; @@ -84,20 +59,19 @@ namespace ehs hashId = varTmpl.hashId; id = varTmpl.id; - type = varTmpl.type; - array = varTmpl.array; size = varTmpl.size; - slaves = varTmpl.slaves; + def = new Byte[varTmpl.size]; - const UInt_64 byteSize = DbTypeToSize(varTmpl.type) * varTmpl.size; - - def = new Byte[byteSize]; - - Util::Copy(def, varTmpl.def, byteSize); + Util::Copy(def, varTmpl.def, varTmpl.size); return *this; } + DbVarTmpl::operator Byte *() const + { + return def; + } + UInt_64 DbVarTmpl::GetHashId() const { return hashId; @@ -114,63 +88,25 @@ namespace ehs return id; } - void DbVarTmpl::SetType(const DbType newType) - { - type = newType; - } - - DbType DbVarTmpl::GetType() const - { - return type; - } - - void DbVarTmpl::SetIsArray(const bool value) - { - array = value; - } - - bool DbVarTmpl::IsArray() const - { - return array; - } - UInt_64 DbVarTmpl::GetSize() const { return size; } - Byte* DbVarTmpl::GetDefault() const - { - return def; - } - - void DbVarTmpl::UpdateSlave(const DbVar* const oldSlave, DbVar* const newSlave) const - { - for (UInt_64 i = 0; i < slaves.Size(); ++i) - if (slaves[i] == oldSlave) - slaves[i] = newSlave; - } - void DbVarTmpl::Serialize(Serializer &data) const { data.WriteStr(id); - data.Write(type); - data.Write(array); - data.Write(size); - data.WriteArray(def, DbTypeToSize(type) * size); + data.WriteArray(def, size); } void DbVarTmpl::Deserialize(Serializer &data) { id = data.ReadStr(); hashId = id.Hash_64(); - type = data.Read(); - array = data.Read(); - size = data.Read(); - UInt_64 byteSize = 0; - data.ReadArray(def, &byteSize); - def = new Byte[byteSize]; - data.ReadArray(def, &byteSize); + size = 0; + data.ReadArray(def, &size); + def = new Byte[size]; + data.ReadArray(def, &size); } }