Simplified the database.

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

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