Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] newbie type problem
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Florian Hars <hars@b...>
Subject: Re: [Caml-list] newbie type problem
Dustin Sallings wrote:
>     Well, part of the problem is that my log files aren't necessarily 
> sequential, so I have to be able to go back to any point in time and 
> update the thing.

Last time I had that problem I used two structures: one to keep the open events 
that come in from the logfiles and one to keep the data for the complete 
transactions where I have read all the relevant events.
If your logfiles are line-oriented (as these critters tend to be), you might 
want to write a function thats folds a function over all lines of a text file.
Then your program will look something like (add error checking, logic for 
handling overlapping transactions of the same type and missing functions to taste):

type evt = Start of string * float | End of string * float | Junk

module M = Map.make (struct type t = string let compare = compare end)

let parse_line s =
   if is_start s then
     Start (get_transaction_type s, get_time_form_log_line s)
   else if is_end s then
     End (get_transaction_type s, get_time_from_log_line s)
   else
      Junk

let operate_on_line (start_events, transactions as init) s =
   match parse_line s with
   | Junk -> init
   | Start (t_type, time) ->
         M.add t_type time start_events, transactions
   | End (t_type, time) ->
	let start_time = M.find t_type start_events in
	let l = try M.find t_type transactions with Not_found -> [] in
         M.remove t_type start_events,
         M.add t_type ((time - start_time)::l) transactions

let parse_logfile filename =
   let ic = open_in filename in
   let start_events, transactions =
     Textfile.fold operate_on_line (M.empty, M.empty) ic in
   close_in ic;
   cleanup_dangling_events start_events transactions

let _ = print_transaction_info (parse_logfile "my_logfile")

Yours, Florian.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners