Skip to content
This repository
tree: 8e30b1e52f
Fetching contributors…

Cannot retrieve contributors at this time

file 292 lines (267 sloc) 9.065 kb
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
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)

(**
Bson: Module to allow the creation of BSON values in strings.

You use module Append to create the values:

let buf = Append.init ?hint () in
Append.int buf 123;
<etc...>
Append.finish buf

Use Iterator to scan BSON values:

let iter = Iterator.init buf;
<or...>
let iter = Iterator.from_buffer str;
let code = Iterator.next iter in
let key = Iterator.key iter in
match code with
| c when c = el_int ->
let i = Iterator.int iter in
<...>

There is a second Iterator module, IteratorSS which is founded on
BaseStringSlice which may be more efficient on deeply-nested
BSON objects (sub is a unit-time operation but note that the
returned sub-string will not be a copy, updating the sub-string
will cause the parent string to be altered, too).

**)

module type S_sig =
  sig
    type t
    val empty : t
    val length : t -> int
    val get : t -> int -> char
    val set : t -> int -> char -> unit
    val create : int -> t
    val make : int -> char -> t
    val copy : t -> t
    val sub : t -> int -> int -> t
    val fill : t -> int -> int -> char -> unit
    val blit : t -> int -> t -> int -> int -> unit
    val concat : t -> t list -> t
    val iter : (char -> unit) -> t -> unit
    val escaped : t -> t
    val index : t -> char -> int
    val rindex : t -> char -> int
    val index_from : t -> int -> char -> int
    val rindex_from : t -> int -> char -> int
    val contains : t -> char -> bool
    val contains_from : t -> int -> char -> bool
    val rcontains_from : t -> int -> char -> bool
    val uppercase : t -> t
    val lowercase : t -> t
    val capitalize : t -> t
    val uncapitalize : t -> t
    val compare : t -> t -> int
    val unsafe_get : t -> int -> char
    val unsafe_set : t -> int -> char -> unit
    val unsafe_blit : t -> int -> t -> int -> int -> unit
    val unsafe_fill : t -> int -> int -> char -> unit
    val to_string : t -> string
    val of_string : string -> t
    val export : t -> string * int * int
    val import : string * int * int -> t
    val widen : t -> unit
    val normalize : t -> t
    val real_size : t -> int
    val set_size : t -> int -> t
    val rebase : t -> unit
    val unsafe_sub : t -> int -> int -> t
  end

module type FILLBUF =
  sig
    type t
    val get : t -> int -> char
    val set : t -> int -> char -> unit
  end

module FillbufF :
  functor (S : FILLBUF) ->
    sig
      val lei32 : S.t -> int -> int -> unit
      val bei32 : S.t -> int -> int -> unit
      val led : S.t -> int -> float -> unit
      val bed : S.t -> int -> float -> unit
      val lei32l : S.t -> int -> int32 -> unit
      val bei32l : S.t -> int -> int32 -> unit
      val lei64L : S.t -> int -> int64 -> unit
      val bei64L : S.t -> int -> int64 -> unit
      val ldi32 : S.t -> int -> int
      val bdi32 : S.t -> int -> int
      val ldi32l : S.t -> int -> int32
      val bdi32l : S.t -> int -> int32
      val ldi64L : S.t -> int -> int64
      val bdi64L : S.t -> int -> int64
      val ldd : S.t -> int -> float
      val bdd : S.t -> int -> float
    end

module FillbufString :
  sig
    val lei32 : string -> int -> int -> unit
    val bei32 : string -> int -> int -> unit
    val led : string -> int -> float -> unit
    val bed : string -> int -> float -> unit
    val lei32l : string -> int -> int32 -> unit
    val bei32l : string -> int -> int32 -> unit
    val lei64L : string -> int -> int64 -> unit
    val bei64L : string -> int -> int64 -> unit
    val ldi32 : string -> int -> int
    val bdi32 : string -> int -> int
    val ldi32l : string -> int -> int32
    val bdi32l : string -> int -> int32
    val ldi64L : string -> int -> int64
    val bdi64L : string -> int -> int64
    val ldd : string -> int -> float
    val bdd : string -> int -> float
  end

val add_le_int32 : Buf.buf -> int -> unit
val add_be_int32 : Buf.buf -> int -> unit
val add_le_d : Buf.buf -> float -> unit
val add_be_d : Buf.buf -> float -> unit
val add_le_int32l : Buf.buf -> int32 -> unit
val add_be_int32l : Buf.buf -> int32 -> unit
val add_le_int64L : Buf.buf -> int64 -> unit
val add_be_int64L : Buf.buf -> int64 -> unit

module S : S_sig with type t = string
module SS : S_sig with type t = BaseStringSlice.t

val el_eoo : char
val el_double : char
val el_string : char
val el_object : char
val el_array : char
val el_bindata : char
val el_undefined : char
val el_oid : char
val el_bool : char
val el_date : char
val el_null : char
val el_minkey : char
val el_maxkey : char
val el_regex : char
val el_dbref : char
val el_code : char
val el_symbol : char
val el_codewscope : char
val el_int : char
val el_timestamp : char
val el_long : char
val el_minkey : char
val el_maxkey : char
val st_bin_binary : char
val st_bin_func : char
val st_bin_binary_old : char
val st_bin_uuid : char
val st_bin_md5 : char
val st_bin_user : char

type buf = {
  buf : Buf.buf;
  mutable stack : int list;
  mutable finished : bool;
}

module Oid :
  sig
    val from_string : string -> S.t
    val to_string : string -> S.t
    val counter : int ref
    val gen : unit -> S.t
    val generated_time : S.t -> Time.t
  end

module Append :
  sig
    val init : ?hint:int -> unit -> buf
    val empty : buf
    val size : buf -> int
    val estart : buf -> char -> S.t -> unit
    val int : buf -> S.t -> int32 -> unit
    val long : buf -> S.t -> int64 -> unit
    val double : buf -> S.t -> float -> unit
    val bool : buf -> S.t -> bool -> unit
    val null : buf -> S.t -> unit
    val minkey : buf -> S.t -> unit
    val maxkey : buf -> S.t -> unit
    val undefined : buf -> S.t -> unit
    val string_base : buf -> S.t -> S.t -> int -> char -> unit
    val string : buf -> S.t -> S.t -> unit
    val symbol : buf -> S.t -> S.t -> unit
    val code : buf -> S.t -> S.t -> unit
    val string_n : buf -> S.t -> S.t -> int -> unit
    val symbol_n : buf -> S.t -> S.t -> int -> unit
    val code_n : buf -> S.t -> S.t -> int -> unit
    val code_w_scope_n : buf -> S.t -> S.t -> int -> buf -> unit
    val code_w_scope : buf -> S.t -> S.t -> buf -> unit
    val start_codewscope : buf -> S.t -> S.t -> unit
    val finish_codewscope : buf -> S.t -> unit
    val binary : buf -> S.t -> char -> S.t -> int -> unit
    val oid : buf -> S.t -> S.t -> unit
    val new_oid : buf -> S.t -> unit
    val regex : buf -> S.t -> S.t -> S.t -> unit
    val bson : buf -> S.t -> buf -> unit
    val timestamp : buf -> S.t -> int32 * int32 -> unit
    val date : buf -> S.t -> int64 -> unit
    val time_t : buf -> S.t -> Time.t -> unit
    val start_object : buf -> S.t -> unit
    val start_array : buf -> S.t -> unit
    val finish_object : buf -> unit
    val finish_array : buf -> unit
    val finish : buf -> unit
    val get : buf -> S.t
  end

module type Iterator_sig =
  sig
    module S : S_sig
    type iter = { ibuf : S.t; mutable pos : int; mutable first : bool; }
    val init : buf -> iter
    val from_buffer : S.t -> iter
    val iterator_type : iter -> char
    val key : iter -> string
    val value : iter -> int
    val int_raw : iter -> int32
    val long_raw : iter -> int64
    val double_raw : iter -> float
    val bool_raw : iter -> bool
    val oid : iter -> string
    val string : ?offset:int -> iter -> string
    val symbol : ?offset:int -> iter -> string
    val cstring : ?offset:int -> iter -> string
    val string_len : iter -> int
    val int : iter -> int32
    val long : iter -> int64
    val double : iter -> float
    val timestamp : iter -> int32 * int32
    val bool : iter -> bool
    val code : iter -> string
    val code_scope : iter -> buf
    val date : iter -> int64
    val time_t : iter -> Time.t
    val bin_type : iter -> char
    val bin_len : iter -> int
    val bin_data : iter -> string
    val regex : iter -> string
    val regex_opts : iter -> string
    val subobject : iter -> buf
    val subiterator : iter -> iter
    val next : iter -> char
    val find : buf -> string -> iter * char
  end

module IteratorF(S : S_sig) : Iterator_sig with module S = S
module Iterator : Iterator_sig with module S = S
module IteratorSS : Iterator_sig with module S = SS

(*module Element :
sig
val element : buf -> S.t option -> Iterator.iter -> unit
end*)

module Print :
sig
  val print : buf -> unit
  val print_raw : S.t -> int -> int -> unit
  val to_pretty : buf -> string
  val to_pretty_raw : S.t -> int -> string
end
Something went wrong with that request. Please try again.