-module Stream : sig
- type 'a t
-
- val create : (unit -> 'a option) -> 'a t
-
- val of_queue : 'a Queue.t -> 'a t
-
- val iter : 'a t -> f:('a -> unit) -> unit
-
- val map : 'a t -> f:('a -> 'b) -> 'b t
-
- val filter : 'a t -> f:('a -> bool) -> 'a t
-
- val concat : ('a t) list -> 'a t
-
- val group_by : 'a t -> f:('a -> 'b) -> ('b * int * 'a list) t
-end = struct
- module S = Stream
-
- type 'a t =
- {mutable streams : ('a S.t) list}
-
- let create f =
- {streams = [S.from (fun _ -> f ())]}
-
- let of_queue q =
- create (fun () ->
- match Queue.take q with
- | exception Queue.Empty ->
- None
- | x ->
- Some x
- )
-
- let rec next t =
- match t.streams with
- | [] ->
- None
- | s :: streams ->
- (match S.next s with
- | exception Stream.Failure ->
- t.streams <- streams;
- next t
- | x ->
- Some x
- )
-
- let map t ~f =
- create (fun () ->
- match next t with
- | None -> None
- | Some x -> Some (f x)
- )
-
- let filter t ~f =
- let rec filter () =
- match next t with
- | None ->
- None
- | Some x when f x ->
- Some x
- | Some _ ->
- filter ()
- in
- create filter
-
- let iter t ~f =
- List.iter t.streams ~f:(S.iter f)
-
- let concat ts =
- {streams = List.concat (List.map ts ~f:(fun {streams} -> streams))}
-
- let group_by t ~f =
- let groups_tbl = Hashtbl.create 1_000_000 in
- let group_update x =
- let group = f x in
- let members =
- match Hashtbl.find_opt groups_tbl group with
- | None ->
- (1, [x])
- | Some (n, xs) ->
- (succ n, x :: xs)
- in
- Hashtbl.replace groups_tbl group members
- in
- iter t ~f:group_update;
- let groups = Queue.create () in
- Hashtbl.iter
- (fun name (length, members) -> Queue.add (name, length, members) groups)
- groups_tbl;
- of_queue groups
-end
-
-module In_channel : sig
- val lines : in_channel -> string Stream.t
-end = struct
- let lines ic =
- Stream.create (fun () ->
- match input_line ic with
- | exception End_of_file ->
- None
- | line ->
- Some line
- )
-end
-
-module File : sig
- type t =
- { path : string
- ; size : int
- }
-
- val find : string -> t Stream.t
- (** Find all files in the directory tree, starting from the given root path *)
-
- val lookup : string Stream.t -> t Stream.t
- (** Lookup file info for given paths *)
-
- val filter_out_unique_sizes : t Stream.t -> metrics:M.t -> t Stream.t
- val filter_out_unique_heads : t Stream.t -> len:int -> metrics:M.t -> t Stream.t
-end = struct
- type t =
- { path : string
- ; size : int
- }
-
- let lookup paths =
- Stream.map paths ~f:(fun path ->
- let {Unix.st_size = size; _} = Unix.lstat path in
- {path; size}
- )
-
- let find root =
- let dirs = Queue.create () in
- let files = Queue.create () in
- let explore parent =
- Array.iter (Sys.readdir parent) ~f:(fun child ->
- let path = Filename.concat parent child in
- let {Unix.st_kind = file_kind; st_size; _} = Unix.lstat path in
- match file_kind with
- | Unix.S_REG ->
- let file = {path; size = st_size} in
- Queue.add file files
- | Unix.S_DIR ->
- Queue.add path dirs
- | Unix.S_CHR
- | Unix.S_BLK
- | Unix.S_LNK
- | Unix.S_FIFO
- | Unix.S_SOCK ->
- ()
- )
- in
- explore root;
- let rec next () =
- match Queue.is_empty files, Queue.is_empty dirs with
- | false, _ -> Some (Queue.take files)
- | true , true -> None
- | true , false ->
- explore (Queue.take dirs);
- next ()
- in
- Stream.create next
-
- let filter_out_singletons files ~group ~handle_singleton =
- let q = Queue.create () in
- Stream.iter (Stream.group_by files ~f:group) ~f:(fun group ->
- let (_, n, members) = group in
- if n > 1 then
- List.iter members ~f:(fun m -> Queue.add m q)
- else
- handle_singleton group
- );
- Stream.of_queue q
-
- let filter_out_unique_sizes files ~metrics =
- filter_out_singletons
- files
- ~group:(fun {size; _} -> size)
- ~handle_singleton:(fun (size, _, _) -> M.file_unique_size metrics ~size)
-
- let head path ~len ~metrics =
- let buf = Bytes.make len ' ' in
- let ic = open_in_bin path in
- let rec read pos len =
- assert (len >= 0);
- if len = 0 then
- ()
- else begin
- let chunk_size = input ic buf pos len in
- M.chunk_read metrics ~size:chunk_size;
- if chunk_size = 0 then (* EOF *)
- ()
- else
- read (pos + chunk_size) (len - chunk_size)
- end
- in
- read 0 len;
- close_in ic;
- Bytes.to_string buf
-
- let filter_out_unique_heads files ~len ~metrics =
- filter_out_singletons
- files
- ~group:(fun {path; _} ->
- M.file_sampled metrics;
- head path ~len ~metrics
- )
- ~handle_singleton:(fun (_, _, files) ->
- let {size; _} = List.hd files in (* Guaranteed non-empty *)
- M.file_unique_sample metrics ~size
- )
-end
-