Skip to content
Permalink
Browse files

Replace valid_fd with check on counters

  • Loading branch information...
icristescu committed Sep 4, 2019
1 parent 2880c05 commit 6cdc01a6cc59c9de8ce9b2b7fad5f00428262ef6
@@ -46,8 +46,6 @@ module type S = sig
val sync : t -> unit

val close : t -> unit

val is_valid : t -> bool
end

let ( ++ ) = Int64.add
@@ -256,12 +254,6 @@ module Unix : S = struct
v ~offset ~version raw

let close t = Unix.close t.raw.fd

let is_valid t =
try
let _ = Unix.fstat t.raw.fd in
true
with Unix.Unix_error (Unix.EBADF, _, _) -> false
end

let ( // ) = Filename.concat
@@ -42,8 +42,6 @@ module type S = sig
val sync : t -> unit

val close : t -> unit

val is_valid : t -> bool
end

module Unix : S
@@ -122,7 +122,7 @@ module Make (IO : IO.S) : S = struct
Hashtbl.reset t.index )

let valid t =
if IO.is_valid t.io then (
if t.counter <> 0 then (
t.counter <- t.counter + 1;
true )
else false
@@ -175,7 +175,7 @@ module Atomic_write (K : Irmin.Type.S) (V : Irmin.Hash.S) = struct
let watches = W.v ()

let valid t =
if IO.is_valid t.block then (
if t.counter <> 0 then (
t.counter <- t.counter + 1;
true )
else false
@@ -147,7 +147,7 @@ struct
Dict.clear t.dict

let valid t =
if IO.is_valid t.block then (
if t.counter <> 0 then (
t.counter <- t.counter + 1;
true )
else false
@@ -174,8 +174,6 @@ struct
IO.close t.block;
Dict.close t.dict )

let valid t = IO.is_valid t.block

module Make (V : ELT with type hash := K.t) = struct
module Tbl = Table (K)
module Lru = Cache (K)
@@ -204,6 +202,12 @@ struct

let create = Lwt_mutex.create ()

let valid t =
if t.counter <> 0 then (
t.counter <- t.counter + 1;
true )
else false

let unsafe_v_no_cache ~fresh ~readonly ~lru_size ~index root =
let pack = v index ~fresh ~readonly root in
let staging = Tbl.create 127 in
@@ -214,8 +218,7 @@ struct
~index root =
try
let t = Hashtbl.find roots (root, readonly) in
if valid t.pack then (
t.counter <- t.counter + 1;
if valid t then (
if fresh then clear t;
t )
else (
@@ -352,7 +355,7 @@ struct
let unsafe_close t =
t.counter <- t.counter - 1;
if t.counter = 0 then (
Log.debug (fun l -> l "[pack] closing %s" (IO.name t.pack.block));
Log.debug (fun l -> l "[pack] close %s" (IO.name t.pack.block));
Tbl.clear t.staging;
ignore (Lru.clear t.lru);
close t.pack )
@@ -184,6 +184,36 @@ let test_reuse_index _switch () =
Alcotest.(check string) "x1" x1 y1;
Lwt.return ()

let test_close_pack_more _switch () =
(*open and close in rw*)
let index = get_index ~fresh:true "test3" in
Pack.v ~fresh:true ~index "test3" >>= fun w ->
let x1 = "foo" in
let h1 = sha1 x1 in
Pack.unsafe_append w h1 x1;
Pack.sync w;
Index.close index;
Pack.close w >>= fun () ->
(*open and close in ro*)
let index = get_index ~fresh:false ~readonly:true "test3" in
Pack.v ~fresh:false ~readonly:true ~index "test3" >>= fun w1 ->
Pack.find w1 h1 >|= get >>= fun y1 ->
Alcotest.(check string) "x1.1" x1 y1;
Index.close index;
Pack.close w1 >>= fun () ->
(* reopen in ro *)
let index = get_index ~fresh:false "test3" in
Pack.v ~fresh:false ~index "test3" >>= fun w2 ->
Pack.find w2 h1 >|= get >>= fun y1 ->
Alcotest.(check string) "x1.2" x1 y1;

(*reopen in ro *)
let index = get_index ~fresh:false ~readonly:true "test3" in
Pack.v ~fresh:false ~readonly:true ~index "test3" >>= fun w3 ->
Pack.find w3 h1 >|= get >>= fun y1 ->
Alcotest.(check string) "x1.3" x1 y1;
Lwt.return ()

let test_close_pack _switch () =
let index = get_index ~fresh:true "test2" in
Pack.v ~fresh:true ~index "test2" >>= fun w ->
@@ -349,5 +379,6 @@ let misc =
Alcotest_lwt.test_case "branch" `Quick test_branch;
Alcotest_lwt.test_case "index" `Quick test_reuse_index;
Alcotest_lwt.test_case "close" `Quick test_close_pack;
Alcotest_lwt.test_case "close readonly" `Quick test_close_pack_more;
Alcotest_lwt.test_case "branch close" `Quick test_close_branch;
] )

0 comments on commit 6cdc01a

Please sign in to comment.
You can’t perform that action at this time.