-
Notifications
You must be signed in to change notification settings - Fork 125
/
common.ml
179 lines (140 loc) · 4.16 KB
/
common.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
(*
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/>.
*)
(*
@author1 Henri Binsztok,
@author2 Gregoire Makridis
**)
(** {6 DATA} *)
module type DATA =
sig
(**
TODO: documentation
*)
type t
val empty : t
(**
returns a debug string (summarized)
*)
val to_string : t -> string
(**
returns the full contents as string
*)
val get_string : t -> string
(** {6 Projections} *)
(**
Mathieu Wed Mar 16 11:14:27 CET 2011
Old situation:
Projections was there for historical design. There was 2 different
representation of datas, one is for being used when we manipulate the
database in memory ([t]), the other representation is used by the IO
module, for writing and reading datas in the physical database (db-files).
Simplification:
We will try now to simplify this design, and to merge the 2 representations
for avoiding the cost of projections.
We will apply the following steps to the code :
1)
The 2 fonctions [write/read] of all modules implementing this interface
will be replaced by the [identity] (module by module)
2)
The interface will be changed, so that t = io, and the 2 fonction exported
as the identity
3)
The rest of the code using [read/write] will be changed for removing the
call to these functions. They will become unused.
4)
The interface will be changed, removing the function [write/read], and the type [io]
*)
(** {6 Index} *)
(**
Indexation. Build a dictionnary entry.
Count how many time a word appears in a data.
*)
val index_fun : t -> int StringMap.t
end
module type COMMON =
sig
(**
A private type for representing positive int.
Used for Eid, Uid, Revision.
*)
type t
(**
Used to be [t -> t option], returning [None]
if [t] is max_int.
But nobody was handling the case [None] ([assert false])
so, now the assert is in this function.
*)
val succ : t -> t
(**
Return the previous index, or [None] if the index is [0]
*)
val pred : t -> t option
val make : int -> t
external value : t -> int = "%identity"
val to_string : t -> string
val equal : t -> t -> bool
val compare : t -> t -> int
val max : t -> t -> t
val min : t -> t -> t
end
(*
Shared implementation for :
- eid
- uid
- revision
Do not coerce there into COMMON,
it would makes eid, uid and revision unifiable.
*)
module IntCommon =
struct
(* if you patch this, make sure that this module doesn't
* use needlessly the SLOW polymorphic comparison
* operators *)
type t = int
(*
About the two following functions
Nobody catches the exception:
{[
exception CommonMake
]}
It is replaced by an assert false.
People using succ and pred used to write stuff like:
{[
match succ uid with
| Some uid -> uid
| _ -> assert false
]}
So, the assert false is now there directly, and no option is allocated.
*)
let succ i =
if i < max_int
then succ i
else assert false
let pred i =
if i > 0
then Some (pred i)
else None
let make i =
(* checking that i <= max_int seems somewhat useless *)
if i >= 0 then i
else assert false
external value : t -> int = "%identity"
let to_string = string_of_int
let equal : int -> int -> bool = (=)
let compare : int -> int -> int = Pervasives.compare
(* Pervasives.max and Pervasives.min are not specialized when coerced
* so we redefine them with a specialized type *)
let max (i:int) j = if i >= j then i else j
let min (i:int) j = if i <= j then i else j
end