Same for schemaEngine

This commit is contained in:
Adrien Bouvais 2025-01-11 17:58:52 +01:00
parent d5f7309869
commit 90edb94f7a

View File

@ -84,12 +84,13 @@ pub const SchemaStruct = struct {
/// Manage everything that is relate to the schema /// Manage everything that is relate to the schema
/// This include keeping in memory the schema and schema file, and some functions to get like all members of a specific struct. /// This include keeping in memory the schema and schema file, and some functions to get like all members of a specific struct.
/// For now it is a bit empty. But this is where I will manage migration /// For now it is a bit empty. But this is where I will manage migration
pub const SchemaEngine = struct { pub const SchemaEngine = @This();
struct_array: []SchemaStruct,
null_terminated: [:0]u8,
// The path is the path to the schema file struct_array: []SchemaStruct,
pub fn init(path: []const u8, file_engine: *FileEngine) ZipponError!SchemaEngine { null_terminated: [:0]u8,
// The path is the path to the schema file
pub fn init(path: []const u8, file_engine: *FileEngine) ZipponError!SchemaEngine {
arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
allocator = arena.allocator(); allocator = arena.allocator();
@ -118,32 +119,32 @@ pub const SchemaEngine = struct {
.struct_array = struct_array.toOwnedSlice() catch return ZipponError.MemoryError, .struct_array = struct_array.toOwnedSlice() catch return ZipponError.MemoryError,
.null_terminated = null_terminated, .null_terminated = null_terminated,
}; };
} }
pub fn deinit(_: SchemaEngine) void { pub fn deinit(_: SchemaEngine) void {
arena.deinit(); arena.deinit();
} }
/// Get the type of the member /// Get the type of the member
pub fn memberName2DataType(self: *SchemaEngine, struct_name: []const u8, member_name: []const u8) ZipponError!DataType { pub fn memberName2DataType(self: *SchemaEngine, struct_name: []const u8, member_name: []const u8) ZipponError!DataType {
for (try self.structName2structMembers(struct_name), 0..) |mn, i| { for (try self.structName2structMembers(struct_name), 0..) |mn, i| {
const dtypes = try self.structName2DataType(struct_name); const dtypes = try self.structName2DataType(struct_name);
if (std.mem.eql(u8, mn, member_name)) return dtypes[i]; if (std.mem.eql(u8, mn, member_name)) return dtypes[i];
} }
return ZipponError.MemberNotFound; return ZipponError.MemberNotFound;
} }
pub fn memberName2DataIndex(self: *SchemaEngine, struct_name: []const u8, member_name: []const u8) ZipponError!usize { pub fn memberName2DataIndex(self: *SchemaEngine, struct_name: []const u8, member_name: []const u8) ZipponError!usize {
for (try self.structName2structMembers(struct_name), 0..) |mn, i| { for (try self.structName2structMembers(struct_name), 0..) |mn, i| {
if (std.mem.eql(u8, mn, member_name)) return i; if (std.mem.eql(u8, mn, member_name)) return i;
} }
return ZipponError.MemberNotFound; return ZipponError.MemberNotFound;
} }
/// Get the list of all member name for a struct name /// Get the list of all member name for a struct name
pub fn structName2structMembers(self: SchemaEngine, struct_name: []const u8) ZipponError![][]const u8 { pub fn structName2structMembers(self: SchemaEngine, struct_name: []const u8) ZipponError![][]const u8 {
var i: usize = 0; var i: usize = 0;
while (i < self.struct_array.len) : (i += 1) if (std.mem.eql(u8, self.struct_array[i].name, struct_name)) break; while (i < self.struct_array.len) : (i += 1) if (std.mem.eql(u8, self.struct_array[i].name, struct_name)) break;
@ -153,9 +154,9 @@ pub const SchemaEngine = struct {
} }
return self.struct_array[i].members; return self.struct_array[i].members;
} }
pub fn structName2SchemaStruct(self: SchemaEngine, struct_name: []const u8) ZipponError!SchemaStruct { pub fn structName2SchemaStruct(self: SchemaEngine, struct_name: []const u8) ZipponError!SchemaStruct {
var i: usize = 0; var i: usize = 0;
while (i < self.struct_array.len) : (i += 1) if (std.mem.eql(u8, self.struct_array[i].name, struct_name)) break; while (i < self.struct_array.len) : (i += 1) if (std.mem.eql(u8, self.struct_array[i].name, struct_name)) break;
@ -165,9 +166,9 @@ pub const SchemaEngine = struct {
} }
return self.struct_array[i]; return self.struct_array[i];
} }
pub fn structName2DataType(self: SchemaEngine, struct_name: []const u8) ZipponError![]const DataType { pub fn structName2DataType(self: SchemaEngine, struct_name: []const u8) ZipponError![]const DataType {
var i: u16 = 0; var i: u16 = 0;
while (i < self.struct_array.len) : (i += 1) { while (i < self.struct_array.len) : (i += 1) {
@ -179,39 +180,39 @@ pub const SchemaEngine = struct {
} }
return self.struct_array[i].types; return self.struct_array[i].types;
} }
/// Chech if the name of a struct is in the current schema /// Chech if the name of a struct is in the current schema
pub fn isStructNameExists(self: SchemaEngine, struct_name: []const u8) bool { pub fn isStructNameExists(self: SchemaEngine, struct_name: []const u8) bool {
var i: u16 = 0; var i: u16 = 0;
while (i < self.struct_array.len) : (i += 1) if (std.mem.eql(u8, self.struct_array[i].name, struct_name)) return true; while (i < self.struct_array.len) : (i += 1) if (std.mem.eql(u8, self.struct_array[i].name, struct_name)) return true;
return false; return false;
} }
/// Check if a struct have the member name /// Check if a struct have the member name
pub fn isMemberNameInStruct(self: SchemaEngine, struct_name: []const u8, member_name: []const u8) ZipponError!bool { pub fn isMemberNameInStruct(self: SchemaEngine, struct_name: []const u8, member_name: []const u8) ZipponError!bool {
for (try self.structName2structMembers(struct_name)) |mn| { for (try self.structName2structMembers(struct_name)) |mn| {
if (std.mem.eql(u8, mn, member_name)) return true; if (std.mem.eql(u8, mn, member_name)) return true;
} }
return false; return false;
} }
/// Return the SchemaStruct of the struct that the member is linked. So if it is not a link, it is itself, if it is a link, it the the sstruct of the link /// Return the SchemaStruct of the struct that the member is linked. So if it is not a link, it is itself, if it is a link, it the the sstruct of the link
pub fn linkedStructName(self: SchemaEngine, struct_name: []const u8, member_name: []const u8) ZipponError!SchemaStruct { pub fn linkedStructName(self: SchemaEngine, struct_name: []const u8, member_name: []const u8) ZipponError!SchemaStruct {
const sstruct = try self.structName2SchemaStruct(struct_name); const sstruct = try self.structName2SchemaStruct(struct_name);
if (sstruct.links.get(member_name)) |struct_link_name| { if (sstruct.links.get(member_name)) |struct_link_name| {
return try self.structName2SchemaStruct(struct_link_name); return try self.structName2SchemaStruct(struct_link_name);
} }
return sstruct; return sstruct;
} }
// Return true if the map have all the member name as key and not more // Return true if the map have all the member name as key and not more
pub fn checkIfAllMemberInMap( pub fn checkIfAllMemberInMap(
self: SchemaEngine, self: SchemaEngine,
struct_name: []const u8, struct_name: []const u8,
map: *std.StringHashMap(ConditionValue), map: *std.StringHashMap(ConditionValue),
error_message_buffer: *std.ArrayList(u8), error_message_buffer: *std.ArrayList(u8),
) ZipponError!bool { ) ZipponError!bool {
const all_struct_member = try self.structName2structMembers(struct_name); const all_struct_member = try self.structName2structMembers(struct_name);
var count: u16 = 0; var count: u16 = 0;
@ -223,20 +224,20 @@ pub const SchemaEngine = struct {
} }
return ((count == all_struct_member.len - 1) and (count == map.count())); return ((count == all_struct_member.len - 1) and (count == map.count()));
} }
pub fn isUUIDExist(self: SchemaEngine, struct_name: []const u8, uuid: UUID) bool { pub fn isUUIDExist(self: SchemaEngine, struct_name: []const u8, uuid: UUID) bool {
const sstruct = self.structName2SchemaStruct(struct_name) catch return false; const sstruct = self.structName2SchemaStruct(struct_name) catch return false;
return sstruct.uuid_file_index.contains(uuid); return sstruct.uuid_file_index.contains(uuid);
} }
/// Create an array of empty RelationMap based on the additionalData /// Create an array of empty RelationMap based on the additionalData
pub fn relationMapArrayInit( pub fn relationMapArrayInit(
self: SchemaEngine, self: SchemaEngine,
alloc: Allocator, alloc: Allocator,
struct_name: []const u8, struct_name: []const u8,
additional_data: AdditionalData, additional_data: AdditionalData,
) ZipponError![]RelationMap { ) ZipponError![]RelationMap {
// So here I should have relationship if children are relations // So here I should have relationship if children are relations
var array = std.ArrayList(RelationMap).init(alloc); var array = std.ArrayList(RelationMap).init(alloc);
const sstruct = try self.structName2SchemaStruct(struct_name); const sstruct = try self.structName2SchemaStruct(struct_name);
@ -251,14 +252,14 @@ pub const SchemaEngine = struct {
}) catch return ZipponError.MemoryError; }) catch return ZipponError.MemoryError;
}; };
return array.toOwnedSlice() catch return ZipponError.MemoryError; return array.toOwnedSlice() catch return ZipponError.MemoryError;
} }
pub fn fileListToParse( pub fn fileListToParse(
self: SchemaEngine, self: SchemaEngine,
alloc: Allocator, alloc: Allocator,
struct_name: []const u8, struct_name: []const u8,
map: std.AutoHashMap([16]u8, JsonString), map: std.AutoHashMap([16]u8, JsonString),
) ZipponError![]usize { ) ZipponError![]usize {
const sstruct = try self.structName2SchemaStruct(struct_name); const sstruct = try self.structName2SchemaStruct(struct_name);
var unique_indices = std.AutoHashMap(usize, void).init(alloc); var unique_indices = std.AutoHashMap(usize, void).init(alloc);
defer unique_indices.deinit(); defer unique_indices.deinit();
@ -279,5 +280,4 @@ pub const SchemaEngine = struct {
} }
return result; return result;
} }
};