diff options
| author | Estevan Castilho <tevo@riajuu.net> | 2026-07-15 01:52:38 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-07-14 22:52:38 -0300 |
| commit | c374635a36d71e8cfff6e1c19b62e5d0e14ea371 (patch) | |
| tree | 597fba69d4b39452eabab6299887282c1d7c6bc6 /lib/concepts/Representation.ml | |
| parent | bb3156b8ab78b035ce886bbe3021ec7cbe269d03 (diff) | |
| download | RNT-c374635a36d71e8cfff6e1c19b62e5d0e14ea371.tar.gz RNT-c374635a36d71e8cfff6e1c19b62e5d0e14ea371.zip | |
Storage layer scaffolding, LMDB backend
Diffstat (limited to 'lib/concepts/Representation.ml')
| -rw-r--r-- | lib/concepts/Representation.ml | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/lib/concepts/Representation.ml b/lib/concepts/Representation.ml index d353a38..856ae11 100644 --- a/lib/concepts/Representation.ml +++ b/lib/concepts/Representation.ml @@ -1,6 +1,34 @@ -type blob = unit +type blob = bytes -let blob_of_value (_ : Value.value) = failwith "TODO" -let value_of_blob (_ : blob) = failwith "TODO" -let blob_of_bytes (_ : bytes) = failwith "TODO" -let bytes_of_blob (_ : blob) = failwith "TODO" +let blob_of_bytes (b : bytes) = b +let bytes_of_blob (b : blob) = b + +(* A blob is a one-byte constructor tag followed by its payload. The + tag makes the encoding self-describing so [value_of_blob] can + recover the constructor: + - String ['\000'] ++ raw utf-8 bytes + - Integer ['\001'] ++ 8-byte big-endian int64 *) +let tag_string = '\000' +let tag_integer = '\001' + +let blob_of_value = function + | Value.String s -> + let b = Bytes.create (1 + String.length s) in + Bytes.set b 0 tag_string; + Bytes.blit_string s 0 b 1 (String.length s); + b + | Value.Integer n -> + let b = Bytes.create 9 in + Bytes.set b 0 tag_integer; + Bytes.set_int64_be b 1 (Int64.of_int n); + b + +let value_of_blob (b : blob) = + let n = Bytes.length b in + if n = 0 then invalid_arg "value_of_blob: empty blob"; + match Bytes.get b 0 with + | c when c = tag_string -> Value.String (Bytes.sub_string b 1 (n - 1)) + | c when c = tag_integer -> + if n <> 9 then invalid_arg "value_of_blob: malformed integer payload"; + Value.Integer (Int64.to_int (Bytes.get_int64_be b 1)) + | c -> invalid_arg (Printf.sprintf "value_of_blob: unknown tag %C" c) |
