package granary

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

Source file persist.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
open Lwt.Syntax
module Row = Granary_encoding.Row
module S = Granary_store.Store

(** Well-known key under which columnar store data is persisted within the
    table's tree_id.  A single zero byte avoids collision with any real
    application key. *)
let data_key = Bytes.make 1 '\x00'

(** [save tx tree_id store] persists [store] to the B-tree at [tree_id].
    The entire store is serialised via {!Col_store.encode} and stored as a
    single value; the B-tree overflow chain handles values up to 1 GiB. *)
let save (tx : S.rw S.txn) (tree_id : S.tree_id) (store : Col_store.t) : unit Lwt.t =
  let encoded = Col_store.encode store in
  S.put tx tree_id data_key encoded
;;

(** [load tx tree_id schema] reads a previously persisted columnar store from
    the B-tree at [tree_id].  Returns [None] when no data has been persisted
    yet (fresh table).  Raises [Failure] on corrupt data.  Works with both RO
    and RW transactions. *)
let load (tx : _ S.txn) (tree_id : S.tree_id) (schema : Row.column list)
  : Col_store.t option Lwt.t
  =
  let* data = S.get tx tree_id data_key in
  match data with
  | None -> Lwt.return None
  | Some buf ->
    let store = Col_store.decode schema buf in
    Lwt.return (Some store)
;;