package proton

  1. Overview
  2. Docs
High-performance OCaml driver for Timeplus Proton (ClickHouse native protocol)

Install

dune-project
 Dependency

Authors

Maintainers

Sources

1.0.14.tar.gz
md5=454bd0918006df2e970c69e6884f1764
sha512=86638eea8c3a97ec3cf465c1e6756434c1d472097f2fc7f6e0057e1bfc3b59a83cb9c5bfa966404356e13cf1ac1ea6a3baddd1aec8d926973a44a8d03031a3f4

doc/src/proton/compress.ml.html

Source file compress.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
(* LZ4 compression support for ClickHouse/Proton protocol *)
open Lwt.Syntax
open Printf
module U = Unix

exception Compression_error of string
exception Checksum_mismatch of string

type method_t = None | LZ4 | ZSTD

let method_to_byte = function None -> 0x02 | LZ4 -> 0x82 | ZSTD -> 0x90

let method_of_byte = function
  | 0x02 -> None
  | 0x82 -> LZ4
  | 0x90 -> ZSTD
  | b -> failwith (Printf.sprintf "Unknown compression method: 0x%02x" b)

(* Constants matching Go driver *)
let checksum_size = 16
let compress_header_size = 1 + 4 + 4 (* method + compressed_size + uncompressed_size *)
let header_size = checksum_size + compress_header_size
let max_block_size = 1 lsl 20 (* 1MB *)

(* Note: We don't need the compressed_block record type since we handle
   compression frame format directly in read/write functions *)

(* Compress data using LZ4 *)
let compress_lz4 (data : bytes) : bytes = LZ4.Bytes.compress data

(* Decompress LZ4 data *)
let decompress_lz4 (data : bytes) (uncompressed_size : int) : bytes =
  LZ4.Bytes.decompress ~length:uncompressed_size data

(* Compress data using ZSTD *)
let compress_zstd (data : bytes) : bytes =
  let data_str = Bytes.to_string data in
  let compressed = Zstd.compress ~level:3 data_str in
  Bytes.of_string compressed

(* Decompress ZSTD data *)
let decompress_zstd (data : bytes) (uncompressed_size : int) : bytes =
  let data_str = Bytes.to_string data in
  let decompressed = Zstd.decompress uncompressed_size data_str in
  Bytes.of_string decompressed

(* Lwt-friendly write that avoids frame + checksum_input allocations *)
let write_compressed_block_lwt (writev_fn : string list -> unit Lwt.t) (data : bytes)
    (method_ : method_t) : unit Lwt.t =
  match method_ with
  | None ->
      (* Write as-is; rely on caller to send uncompressed payloads appropriately *)
      writev_fn [ Bytes.unsafe_to_string data ]
  | LZ4 | ZSTD ->
      (* Stream in <= 1MB chunks, each with its own header+checksum. *)
      let total_len = Bytes.length data in
      let rec loop off =
        if off >= total_len then Lwt.return_unit
        else
          let remain = total_len - off in
          let chunk_len = if remain > max_block_size then max_block_size else remain in
          let chunk = Bytes.sub data off chunk_len in
          let compressed_chunk =
            match method_ with
            | LZ4 -> compress_lz4 chunk
            | ZSTD -> compress_zstd chunk
            | None -> assert false
          in
          let compressed_size = Bytes.length compressed_chunk in
          let total = checksum_size + compress_header_size + compressed_size in
          let frame = Bytes.create total in
          let method_byte =
            match method_ with
            | LZ4 -> method_to_byte LZ4
            | ZSTD -> method_to_byte ZSTD
            | None -> assert false
          in
          (* header inside frame *)
          Bytes.set frame checksum_size (Char.chr method_byte);
          Binary.bytes_set_int32_le frame (checksum_size + 1)
            (Int32.of_int (compressed_size + compress_header_size));
          Binary.bytes_set_int32_le frame (checksum_size + 5) (Int32.of_int chunk_len);
          Bytes.blit compressed_chunk 0 frame (checksum_size + compress_header_size) compressed_size;
          (* checksum over contiguous header+payload slice (method-first)
             Use C++ CityHash128 binding to match ClickHouse exactly. *)
          let h_cxx =
            Cityhash.cityhash128_sub frame checksum_size (compress_header_size + compressed_size)
          in
          let checksum = Bytes.create 16 in
          Binary.bytes_set_int64_le checksum 0 h_cxx.low;
          Binary.bytes_set_int64_le checksum 8 h_cxx.high;
          (* debug: also compute alternate variants to compare with server *)
          let dbg =
            match Sys.getenv_opt "PROTON_DEBUG" with
            | Some ("1" | "true" | "TRUE" | "yes" | "YES") -> true
            | _ -> false
          in
          if dbg then (
            let hex s =
              let len = Bytes.length s in
              let out = Bytes.create (2 * len) in
              let hexdig = "0123456789abcdef" in
              for i = 0 to len - 1 do
                let v = Char.code (Bytes.get s i) in
                Bytes.set out (2 * i) (String.unsafe_get hexdig ((v lsr 4) land 0xF));
                Bytes.set out ((2 * i) + 1) (String.unsafe_get hexdig (v land 0xF))
              done;
              Bytes.unsafe_to_string out
            in
            (* sizes-first alt header *)
            let alt = Bytes.create (compress_header_size + compressed_size) in
            Binary.bytes_set_int32_le alt 0 (Int32.of_int (compressed_size + compress_header_size));
            Binary.bytes_set_int32_le alt 4 (Int32.of_int chunk_len);
            Bytes.set alt 8 (Char.chr method_byte);
            Bytes.blit compressed_chunk 0 alt compress_header_size compressed_size;
            let h_alt = Cityhash.cityhash128_sub alt 0 (Bytes.length alt) in
            let c_sizes_first = Cityhash.to_bytes h_alt in
            printf
              "[compress] frame(method=0x%02x) comp_size=%d uncomp=%d checksum=%s \
               alt_sizes_first=%s use=%s\n\
               %!"
              method_byte
              (compressed_size + compress_header_size)
              chunk_len (hex checksum) (hex c_sizes_first) "cxx";
            (* Dump hashed slice header and first 32 bytes of payload for verification *)
            let hdr = Bytes.sub frame checksum_size compress_header_size in
            let payload0 =
              let take = min 32 compressed_size in
              Bytes.sub frame (checksum_size + compress_header_size) take
            in
            printf "[compress] hashed_hdr=%s payload0=%s hashed_len=%d\n%!" (hex hdr) (hex payload0)
              (compress_header_size + compressed_size);
            (* Optional full dump of hashed slice to files for debugging *)
            match Sys.getenv_opt "PROTON_DUMP_HASH" with
            | Some ("1" | "true" | "TRUE" | "yes" | "YES") ->
                let to_hash_len = compress_header_size + compressed_size in
                let to_hash = Bytes.create to_hash_len in
                Bytes.blit frame checksum_size to_hash 0 compress_header_size;
                Bytes.blit frame
                  (checksum_size + compress_header_size)
                  to_hash compress_header_size compressed_size;
                (try if not (Sys.file_exists "debug") then U.mkdir "debug" 0o755 with _ -> ());
                let ts = int_of_float (U.gettimeofday ()) in
                let base =
                  sprintf "debug/hashed_m%02x_c%d_u%d_t%d" method_byte
                    (compressed_size + compress_header_size)
                    chunk_len ts
                in
                let binf = base ^ ".bin" in
                let hexf = base ^ ".hex" in
                let metaf = base ^ ".meta" in
                let ocb = open_out_bin binf in
                output_bytes ocb to_hash;
                close_out ocb;
                let och = open_out hexf in
                let n = Bytes.length to_hash in
                for i = 0 to n - 1 do
                  let v = Char.code (Bytes.get to_hash i) in
                  output_string och (Printf.sprintf "%02x" v)
                done;
                close_out och;
                let ocm = open_out metaf in
                fprintf ocm
                  "method=0x%02x\ncomp_size=%d\nuncomp=%d\nhash_len=%d\nuse=%s\nchecksum=%s\n"
                  method_byte
                  (compressed_size + compress_header_size)
                  chunk_len to_hash_len "cxx" (hex checksum);
                close_out ocm
            | _ -> ());
          let dbg =
            match Sys.getenv_opt "PROTON_DEBUG" with
            | Some ("1" | "true" | "TRUE" | "yes" | "YES") -> true
            | _ -> false
          in
          (if dbg then
             let hex s =
               let len = Bytes.length s in
               let out = Bytes.create (2 * len) in
               let hexdig = "0123456789abcdef" in
               for i = 0 to len - 1 do
                 let v = Char.code (Bytes.get s i) in
                 Bytes.set out (2 * i) (String.unsafe_get hexdig ((v lsr 4) land 0xF));
                 Bytes.set out ((2 * i) + 1) (String.unsafe_get hexdig (v land 0xF))
               done;
               Bytes.unsafe_to_string out
             in
             printf "[compress] frame(method=0x%02x) comp_size=%d uncomp=%d checksum=%s\n%!"
               method_byte
               (compressed_size + compress_header_size)
               chunk_len (hex checksum));

          Bytes.blit checksum 0 frame 0 checksum_size;
          let dbg =
            match Sys.getenv_opt "PROTON_DEBUG" with
            | Some ("1" | "true" | "TRUE" | "yes" | "YES") -> true
            | _ -> false
          in
          (if dbg then
             let hex s =
               let len = Bytes.length s in
               let out = Bytes.create (2 * len) in
               let hexdig = "0123456789abcdef" in
               for i = 0 to len - 1 do
                 let v = Char.code (Bytes.get s i) in
                 Bytes.set out (2 * i) (String.unsafe_get hexdig ((v lsr 4) land 0xF));
                 Bytes.set out ((2 * i) + 1) (String.unsafe_get hexdig (v land 0xF))
               done;
               Bytes.unsafe_to_string out
             in
             let onwire = Bytes.sub frame 0 16 in
             printf "[compress] onwire checksum=%s\n%!" (hex onwire));
          let* () = writev_fn [ Bytes.unsafe_to_string frame ] in
          loop (off + chunk_len)
      in
      loop 0

module Stream = struct
  type t = {
    method_ : method_t;
    writev : string list -> unit Lwt.t;
    buf : bytes; (* uncompressed accumulation buffer *)
    mutable off : int; (* current fill offset *)
  }

  let create writev method_ = { method_; writev; buf = Bytes.create max_block_size; off = 0 }

  let[@inline] write_frame (t : t) (chunk : bytes) (len : int) : unit Lwt.t =
    match t.method_ with
    | None ->
        (* No compression: write raw chunk directly. This path is not
           currently used by callers, but keep it safe. *)
        t.writev [ Bytes.unsafe_to_string (Bytes.sub chunk 0 len) ]
    | LZ4 | ZSTD ->
        let to_compress = if len = Bytes.length chunk then chunk else Bytes.sub chunk 0 len in
        let compressed =
          match t.method_ with
          | LZ4 -> compress_lz4 to_compress
          | ZSTD -> compress_zstd to_compress
          | None -> assert false
        in
        let compressed_size = Bytes.length compressed in
        let total = checksum_size + compress_header_size + compressed_size in
        let frame = Bytes.create total in
        let method_byte =
          match t.method_ with
          | LZ4 -> method_to_byte LZ4
          | ZSTD -> method_to_byte ZSTD
          | None -> assert false
        in
        (* method-first header layout inside frame *)
        Bytes.set frame checksum_size (Char.chr method_byte);
        Binary.bytes_set_int32_le frame (checksum_size + 1)
          (Int32.of_int (compressed_size + compress_header_size));
        Binary.bytes_set_int32_le frame (checksum_size + 5) (Int32.of_int len);
        Bytes.blit compressed 0 frame (checksum_size + compress_header_size) compressed_size;
        (* Use C++ CityHash128 binding to match ClickHouse exactly. *)
        let h_cxx =
          Cityhash.cityhash128_sub frame checksum_size (compress_header_size + compressed_size)
        in
        let checksum = Bytes.create 16 in
        Binary.bytes_set_int64_le checksum 0 h_cxx.low;
        Binary.bytes_set_int64_le checksum 8 h_cxx.high;
        let dbg =
          match Sys.getenv_opt "PROTON_DEBUG" with
          | Some ("1" | "true" | "TRUE" | "yes" | "YES") -> true
          | _ -> false
        in
        if dbg then (
          let hex s =
            let len = Bytes.length s in
            let out = Bytes.create (2 * len) in
            let hexdig = "0123456789abcdef" in
            for i = 0 to len - 1 do
              let v = Char.code (Bytes.get s i) in
              Bytes.set out (2 * i) (String.unsafe_get hexdig ((v lsr 4) land 0xF));
              Bytes.set out ((2 * i) + 1) (String.unsafe_get hexdig (v land 0xF))
            done;
            Bytes.unsafe_to_string out
          in
          let alt = Bytes.create (compress_header_size + compressed_size) in
          Binary.bytes_set_int32_le alt 0 (Int32.of_int (compressed_size + compress_header_size));
          Binary.bytes_set_int32_le alt 4 (Int32.of_int len);
          Bytes.set alt 8 (Char.chr method_byte);
          Bytes.blit compressed 0 alt compress_header_size compressed_size;
          let h_alt = Cityhash.cityhash128_sub alt 0 (Bytes.length alt) in
          let c_sizes_first = Cityhash.to_bytes h_alt in
          printf
            "[compress] frame(method=0x%02x) comp_size=%d uncomp=%d checksum=%s alt_sizes_first=%s \
             use=%s\n\
             %!"
            method_byte
            (compressed_size + compress_header_size)
            len (hex checksum) (hex c_sizes_first) "cxx";
          let hdr = Bytes.sub frame checksum_size compress_header_size in
          let payload0 =
            let take = min 32 compressed_size in
            Bytes.sub frame (checksum_size + compress_header_size) take
          in
          printf "[compress] hashed_hdr=%s payload0=%s hashed_len=%d\n%!" (hex hdr) (hex payload0)
            (compress_header_size + compressed_size);
          match Sys.getenv_opt "PROTON_DUMP_HASH" with
          | Some ("1" | "true" | "TRUE" | "yes" | "YES") ->
              let to_hash_len = compress_header_size + compressed_size in
              let to_hash = Bytes.create to_hash_len in
              Bytes.blit frame checksum_size to_hash 0 compress_header_size;
              Bytes.blit frame
                (checksum_size + compress_header_size)
                to_hash compress_header_size compressed_size;
              (try if not (Sys.file_exists "debug") then U.mkdir "debug" 0o755 with _ -> ());
              let ts = int_of_float (U.gettimeofday ()) in
              let base =
                sprintf "debug/hashed_m%02x_c%d_u%d_t%d" method_byte
                  (compressed_size + compress_header_size)
                  len ts
              in
              let ocb = open_out_bin (base ^ ".bin") in
              output_bytes ocb to_hash;
              close_out ocb;
              let och = open_out (base ^ ".hex") in
              let n = Bytes.length to_hash in
              for i = 0 to n - 1 do
                let v = Char.code (Bytes.get to_hash i) in
                output_string och (Printf.sprintf "%02x" v)
              done;
              close_out och;
              let ocm = open_out (base ^ ".meta") in
              fprintf ocm
                "method=0x%02x\ncomp_size=%d\nuncomp=%d\nhash_len=%d\nuse=%s\nchecksum=%s\n"
                method_byte
                (compressed_size + compress_header_size)
                len to_hash_len "cxx" (hex checksum);
              close_out ocm
          | _ -> ());
        let dbg =
          match Sys.getenv_opt "PROTON_DEBUG" with
          | Some ("1" | "true" | "TRUE" | "yes" | "YES") -> true
          | _ -> false
        in
        (if dbg then
           let hex s =
             let len = Bytes.length s in
             let out = Bytes.create (2 * len) in
             let hexdig = "0123456789abcdef" in
             for i = 0 to len - 1 do
               let v = Char.code (Bytes.get s i) in
               Bytes.set out (2 * i) (String.unsafe_get hexdig ((v lsr 4) land 0xF));
               Bytes.set out ((2 * i) + 1) (String.unsafe_get hexdig (v land 0xF))
             done;
             Bytes.unsafe_to_string out
           in
           printf "[compress] frame(method=0x%02x) comp_size=%d uncomp=%d checksum=%s\n%!"
             method_byte
             (compressed_size + compress_header_size)
             len (hex checksum));
        Bytes.blit checksum 0 frame 0 checksum_size;
        let dbg =
          match Sys.getenv_opt "PROTON_DEBUG" with
          | Some ("1" | "true" | "TRUE" | "yes" | "YES") -> true
          | _ -> false
        in
        (if dbg then
           let hex s =
             let len = Bytes.length s in
             let out = Bytes.create (2 * len) in
             let hexdig = "0123456789abcdef" in
             for i = 0 to len - 1 do
               let v = Char.code (Bytes.get s i) in
               Bytes.set out (2 * i) (String.unsafe_get hexdig ((v lsr 4) land 0xF));
               Bytes.set out ((2 * i) + 1) (String.unsafe_get hexdig (v land 0xF))
             done;
             Bytes.unsafe_to_string out
           in
           let onwire = Bytes.sub frame 0 16 in
           printf "[compress] onwire checksum=%s\n%!" (hex onwire));
        t.writev [ Bytes.unsafe_to_string frame ]

  let rec write_bytes (t : t) (b : bytes) (off : int) (len : int) : unit Lwt.t =
    if len = 0 then Lwt.return_unit
    else
      let remaining = max_block_size - t.off in
      if len <= remaining then (
        Bytes.blit b off t.buf t.off len;
        t.off <- t.off + len;
        Lwt.return_unit)
      else (
        (* fill up, flush frame, continue *)
        Bytes.blit b off t.buf t.off remaining;
        let* () = write_frame t t.buf max_block_size in
        t.off <- 0;
        write_bytes t b (off + remaining) (len - remaining))

  let write_string (t : t) (s : string) : unit Lwt.t =
    write_bytes t (Bytes.unsafe_of_string s) 0 (String.length s)

  let write_char (t : t) (c : char) : unit Lwt.t =
    if t.off < max_block_size then (
      Bytes.set t.buf t.off c;
      t.off <- t.off + 1;
      Lwt.return_unit)
    else
      let* () = write_frame t t.buf max_block_size in
      t.off <- 0;
      Bytes.set t.buf 0 c;
      t.off <- 1;
      Lwt.return_unit

  let flush (t : t) : unit Lwt.t =
    if t.off = 0 then Lwt.return_unit
    else
      let* () = write_frame t t.buf t.off in
      t.off <- 0;
      Lwt.return_unit
end