Skip to content

Commit aa42450

Browse files
committed
vhd_format: Switch to sparse interval format for blocks_json
Now that qcow-stream-tool output intervals of allocated clusters, do the same for vhd-tool. This greatly decreases the filesize and memory usage in vhd-tool. Signed-off-by: Andrii Sultanov <andriy.sultanov@vates.tech>
1 parent e457736 commit aa42450

2 files changed

Lines changed: 29 additions & 10 deletions

File tree

ocaml/libs/vhd/vhd_format/f.ml

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2911,16 +2911,35 @@ functor
29112911

29122912
let include_block = include_block None t in
29132913

2914-
let blocks =
2914+
let blocks, last_block =
29152915
Seq.init max_table_entries Fun.id
2916-
|> Seq.filter_map (fun i ->
2917-
if include_block i then
2918-
Some (`Int i)
2919-
else
2920-
None
2921-
)
2922-
|> List.of_seq
2916+
|> Seq.fold_left
2917+
(fun (acc, left_block) i ->
2918+
if include_block i then
2919+
match left_block with
2920+
| Some _ ->
2921+
(acc, left_block)
2922+
| None ->
2923+
(acc, Some i)
2924+
else
2925+
match left_block with
2926+
| Some x ->
2927+
(`List [`Int x; `Int (i - 1)] :: acc, None)
2928+
| None ->
2929+
(acc, None)
2930+
)
2931+
([], None)
29232932
in
2933+
(* Close off the interval we were tracking we ran off the end of the seq *)
2934+
let blocks =
2935+
match last_block with
2936+
| Some x ->
2937+
`List [`Int x; `Int (max_table_entries - 1)] :: blocks
2938+
| None ->
2939+
blocks
2940+
in
2941+
let blocks = List.rev blocks in
2942+
29242943
let json =
29252944
`Assoc
29262945
[

ocaml/vhd-tool/cli/main.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,8 @@ let stream_cmd =
387387

388388
let read_headers_cmd =
389389
let doc =
390-
{|Parse VHD headers and output allocated blocks information in JSON format \
391-
like: {"virtual_size": X, "cluster_bits": X, "data_clusters": [1,2,3]}|}
390+
{|Parse VHD headers and output allocated blocks intervals information in JSON format \
391+
like: {"virtual_size": X, "cluster_bits": X, "data_clusters": [[1,13],[17,17],[19,272]]}|}
392392
in
393393
let source =
394394
let doc = Printf.sprintf "Path to the VHD file" in

0 commit comments

Comments
 (0)