-
Notifications
You must be signed in to change notification settings - Fork 125
/
fileIo.ml
468 lines (367 loc) · 12.8 KB
/
fileIo.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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
(*
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
#<Debugvar:DEBUG_DB$flag "io">
let debug fmt =
#<If> Printf.fprintf stdout ("[35m[FileIo][0m"^^fmt^^"\n%!")
#<Else> Printf.ifprintf stderr ("[35m[FileIo][0m"^^fmt^^"\n%!")
#<End>
let print fmt =
Printf.fprintf stdout ("[35m[FileIo] [0m"^^fmt^^"\n%!")
let error fmt =
Printf.fprintf stderr ("[35m[FileIo] [31m"^^fmt^^"[0m\n%!")
(* -- *)
module Make = functor (WR:IoSig.S) ->
struct
(* keep it for the export of the exception EOF *)
include WR
exception Corruption
type t = {
mutable is_open : bool;
location : string;
node_filename : string;
uid_filename : string;
uid_rev_filename : string;
timestamp_filename : string;
trans_filename : string;
flags_filename : string;
db_state_filename : string;
config_filename : string;
lock_filename : string;
mutable node_chan : WR.chan;
mutable uid_chan : WR.chan;
mutable uid_rev_chan : WR.chan;
mutable trans_chan : WR.chan;
mutable timestamp_chan : WR.chan;
mutable flags_chan : WR.chan;
mutable db_state_chan : WR.chan;
mutable config_chan : WR.chan;
}
type mode = Create | Append | ReadOnly
type file = Node | Uid | Uid_rev | Timestamp | Flags | Trans | Db_state | Config | Lock
type write = WChar of char | WInt of int | WString of string | WFloat of float | WInt32 of int32 | WInt64 of int64
type read = RChar | RInt | RString | RFloat | RInt32 | RInt64
let node_file = "_node_file"
let uid_file = "_uid_file"
let uid_rev_file = "_uid_rev_file"
let timestamp_file = "_timestamps_file"
let flags_file = "_flags_file"
let trans_file = "_trans_file"
let db_state_file= "_db_state"
let config_file= "_config"
let lock_file = "_lock"
let create_filename ?(suffix="") file kind =
Printf.sprintf "%s%s%s" file kind suffix
let make_channel = function
| Create -> WR.make_chan_create
| Append -> WR.make_chan_append
| ReadOnly -> WR.make_chan_readonly
let make ?(_extension) mode file =
let cf = create_filename file in
let cw f =
(match mode with
| Create ->
if Sys.file_exists f then
(#<If> Logger.warning "New db, purge: deleting file %s" f #<End>;
Sys.remove f);
make_channel Create f
| mode -> make_channel mode f)
in
let node_filename = cf node_file in
let uid_filename = cf uid_file in
let uid_rev_filename = cf uid_rev_file in
let timestamp_filename = cf timestamp_file in
let trans_filename = cf trans_file in
let flags_filename = cf flags_file in
let db_state_filename = cf db_state_file in
let config_filename = cf config_file in
let node_chan = cw node_filename in
let uid_chan = cw uid_filename in
let uid_rev_chan = cw uid_rev_filename in
let trans_chan = cw trans_filename in
let timestamp_chan = cw timestamp_filename in
let flags_chan = cw flags_filename in
let db_state_chan = cw db_state_filename in
let config_chan = cw config_filename in
{
is_open = true;
location = file;
node_filename = node_filename;
uid_filename = uid_filename;
uid_rev_filename = uid_rev_filename;
timestamp_filename = timestamp_filename;
trans_filename = trans_filename;
flags_filename = flags_filename;
db_state_filename = db_state_filename;
config_filename = config_filename;
lock_filename = cf lock_file;
node_chan = node_chan;
uid_chan = uid_chan;
uid_rev_chan = uid_rev_chan;
trans_chan = trans_chan;
timestamp_chan = timestamp_chan;
flags_chan = flags_chan;
db_state_chan = db_state_chan;
config_chan = config_chan;
}
let get_name entity = function
| Node -> entity.node_filename
| Uid -> entity.uid_filename
| Uid_rev -> entity.uid_rev_filename
| Timestamp -> entity.timestamp_filename
| Trans -> entity.trans_filename
| Flags -> entity.flags_filename
| Db_state -> entity.db_state_filename
| Config -> entity.config_filename
| Lock -> entity.lock_filename
let get_channel entity file =
match file with
| Node -> entity.node_chan
| Uid -> entity.uid_chan
| Uid_rev -> entity.uid_rev_chan
| Timestamp -> entity.timestamp_chan
| Trans -> entity.trans_chan
| Flags -> entity.flags_chan
| Db_state -> entity.db_state_chan
| Config -> entity.config_chan
| Lock -> assert false
let get_location entity = entity.location
(* todo rename *)
let empty_file entity file =
let foc = get_channel entity file in
match file with
| Db_state
| Trans
| Config ->
WR.erase_file foc
| _ -> assert false
let output entity file =
let f = get_channel entity file in
WR.output f
let add_int entity file i =
let f = get_channel entity file in
WR.add_int f i
let add_char entity file c =
let f = get_channel entity file in
WR.add_char f c
let add_string entity file c =
let f = get_channel entity file in
WR.add_string f c
let add_float entity file c =
let f = get_channel entity file in
WR.add_float f c
let add_int32 entity file i =
let f = get_channel entity file in
WR.add_int32 f i
let add_int64 entity file i =
let f = get_channel entity file in
WR.add_int64 f i
let add entity file ?(output=false) lst =
let f = get_channel entity file in
List.iter (function
WChar c -> WR.add_char f c
| WInt i -> WR.add_int f i
| WString s -> WR.add_string f s
| WFloat s -> WR.add_float f s
| WInt32 i -> WR.add_int32 f i
| WInt64 i -> WR.add_int64 f i
) lst;
if output then WR.output f
let seek_out entity file pos =
let f = get_channel entity file in
WR.seek_out f pos
let position_out entity file =
let f = get_channel entity file in
WR.position_out f
let read_int entity file =
let f = get_channel entity file in
WR.read_int f
let read_char entity file =
let f = get_channel entity file in
WR.read_char f
let read_string entity file =
let f = get_channel entity file in
WR.read_string f
let read_float entity file =
let f = get_channel entity file in
WR.read_float f
let read_int32 entity file =
let f = get_channel entity file in
WR.read_int32 f
let read_int64 entity file =
let f = get_channel entity file in
WR.read_int64 f
let read entity file lst =
let f = get_channel entity file in
List.tail_map (function
RChar -> WChar (WR.read_char f)
| RInt -> WInt (WR.read_int f)
| RString -> WString (WR.read_string f)
| RFloat -> WFloat (WR.read_float f)
| RInt32 -> WInt32 (WR.read_int32 f)
| RInt64 -> WInt64 (WR.read_int64 f)
) lst
let position_in entity file =
let f = get_channel entity file in
WR.position_in f
let length entity file =
let f = get_channel entity file in
WR.length f
let seek_in entity file =
let f = get_channel entity file in
WR.seek_in f
let get_list_chan entity =
[entity.node_chan; entity.uid_chan; entity.uid_rev_chan; entity.timestamp_chan; entity.flags_chan; entity.config_chan; entity.db_state_chan; entity.trans_chan]
let get_reset_list entity =
[entity.node_chan; entity.uid_chan; entity.uid_rev_chan; (*entity.timestamp_chan;*) (*entity.trans_chan*)]
let close entity =
entity.is_open <- false;
List.iter WR.close (get_list_chan entity)
let is_open entity = entity.is_open
let reset_files entity =
List.iter WR.reset_file (get_reset_list entity)
let set_size entity file =
WR.truncate_file (get_channel entity file)
let copy_file entity file ?location ext =
let chan = get_channel entity file in
let get_name_ = function
| Node -> node_file
| Uid -> uid_file
| Uid_rev -> uid_rev_file
| Timestamp -> timestamp_file
| Flags -> flags_file
| Trans -> trans_file
| Db_state -> db_state_file
| Config -> config_file
| Lock -> assert false in
(* create new name *)
let new_name = get_name_ file in
let new_name = (new_name ^ ext) in
let new_name = Option.default_map (entity.location ^ new_name) (fun x -> x ^ new_name) location in
(* flush the file *)
WR.output chan;
let name = get_name entity file in
(* copy the file *)
let res = File.copy ~force:true name new_name in
if res <> 0 then failwith "No cp"
(* fichiers a unique ecriture / lecture *)
let single_write filename lst =
let f = make_channel Append filename in
List.iter (function
WChar c -> WR.add_char f c
| WInt i -> WR.add_int f i
| WString s -> WR.add_string f s
| WFloat s -> WR.add_float f s
| WInt32 i -> WR.add_int32 f i
| WInt64 i -> WR.add_int64 f i
) lst;
WR.output f;
WR.close f
let single_read filename lst =
let f = make_channel Append filename in
let res = List.tail_map (function
RChar -> WChar (WR.read_char f)
| RInt -> WInt (WR.read_int f)
| RString -> WString (WR.read_string f)
| RFloat -> WFloat (WR.read_float f)
| RInt32 -> WInt32 (WR.read_int32 f)
| RInt64 -> WInt64 (WR.read_int64 f)
) lst in
WR.close f;
res
let get_single_length filename =
let f = make_channel Append filename in
let res = WR.length f in
WR.close f;
res
(* fichiers a usage "unique" *)
type unik = { u_filename : string; unik_chan : WR.chan }
let create_unik ?(mode=Create) filename =
{ u_filename = filename;
unik_chan = make_channel mode filename;
}
let add_unik kentity ?(output=false) lst =
let f = kentity.unik_chan in
List.iter (function
WChar c -> WR.add_char f c
| WInt i -> WR.add_int f i
| WString s -> WR.add_string f s
| WFloat s -> WR.add_float f s
| WInt32 i -> WR.add_int32 f i
| WInt64 i -> WR.add_int64 f i
) lst;
if output then WR.output f
let read_unik kentity lst =
let f = kentity.unik_chan in
List.tail_map (function
RChar -> WChar (WR.read_char f)
| RInt -> WInt (WR.read_int f)
| RString -> WString (WR.read_string f)
| RFloat -> WFloat (WR.read_float f)
| RInt32 -> WInt32 (WR.read_int32 f)
| RInt64 -> WInt64 (WR.read_int64 f)
) lst
let read_unik_int kentity =
let f = kentity.unik_chan in
WR.read_int f
let read_unik_char kentity =
let f = kentity.unik_chan in
WR.read_char f
let read_unik_string kentity =
let f = kentity.unik_chan in
WR.read_string f
let read_unik_float kentity =
let f = kentity.unik_chan in
WR.read_float f
let read_unik_int32 kentity =
let f = kentity.unik_chan in
WR.read_int32 f
let read_unik_int64 kentity =
let f = kentity.unik_chan in
WR.read_int64 f
let output_unik kentity =
WR.output kentity.unik_chan
let close_unik kentity =
WR.output kentity.unik_chan;
WR.close kentity.unik_chan
let position_out_unik kentity =
WR.position_out kentity.unik_chan
let seek_out_unik kentity =
WR.seek_out kentity.unik_chan
(* move unik file to permanent one *)
let mv entity kfile file =
let chan = get_channel entity file in
(* je vide l'autre fichier *)
(* WR.erase_file chan;*)
(* je ferme le fichier temporaire *)
close_unik kfile;
(* et je move *)
let res = File.mv ~force:true kfile.u_filename (get_name entity file) in
if res <> 0 then failwith "No mv";
WR.reload chan
module Channel =
struct
type channel = WR.chan
let get = get_channel
include WR
let add f = function
| WChar c -> add_char f c
| WInt i -> add_int f i
| WString s -> add_string f s
| WFloat s -> add_float f s
| WInt32 i -> add_int32 f i
| WInt64 i -> add_int64 f i
end
end