SchemaStruct use []const u8

Before it was link that and I changed to use Loc as I throught that I
would save memory.

But a slice is just a pointer and a leng, so it should be better. As
long as I keep the original string in memory. The query and schema
This commit is contained in:
Adrien Bouvais 2024-10-20 17:51:40 +02:00
parent 6b1d3d7495
commit 9d2948d686
3 changed files with 25 additions and 29 deletions

View File

@ -264,12 +264,12 @@ pub const FileEngine = struct {
var data_dir = std.fs.cwd().openDir(path, .{}) catch return FileEngineError.CantOpenDir; var data_dir = std.fs.cwd().openDir(path, .{}) catch return FileEngineError.CantOpenDir;
defer data_dir.close(); defer data_dir.close();
for (self.struct_array.items) |struct_item| { for (self.struct_array.items) |schema_struct| {
data_dir.makeDir(self.locToSlice(struct_item.name)) catch |err| switch (err) { data_dir.makeDir(schema_struct.name) catch |err| switch (err) {
error.PathAlreadyExists => {}, error.PathAlreadyExists => {},
else => return FileEngineError.CantMakeDir, else => return FileEngineError.CantMakeDir,
}; };
const struct_dir = data_dir.openDir(self.locToSlice(struct_item.name), .{}) catch return FileEngineError.CantOpenDir; const struct_dir = data_dir.openDir(schema_struct.name, .{}) catch return FileEngineError.CantOpenDir;
_ = struct_dir.createFile("0.csv", .{}) catch |err| switch (err) { _ = struct_dir.createFile("0.csv", .{}) catch |err| switch (err) {
error.PathAlreadyExists => {}, error.PathAlreadyExists => {},
@ -370,10 +370,10 @@ pub const FileEngine = struct {
for (try self.structName2structMembers(struct_name), try self.structName2DataType(struct_name)) |member_name, member_type| { for (try self.structName2structMembers(struct_name), try self.structName2DataType(struct_name)) |member_name, member_type| {
token = data_toker.next(); token = data_toker.next();
// FIXME: When relationship will be implemented, need to check if the len of NON link is 0 // FIXME: When relationship will be implemented, need to check if the len of NON link is 0
if (!(additional_data.member_to_find.items.len == 0 or additional_data.contains(self.locToSlice(member_name)))) continue; if (!(additional_data.member_to_find.items.len == 0 or additional_data.contains(member_name))) continue;
// write the member name and = sign // write the member name and = sign
out_writer.print("{s}: ", .{self.locToSlice(member_name)}) catch return FileEngineError.WriteError; out_writer.print("{s}: ", .{member_name}) catch return FileEngineError.WriteError;
switch (member_type) { switch (member_type) {
.str => { .str => {
@ -590,8 +590,8 @@ pub const FileEngine = struct {
const uuid = UUID.parse(output_fbs.getWritten()[0..36]) catch return FileEngineError.InvalidUUID; const uuid = UUID.parse(output_fbs.getWritten()[0..36]) catch return FileEngineError.InvalidUUID;
// Skip unwanted token // Skip unwanted token
for (try self.structName2structMembers(condition.struct_name)) |mn| { for (try self.structName2structMembers(condition.struct_name)) |member_name| {
if (std.mem.eql(u8, self.locToSlice(mn), condition.member_name)) break; if (std.mem.eql(u8, member_name, condition.member_name)) break;
_ = data_toker.next(); _ = data_toker.next();
} }
token = data_toker.next(); token = data_toker.next();
@ -716,7 +716,7 @@ pub const FileEngine = struct {
for (try self.structName2structMembers(struct_name)) |member_name| { for (try self.structName2structMembers(struct_name)) |member_name| {
writer.writeByte(CSV_DELIMITER) catch return FileEngineError.WriteError; writer.writeByte(CSV_DELIMITER) catch return FileEngineError.WriteError;
writer.print("{s}", .{data_map.get(self.locToSlice(member_name)).?}) catch return FileEngineError.WriteError; // Change that for csv writer.print("{s}", .{data_map.get(member_name).?}) catch return FileEngineError.WriteError; // Change that for csv
} }
writer.print("\n", .{}) catch return FileEngineError.WriteError; writer.print("\n", .{}) catch return FileEngineError.WriteError;
@ -869,9 +869,9 @@ pub const FileEngine = struct {
new_writer.writeByte(CSV_DELIMITER) catch return FileEngineError.WriteError; new_writer.writeByte(CSV_DELIMITER) catch return FileEngineError.WriteError;
if (new_data_map.contains(self.locToSlice(member_name))) { if (new_data_map.contains(member_name)) {
// Write the new data // Write the new data
new_writer.print("{s}", .{new_data_map.get(self.locToSlice(member_name)).?}) catch return FileEngineError.WriteError; new_writer.print("{s}", .{new_data_map.get(member_name).?}) catch return FileEngineError.WriteError;
} else { } else {
// Write the old data // Write the old data
switch (member_type) { switch (member_type) {
@ -1096,17 +1096,13 @@ pub const FileEngine = struct {
file.writeAll(self.null_terminated_schema_buff) catch return FileEngineError.WriteError; file.writeAll(self.null_terminated_schema_buff) catch return FileEngineError.WriteError;
} }
pub fn locToSlice(self: *FileEngine, loc: Loc) []const u8 {
return self.null_terminated_schema_buff[loc.start..loc.end];
}
/// Get the type of the member /// Get the type of the member
pub fn memberName2DataType(self: *FileEngine, struct_name: []const u8, member_name: []const u8) FileEngineError!DataType { pub fn memberName2DataType(self: *FileEngine, struct_name: []const u8, member_name: []const u8) FileEngineError!DataType {
var i: u16 = 0; var i: u16 = 0;
for (try self.structName2structMembers(struct_name)) |mn| { for (try self.structName2structMembers(struct_name)) |mn| {
const dtypes = try self.structName2DataType(struct_name); const dtypes = try self.structName2DataType(struct_name);
if (std.mem.eql(u8, self.locToSlice(mn), member_name)) return dtypes[i]; if (std.mem.eql(u8, mn, member_name)) return dtypes[i];
i += 1; i += 1;
} }
@ -1114,10 +1110,10 @@ pub const FileEngine = struct {
} }
/// 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: *FileEngine, struct_name: []const u8) FileEngineError![]Loc { pub fn structName2structMembers(self: *FileEngine, struct_name: []const u8) FileEngineError![][]const u8 {
var i: u16 = 0; var i: u16 = 0;
while (i < self.struct_array.items.len) : (i += 1) if (std.mem.eql(u8, self.locToSlice(self.struct_array.items[i].name), struct_name)) break; while (i < self.struct_array.items.len) : (i += 1) if (std.mem.eql(u8, self.struct_array.items[i].name, struct_name)) break;
if (i == self.struct_array.items.len) { if (i == self.struct_array.items.len) {
return FileEngineError.StructNotFound; return FileEngineError.StructNotFound;
@ -1130,10 +1126,10 @@ pub const FileEngine = struct {
var i: u16 = 0; var i: u16 = 0;
while (i < self.struct_array.items.len) : (i += 1) { while (i < self.struct_array.items.len) : (i += 1) {
if (std.mem.eql(u8, self.locToSlice(self.struct_array.items[i].name), struct_name)) break; if (std.mem.eql(u8, self.struct_array.items[i].name, struct_name)) break;
} }
if (i == self.struct_array.items.len and !std.mem.eql(u8, self.locToSlice(self.struct_array.items[i].name), struct_name)) { if (i == self.struct_array.items.len and !std.mem.eql(u8, self.struct_array.items[i].name, struct_name)) {
return FileEngineError.StructNotFound; return FileEngineError.StructNotFound;
} }
@ -1154,14 +1150,14 @@ pub const FileEngine = struct {
/// 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: *FileEngine, struct_name: []const u8) bool { pub fn isStructNameExists(self: *FileEngine, struct_name: []const u8) bool {
var i: u16 = 0; var i: u16 = 0;
while (i < self.struct_array.items.len) : (i += 1) if (std.mem.eql(u8, self.locToSlice(self.struct_array.items[i].name), struct_name)) return true; while (i < self.struct_array.items.len) : (i += 1) if (std.mem.eql(u8, self.struct_array.items[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: *FileEngine, struct_name: []const u8, member_name: []const u8) FileEngineError!bool { pub fn isMemberNameInStruct(self: *FileEngine, struct_name: []const u8, member_name: []const u8) FileEngineError!bool {
for (try self.structName2structMembers(struct_name)) |mn| { // I do not return an error here because I should already check before is the struct exist for (try self.structName2structMembers(struct_name)) |mn| { // I do not return an error here because I should already check before is the struct exist
if (std.mem.eql(u8, self.locToSlice(mn), member_name)) return true; if (std.mem.eql(u8, mn, member_name)) return true;
} }
return false; return false;
} }
@ -1174,7 +1170,7 @@ pub const FileEngine = struct {
const writer = error_message_buffer.writer(); const writer = error_message_buffer.writer();
for (all_struct_member) |mn| { for (all_struct_member) |mn| {
if (map.contains(self.locToSlice(mn))) count += 1 else writer.print(" {s},", .{self.locToSlice(mn)}) catch return FileEngineError.WriteError; // TODO: Handle missing print better if (map.contains(mn)) count += 1 else writer.print(" {s},", .{mn}) catch return FileEngineError.WriteError; // TODO: Handle missing print better
} }
return ((count == all_struct_member.len) and (count == map.count())); return ((count == all_struct_member.len) and (count == map.count()));

View File

@ -35,15 +35,15 @@ pub const Parser = struct {
pub const SchemaStruct = struct { pub const SchemaStruct = struct {
allocator: Allocator, allocator: Allocator,
name: Loc, name: []const u8,
members: std.ArrayList(Loc), members: std.ArrayList([]const u8),
types: std.ArrayList(DataType), types: std.ArrayList(DataType),
pub fn init(allocator: Allocator, name: Loc) SchemaStruct { pub fn init(allocator: Allocator, name: []const u8) SchemaStruct {
return SchemaStruct{ return SchemaStruct{
.allocator = allocator, .allocator = allocator,
.name = name, .name = name,
.members = std.ArrayList(Loc).init(allocator), .members = std.ArrayList([]const u8).init(allocator),
.types = std.ArrayList(DataType).init(allocator), .types = std.ArrayList(DataType).init(allocator),
}; };
} }
@ -77,7 +77,7 @@ pub const Parser = struct {
.expect_struct_name_OR_end => switch (token.tag) { .expect_struct_name_OR_end => switch (token.tag) {
.identifier => { .identifier => {
state = .expect_l_paren; state = .expect_l_paren;
struct_array.append(SchemaStruct.init(self.allocator, token.loc)) catch return SchemaParserError.MemoryError; struct_array.append(SchemaStruct.init(self.allocator, self.toker.getTokenSlice(token))) catch return SchemaParserError.MemoryError;
}, },
.eof => state = .end, .eof => state = .end,
else => return printError( else => return printError(
@ -120,7 +120,7 @@ pub const Parser = struct {
.expect_member_name => { .expect_member_name => {
state = .expect_two_dot; state = .expect_two_dot;
struct_array.items[index].members.append(token.loc) catch return SchemaParserError.MemoryError; struct_array.items[index].members.append(self.toker.getTokenSlice(token)) catch return SchemaParserError.MemoryError;
}, },
.expect_two_dot => switch (token.tag) { .expect_two_dot => switch (token.tag) {

View File

@ -30,7 +30,7 @@ pub const AdditionalData = struct {
// This is name in: [name] // This is name in: [name]
// There is an additional data because it can be [friend [1; name]] // There is an additional data because it can be [friend [1; name]]
pub const AdditionalDataMember = struct { pub const AdditionalDataMember = struct {
name: []const u8, // Use loc name: []const u8,
additional_data: AdditionalData, additional_data: AdditionalData,
pub fn init(allocator: Allocator, name: []const u8) AdditionalDataMember { pub fn init(allocator: Allocator, name: []const u8) AdditionalDataMember {