Skip to content
This repository
tag: v1028
Fetching contributors…

Cannot retrieve contributors at this time

file 186 lines (161 sloc) 7.049 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
(*
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/>.
*)
(*
@author Louis Gesbert
**)

module D = Badop_lib
module Node_property = Badop_structure.Node_property

type 'a answer = [ `Answer of 'a | `Absent | `Linkto of Badop.path ]
(** This module provides a simple db, no specific results to handle *)

type database = { session: Session.t; file: string; mutable node_config : Node_property.config }
type transaction = { db: database; tr: Transaction.t }

let (|>) x f = f x

let open_database options k =
  let options =
    match options with
    | Badop.Options_Local options -> options
    | _ -> assert false in
  let open_db ?readonly ?dot ?restore p =
    match options.Badop.revision with
    | Some rev -> Session.open_db ?readonly ?dot ?restore ~rev p
    | None -> Session.open_db ?readonly ?dot ?restore p
  in
  let open_db ?readonly ?dot p =
    match options.Badop.restore with
    | Some restore -> open_db ?readonly ?dot ~restore p
    | None -> open_db ?readonly ?dot p
  in
  let open_db ?readonly p =
    if options.Badop.dot then open_db ?readonly ~dot:true p
    else open_db ?readonly p
  in
  let open_db p =
    if options.Badop.readonly then open_db ~readonly:true p
    else open_db p
  in
  let path = options.Badop.path in

  open_db path |> fun (db,_) -> { session = db; file = path; node_config = [] } |> k

let close_database db k =
  Session.close_db db.session |> k

let status db k = Badop.Local db.file |> k

module Tr = struct
  let start db _errk k =
    { db = db; tr = Session.new_trans db.session } |> k

  let start_at_revision db rev _errk k =
    { db = db; tr = Session.new_trans ~read_only:(true, Some rev) db.session } |> k

  let prepare trans k =
    (* Executes [k] as soon as prepare finished, asynchronously, nonblocking.
When prepare is postponed and stored on the FIFO,
the continuation is stored as well. The exceptions from [k]
are never caught here. *)
    if Transaction.modified trans.tr then
      Session.try_trans_prepare trans.db.session trans.tr
      (fun (tr, b) -> ({db = trans.db; tr = tr}, b) |> k)
    else
      (* Non-modifying trans, so nothing to do; commit will be void, too. *)
      ({db = trans.db; tr = trans.tr}, true) |> k

  let commit trans k =
    if Transaction.modified trans.tr then
      (* Assumption: [trans] is prepared by [execute_trans_prepare].
Here some continuations of [prepare] may be executed, but only in case
when some transactions are on the FIFO and are being prepared
after the actual commit is completed. *)
      Session.really_commit trans.db.session trans.tr |> k
    else
      true |> k

  let abort trans k = Session.abort_or_rollback trans.db.session trans.tr |> k
end

type revision = Revision.t

(** All the operations that query the db *)
type 'which read_op = ('which,revision) Badop.generic_read_op

let read trans path op k = match op with
  | Badop.Stat (D.Query () as q) ->
      (try `Answer (Badop.Stat (D.Dialog_aux.respond q (Session.stat trans.tr path)))
       with Hldb.UnqualifiedPath -> `Absent) |> k
  | Badop.Contents (D.Query () as q) ->
      (try `Answer (Badop.Contents (D.Dialog_aux.respond q (Session.get trans.db.session trans.tr path)))
       with Hldb.UnqualifiedPath -> `Absent) |> k
  | Badop.Children (D.Query range as q) ->
      (try
         `Answer
           (Badop.Children
              (D.Dialog_aux.respond q
                 (Session.get_children trans.db.session trans.tr range path
                 |> List.map fst)))
       with Hldb.UnqualifiedPath -> `Absent) |> k
  | Badop.Revisions (D.Query range as q) ->
      (try
         `Answer
           (Badop.Revisions
              (D.Dialog_aux.respond q
                 ((if (range = (None, -1)
                       && (try ignore (Session.stat trans.tr path); true
                           with Hldb.UnqualifiedPath -> false))
                   then
                     (* current revision *)
                     [Session.get_rev trans.db.session]
                   else
                     (Session.get_all_rev_of_path trans.tr path
                     |> BaseList.filterbounds range Base.identity))
                 |> List.map (fun rev -> rev, Session.get_timestamp_from_rev trans.db.session rev))))
       with Hldb.UnqualifiedPath -> `Absent) |> k
  | Badop.Search (D.Query (words, _range_FIXME) as q) ->
      (try
         `Answer
           (Badop.Search
              (D.Dialog_aux.respond q
                 (Session.full_search trans.tr words path)))
         (* FIXME: limit number of results *)
       with Hldb.UnqualifiedPath -> `Absent) |> k
  | _ -> assert false (* _ (Response _) can't happen (ensured by typing) *)

(** All the operations that write to the db *)
type 'which write_op = ('which,transaction,revision) Badop.generic_write_op

let write trans path op k = match op with
  | Badop.Set (D.Query data as q) ->
      Badop.Set (D.Dialog_aux.respond q { trans with tr = Session.set trans.db.session trans.tr path data }) |> k
  | Badop.Clear (D.Query () as q) ->
      Badop.Clear
        (D.Dialog_aux.respond q
           (try
              { trans with tr = Session.remove trans.db.session trans.tr path }
            with Hldb.UnqualifiedPath -> trans)) |> k
  | Badop.Link (D.Query linkpath as q) ->
      Badop.Link
        (D.Dialog_aux.respond q
           { trans with tr = Session.set_link trans.db.session trans.tr path linkpath }) |> k
  | Badop.Copy (D.Query (copypath,copyrev) as q) ->
      Badop.Copy
        (D.Dialog_aux.respond q
           { trans with tr = Session.set_copy trans.db.session trans.tr path (copypath, copyrev) }) |> k
  | _ -> assert false (* _ (Response _) can't happen (ensured by typing) *)

let write_list trans path_op_list k =
  let wr trans (path, op) k =
    write trans path op (fun resp -> Badop.Aux.result_transaction resp |> k)
  in
  Cps.List.fold wr trans path_op_list k

let node_properties db config k =
  (match db.node_config with
  | [] ->
    #<If:BADOP_DEBUG$minlevel 10>
      Printf.printf "Set node config\n%s\n%!" (Node_property.StringOf.config config) #<End>;
    db.node_config <- config
  | nc ->
      if nc <> config then
        (#<If:BADOP_DEBUG$minlevel 5> Printf.eprintf "Try to set another config, refuse\n%!" #<End>;
        failwith "Badop local: Invalid config"));
  () |> k

module Debug = struct
  let revision_to_string = Revision.to_string
  let path_to_string = Path.to_string
end
Something went wrong with that request. Please try again.