Simplified the database.

This commit is contained in:
Arron David Nelson 2024-04-10 18:26:20 -07:00
parent 72347ea9a2
commit 3196c5021e
9 changed files with 131 additions and 279 deletions

View File

@ -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

View File

@ -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();

View File

@ -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;
}
}
}

View File

@ -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<typename T>
void SetValueArray(const T* const newValue, const UInt_64 newSize)
{
size = sizeof(T) * newSize;
value = new Byte[size];
Util::Copy(value, newValue, size);
}
template<typename T = Char_8>
void SetValueStr(const T * const newValue)
{
size = sizeof(T) * Str<Char_8, UInt_64>::Len(newValue);
value = new Byte[size];
Util::Copy(value, newValue, size);
}
template<typename T = Char_8, typename I = UInt_64>
void SetValueStr(const Str<T, I>& newValue)
{
size = newValue.Size(true);
value = new Byte[size];
Util::Copy(value, &newValue[0], size);
}
template<typename T>
void SetValue(const Byte* newValue)
{
size = sizeof(T);
value = new Byte[size];
Util::Copy(value, newValue, size);
}
template<typename T>
T* GetValueArray() const
{
return (T*)value;
}
template<typename T = Char_8, typename I = UInt_64>
Str<T, I> GetValueStr() const
{
return {(T*)value, size / sizeof(T)};
}
template<typename T>
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<UInt_64> &data) const;

View File

@ -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<DbVar*> slaves;
UInt_64 size;
public:
~DbVarTmpl();
DbVarTmpl();
DbVarTmpl(Str_8 id, DbType type, UInt_64 size, const Byte* def);
template<typename T>
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<typename T>
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<typename T>
T* GetDefaultArray() const
{
return (T*)def;
}
DbType GetType() const;
void SetIsArray(bool value);
bool IsArray() const;
template<typename T>
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<UInt_64> &data) const;
void Deserialize(Serializer<UInt_64> &data);

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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<UInt_64> &data) const
{
data.Write(hashId);
data.Write(size);
data.WriteArray(this->data, DbTypeToSize(master->GetType()) * size);
data.WriteArray(value, size);
}
void DbVar::Deserialize(Serializer<UInt_64> &data)
{
hashId = data.Read<UInt_64>();
master = parent->parent->GetVariableTemplate(hashId);
size = data.Read<UInt_64>();
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);
}
}

View File

@ -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<DbVar*>&&)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<UInt_64> &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<UInt_64> &data)
{
id = data.ReadStr<Char_8, UInt_64>();
hashId = id.Hash_64();
type = data.Read<DbType>();
array = data.Read<bool>();
size = data.Read<UInt_64>();
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);
}
}