@@ -621,9 +621,9 @@ let rrd_add_ds_unsafe rrd timestamp newds =
621
621
rrd.rrd_rras
622
622
}
623
623
624
- (* * Add in a new DS into a pre-existing RRD. Preserves data of all the other archives
625
- and fills the new one full of NaNs. Note that this doesn't fill in the CDP values
626
- correctly at the moment!
624
+ (* * Add in a new DS into a pre-existing RRD. Preserves data of all the other
625
+ archives and fills the new one full of NaNs. Note that this doesn't fill
626
+ in the CDP values correctly at the moment!
627
627
*)
628
628
629
629
let rrd_add_ds rrd timestamp newds =
@@ -632,28 +632,27 @@ let rrd_add_ds rrd timestamp newds =
632
632
else
633
633
rrd_add_ds_unsafe rrd timestamp newds
634
634
635
- (* * Remove the named DS from an RRD. Removes all of the data associated with it, too *)
635
+ (* * Remove the named DS from an RRD. Removes all of the data associated with
636
+ it, too. THe function is idempotent. *)
636
637
let rrd_remove_ds rrd ds_name =
637
- let n =
638
- Utils. array_index ds_name (Array. map (fun ds -> ds.ds_name) rrd.rrd_dss)
639
- in
640
- if n = - 1 then
641
- raise (Invalid_data_source ds_name)
642
- else
643
- {
644
- rrd with
645
- rrd_dss= Utils. array_remove n rrd.rrd_dss
646
- ; rrd_rras=
647
- Array. map
648
- (fun rra ->
649
- {
650
- rra with
651
- rra_data= Utils. array_remove n rra.rra_data
652
- ; rra_cdps= Utils. array_remove n rra.rra_cdps
653
- }
654
- )
655
- rrd.rrd_rras
656
- }
638
+ match Utils. find_index (fun ds -> ds.ds_name = ds_name) rrd.rrd_dss with
639
+ | None ->
640
+ rrd
641
+ | Some n ->
642
+ {
643
+ rrd with
644
+ rrd_dss= Utils. array_remove n rrd.rrd_dss
645
+ ; rrd_rras=
646
+ Array. map
647
+ (fun rra ->
648
+ {
649
+ rra with
650
+ rra_data= Utils. array_remove n rra.rra_data
651
+ ; rra_cdps= Utils. array_remove n rra.rra_cdps
652
+ }
653
+ )
654
+ rrd.rrd_rras
655
+ }
657
656
658
657
(* * Find the RRA with a particular CF that contains a particular start
659
658
time, and also has a minimum pdp_cnt. If it can't find an
@@ -698,18 +697,17 @@ let find_best_rras rrd pdp_interval cf start =
698
697
List. filter (contains_time newstarttime) rras
699
698
700
699
let query_named_ds rrd as_of_time ds_name cf =
701
- let n =
702
- Utils. array_index ds_name (Array. map (fun ds -> ds.ds_name) rrd.rrd_dss)
703
- in
704
- if n = - 1 then
705
- raise (Invalid_data_source ds_name)
706
- else
707
- let rras = find_best_rras rrd 0 (Some cf) (Int64. of_float as_of_time) in
708
- match rras with
709
- | [] ->
710
- raise No_RRA_Available
711
- | rra :: _ ->
712
- Fring. peek rra.rra_data.(n) 0
700
+ match Utils. find_index (fun ds -> ds.ds_name = ds_name) rrd.rrd_dss with
701
+ | None ->
702
+ raise (Invalid_data_source ds_name)
703
+ | Some n -> (
704
+ let rras = find_best_rras rrd 0 (Some cf) (Int64. of_float as_of_time) in
705
+ match rras with
706
+ | [] ->
707
+ raise No_RRA_Available
708
+ | rra :: _ ->
709
+ Fring. peek rra.rra_data.(n) 0
710
+ )
713
711
714
712
(* *****************************************************************************)
715
713
(* Marshalling/Unmarshalling functions *)
@@ -876,30 +874,26 @@ let from_xml input =
876
874
877
875
(* Purge any repeated data sources from the RRD *)
878
876
let ds_names = ds_names rrd in
879
- let ds_names_set = Utils. setify ds_names in
880
- let ds_name_counts =
881
- List. map
882
- (fun name ->
883
- let x, _ = List. partition (( = ) name) ds_names in
884
- (name, List. length x)
885
- )
886
- ds_names_set
887
- in
888
- let removals_required =
889
- List. filter (fun (_ , x ) -> x > 1 ) ds_name_counts
890
- in
891
- List. fold_left
892
- (fun rrd (name , n ) ->
893
- (* Remove n-1 lots of this data source *)
894
- let rec inner rrd n =
895
- if n = 1 then
896
- rrd
897
- else
898
- inner (rrd_remove_ds rrd name) (n - 1 )
899
- in
900
- inner rrd n
901
- )
902
- rrd removals_required
877
+ List. sort_uniq String. compare ds_names
878
+ |> List. filter_map (fun name ->
879
+ match List. filter (String. equal name) ds_names with
880
+ | [] | [_] ->
881
+ None
882
+ | x ->
883
+ Some (name, List. length x)
884
+ )
885
+ |> List. fold_left
886
+ (fun rrd (name , n ) ->
887
+ (* Remove n-1 lots of this data source *)
888
+ let rec inner rrd n =
889
+ if n = 1 then
890
+ rrd
891
+ else
892
+ inner (rrd_remove_ds rrd name) (n - 1 )
893
+ in
894
+ inner rrd n
895
+ )
896
+ rrd
903
897
)
904
898
input
905
899
0 commit comments