caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] [ANN] tjr_btree 0.1.0 (initial release)
@ 2017-05-04  8:24 Tom Ridge
  0 siblings, 0 replies; only message in thread
From: Tom Ridge @ 2017-05-04  8:24 UTC (permalink / raw)
  To: caml-list, mirageos-devel

[-- Attachment #1: Type: text/plain, Size: 1639 bytes --]

tjr_btree is a B-tree library written in OCaml. The examples include a
simple on-disk key-value store.

https://github.com/tomjridge/tjr_btree

Example:

----
(** A simple example of a kv store. *)

open Small_string.O
(* SS is now an alias for Small_string *)

open Ss_ss_map_on_fd

(* filename *)
let fn = Default.default_filename

(* construct keys and values from an int *)
let k x = "k"^(string_of_int x) |> SS.of_string
let v x = "v"^(string_of_int x) |> SS.of_string

(* create and init store, write some values, and close *)
let do_write () = (
  print_endline "Writing...";
  (* create and initialize *)
  let s = ref (from_file ~fn ~create:true ~init:true) in
  (* get map operations *)
  let map_ops = imperative_map_ops s in
  (* write values *)
  for x=1 to 1000 do
    (* TODO this would be much faster if we used insert_many *)
    map_ops.insert (k x) (v x);
  done;
  (* close *)
  close !s
)

(* open store, delete some values, and close *)
let do_delete () = (
  print_endline "Deleting...";
  let s = ref (from_file ~fn ~create:false ~init:false) in
  let map_ops = imperative_map_ops s in
  for x=100 to 200 do
    map_ops.delete (k x);
  done;
  close !s
)

(* open store and check whether various keys and values are correct *)
let do_check () = (
  print_endline "Checking...";
  let s = ref (from_file ~fn ~create:false ~init:false) in
  let map_ops = imperative_map_ops s in
  assert(map_ops.find (SS.of_string("k100")) = None);
  assert(map_ops.find (SS.of_string("k1000")) =
Some(SS.of_string("v1000")));
  close !s
)

(* actually execute the above *)
let _ = (
  do_write();
  do_delete();
  do_check()
)
----

[-- Attachment #2: Type: text/html, Size: 2545 bytes --]

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2017-05-04  8:25 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-04  8:24 [Caml-list] [ANN] tjr_btree 0.1.0 (initial release) Tom Ridge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).