package irmin-containers

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file counter.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
(*
 * Copyright (c) 2020 KC Sivaramakrishnan <kc@kcsrk.info>
 * Copyright (c) 2020 Anirudh Sunder Raj <anirudh6626@gmail.com>
 *
 * 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.
 *)

open! Import

module Counter : Irmin.Contents.S with type t = int64 = struct
  type t = int64

  let t = Irmin.Type.int64
  let merge = Irmin.Merge.(option counter)
end

module type S = sig
  module Store : Irmin.KV

  val inc :
    ?by:int64 -> ?info:Store.Info.f -> path:Store.path -> Store.t -> unit Lwt.t

  val dec :
    ?by:int64 -> ?info:Store.Info.f -> path:Store.path -> Store.t -> unit Lwt.t

  val read : path:Store.path -> Store.t -> int64 Lwt.t
end

module Make (Backend : Irmin.KV_maker) = struct
  module Store = Backend.Make (Counter)

  let empty_info = Store.Info.none

  let modify by info t path fn =
    Store.find t path >>= function
    | None -> Store.set_exn ~info t path (fn 0L by)
    | Some v -> Store.set_exn ~info t path (fn v by)

  let inc ?(by = 1L) ?(info = empty_info) ~path t =
    modify by info t path (fun x by -> Int64.add x by)

  let dec ?(by = 1L) ?(info = empty_info) ~path t =
    modify by info t path (fun x by -> Int64.sub x by)

  let read ~path t = Store.find t path >|= function None -> 0L | Some v -> v
end

module FS = Make (Irmin_unix.FS.KV)
module Mem = Make (Irmin_mem.KV)
OCaml

Innovation. Community. Security.