diff --git a/lib/config.zig b/lib/config.zig index 9ead0e9..186aca3 100644 --- a/lib/config.zig +++ b/lib/config.zig @@ -63,7 +63,7 @@ pub const HELP_MESSAGE = struct { pub const no_schema: []const u8 = \\A database was found here `{s}` but no schema find inside. \\To start a database, you need to attach it a schema using a schema file. - \\By using 'schema init path/to/schema'. For more informations on how to create a schema: TODO add link + \\By using 'schema use path/to/schema'. For more informations on how to create a schema: TODO add link \\ \\You can also set the environment variable ZIPPONDB_SCHEMA to the path to a schema file. \\ diff --git a/lib/config_benchmark.zig b/lib/config_benchmark.zig index 1d512ec..764bbda 100644 --- a/lib/config_benchmark.zig +++ b/lib/config_benchmark.zig @@ -1,4 +1,4 @@ -pub const BUFFER_SIZE = 1024 * 10; // Used a bit everywhere. The size for the schema for example. 10kB +pub const BUFFER_SIZE = 1024 * 1024; // Used a bit everywhere. The size for the schema for example. 10kB pub const MAX_FILE_SIZE = 1024 * 1024; // 1MB pub const CPU_CORE = 16; diff --git a/schema/simple b/schema/simple new file mode 100644 index 0000000..e91dd91 --- /dev/null +++ b/schema/simple @@ -0,0 +1 @@ +User (name:str,) diff --git a/src/cli/parser.zig b/src/cli/parser.zig index 6f587b0..39df90e 100644 --- a/src/cli/parser.zig +++ b/src/cli/parser.zig @@ -165,8 +165,16 @@ pub fn parse(self: *Self, null_term_line_str: [:0]const u8) !bool { .expect_schema_command => switch (token.tag) { .keyword_describe => { - if (self.state == .MissingFileEngine) send("Error: No database selected. Please use 'db new' or 'db use'.", .{}); - if (self.state == .MissingSchemaEngine) send("Error: No schema in database. Please use 'schema init'.", .{}); + if (self.state == .MissingFileEngine) { + send("{s}", .{config.HELP_MESSAGE.no_engine}); + state = .end; + continue; + } + if (self.state == .MissingSchemaEngine) { + send(config.HELP_MESSAGE.no_schema, .{self.file_engine.path_to_ZipponDB_dir}); + state = .end; + continue; + } send("Schema:\n {s}", .{self.schema_engine.null_terminated}); state = .end; }, diff --git a/src/file/read.zig b/src/file/read.zig index d62dfe9..f101447 100644 --- a/src/file/read.zig +++ b/src/file/read.zig @@ -3,7 +3,6 @@ const config = @import("config"); const utils = @import("../utils.zig"); const zid = @import("ZipponData"); const Allocator = std.mem.Allocator; -const Self = @import("core.zig").Self; const ZipponError = @import("error").ZipponError; const SchemaStruct = @import("../schema/struct.zig"); @@ -22,6 +21,8 @@ const DateTime = dtype.DateTime; const DataType = dtype.DataType; const log = std.log.scoped(.fileEngine); +const Self = @import("core.zig").Self; + var path_buffer: [1024]u8 = undefined; /// Use a struct name to populate a list with all UUID of this struct diff --git a/src/parser/core.zig b/src/parser/core.zig index 1a7c770..d23f651 100644 --- a/src/parser/core.zig +++ b/src/parser/core.zig @@ -235,9 +235,14 @@ pub fn parse(self: *Self, buffer: [:0]const u8) ZipponError!void { token.loc.end, ); + const sstruct = try self.schema_engine.structName2SchemaStruct(struct_name); + var members = std.ArrayList([]const u8).init(allocator); + defer members.deinit(); + members.appendSlice(sstruct.members[1..]) catch return ZipponError.MemoryError; + var data_map = std.StringHashMap(ConditionValue).init(allocator); defer data_map.deinit(); - try self.parseNewData(allocator, &data_map, struct_name, null, null); + try self.parseNewData(allocator, &data_map, struct_name, &members); var buff = std.ArrayList(u8).init(allocator); defer buff.deinit(); @@ -256,9 +261,14 @@ pub fn parse(self: *Self, buffer: [:0]const u8) ZipponError!void { token.loc.end, ); + const sstruct = try self.schema_engine.structName2SchemaStruct(struct_name); + var members = std.ArrayList([]const u8).init(allocator); + defer members.deinit(); + members.appendSlice(sstruct.members[1..]) catch return ZipponError.MemoryError; + var data_map = std.StringHashMap(ConditionValue).init(allocator); defer data_map.deinit(); - try self.parseNewData(allocator, &data_map, struct_name, null, null); + try self.parseNewData(allocator, &data_map, struct_name, &members); var buff = std.ArrayList(u8).init(allocator); defer buff.deinit(); @@ -320,9 +330,10 @@ pub fn parse(self: *Self, buffer: [:0]const u8) ZipponError!void { }, .parse_new_data_and_add_data => { + const sstruct = try self.schema_engine.structName2SchemaStruct(struct_name); var order = std.ArrayList([]const u8).init(allocator); defer order.deinit(); - var ordered = false; + order.appendSlice(sstruct.members[1..]) catch return ZipponError.MemoryError; var buff = std.ArrayList(u8).init(allocator); defer buff.deinit(); @@ -340,8 +351,7 @@ pub fn parse(self: *Self, buffer: [:0]const u8) ZipponError!void { while (true) { // I could multithread that as it do take a long time for big benchmark data_map.clearRetainingCapacity(); - try self.parseNewData(local_allocator, &data_map, struct_name, &order, ordered); - ordered = true; + try self.parseNewData(local_allocator, &data_map, struct_name, &order); var error_message_buffer = std.ArrayList(u8).init(local_allocator); defer error_message_buffer.deinit(); diff --git a/src/parser/parts/newData.zig b/src/parser/parts/newData.zig index 9d5b822..71d2961 100644 --- a/src/parser/parts/newData.zig +++ b/src/parser/parts/newData.zig @@ -16,11 +16,11 @@ pub fn parseNewData( allocator: Allocator, map: *std.StringHashMap(ConditionValue), struct_name: []const u8, - order: ?*std.ArrayList([]const u8), - order_full: ?bool, + order: *std.ArrayList([]const u8), ) !void { var token = self.toker.next(); var keep_next = false; + var reordering: bool = false; var member_name: []const u8 = undefined; var state: Self.State = .expect_member_OR_value; var i: usize = 0; @@ -32,6 +32,10 @@ pub fn parseNewData( }) switch (state) { .expect_member_OR_value => switch (token.tag) { .identifier => { + if (!reordering) { + order.*.clearRetainingCapacity(); + reordering = true; + } member_name = self.toker.getTokenSlice(token); if (!(self.schema_engine.isMemberNameInStruct(struct_name, member_name) catch { return ZipponError.StructNotFound; @@ -42,7 +46,7 @@ pub fn parseNewData( token.loc.start, token.loc.end, ); - if (order_full) |o| if (!o) order.?.*.append(allocator.dupe(u8, member_name) catch return ZipponError.MemoryError) catch return ZipponError.MemoryError; + order.*.append(allocator.dupe(u8, member_name) catch return ZipponError.MemoryError) catch return ZipponError.MemoryError; state = .expect_equal; }, .string_literal, @@ -58,27 +62,11 @@ pub fn parseNewData( .l_brace, .keyword_none, .keyword_now, - => if (order_full) |o| { - if (!o) return printError( - "Expected member name.", - ZipponError.MemberMissing, - self.toker.buffer, - token.loc.start, - token.loc.end, - ); - - member_name = order.?.items[i]; + => { + member_name = order.items[i]; i += 1; keep_next = true; state = .expect_new_value; - } else { - return printError( - "Expected member name.", - ZipponError.MemberMissing, - self.toker.buffer, - token.loc.start, - token.loc.end, - ); }, else => return printError( "Error: Expected member name.", diff --git a/src/schema/utils.zig b/src/schema/utils.zig index 2c0b5ca..89b70b0 100644 --- a/src/schema/utils.zig +++ b/src/schema/utils.zig @@ -12,6 +12,10 @@ const JsonString = RelationMap.JsonString; const ZipponError = @import("error").ZipponError; +// I need to redo how SchemaStruct work because it is a mess +// I mean I use wayyyyyyyyyyyyyyyyyyyyyyy too much structName2SchemaStruct or stuff like that +// I mean that not good to always for loop and compare when a map would work + pub fn memberName2DataType(self: *Self, struct_name: []const u8, member_name: []const u8) ZipponError!DataType { for (try self.structName2structMembers(struct_name), 0..) |mn, i| { const dtypes = try self.structName2DataType(struct_name); @@ -42,6 +46,7 @@ pub fn structName2structMembers(self: Self, struct_name: []const u8) ZipponError return self.struct_array[i].members; } +// TODO: This is the first one I want to change to use a map pub fn structName2SchemaStruct(self: Self, struct_name: []const u8) ZipponError!SchemaStruct { var i: usize = 0;