Storage: add readonly option

This commit is contained in:
vbotbol 2018-06-05 15:27:32 +02:00 committed by Benjamin Canou
parent 8fd79d1a89
commit e314ac635d
4 changed files with 20 additions and 7 deletions

View File

@ -202,9 +202,9 @@ let fork_test_chain v ~protocol ~expiration =
(*-- Initialisation ----------------------------------------------------------*) (*-- Initialisation ----------------------------------------------------------*)
let init ?patch_context ?mapsize root = let init ?patch_context ?mapsize ?readonly root =
GitStore.Repo.v GitStore.Repo.v
(Irmin_lmdb.config ?mapsize root) >>= fun repo -> (Irmin_lmdb.config ?mapsize ?readonly root) >>= fun repo ->
Lwt.return { Lwt.return {
path = root ; path = root ;
repo ; repo ;

View File

@ -20,6 +20,7 @@ type context = t
val init: val init:
?patch_context:(context -> context Lwt.t) -> ?patch_context:(context -> context Lwt.t) ->
?mapsize:int64 -> ?mapsize:int64 ->
?readonly:bool ->
string -> string ->
index Lwt.t index Lwt.t

View File

@ -56,20 +56,29 @@ let int64_of_string s =
with Failure _ -> with Failure _ ->
Error (`Msg (Printf.sprintf "%s is not the representation of an int64" s)) Error (`Msg (Printf.sprintf "%s is not the representation of an int64" s))
let bool_of_string s =
try Ok (bool_of_string s)
with Failure _ ->
Error (`Msg (Printf.sprintf "%s is not the representation of a boolean" s))
let int64_converter = int64_of_string, Fmt.uint64 let int64_converter = int64_of_string, Fmt.uint64
let bool_converter = bool_of_string, Fmt.bool
module Conf = struct module Conf = struct
let root = Irmin.Private.Conf.root let root = Irmin.Private.Conf.root
let mapsize = let mapsize =
Irmin.Private.Conf.key "mapsize" int64_converter 40_960_000_000L Irmin.Private.Conf.key "mapsize" int64_converter 40_960_000_000L
let readonly =
Irmin.Private.Conf.key "readonly" bool_converter false
end end
let config let config
?(config=Irmin.Private.Conf.empty) ?mapsize file = ?(config=Irmin.Private.Conf.empty) ?mapsize ?(readonly=false) file =
let module C = Irmin.Private.Conf in let module C = Irmin.Private.Conf in
let config = C.add config Conf.root (Some file) in let config = C.add config Conf.root (Some file) in
let config = C.add config Conf.readonly readonly in
Option.value_map mapsize ~default:config ~f:(C.add config Conf.mapsize) Option.value_map mapsize ~default:config ~f:(C.add config Conf.mapsize)
let mem db k = let mem db k =
@ -613,6 +622,7 @@ module Make
type config = { type config = {
root : string option ; root : string option ;
mapsize : int64 ; mapsize : int64 ;
readonly : bool ;
(* TODO *) (* TODO *)
(* ?write_buffer_size:int -> *) (* ?write_buffer_size:int -> *)
(* ?max_open_files:int -> *) (* ?max_open_files:int -> *)
@ -624,13 +634,15 @@ module Make
let config c = let config c =
let root = Irmin.Private.Conf.get c Conf.root in let root = Irmin.Private.Conf.get c Conf.root in
let mapsize = Irmin.Private.Conf.get c Conf.mapsize in let mapsize = Irmin.Private.Conf.get c Conf.mapsize in
{ root ; mapsize } let readonly = Irmin.Private.Conf.get c Conf.readonly in
{ root ; mapsize ; readonly }
let v conf = let v conf =
let { root ; mapsize } = config conf in let { root ; mapsize ; readonly } = config conf in
let root = match root with None -> "irmin.ldb" | Some root -> root in let root = match root with None -> "irmin.ldb" | Some root -> root in
if not (Sys.file_exists root) then Unix.mkdir root 0o755 ; if not (Sys.file_exists root) then Unix.mkdir root 0o755 ;
match Lmdb.opendir ~mapsize ~flags:[NoTLS] root 0o644 with let flags = Lmdb.NoTLS :: if readonly then [ Lmdb.RdOnly ] else [] in
match Lmdb.opendir ~mapsize ~flags root 0o644 with
| Error err -> Lwt.fail_with (Lmdb.string_of_error err) | Error err -> Lwt.fail_with (Lmdb.string_of_error err)
| Ok db -> | Ok db ->
let db = { db ; root } in let db = { db ; root } in

View File

@ -18,6 +18,6 @@
(** Quick-and-dirty LevelDB backend for Irmin. *) (** Quick-and-dirty LevelDB backend for Irmin. *)
val config: val config:
?config:Irmin.config -> ?mapsize:int64 -> string -> Irmin.config ?config:Irmin.config -> ?mapsize:int64 -> ?readonly:bool -> string -> Irmin.config
module Make : Irmin.S_MAKER module Make : Irmin.S_MAKER