Download The demexp Book - Linux
Transcript
Chapter 44 Input/Ouput The module Io is used to save and load the databases in binary format. The Marshal module is used for this. This implies that saving of databases is not type safe, it works accross platforms supported by OCaml but only for a given release of the OCaml compiler. In other words, the binary saving of databases is used only for a local use and not for long term storage of databases. All bases are a reference on the base itself to allow easy saving and loading. By default, the bases are saved and loaded in current directory. 260a hio.ml 260ai≡ (* copyright 2003-2004 David MENTRE *) (* this software is under GNU GPL. See COPYING.GPL file for details *) 260b . open Printf open Srvflags open Sys The suffix for bases in XML format is .dmxp (DeMeXP). 44.1 File rotation To avoid any issues if a database can’t be saved (e.g. server crashing while bases are saved), we rotate amongs several copies of the same file. Function rotate file takes a file name of name and it renames files such that file named name is renamed name.0, file named name.0 is renamed name.1, ..., until name.max rotation is reached. 260b hio.ml 260ai+≡ let rotate_file name max_rotation = for i = max_rotation downto 1 do let previous = name ˆ "." ˆ (string_of_int (i - 1)) in if file_exists previous then rename previous (name ˆ "." ˆ (string_of_int i)) done; if file_exists name then rename name (name ˆ ".0") 44.2 / 260a 260c . Data bases saving in XML format We define the current version of the XML format. 260c hio.ml 260ai+≡ let xml_version = "0.6" / 260b 261a . 260