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"
I would have a .csv file
data.csvcontaining your mysql table, exported to csv format.Then in OCaml, I would read and parse this file, once, when the program is launched:
datais thus a variable, of typeHashtbl.t, containing your 150K records.Then, an OCaml function (which is called
computein your question) uses this variable: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
computeuses the already in memory variabledata.If you’re concerned about reading and parsing the csv file, you might have a look at the
Marshalmodule, to store a binary version of the variabledata.Note that
read_csvis not in the standard library, but there is for instance http://csv.forge.ocamlcore.org/ .