Skip to content
This repository
tag: v573
Fetching contributors…

Cannot retrieve contributors at this time

file 287 lines (266 sloc) 11.919 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
module List = BaseList
module A = ServerArg

module type Badop_wrapper = functor (Backend: Badop.S) -> Badop.S

let default_file ?(name="db") () =
  let (/) = Filename.concat in
  let mlstate_dir =
    try
      let id = Unix.geteuid() in
      (Unix.getpwuid id).Unix.pw_dir /
        (if id = 0 || id >= 500 then ".mlstate" else "mlstate")
    with Not_found ->
      Lazy.force File.mlstate_dir (* Useful on windows (getpwuid raises that) *)
  in
  mlstate_dir / (Filename.basename (Sys.argv.(0))) / name

let default_port = 4849
let default_local_options =
  { Badop.
    path = default_file();
    revision = None;
    restore = None;
    dot = false;
    readonly = false;
  }
let default_light_options =
  { Badop.
    lpath = default_file ~name:"db_light" ();
    ondemand = Some true;
    direct = Some true;
    max_size = Some max_int;
  }

let default =
  (module Badop_local : Badop.S), (Badop.Options_Local default_local_options)

let badop_wrapper functo modul =
  let module M = (val modul : Badop.S) in
  let module F = (val functo : Badop_wrapper) in
  (module F(M) : Badop.S)

let get_local_options = function
  | Badop.Options_Local o -> o
  | _ -> raise Not_found

let get_light_options = function
  | Badop.Options_Light o -> o
  | _ -> raise Not_found


let consume_option name =
  let rec aux acc lst =
    match lst with
    | [] -> false, acc
    | x::y ->
        if x = name then
          true, y @ acc
        else
          aux (x::acc) y
  in
  aux []


let options_parser_with_default ?name (_default_m, default_o) =
  let spec_msg = match name with None -> "" | Some n -> Printf.sprintf " for database \"%s\"" n in
  [
    ["--db-remote"],
    A.func A.parse_addr
      (fun (_,o) (addr,portopt) ->
         if o != default_o
         then Logger.warning "Warning: database options before --db-remote will be ignored%s" spec_msg;
         (module Badop_client : Badop.S),
         Badop.Options_Client (Scheduler.default, (addr, Option.default default_port portopt), fun () -> `abort)),
      "<host>[:<port>]",
      (let default_str = match default_o with
         | Badop.Options_Client(_,(host,port),_) ->
             Printf.sprintf " (default: %s:%d)" (Unix.string_of_inet_addr host) port
         | _ -> ""
       in
       Printf.sprintf "Use a remote database on given server%s" default_str)
    ;
    ["--db-local"],
    A.func (A.option A.string)
      (fun (_,o) str_opt ->
         if o != default_o
         then Logger.warning "Warning: database options before --db-local will be ignored%s" spec_msg;
         let path,flags = match str_opt with
           | Some str -> Base.String.split_char_last ':' str
           | None -> "", ""
         in
         let path = match path with
           | "" ->
               (match o with Badop.Options_Local({ Badop.path = path; _ }) -> path | _ -> default_file ?name ())
           | p -> (match name with None -> p | Some n -> Filename.concat p n)
         in

         let lflags = BaseString.slice ',' flags in
         let restore,lflags =
           let found, lflags = consume_option "restore" lflags in
           let r =
             if found then Some true
             else
               (match o with Badop.Options_Local({ Badop.restore = r; _ }) -> r | _ -> None)
           in r,lflags in

         let dot,lflags =
           let found, lflags = consume_option "dot" lflags in
           let r =
             if found then true
             else
               (match o with Badop.Options_Local({ Badop.dot = d; _ }) -> d | _ -> false)
           in r,lflags in

         let readonly,lflags =
           let found, lflags = consume_option "readonly" lflags in
           let r =
             if found then true
             else
               (match o with Badop.Options_Local({ Badop.readonly = d; _ }) -> d | _ -> false)
           in r,lflags in

        if not (List.is_empty lflags) then
          (Logger.warning "Error: unknown db flag %s%s" (List.print (fun x -> x) lflags) spec_msg; raise Exit);

         (module Badop_local : Badop.S),
         Badop.Options_Local { Badop. path; restore; dot; readonly; revision = None }),
      "[<path>][:<flags>]",
      (let default_str = match default_o with
         | Badop.Options_Local({ Badop.path = path; _ }) ->
             Printf.sprintf " (default: %s)" path
         | _ -> ""
       in
       Printf.sprintf
         "Use a local database at given path%s. Use additional flag 'restore' to try and recover a corrupted database, \
or 'dot' to have a database dot output each commit. You can specify several flags, separated by ','." default_str)
  ] @
#<Ifstatic:HAS_DBM 1>
  [
    ["--db-light"],
    A.func (A.option A.string)
      (fun (_,o) str_opt ->
         (*#<If:BADOP_DEBUG$minlevel 10>Logger.log ~color:`red "--db-light (str_opt:%s)"
(Option.to_string (fun s -> s) str_opt)#<End>;*)
         if o <> default_o
         then prerr_endline ("Warning: database options before --db-light will be ignored"^spec_msg);
         let path,flags = match str_opt with
           | Some str -> Base.String.split_char_last ':' str
           | None -> "", ""
         in
         let path = match path with
           | "" ->
               (match o with Badop.Options_Light({ Badop.lpath = path; _ }) -> path | _ -> default_file ?name ())
           | p -> (match name with None -> p | Some n -> Filename.concat p n)
         in
         let lflags = List.map (BaseString.slice '=') (BaseString.slice ',' flags) in
         let ondemand,direct,max_size,lflags =
           List.fold_left
             (fun (od,di,ms,lf) -> function
              | ["ondemand"] -> (Some true,di,ms,lf)
              | ["direct"] -> (od,Some true,ms,lf)
              | ["max_size";str] -> (od,di,(try Some (int_of_string str) with Failure "int_of_string" -> ms),lf)
              | opt -> (od,di,ms,opt@lf))
             ((match o with Badop.Options_Light({ Badop.ondemand = od; _ }) -> od | _ -> None),
              (match o with Badop.Options_Light({ Badop.direct = di; _ }) -> di | _ -> None),
              (match o with Badop.Options_Light({ Badop.max_size = ms; _ }) -> ms | _ -> None),
              []) lflags
         in
         if not (List.is_empty lflags) then
           (prerr_endline ("Error: unknown db flag "^(List.print (fun x -> x) lflags)^spec_msg); raise Exit);
         let lpath = path^"_light" in
         #<If:BADOP_DEBUG$minlevel 10>Logger.log ~color:`red "path: %s" path#<End>;
         #<If:BADOP_DEBUG$minlevel 10>Logger.log ~color:`red "ondemand: %s" (Option.to_string string_of_bool ondemand)#<End>;
         #<If:BADOP_DEBUG$minlevel 10>Logger.log ~color:`red "direct: %s" (Option.to_string string_of_bool direct)#<End>;
         #<If:BADOP_DEBUG$minlevel 10>Logger.log ~color:`red "max_size: %s" (Option.to_string string_of_int max_size)#<End>;
         let module Badop_light = Badop_light.F(DbmDB) in
         (module Badop_light : Badop.S),
         Badop.Options_Light { Badop.lpath; ondemand; direct; max_size; }),
      "[<path>][:<flags>]",
      (let default_str = match default_o with
         | Badop.Options_Light({ Badop.lpath = lpath; _ }) ->
             Printf.sprintf " (default: %s_light)" lpath
         | _ -> ""
       in
       Printf.sprintf
         "Same as --db-local, but using the lightweight, history-less backend%s."
         default_str)
  ]
#<Else>
  []
#<End>
  @
  #<If:BADOP_DEBUG> [
    ["--db-remote-replicated"],
    A.func (A.list ',' A.parse_addr)
      (fun (_,o) addrlist ->
         if o != default_o
         then Logger.warning "Warning: database options before --db-remote-replicated will be ignored%s "spec_msg;
         (module Badop_dispatcher.F(Badop_client) : Badop.S),
         Badop.Options_Dispatcher
           (List.length addrlist,
            List.map
              (fun (addr,portopt) ->
                 Badop.Options_Client (Scheduler.default, (addr, Option.default default_port portopt), fun () -> `abort))
              addrlist)),
    "<host>[:<port>],<host>[:<port>],...",
    "Use a remote database replicated on all the given servers"
    ;
    ["--db-revision"],
    A.func A.int
       (fun (_,o) i ->
         let opt = get_local_options o in
         (module Badop_local : Badop.S), Badop.Options_Local { opt with Badop.revision = Some i }),
     "<int>", "Revert the database to the given revision. Be careful, all data after that revision will be cleared";
    ["--db-template"],
    A.func A.unit
      (fun (m,o) () ->
         badop_wrapper (module Badop_wrapper_template.F : Badop_wrapper) m, o),
    "", "Wrap the database defined in previous arguments with a template identity layer";
    ["--db-stash"],
    A.func A.unit
      (fun (m,o) () ->
         badop_wrapper (module Badop_stash.F : Badop_wrapper) m, o),
    "", "Wrap the database defined in previous arguments with a trivial caching layer";
    ["--db-cache"],
    A.func A.unit
      (fun (m,o) () ->
         badop_wrapper (module Badop_cache.F : Badop_wrapper) m, o),
    "", "Wrap the database defined in previous arguments with another trivial caching layer";
    ["--db-debug"],
    A.func (A.option A.string)
      (fun (m,o) pfx ->
         badop_wrapper (module Badop_debug.F : Badop_wrapper) m,
         Badop.Options_Debug
           (Printf.sprintf
              "[Badop%s] "
              (Option.default_map "" (Printf.sprintf "(%s)") pfx), o)),
    "[string]", "Wrap the database defined in previous arguments with a debugging layer (prints about all database operations on stderr, with an optional tag)";
    ["--db-stats"],
    A.func A.unit
      (fun (m,o) () ->
         badop_wrapper (module Badop_stats.F : Badop_wrapper) m, o),
    "", "Wrap the database defined in previous arguments with a layer that prints statistics at exit";
    ["--db-dispatch"],
    A.func A.int
      (fun (m,o) n ->
         let rec specialise_opts i = function
           | Badop.Options_Local opt -> Badop.Options_Local {opt with Badop.path = (opt.Badop.path^"_"^string_of_int i)}
           | Badop.Options_Debug (s,o) ->
               Badop.Options_Debug (Printf.sprintf "%s[3%dm<%d> " s (1 + i mod 6) i, specialise_opts i o)
           | _ -> failwith ("dispatch to that backend unsupported yet"^spec_msg)
         in
         badop_wrapper (module Badop_dispatcher.F : Badop_wrapper) m,
         Badop.Options_Dispatcher
           (1, Base.List.init n (fun i -> specialise_opts i o))),
    "<int>", "Dispatch the database accesses to <n> database instances";
    ["--db-workaround"],
    A.func A.unit
      (fun (m,o) () ->
         badop_wrapper (module Badop_workaround.F : Badop_wrapper) m, o),
    "", "Wrap the database defined in previous arguments with a layer that starts all operations from the root. This is inefficient but makes the dispatcher work properly until we handle locality correctly in links and maps";
    ["--db-check"],
    A.func A.unit
     (fun (m,o) () ->
       badop_wrapper (module Badop_check.F : Badop_wrapper) m, o),
    "", "Wrap the database defined in previous arguments with a layer that check some operations";
  ]
  #<Else> [] #<End>

let options_parser = options_parser_with_default default
Something went wrong with that request. Please try again.