diff options
| author | Marcos Magueta <maguetamarcos@gmail.com> | 2024-12-08 13:30:48 -0300 |
|---|---|---|
| committer | Marcos Magueta <maguetamarcos@gmail.com> | 2024-12-08 13:30:48 -0300 |
| commit | 75f1ff70f92b9b7e7c72580ce210b5c7622918bf (patch) | |
| tree | 731c2538bcff35a47501a3a4350e622e3ed401dc /lib/schema.ml | |
| parent | f43943346e3c6d347b829bbbaeccae5f49327749 (diff) | |
| download | Sakura-75f1ff70f92b9b7e7c72580ce210b5c7622918bf.tar.gz Sakura-75f1ff70f92b9b7e7c72580ce210b5c7622918bf.zip | |
schema as a meta relation
Diffstat (limited to 'lib/schema.ml')
| -rw-r--r-- | lib/schema.ml | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/schema.ml b/lib/schema.ml new file mode 100644 index 0000000..74ce720 --- /dev/null +++ b/lib/schema.ml @@ -0,0 +1,55 @@ +module Protocol = struct + open Data_encoding + type attribute = + { name: string; + attr_type: string } + type relation = + { name: string; + attributes: attribute list } + let attribute_encoding = + conv + (fun {name; attr_type} -> (name, attr_type)) + (fun (name, attr_type) -> {name; attr_type}) + Data_encoding.(tup2 string string) + + let parse_attribute ~data = + let contract_encoding = Data_encoding.(tup2 string string) in + match Binary.of_bytes_opt contract_encoding data with + | Some (name, attr_type) -> Ok {name; attr_type} + | None -> Error "Failed to parse attribute" + + let relation_encoding = + conv + (fun {name; attributes} -> (name, attributes)) + (fun (name, attributes) -> {name; attributes}) + Data_encoding.(tup2 string (list attribute_encoding)) + + let parse_relation ~data = + let contract_encoding = Data_encoding.(tup2 string (list attribute_encoding)) in + match Binary.of_bytes_opt contract_encoding data with + | Some (name, attributes) -> Ok {name; attributes} + | None -> Error "Failed to parse relation" +end + +let test_schema_write_and_retrieve() = + let open Disk in + let open Data_encoding in + let stream: Executor.history = [{state = Executor._EMPTY_SHA_HASH_; files = Executor.StringMap.empty}] in + let locations: Executor.locations = Executor.StringMap.empty in + let open Extensions.Result in + let+ user_relation = + let relation: Protocol.relation = { name = "user"; attributes = [{name = "first-name"; attr_type = "string"}; {name = "last-name"; attr_type = "string"}] } in + Result.map_error (fun _ -> "Failed to serialize relation to binary format.") + @@ Binary.to_bytes Protocol.relation_encoding relation + in + let command_write: Command.t = + {kind = Command.WRITE; timestamp = 10.0; hash = ""; content = Bytes.to_string user_relation; filename = "user"; references = []} in + let+ ((stream, locations), Command.ComputedHash handle) = Command.commit_and_perform stream locations command_write in + let command_read: Command.t = + {kind = Command.READ; timestamp = 10.0; hash = handle; content = ""; filename = "user"; references = []} in + let+ ((stream, locations), Command.Read content) = Command.commit_and_perform stream locations command_read in + let+ user_relation = + Result.map_error (fun _ -> "Failed to deserialize relation from binary format.") + @@ Binary.of_bytes Protocol.relation_encoding (Bytes.concat Bytes.empty content) in + Ok (stream, locations, user_relation) + [@@warning "-8"] (* Suppress pattern match incomplete warnings *) |
