Source file commit_storage.ml
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
open Commit
open Utils
open Result_lwt.Syntax
module B = Mmap.Buffer
(** How to write the entry to context *)
let read storage (i : Index.t) =
let buf = Storage.get_cell storage i in
let index = B.get_index buf 28 in
let info = B.get_index buf 20 in
let prev = Index.zero_then_none @@ B.get_index buf 24 in
let j = Index.pred i in
let buf = Storage.get_cell storage j in
let hash = Commit_hash.of_string @@ B.copy buf 0 32 in
let k = Index.pred j in
let buf = Storage.get_cell storage k in
let parent = Commit_hash.of_string @@ B.copy buf 0 32 in
let parent = if parent = Commit_hash.zero then None else Some parent in
Commit.{ index; parent; hash; info }, prev
let read_the_latest storage =
match Storage.get_last_root_index storage with
| None -> None
| Some i -> Some (fst @@ read storage i)
let write_at storage ~at:i ~prev commit =
let j = Index.pred i in
assert (Option.default Index.zero prev < j);
let buf = Storage.get_cell storage j in
let s = Commit_hash.to_string commit.hash in
assert (String.length s = 32);
B.write_string s buf 0;
let k = Index.pred j in
assert (Option.default Index.zero prev < k);
let buf = Storage.get_cell storage k in
let s = Commit_hash.to_string (Option.default Commit_hash.zero commit.parent) in
assert (String.length s = 32);
B.write_string s buf 0;
let buf = Storage.get_cell storage i in
B.set_index buf 28 commit.index;
B.set_index buf 24 (Option.default Index.zero prev);
B.set_index buf 20 commit.info;
B.write_string (String.make 20 '\000') buf 0;
let read_commit, read_prev = read storage i in
if commit <> read_commit || prev <> read_prev then begin
Log.fatal "copy check error!";
Log.fatal " written: %a@ %a"
pp commit (Format.option Index.pp) prev;
Log.fatal " read: %a@ %a"
pp read_commit (Format.option Index.pp) read_prev;
assert false
end
let index_2 = Index.Unsafe.of_int 2
let write storage commit =
let prev = Storage.get_last_root_index storage in
match Storage.new_indices storage 3 with
| Error _ as e -> Lwt.return e
| Ok i ->
let i = Index.(+) i index_2 in
write_at storage ~at:i ~prev commit;
Storage.set_last_root_index storage (Some i);
let+= () = Storage.commit storage in
Ok i