From 9020037c2039d6d04d35f1c98f5ea49e1c49d8a4 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Thu, 1 Feb 2024 09:40:30 -0800 Subject: [PATCH] remove ddb_types --- src/batchwriteitem.zig | 8 +++--- src/createtable.zig | 11 ++++---- src/ddb.zig | 61 ++++++++++++++++++++++++++++++++++++------ src/ddb_types.zig | 40 --------------------------- 4 files changed, 62 insertions(+), 58 deletions(-) delete mode 100644 src/ddb_types.zig diff --git a/src/batchwriteitem.zig b/src/batchwriteitem.zig index a45e854..cdd10e1 100644 --- a/src/batchwriteitem.zig +++ b/src/batchwriteitem.zig @@ -3,7 +3,7 @@ const sqlite = @import("sqlite"); const AuthenticatedRequest = @import("AuthenticatedRequest.zig"); const Account = @import("Account.zig"); const encryption = @import("encryption.zig"); -const ddb_types = @import("ddb_types.zig"); +const ddb = @import("ddb.zig"); const returnException = @import("main.zig").returnException; // Copied from ddb_type and made inferred. Yuck :( @@ -21,7 +21,7 @@ pub const AttributeTypeName = enum { }; // Cannot use AttributeTypeName enum as it is not inferred -// const AttributeValue = union(ddb_types.AttributeTypeName) { +// const AttributeValue = union(ddb.AttributeTypeName) { const AttributeValue = union(AttributeTypeName) { string: []const u8, number: []const u8, // Floating point stored as string @@ -393,7 +393,7 @@ const Params = struct { const attribute_type = val_val.key_ptr.*; // This should be "S", "N", "NULL", "BOOL", etc const attribute_value = val_val.value_ptr.*; // Convert this to our enum - const attribute_type_enum = std.meta.stringToEnum(ddb_types.AttributeTypeDescriptor, attribute_type); + const attribute_type_enum = std.meta.stringToEnum(ddb.AttributeTypeDescriptor, attribute_type); if (attribute_type_enum == null) try returnException( request, @@ -403,7 +403,7 @@ const Params = struct { "Request in RequestItems found attribute with invalid type", ); // Convert our enum to something that looks better when reading code - const attribute_type_enum_converted = @as(ddb_types.AttributeTypeName, @enumFromInt(@intFromEnum(attribute_type_enum.?))); + const attribute_type_enum_converted = @as(ddb.AttributeTypeName, @enumFromInt(@intFromEnum(attribute_type_enum.?))); // Now we need to get *THIS* enum over to our union, which uses the same values // We'll just use a switch here, because each of these cases must // be handled slightly differently diff --git a/src/createtable.zig b/src/createtable.zig index 91bbd75..17e5c20 100644 --- a/src/createtable.zig +++ b/src/createtable.zig @@ -5,7 +5,6 @@ const Account = @import("Account.zig"); const encryption = @import("encryption.zig"); const returnException = @import("main.zig").returnException; const ddb = @import("ddb.zig"); -const ddb_types = @import("ddb_types.zig"); // These are in the original casing so as to make the error messages nice const RequiredFields = enum(u3) { @@ -18,7 +17,7 @@ const RequiredFields = enum(u3) { const Params = struct { table_name: []const u8, - table_info: ddb_types.TableInfo, + table_info: ddb.TableInfo, read_capacity_units: ?i64 = null, write_capacity_units: ?i64 = null, billing_mode_pay_per_request: bool = false, @@ -337,9 +336,9 @@ fn parseRequest( return request_params; } -fn parseAttributeDefinitions(request: *AuthenticatedRequest, definitions: []std.json.Value, writer: anytype) ![]*ddb_types.AttributeDefinition { +fn parseAttributeDefinitions(request: *AuthenticatedRequest, definitions: []std.json.Value, writer: anytype) ![]*ddb.AttributeDefinition { const allocator = request.allocator; - var rc = try allocator.alloc(*ddb_types.AttributeDefinition, definitions.len); + var rc = try allocator.alloc(*ddb.AttributeDefinition, definitions.len); errdefer allocator.free(rc); // "AttributeDefinitions": [ // { @@ -371,7 +370,7 @@ fn parseAttributeDefinitions(request: *AuthenticatedRequest, definitions: []std. "Attribute definitions array can only consist of objects with AttributeName and AttributeType strings", ); const type_string = attribute_type.?.string; - const type_enum = std.meta.stringToEnum(ddb_types.AttributeTypeDescriptor, type_string); + const type_enum = std.meta.stringToEnum(ddb.AttributeTypeDescriptor, type_string); if (type_enum == null) try returnException( request, @@ -382,7 +381,7 @@ fn parseAttributeDefinitions(request: *AuthenticatedRequest, definitions: []std. ); // TODO: This is kind of a lousy error message // TODO: This can leak memory if a later validation error occurs. // we are de-facto passed an arena here, but we shouldn't assume that - var definition = try allocator.create(ddb_types.AttributeDefinition); + var definition = try allocator.create(ddb.AttributeDefinition); definition.name = try allocator.dupe(u8, name.?.string); definition.type = type_enum.?; rc[i] = definition; diff --git a/src/ddb.zig b/src/ddb.zig index c5731e8..c2a5d7a 100644 --- a/src/ddb.zig +++ b/src/ddb.zig @@ -2,10 +2,55 @@ const std = @import("std"); const sqlite = @import("sqlite"); const AuthenticatedRequest = @import("AuthenticatedRequest.zig"); const Account = @import("Account.zig"); -const ddb_types = @import("ddb_types.zig"); const encryption = @import("encryption.zig"); const builtin = @import("builtin"); +/// Serialized into metadata table. This is an explicit enum with a twin +/// AttributeTypeName enum to make coding with these types easier. Use +/// Descriptor for storage or communication with the outside world, and +/// Name for internal use +pub const AttributeTypeDescriptor = enum(u4) { + S = 0, + N = 1, + B = 2, + BOOL = 3, + NULL = 4, + M = 5, + L = 6, + SS = 7, + NS = 8, + BS = 9, +}; + +pub const AttributeTypeName = enum(u4) { + string = 0, + number = 1, + binary = 2, + boolean = 3, + null = 4, + map = 5, + list = 6, + string_set = 7, + number_set = 8, + binary_set = 9, +}; + +/// Serialized into metadata table +pub const AttributeDefinition = struct { + name: []const u8, + type: AttributeTypeDescriptor, +}; + +/// TableInfo is serialized directly into the underlying metadata table, along +/// with AttributeDefinition structure and types +pub const TableInfo = struct { + attribute_definitions: []*const AttributeDefinition, + // gsi_list: []const u8, // Not sure how this is used + // gsi_description_list: []const u8, // Not sure how this is used + // sqlite_index: []const u8, // Not sure how this is used + table_key: [encryption.encoded_key_length]u8, +}; + pub const TableArray = struct { items: []Table, allocator: std.mem.Allocator, @@ -73,7 +118,7 @@ pub fn tablesForAccount(allocator: std.mem.Allocator, account_id: []const u8) !T ); defer allocator.free(table_info_str); // std.debug.print(" \n===TableInfo: {s}\n===\n", .{table_info_str}); - const table_info = try std.json.parseFromSlice(ddb_types.TableInfo, allocator, table_info_str, .{}); + const table_info = try std.json.parseFromSlice(TableInfo, allocator, table_info_str, .{}); defer table_info.deinit(); // errdefer allocator.free(table_info.table_key); // defer { @@ -101,7 +146,7 @@ pub fn createDdbTable( db: *sqlite.Db, account: Account, table_name: []const u8, - table_info: ddb_types.TableInfo, + table_info: TableInfo, read_capacity_units: i64, write_capacity_units: i64, billing_mode_pay_per_request: bool, @@ -160,7 +205,7 @@ fn insertIntoDatabaseMetadata( db: *sqlite.Db, account: Account, table_name: []const u8, - table_info: ddb_types.TableInfo, + table_info: TableInfo, read_capacity_units: i64, write_capacity_units: i64, billing_mode_pay_per_request: bool, @@ -229,13 +274,13 @@ fn testCreateTable(allocator: std.mem.Allocator, account_id: []const u8) !sqlite var db = try Account.dbForAccount(allocator, account_id); const account = try Account.accountForId(allocator, account_id); // This will get us the encryption key needed defer account.deinit(); - var hash = ddb_types.AttributeDefinition{ .name = "Artist", .type = .S }; - var range = ddb_types.AttributeDefinition{ .name = "SongTitle", .type = .S }; - var definitions = @constCast(&[_]*ddb_types.AttributeDefinition{ + var hash = AttributeDefinition{ .name = "Artist", .type = .S }; + var range = AttributeDefinition{ .name = "SongTitle", .type = .S }; + var definitions = @constCast(&[_]*AttributeDefinition{ &hash, &range, }); - var table_info: ddb_types.TableInfo = .{ + var table_info: TableInfo = .{ .table_key = undefined, .attribute_definitions = definitions[0..], }; diff --git a/src/ddb_types.zig b/src/ddb_types.zig deleted file mode 100644 index 2485444..0000000 --- a/src/ddb_types.zig +++ /dev/null @@ -1,40 +0,0 @@ -const encryption = @import("encryption.zig"); - -pub const AttributeTypeDescriptor = enum(u4) { - S = 0, - N = 1, - B = 2, - BOOL = 3, - NULL = 4, - M = 5, - L = 6, - SS = 7, - NS = 8, - BS = 9, -}; - -pub const AttributeTypeName = enum(u4) { - string = 0, - number = 1, - binary = 2, - boolean = 3, - null = 4, - map = 5, - list = 6, - string_set = 7, - number_set = 8, - binary_set = 9, -}; - -pub const AttributeDefinition = struct { - name: []const u8, - type: AttributeTypeDescriptor, -}; - -pub const TableInfo = struct { - attribute_definitions: []*const AttributeDefinition, - // gsi_list: []const u8, // Not sure how this is used - // gsi_description_list: []const u8, // Not sure how this is used - // sqlite_index: []const u8, // Not sure how this is used - table_key: [encryption.encoded_key_length]u8, -};