Skip to content
This repository
tag: v1699
Fetching contributors…

Cannot retrieve contributors at this time

file 420 lines (375 sloc) 12.586 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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
(*
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/>.
*)
(* depends *)
module List = BaseList

(* -- *)

type full = {
  cur_rev : Revision.t ;
  content : Datas.t ;
  map : Eid.t KeyMap.t ;
}

type delta = {
  new_content : Datas.t option ;
  new_childs : Eid.t KeyMap.t ;
    (* TODO: verify: I think the semantics is that the children
cannot exist in the old node; they are always added,
never overwritten. *)
  prof : int ;
}

(* Reverse delta. Tells what to do to get the previous version of a node. *)
type rev_delta =
    { old_content : Datas.t option;
      (* The content as it was back then.*)
      extra_children : Keys.t list;
      (* Which children to remove to get the old list. *)
      rev_prof : int;
      (* Auxiliary: how many deltas to here, counting form older to newer. *)
    }

type io_t =
  | Full of full
  | Delta of (Uid.t * Revision.t * delta)
  | RevDelta of Uid.t * Revision.t * rev_delta

type t = io_t

let max_full = 103
let max_delta =
  #<If:DEBUG_DB_MAX_DELTA$defined>
    begin
      match DebugVariables.debug_db_max_delta with
      | Some s when int_of_string s >= 0 ->
          int_of_string s
      | _ ->
          failwith "Bad value for debug variable DEBUG_DB_MAX_DELTA"
    end
    #<Else>
    103
    #<End>


(*******************)
(* Screen printing *)
(*******************)

let print_delta_bis delta =
  match delta.new_content with
  | Some c -> (Printf.sprintf "content=%s" (Datas.to_string c))
  | None -> (Printf.sprintf "content=%s" (Datas.to_string (Datas.empty)))

let print_map map = KeyMap.fold (
  fun k eid acc -> Printf.sprintf "%s%s -> %s "
    acc (Keys.to_string k) (Eid.to_string eid)
) map ""

let print_delta delta =
  Printf.sprintf "{new_content = %s, new_chilren = %s}"
    (match delta.new_content with
    | Some c -> Datas.to_string c
    | _ -> "None")
    (print_map delta.new_childs)

let print_rev_delta delta =
  Printf.sprintf "{old_content = %s, extra_chilren = %s}"
    (match delta.old_content with
    | Some c -> Datas.to_string c
    | _ -> "None")
    (BaseString.concat_map
       ~left:"[" "; " Keys.to_string delta.extra_children ~right:"]")

let print_full n =
  Printf.sprintf
    "rev=%s, content=%s, map=%s"
    (Revision.to_string n.cur_rev)
    (Datas.to_string n.content)
    (print_map n.map)

let to_string = function
  | Full node -> Printf.sprintf "Full {%s}" (print_full node)
  | Delta (uid, rev, delta) -> Printf.sprintf "Delta {uid = %d; rev = %d; %s}"
      (Uid.value uid) (Revision.value rev) (print_delta delta)
  | RevDelta (uid, rev, rev_delta) ->
      Printf.sprintf "RevDelta {uid = %d; rev = %d; %s}"
        (Uid.value uid) (Revision.value rev)
        (print_rev_delta rev_delta)


(************************)
(* Access to the fields *)
(************************)

let rec get_map ~f node =
  match node with
  | Full node -> node.map
  | Delta (uid, _, delta) ->
      let delta_map = delta.new_childs in
      let rec_map = get_map ~f (f uid) in
      (* TODO: probably not true:
Subsequent revisions may overwrite the same children,
so [KeyMap.safe_merge] is too strict here. *)
      KeyMap.merge (fun a _ -> a) delta_map rec_map
  | RevDelta (uid, _rev, delta) ->
      let rec_map = get_map ~f (f uid) in
      List.fold_left
        (fun acc k -> KeyMap.remove k acc)
        rec_map delta.extra_children

let rec get_children ~f = function
  | Full full -> KeyMap.fold (fun k _ acc -> k :: acc) full.map []
  | Delta (uid, _, delta) ->
      let delta_children =
        let map = delta.new_childs in
        KeyMap.fold (fun k _eid acc -> k :: acc) map [] in
      let rec_children = get_children ~f (f uid) in
      List.merge (fun k1 k2 -> compare k1 k2)
        delta_children rec_children
  | RevDelta (_uid, _rev, _delta) as node ->
      let map = get_map ~f node in
      KeyMap.fold (fun k _eid acc -> k :: acc) map []

let rec get_children_eid ~f = function
  | Full full -> KeyMap.fold (fun _ eid acc -> eid :: acc) full.map []
  | Delta (uid, _, delta) ->
      let delta_children =
        let map = delta.new_childs in
        KeyMap.fold (fun _k eid acc -> eid :: acc) map [] in
      let rec_children = get_children_eid ~f (f uid) in
      List.merge (fun eid1 eid2 -> compare eid1 eid2)
        delta_children rec_children
  | RevDelta (_uid, _rev, _delta) as node ->
      let map = get_map ~f node in
      KeyMap.fold (fun _k eid acc -> eid :: acc) map []

let rec get_content ~f = function
  | Full node -> node.content
  | Delta (uid, _, delta) -> (
      match delta.new_content with
      | Some d -> d
      | None -> get_content ~f (f uid)
    )
  | RevDelta (uid, _rev, delta) ->
      begin match delta.old_content with
      | Some d -> d
      | None -> get_content ~f (f uid)
      end

let get_cur_rev = function
  | Full node -> node.cur_rev
  | Delta (_, rev, _) -> rev
  | RevDelta (_uid, rev, _delta) -> rev

let rec next_eid ~f k node =
  #<If:DEBUG_DB$minlevel 1000>
    Logger.log ~color:`green "DB : next_eid node(%s) k(%s)"
      (to_string node) (Keys.to_string k)
  #<End>;
  match node with
  | Full node -> KeyMap.find k node.map
  | Delta (uid, _, delta) -> (
      match KeyMap.find_opt k delta.new_childs with
      | Some neid -> neid
      | _ -> next_eid ~f k (f uid)
    )
  | RevDelta (uid, _rev, _delta) -> next_eid ~f k (f uid)

let rec find_opt ~f k n =
  match n with
  | Full node -> KeyMap.find_opt k node.map
  | Delta (uid, _, delta) -> (
      match KeyMap.find_opt k delta.new_childs with
      | Some neid -> Some neid
      | _ -> find_opt ~f k (f uid)
    )
  | RevDelta (uid, _rev, delta) ->
      if List.mem k delta.extra_children then None
      else find_opt ~f k (f uid)


(************************)
(* Creation and updates *)
(************************)

let create ?content rev =
  let content =
    match content with
    | Some d -> d
    | _ -> Datas.empty
  in
  Full { cur_rev = rev
       ; content = content
       ; map = KeyMap.empty }

let is_full_map node =
  match node with
  | Full node -> (KeyMap.size node.map >= max_full)
  | Delta (_, _, delta) -> (KeyMap.size delta.new_childs >= max_delta)
  | RevDelta (_uid, _rev, delta) ->
      (List.length delta.extra_children >= max_delta)

let update_full_to_full ?content ?child _uid rev node =
  let new_map =
    match child with
    | Some (k, eid) ->
        KeyMap.add k eid node.map
    | _ -> node.map
  in
  let new_rev = rev in
  let new_content =
    match content with
    | Some d -> d
    | _ -> node.content
  in
  Full { cur_rev = new_rev
       ; content = new_content
       ; map = new_map}

let update_full_to_delta ?content ?child uid rev node =
  if max_delta = 0 then
    update_full_to_full ?content ?child uid rev node
  else
    let delta =
      let new_content = content in
      let new_childs =
        match child with
        | Some (k, eid) -> KeyMap.add k eid KeyMap.empty
        | _ -> KeyMap.empty
      in
      {new_content = new_content
      ; new_childs = new_childs
      ; prof = 1}
    in
    Delta (uid, rev, delta)

let update_full_to_rev_delta ?content ?child uid rev node =
  (* TODO *)
  if max_delta = 0 then
    update_full_to_full ?content ?child uid rev node
  else
    let delta =
      let new_content = content in
      let new_childs =
        match child with
        | Some (k, eid) -> KeyMap.add k eid KeyMap.empty
        | _ -> KeyMap.empty
      in
      {new_content = new_content
      ; new_childs = new_childs
      ; prof = 1}
    in
    Delta (uid, rev, delta)

(* delta: if node updated by a new transaction *)
let update_delta ~f ?content ?child uid rev old_uid old_rev old_delta delta =
  if delta then
    match old_delta.prof with
    | x when x >= max_delta ->
        let old_node = Delta (old_uid, old_rev, old_delta) in
        let map = get_map ~f old_node in
        let map =
          match child with
          | Some (k, eid) -> KeyMap.add k eid map
          | _ -> map
        in
        let new_content =
          match content with
          | Some d -> d
          | _ -> get_content ~f old_node
        in
        Full { cur_rev = rev
             ; content = new_content
             ; map = map }
    | _ ->
        let new_delta =
          let new_content =
            match content with
            | Some d -> Some d
            | _ -> None
          in
          let new_childs =
            match child with
            | Some (k, eid) -> KeyMap.add k eid KeyMap.empty
            | _ -> KeyMap.empty
          in
          {new_content = new_content
          ; new_childs = new_childs
          ; prof = succ old_delta.prof}
        in
        Delta (uid, rev, new_delta)
  else
    match old_delta.prof with
    | x when x >= max_delta ->
        let old_node = Delta (old_uid, old_rev, old_delta) in
        let map = get_map ~f old_node in
        let map =
          match child with
          | Some (k, eid) -> KeyMap.add k eid map
          | _ -> map
        in
        let new_content =
          match content with
          | Some d -> d
          | _ -> get_content ~f old_node
        in
        Full { cur_rev = old_rev
             ; content = new_content
             ; map = map }
    | _ ->
        let new_delta =
          let new_content =
            match content with
            | Some d -> Some d
            | _ -> old_delta.new_content
          in
          let new_childs =
            match child with
            | Some (k, eid) -> KeyMap.add k eid old_delta.new_childs
            | _ -> old_delta.new_childs
          in
          {new_content = new_content
          ; new_childs = new_childs
          ; prof = old_delta.prof} in
        Delta (old_uid, old_rev, new_delta)

let update ~f uid node rev ?content ?child delta =
  let new_node =
    match (node, delta) with
    | (Full node, true) ->
        begin match child with
        | Some _ ->
            update_full_to_rev_delta ?content ?child uid rev node
            (* Old version: update_full_to_delta ?content ?child uid rev node *)
        | None -> update_full_to_full ?content uid rev node
        end
    | (Full node, false) -> update_full_to_full ?content ?child uid rev node
    | (Delta (old_uid, old_rev, old_delta), _) ->
        update_delta ~f ?content ?child
          uid rev old_uid old_rev old_delta delta
    | (RevDelta (_uid, _rev, _delta), _) ->
        (* Normally, this node should never be updated,
because the last revision will alsways be the Full node. *)
        assert false
  in
  (new_node, is_full_map new_node)

let rec remove_child ~f rev node key =
  match node with
  | Full node ->
      let new_map = KeyMap.remove key node.map in
      Full {node with
              map = new_map; cur_rev = rev}
  | Delta (uid, _rev, _delta) ->
      let new_map = KeyMap.remove key (get_map ~f node) in
      let content = get_content ~f node in
      let new_node = create ~content rev in
      let new_node = KeyMap.fold (
        fun key eid node ->
          let node, _ = update ~f uid node rev ~child:(key, eid) false in
          node
      ) new_map new_node
      in
      new_node
  | RevDelta (uid, _rev, delta) ->
      let delta = { delta with extra_children = key :: delta.extra_children } in
      RevDelta (uid, rev, delta)


(***********)
(* Folding *)
(***********)

let fold ~f foo node acc = KeyMap.fold foo (get_map ~f node) acc

let fold_range (start_opt, length) ~f foo node acc =
  let map = get_map ~f node in
  if KeyMap.is_empty map then acc else
    let start =
      match start_opt with
      | Some start -> start
      | None -> if length >= 0 then fst (KeyMap.min map) else fst (KeyMap.max map)
    in
    if length = 0 then
      KeyMap.fold_range foo map start (fst (KeyMap.max map)) acc
    else
      KeyMap.fold_length ~start ~length foo map acc


(****************************)
(* Disk writing and reading *)
(****************************)

external write : t -> io_t = "%identity"
external read : io_t -> t = "%identity"
Something went wrong with that request. Please try again.