package aches-lwt

  1. Overview
  2. Docs

Source file functors.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
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2022 Nomadic Labs, <contact@nomadic-labs.com>               *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

(* Main functor that is exported to the outside *)
module Make
  (R: Rache.TRANSFER)
: Sigs.MAP with type key = R.key
= struct

  type key = R.key
  type 'a promise = {
    p : 'a Lwt.t;
    mutable binded : bool;
  }
  let cancel { p; binded } =
    if binded then
      ()
    else
      Lwt.cancel p
  let of_lwt p = { p; binded = false }
  let to_lwt { p; _ } = p
  type 'a t = 'a promise R.t

  let create n =
    (* Promises are resources which are destroyed by cancelation *)
    R.create (fun _k p -> cancel p) n
  let put c k r = R.put c k (of_lwt r)
  let take c k = Option.map to_lwt (R.take c k)
  let take_all c = List.map (fun (k, r) -> (k, to_lwt r)) (R.take_all c)
  let take_some c f =
    List.map
      (fun (k, r) -> (k, to_lwt r))
      (* You can't meaningfully test on the promise so we only offer the key *)
      (R.take_some c (fun k _p -> f k))

  let bind c k f =
    R.borrow c k
      (fun r ->
        r.binded <- true;
        let p = to_lwt r in
        let q = Lwt.bind p f in
        (* NOTE: you might be thinking that the code is missing
           [r.binded <- false] to signify the promise being returned. This
           could happen when [q] resolves (which in some sense is when the
           function [f] finally "returns"). (Or actually when [f] is called.)
           However, note that in this (these) cases, the promise [p] is already
           resolved and [Lwt.cancel] is a no-op. *)
        q
      )

  let bind_or_put c k make f =
    match bind c k f with
    | Some q -> q
    | None ->
        let p = make k in
        let r = of_lwt p in
        R.put c k r;
        r.binded <- true;
        let q = Lwt.bind p f in
        q

  let fold f c acc =
    R.fold
      (fun k r acc ->
        r.binded <- true;
        let open Lwt.Syntax in
        let* acc = acc in
        let* x = to_lwt r in
        f k x acc
      )
      c
      (Lwt.return acc)

  let fold_oldest_first f c acc =
    R.fold_oldest_first
      (fun k r acc ->
        r.binded <- true;
        let open Lwt.Syntax in
        let* acc = acc in
        let* x = to_lwt r in
        f k x acc
      )
      c
      (Lwt.return acc)

  let remove c k = R.remove c k
  let clear c = R.clear c
  let filter c f = R.filter c (fun k _r -> f k)
  let length c = R.length c
  let capacity c = R.capacity c

end

module Make_option
  (R: Rache.TRANSFER)
: Sigs.MAP_OPTION with type key = R.key
= struct

  type key = R.key
  type 'a promise = {
    p : 'a option Lwt.t;
    mutable binded : bool;
    on_take : unit Lwt.t;
    resolve_on_take : unit Lwt.u;
  }
  let cancel { p; binded; _ } =
    if binded then
      ()
    else
      Lwt.cancel p
  let of_lwt p =
    let (on_take, resolve_on_take) = Lwt.wait () in
    { p; binded = false; on_take; resolve_on_take; }
  let to_lwt { p; _ } = p
  let monitor_none c k { p; on_take; _ } =
    Lwt.on_failure
      (Lwt.choose [
        on_take; (* on_take is successfully resolved when the promise is taken
                    out of the cache so this callback is called and can be
                    garbage collected. *)
        (Lwt.try_bind
          (fun () -> p)
          (function None -> raise Exit | Some _ -> Lwt.return_unit)
          (fun (_ : exn) -> Lwt.return_unit))
      ])
      (fun (_ : exn) -> R.remove c k)
  type 'a t = 'a promise R.t

  let create n =
    (* Promises are resources which are destroyed by cancelation *)
    R.create (fun _k p -> cancel p) n
  let put c k p =
    match Lwt.state p with
    | Lwt.Return None -> ()
    | _ ->
      let r = of_lwt p in
      monitor_none c k r;
      R.put c k r
  let take c k =
    match R.take c k with
    | None -> None
    | Some r ->
        Lwt.wakeup r.resolve_on_take ();
        Some (to_lwt r)
  let take_all c =
    List.map
      (fun (k, r) ->
        Lwt.wakeup r.resolve_on_take ();
        (k, to_lwt r))
      (R.take_all c)
  let take_some c f =
    List.map
      (fun (k, r) ->
        Lwt.wakeup r.resolve_on_take ();
        (k, to_lwt r))
      (* You can't meaningfully test on the promise so we only offer the key *)
      (R.take_some c (fun k _p -> f k))

  let bind c k f =
    R.borrow c k
      (fun r ->
        r.binded <- true;
        let p = to_lwt r in
        let q = Lwt.bind p f in
        (* NOTE: you might be thinking that the code is missing
           [r.binded <- false] to signify the promise being returned. This
           could happen when [q] resolves (which in some sense is when the
           function [f] finally "returns"). (Or actually when [f] is called.)
           However, note that in this (these) cases, the promise [p] is already
           resolved and [Lwt.cancel] is a no-op. *)
        q
      )

  let bind_or_put c k make f =
    match bind c k f with
    | Some q -> q
    | None ->
        let p = make k in
        match Lwt.state p with
        | Lwt.Return None -> f None
        | _ ->
          let r = of_lwt p in
          monitor_none c k r;
          R.put c k r;
          r.binded <- true;
          let q = Lwt.bind p f in
          q

  let fold f c acc =
    R.fold
      (fun k r acc ->
        r.binded <- true;
        let open Lwt.Syntax in
        let* acc = acc in
        let* x = to_lwt r in
        match x with
        | None -> Lwt.return acc
        | Some x -> f k x acc
      )
      c
      (Lwt.return acc)

  let fold_oldest_first f c acc =
    R.fold_oldest_first
      (fun k r acc ->
        r.binded <- true;
        let open Lwt.Syntax in
        let* acc = acc in
        let* x = to_lwt r in
        match x with
        | None -> Lwt.return acc
        | Some x -> f k x acc
      )
      c
      (Lwt.return acc)

  let remove c k = R.remove c k
  let clear c = R.clear c
  let filter c f = R.filter c (fun k _r -> f k)
  let length c = R.length c
  let capacity c = R.capacity c

end

module Make_result
  (R: Rache.TRANSFER)
: Sigs.MAP_RESULT with type key = R.key
= struct

  type key = R.key
  type ('a, 'err) promise = {
    p : ('a, 'err) result Lwt.t;
    mutable binded : bool;
    on_take : unit Lwt.t;
    resolve_on_take : unit Lwt.u;
  }
  let cancel { p; binded; _ } =
    if binded then
      ()
    else
      Lwt.cancel p
  let of_lwt p =
    let (on_take, resolve_on_take) = Lwt.wait () in
    { p; binded = false; on_take; resolve_on_take; }
  let to_lwt { p; _ } = p
  let monitor_error c k { p; on_take; _ } =
    Lwt.on_failure
      (Lwt.choose [
        on_take; (* on_take is successfully resolved when the promise is taken
                    out of the cache so this callback is called and can be
                    garbage collected. *)
        (Lwt.try_bind
          (fun () -> p)
          (function Error _ -> raise Exit | Ok _ -> Lwt.return_unit)
          (fun (_ : exn) -> Lwt.return_unit))
      ])
      (fun (_ : exn) -> R.remove c k)
  type ('a, 'err) t = ('a, 'err) promise R.t

  let create n =
    (* Promises are resources which are destroyed by cancelation *)
    R.create (fun _k p -> cancel p) n
  let put c k p =
    match Lwt.state p with
    | Lwt.Return (Error _) -> ()
    | _ ->
      let r = of_lwt p in
      monitor_error c k r;
      R.put c k r
  let take c k =
    match R.take c k with
    | None -> None
    | Some r ->
        Lwt.wakeup r.resolve_on_take ();
        Some (to_lwt r)
  let take_all c =
    List.map
      (fun (k, r) ->
        Lwt.wakeup r.resolve_on_take ();
        (k, to_lwt r))
      (R.take_all c)
  let take_some c f =
    List.map
      (fun (k, r) ->
        Lwt.wakeup r.resolve_on_take ();
        (k, to_lwt r))
      (* You can't meaningfully test on the promise so we only offer the key *)
      (R.take_some c (fun k _p -> f k))

  let bind c k f =
    R.borrow c k
      (fun r ->
        r.binded <- true;
        let p = to_lwt r in
        let q = Lwt.bind p f in
        (* NOTE: you might be thinking that the code is missing
           [r.binded <- false] to signify the promise being returned. This
           could happen when [q] resolves (which in some sense is when the
           function [f] finally "returns"). (Or actually when [f] is called.)
           However, note that in this (these) cases, the promise [p] is already
           resolved and [Lwt.cancel] is a no-op. *)
        q
      )

  let bind_or_put c k make f =
    match bind c k f with
    | Some q -> q
    | None ->
        let p = make k in
        match Lwt.state p with
        | Lwt.Return (Error _ as error) -> f error
        | _ ->
          let r = of_lwt p in
          monitor_error c k r;
          R.put c k r;
          r.binded <- true;
          let q = Lwt.bind p f in
          q

  let fold f c acc =
    R.fold
      (fun k r acc ->
        r.binded <- true;
        let open Lwt.Syntax in
        let* acc = acc in
        let* x = to_lwt r in
        match x with
        | Error _ -> Lwt.return acc
        | Ok x -> f k x acc
      )
      c
      (Lwt.return acc)

  let fold_oldest_first f c acc =
    R.fold_oldest_first
      (fun k r acc ->
        r.binded <- true;
        let open Lwt.Syntax in
        let* acc = acc in
        let* x = to_lwt r in
        match x with
        | Error _ -> Lwt.return acc
        | Ok x -> f k x acc
      )
      c
      (Lwt.return acc)

  let remove c k = R.remove c k
  let clear c = R.clear c
  let filter c f = R.filter c (fun k _r -> f k)
  let length c = R.length c
  let capacity c = R.capacity c

end
OCaml

Innovation. Community. Security.