Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

117 lines (99 sloc) 3.904 kB
(*
Copyright © 2011 MLstate
This file is part of OPA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(*
@author Louis Gesbert
**)
open Cps.Ops
module Dialog = Badop_lib.Dialog
module F (Bk: Badop.S) = struct
type database = Bk.database
type transaction_status = Fresh | Changed | Prepared | Committed | Failed
type transaction = { (* Extended transaction (called xtr below) *)
db: Bk.database;
status: transaction_status;
tr: Bk.transaction option;
errk: exn -> unit;
stash: (Badop.path * Dialog.query Bk.write_op) list;
}
type revision = Bk.revision
let open_database = Bk.open_database
let close_database = Bk.close_database
let status db k = Bk.status db @> fun st -> Badop.Layer("Stash", st) |> k
let get_tr xtr k = match xtr.tr with
| None -> Bk.Tr.start xtr.db xtr.errk @> k
| Some tr -> tr |> k
let flush xtr k = match xtr.stash with
| [] -> xtr |> k
| l ->
get_tr xtr
@> fun tr -> Bk.write_list tr (List.rev l)
@> fun tr ->
{ xtr with tr = Some tr; status = Changed; stash = [] } |> k
module Tr = struct
let start db errk k =
{ db = db; tr = None; errk = errk; status = Fresh; stash = [] } |> k
let start_at_revision db rev errk k =
Bk.Tr.start_at_revision db rev errk
@> fun tr -> { db = db; tr = Some tr; errk = errk; status = Fresh; stash = [] } |> k
let prepare xtr k =
flush xtr
@> fun xtr ->
match xtr.status with
| Changed ->
get_tr xtr
@> fun tr -> Bk.Tr.prepare tr
@> fun (tr,ok) -> ({ xtr with tr = Some tr; status = if ok then Prepared else Failed}, ok) |> k
| Fresh | Prepared -> (xtr,true) |> k
| Failed | Committed -> (xtr,false) |> k
let rec commit xtr k =
match xtr.status with
| Prepared ->
assert(xtr.stash = []);
get_tr xtr @> fun tr -> Bk.Tr.commit tr @> k
| Changed ->
prepare xtr
@> fun (xtr,ok) -> if ok then get_tr xtr
@> fun tr -> Bk.Tr.commit tr @> k else false |> k
| Fresh ->
if xtr.stash = [] then true |> k else flush xtr
@> fun xtr -> commit xtr @> k
| Committed -> true |> k
| Failed -> false |> k
let abort xtr k =
match xtr.status with
| Failed | Committed -> () |> k
| _ -> match xtr.tr with Some tr -> Bk.Tr.abort tr @> k | None -> () |> k
end
type 'which read_op = 'which Bk.read_op
type 'which write_op = ('which,transaction,revision) Badop.generic_write_op
let read xtr path read_op k =
flush xtr
@> fun xtr ->
get_tr xtr @> fun tr -> Bk.read tr path read_op @> k
let write xtr path write_op k =
Badop.Aux.map_write_op ~transaction:(fun xtr k -> get_tr xtr @> k) ~revision:(fun r k -> r |> k) write_op
(* only for types, no tr in queries *)
@> fun bk_write_op ->
Badop.Aux.respond_set_transaction write_op { xtr with stash = (path,bk_write_op)::xtr.stash }
|> k
let write_list xtr path_write_op_list k =
let wr xtr (path, op) k =
write xtr path op @> fun resp -> Badop.Aux.result_transaction resp |> k
in
Cps.List.fold wr xtr path_write_op_list k
let node_properties db config k = Bk.node_properties db config @> k
module Debug = struct
let revision_to_string = Bk.Debug.revision_to_string
end
end
Jump to Line
Something went wrong with that request. Please try again.