Source file data.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
(*
 * Copyright (c) 2021 Tarides <contact@tarides.com>
 * Copyright (c) 2021 Gabriel Belouze <gabriel.belouze@ens.psl.eu>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *)

module type K = sig
  type t
  (** The type for keys. *)

  type input_key

  val size : int

  val min : t (* minimal key *)

  val of_input : input_key -> t

  val to_input : t -> input_key

  val empty : t

  val equal : t -> t -> bool
  (** The equality function for keys. *)

  val compare : t -> t -> int

  val set : marker:(unit -> unit) -> bytes -> off:int -> t -> unit

  val get : bytes -> off:int -> t

  val dump : t -> string

  val pp : Format.formatter -> t -> unit
end

module type V = sig
  type t
  (** The type for values. *)

  type input_value

  val size : int

  val of_input : input_value -> t

  val to_input : t -> input_value

  val set : marker:(unit -> unit) -> bytes -> off:int -> t -> unit

  val get : bytes -> off:int -> t

  val pp : Format.formatter -> t -> unit
end

module type Entry = sig
  type input_key

  type input_value

  module Key : K with type input_key = input_key

  module Value : V with type input_value = input_value
end

module Make : functor (InKey : Input.Key) (InValue : Input.Value) ->
  Entry with type input_key = InKey.t and type input_value = InValue.t =
functor
  (InKey : Input.Key)
  (InValue : Input.Value)
  ->
  struct
    type input_key = InKey.t

    type input_value = InValue.t

    module Key = struct
      type t = string

      type input_key = InKey.t

      let size = InKey.encoded_size

      let empty = ""

      let min = Utils.min_key size

      let equal = String.equal

      let compare = String.compare

      let set ~marker buff ~off t =
        marker ();
        Bytes.blit_string t 0 buff off size

      let get buff ~off = Bytes.sub_string buff off size

      let of_input k =
        let ret = InKey.encode k in
        assert (size = String.length ret);
        ret

      let to_input = InKey.decode

      let dump s = s

      open Fmt

      let ascii =
        String.map (fun c ->
            let code = Char.code c in
            if 0x20 <= code && code <= 0x7E then c else '.')

      let pp ppf s = pf ppf "%a" (string |> styled (`Bg `Green) |> styled `Reverse) (ascii s)
    end

    module Value = struct
      type t = string

      type input_value = InValue.t

      let size = InValue.encoded_size

      let set ~marker buff ~off t =
        marker ();
        Bytes.blit_string t 0 buff off size

      let get buff ~off = Bytes.sub_string buff off size

      let of_input v =
        let ret = InValue.encode v in
        assert (size = String.length ret);
        ret

      let to_input = InValue.decode

      open Fmt

      let pp = string |> styled (`Bg `Blue) |> styled `Reverse
    end
  end