ocaml hash from mysql

2019-07-21 08:41发布

问题:

I have a large database (approx. 150 000 records) that I want to embed into an OCaml source code named compute.ml. I am trying (without success) to transform these tables into hashtables and to embed these hastables into a function compute, so as to have the binary program run quicly without having to do queries to an external sql database.

I have 2 questions:

  • Is there a way to export for once a mysql table into an associative array (Hashtbl) that can be accessed by (or even embedded into) my OCaml function compute (itself compiled and used as a binary)?
  • Is this hashtable permanently loaded in the function or has it to be re-initiated each time that the function is called within the binary?

I have a mysql table with 142741 records which, exported in CSV format, looks like this:

"1";"27";"10";"coco";"0";"730";"1641025";"1641053";"foo";"1";"S";"0"
"2";"27";"11";"kiki";"0";"730";"1641054";"1641083";"bar";"1";"S";"0"
"3";"27";"12";"toto";"0";"730";"1641084";"1641113";"foofoo";"1";"S";"0"
"4";"27";"1";"tata";"0";"730";"1641114";"1641142";"barbar";"1";"S";"0"
...
"142741";"5";"7";"chotto";"0";"1347";"1971472";"1971500";"lastrecord";"1";"S";"0"

回答1:

I would have a .csv file data.csv containing your mysql table, exported to csv format. Then in OCaml, I would read and parse this file, once, when the program is launched:

let data = read_csv("data.csv")

data is thus a variable, of type Hashtbl.t, containing your 150K records. Then, an OCaml function (which is called compute in your question) uses this variable:

let compute x =
  let foo = Hashtbl.find data x in
  ...

This way, there are no calls to a MySQL server, the data is read only once when you launch the program, and then each call to the function compute uses the already in memory variable data.

If you're concerned about reading and parsing the csv file, you might have a look at the Marshal module, to store a binary version of the variable data.

Note that read_csv is not in the standard library, but there is for instance http://csv.forge.ocamlcore.org/ .