vantabot/store.ml

29 lines
815 B
OCaml

(** main entry top-level function: `register_nick` *)
module Rwdb = Dokeysto.Db.RW
let get_db (db : string) : Rwdb.t =
let open Unix in
let already_exists =
try access db [F_OK]; true
with Unix_error(ENOENT,_,_) -> false in
if already_exists
then Rwdb.open_existing db
else Rwdb.create db
let release_db (h : Rwdb.t) : unit = Rwdb.sync h; Rwdb.close h
let try_finalize f x finally y = let res = try f x with exn -> finally y; raise exn in finally y; res
let inc_nick_value (h : Rwdb.t) (nick : string) : unit =
if Rwdb.mem h nick
then let n = int_of_string (Rwdb.find h nick) in
Rwdb.replace h nick (string_of_int (n+1))
else Rwdb.add h nick "1"
let register_nick (db : string) (nick : string) =
let h = get_db db in
try_finalize (inc_nick_value h) nick
release_db h