readcon-db 0.2.0

Mmap-backed CON frame corpus (Heed/LMDB), xxHash exact match, multi-language FFI
Documentation
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
module readcon_db
  use, intrinsic :: iso_c_binding
  implicit none
  private
  public :: rkrdb_ok, rkrdb_err, rkrdb_not_found, db_open, db_open_readonly, db_close, db_append, db_append_units, &
            db_extend, db_extend_units, db_select_basic, &
            db_result_count, db_result_key, db_frame_hash, db_frame_formula, db_xxh3_128, &
            db_get_frame, db_pack_frame, db_pack_frames, db_unpack_positions, &
            db_unpack_batch_nframes, db_unpack_batch_item, db_set_units, db_frame_units, &
            db_h5md_times, db_h5md_shape, db_h5md_positions, db_h5md_edges, &
            db_h5md_forces, db_h5md_velocities, db_h5md_species, &
            db_get_positions, db_get_forces, db_get_velocities, &
            db_cook_frame, db_delete_cooked, db_has_valid_cooked, db_recook_all

  integer(c_int), parameter :: rkrdb_ok = 0
  integer(c_int), parameter :: rkrdb_err = -1
  integer(c_int), parameter :: rkrdb_not_found = -2

  interface
    function rkrdb_open(path, out_id) bind(C, name="rkrdb_open") result(st)
      import :: c_char, c_int, c_size_t
      character(kind=c_char), intent(in) :: path(*)
      integer(c_size_t), intent(out) :: out_id
      integer(c_int) :: st
    end function
    function rkrdb_open_readonly(path, out_id) bind(C, name="rkrdb_open_readonly") result(st)
      import :: c_char, c_int, c_size_t
      character(kind=c_char), intent(in) :: path(*)
      integer(c_size_t), intent(out) :: out_id
      integer(c_int) :: st
    end function
    function rkrdb_pack_frame(id, traj_id, frame_idx, buf, buflen) bind(C, name="rkrdb_pack_frame") result(n)
      import :: c_int, c_size_t, c_int64_t, c_int32_t, c_ptr
      integer(c_size_t), value :: id, buflen
      integer(c_int64_t), value :: traj_id
      integer(c_int32_t), value :: frame_idx
      type(c_ptr), value :: buf
      integer(c_int) :: n
    end function
    function rkrdb_pack_frames(id, traj_ids, frame_idxs, nkeys, buf, buflen) &
        bind(C, name="rkrdb_pack_frames") result(n)
      import :: c_int, c_size_t, c_int64_t, c_int32_t, c_ptr
      integer(c_size_t), value :: id, buflen
      integer(c_int64_t), intent(in) :: traj_ids(*)
      integer(c_int32_t), intent(in) :: frame_idxs(*)
      integer(c_int32_t), value :: nkeys
      type(c_ptr), value :: buf
      integer(c_int) :: n
    end function
    function rkrdb_unpack_batch_nframes(buf, buflen, out_n) &
        bind(C, name="rkrdb_unpack_batch_nframes") result(st)
      import :: c_int, c_size_t, c_int8_t, c_int32_t
      integer(c_int8_t), intent(in) :: buf(*)
      integer(c_size_t), value :: buflen
      integer(c_int32_t), intent(out) :: out_n
      integer(c_int) :: st
    end function
    function rkrdb_unpack_batch_item(buf, buflen, idx, out_xyz, cap, out_n) &
        bind(C, name="rkrdb_unpack_batch_item") result(st)
      import :: c_int, c_size_t, c_int8_t, c_double, c_int32_t
      integer(c_int8_t), intent(in) :: buf(*)
      integer(c_size_t), value :: buflen
      integer(c_int32_t), value :: idx, cap
      real(c_double), intent(out) :: out_xyz(*)
      integer(c_int32_t), intent(out) :: out_n
      integer(c_int) :: st
    end function
    function rkrdb_unpack_positions(buf, buflen, out_xyz, cap, out_n) bind(C, name="rkrdb_unpack_positions") result(st)
      import :: c_int, c_size_t, c_int8_t, c_double, c_int32_t
      integer(c_int8_t), intent(in) :: buf(*)
      integer(c_size_t), value :: buflen
      real(c_double), intent(out) :: out_xyz(*)
      integer(c_int32_t), value :: cap
      integer(c_int32_t), intent(out) :: out_n
      integer(c_int) :: st
    end function
    function rkrdb_close(id) bind(C, name="rkrdb_close") result(st)
      import :: c_int, c_size_t
      integer(c_size_t), value :: id
      integer(c_int) :: st
    end function
    function rkrdb_append_trajectory(id, traj_id, path, out_n) bind(C, name="rkrdb_append_trajectory") result(st)
      import :: c_char, c_int, c_size_t, c_int64_t, c_int32_t
      integer(c_size_t), value :: id
      integer(c_int64_t), value :: traj_id
      character(kind=c_char), intent(in) :: path(*)
      integer(c_int32_t), intent(out) :: out_n
      integer(c_int) :: st
    end function
    function rkrdb_append_trajectory_units(id, traj_id, path, units_json, out_n) &
        bind(C, name="rkrdb_append_trajectory_units") result(st)
      import :: c_char, c_int, c_size_t, c_int64_t, c_int32_t
      integer(c_size_t), value :: id
      integer(c_int64_t), value :: traj_id
      character(kind=c_char), intent(in) :: path(*)
      character(kind=c_char), intent(in) :: units_json(*)
      integer(c_int32_t), intent(out) :: out_n
      integer(c_int) :: st
    end function
    function rkrdb_extend_trajectory(id, traj_id, path, out_n) &
        bind(C, name="rkrdb_extend_trajectory") result(st)
      import :: c_char, c_int, c_size_t, c_int64_t, c_int32_t
      integer(c_size_t), value :: id
      integer(c_int64_t), value :: traj_id
      character(kind=c_char), intent(in) :: path(*)
      integer(c_int32_t), intent(out) :: out_n
      integer(c_int) :: st
    end function
    function rkrdb_extend_trajectory_units(id, traj_id, path, units_json, out_n) &
        bind(C, name="rkrdb_extend_trajectory_units") result(st)
      import :: c_char, c_int, c_size_t, c_int64_t, c_int32_t
      integer(c_size_t), value :: id
      integer(c_int64_t), value :: traj_id
      character(kind=c_char), intent(in) :: path(*)
      character(kind=c_char), intent(in) :: units_json(*)
      integer(c_int32_t), intent(out) :: out_n
      integer(c_int) :: st
    end function
    function rkrdb_set_units(id, traj_id, units_json, out_n) bind(C, name="rkrdb_set_units") result(st)
      import :: c_char, c_int, c_size_t, c_int64_t, c_int32_t
      integer(c_size_t), value :: id
      integer(c_int64_t), value :: traj_id
      character(kind=c_char), intent(in) :: units_json(*)
      integer(c_int32_t), intent(out) :: out_n
      integer(c_int) :: st
    end function
    function rkrdb_frame_units(id, traj_id, frame_idx, buf, buflen) &
        bind(C, name="rkrdb_frame_units") result(st)
      import :: c_char, c_int, c_size_t, c_int64_t, c_int32_t
      integer(c_size_t), value :: id, buflen
      integer(c_int64_t), value :: traj_id
      integer(c_int32_t), value :: frame_idx
      character(kind=c_char), intent(out) :: buf(*)
      integer(c_int) :: st
    end function
    function rkrdb_h5md_times(id, traj_id, out, cap, out_n) bind(C, name="rkrdb_h5md_times") result(st)
      import :: c_int, c_size_t, c_int64_t, c_int32_t, c_double
      integer(c_size_t), value :: id, cap
      integer(c_int64_t), value :: traj_id
      real(c_double), intent(out) :: out(*)
      integer(c_int32_t), intent(out) :: out_n
      integer(c_int) :: st
    end function
    function rkrdb_h5md_shape(id, traj_id, out_nframes, out_natoms) &
        bind(C, name="rkrdb_h5md_shape") result(st)
      import :: c_int, c_size_t, c_int64_t, c_int32_t
      integer(c_size_t), value :: id
      integer(c_int64_t), value :: traj_id
      integer(c_int32_t), intent(out) :: out_nframes, out_natoms
      integer(c_int) :: st
    end function
    function rkrdb_h5md_positions(id, traj_id, out, cap, out_nframes, out_natoms) &
        bind(C, name="rkrdb_h5md_positions") result(st)
      import :: c_int, c_size_t, c_int64_t, c_int32_t, c_double
      integer(c_size_t), value :: id, cap
      integer(c_int64_t), value :: traj_id
      real(c_double), intent(out) :: out(*)
      integer(c_int32_t), intent(out) :: out_nframes, out_natoms
      integer(c_int) :: st
    end function
    function rkrdb_h5md_edges(id, traj_id, out, cap) bind(C, name="rkrdb_h5md_edges") result(st)
      import :: c_int, c_size_t, c_int64_t, c_double
      integer(c_size_t), value :: id, cap
      integer(c_int64_t), value :: traj_id
      real(c_double), intent(out) :: out(*)
      integer(c_int) :: st
    end function
    function rkrdb_h5md_forces(id, traj_id, out, cap) bind(C, name="rkrdb_h5md_forces") result(st)
      import :: c_int, c_size_t, c_int64_t, c_double
      integer(c_size_t), value :: id, cap
      integer(c_int64_t), value :: traj_id
      real(c_double), intent(out) :: out(*)
      integer(c_int) :: st
    end function
    function rkrdb_h5md_velocities(id, traj_id, out, cap) bind(C, name="rkrdb_h5md_velocities") result(st)
      import :: c_int, c_size_t, c_int64_t, c_double
      integer(c_size_t), value :: id, cap
      integer(c_int64_t), value :: traj_id
      real(c_double), intent(out) :: out(*)
      integer(c_int) :: st
    end function
    function rkrdb_h5md_species(id, traj_id, out, cap, out_natoms) bind(C, name="rkrdb_h5md_species") result(st)
      import :: c_int, c_size_t, c_int64_t, c_int32_t
      integer(c_size_t), value :: id, cap
      integer(c_int64_t), value :: traj_id
      integer(c_int32_t), intent(out) :: out(*)
      integer(c_int32_t), intent(out) :: out_natoms
      integer(c_int) :: st
    end function
    function rkrdb_get_positions(id, traj_id, frame_idx, out_xyz, capacity_atoms, out_natoms) &
        bind(C, name="rkrdb_get_positions") result(st)
      import :: c_int, c_size_t, c_int64_t, c_int32_t, c_double
      integer(c_size_t), value :: id
      integer(c_int64_t), value :: traj_id
      integer(c_int32_t), value :: frame_idx, capacity_atoms
      real(c_double), intent(out) :: out_xyz(*)
      integer(c_int32_t), intent(out) :: out_natoms
      integer(c_int) :: st
    end function
    function rkrdb_get_forces(id, traj_id, frame_idx, out_xyz, capacity_atoms, out_natoms, out_has) &
        bind(C, name="rkrdb_get_forces") result(st)
      import :: c_int, c_size_t, c_int64_t, c_int32_t, c_int8_t, c_double
      integer(c_size_t), value :: id
      integer(c_int64_t), value :: traj_id
      integer(c_int32_t), value :: frame_idx, capacity_atoms
      real(c_double), intent(out) :: out_xyz(*)
      integer(c_int32_t), intent(out) :: out_natoms
      integer(c_int8_t), intent(out) :: out_has
      integer(c_int) :: st
    end function
    function rkrdb_get_velocities(id, traj_id, frame_idx, out_xyz, capacity_atoms, out_natoms, &
        out_has) bind(C, name="rkrdb_get_velocities") result(st)
      import :: c_int, c_size_t, c_int64_t, c_int32_t, c_int8_t, c_double
      integer(c_size_t), value :: id
      integer(c_int64_t), value :: traj_id
      integer(c_int32_t), value :: frame_idx, capacity_atoms
      real(c_double), intent(out) :: out_xyz(*)
      integer(c_int32_t), intent(out) :: out_natoms
      integer(c_int8_t), intent(out) :: out_has
      integer(c_int) :: st
    end function
    function rkrdb_select_basic(id, traj_id, symbol, nmin, nmax, limit) bind(C, name="rkrdb_select_basic") result(st)
      import :: c_char, c_int, c_size_t, c_int64_t, c_int32_t
      integer(c_size_t), value :: id
      integer(c_int64_t), value :: traj_id
      character(kind=c_char), intent(in) :: symbol(*)
      integer(c_int32_t), value :: nmin, nmax, limit
      integer(c_int) :: st
    end function
    function rkrdb_result_count(id) bind(C, name="rkrdb_result_count") result(n)
      import :: c_int, c_size_t
      integer(c_size_t), value :: id
      integer(c_int) :: n
    end function
    function rkrdb_result_key(id, i, out_traj, out_frame) bind(C, name="rkrdb_result_key") result(st)
      import :: c_int, c_size_t, c_int64_t, c_int32_t
      integer(c_size_t), value :: id, i
      integer(c_int64_t), intent(out) :: out_traj
      integer(c_int32_t), intent(out) :: out_frame
      integer(c_int) :: st
    end function
    function rkrdb_frame_hash(id, traj_id, frame_idx, out_hash) bind(C, name="rkrdb_frame_hash") result(st)
      import :: c_int, c_size_t, c_int64_t, c_int32_t, c_int8_t
      integer(c_size_t), value :: id
      integer(c_int64_t), value :: traj_id
      integer(c_int32_t), value :: frame_idx
      integer(c_int8_t), intent(out) :: out_hash(*)
      integer(c_int) :: st
    end function
    function rkrdb_xxh3_128(data, n, out_hash) bind(C, name="rkrdb_xxh3_128") result(st)
      import :: c_int, c_size_t, c_int8_t
      integer(c_int8_t), intent(in) :: data(*)
      integer(c_size_t), value :: n
      integer(c_int8_t), intent(out) :: out_hash(*)
      integer(c_int) :: st
    end function
    function rkrdb_get_frame(id, traj_id, frame_idx) bind(C, name="rkrdb_get_frame") result(p)
      import :: c_size_t, c_int64_t, c_int32_t, c_ptr
      integer(c_size_t), value :: id
      integer(c_int64_t), value :: traj_id
      integer(c_int32_t), value :: frame_idx
      type(c_ptr) :: p
    end function
    function rkrdb_frame_formula(id, traj_id, frame_idx, buf, buflen) bind(C, name="rkrdb_frame_formula") result(st)
      import :: c_int, c_size_t, c_int64_t, c_int32_t, c_char
      integer(c_size_t), value :: id
      integer(c_int64_t), value :: traj_id
      integer(c_int32_t), value :: frame_idx
      character(kind=c_char), intent(out) :: buf(*)
      integer(c_size_t), value :: buflen
      integer(c_int) :: st
    end function

    function rkrdb_cook_frame(id, traj_id, frame_idx) bind(C, name="rkrdb_cook_frame") result(st)
      import :: c_int, c_size_t, c_int64_t, c_int32_t
      integer(c_size_t), value :: id
      integer(c_int64_t), value :: traj_id
      integer(c_int32_t), value :: frame_idx
      integer(c_int) :: st
    end function
    function rkrdb_delete_cooked(id, traj_id, frame_idx) bind(C, name="rkrdb_delete_cooked") result(st)
      import :: c_int, c_size_t, c_int64_t, c_int32_t
      integer(c_size_t), value :: id
      integer(c_int64_t), value :: traj_id
      integer(c_int32_t), value :: frame_idx
      integer(c_int) :: st
    end function
    function rkrdb_has_valid_cooked(id, traj_id, frame_idx) bind(C, name="rkrdb_has_valid_cooked") result(st)
      import :: c_int, c_size_t, c_int64_t, c_int32_t
      integer(c_size_t), value :: id
      integer(c_int64_t), value :: traj_id
      integer(c_int32_t), value :: frame_idx
      integer(c_int) :: st
    end function
    function rkrdb_recook_all(id) bind(C, name="rkrdb_recook_all") result(st)
      import :: c_int, c_size_t
      integer(c_size_t), value :: id
      integer(c_int) :: st
    end function
  end interface

contains

  function f_c_string(s) result(c)
    character(len=*), intent(in) :: s
    character(kind=c_char), allocatable :: c(:)
    integer :: i, n
    n = len_trim(s)
    allocate(c(n+1))
    do i = 1, n
      c(i) = s(i:i)
    end do
    c(n+1) = c_null_char
  end function

  subroutine db_open(path, id, status)
    character(len=*), intent(in) :: path
    integer(c_size_t), intent(out) :: id
    integer(c_int), intent(out) :: status
    character(kind=c_char), allocatable :: cp(:)
    cp = f_c_string(path)
    status = rkrdb_open(cp, id)
  end subroutine

  subroutine db_open_readonly(path, id, status)
    character(len=*), intent(in) :: path
    integer(c_size_t), intent(out) :: id
    integer(c_int), intent(out) :: status
    character(kind=c_char), allocatable :: cp(:)
    cp = f_c_string(path)
    status = rkrdb_open_readonly(cp, id)
  end subroutine

  subroutine db_pack_frame(id, traj_id, frame_idx, buf, buflen, n, status)
    integer(c_size_t), intent(in) :: id, buflen
    integer(c_int64_t), intent(in) :: traj_id
    integer(c_int32_t), intent(in) :: frame_idx
    integer(c_int8_t), intent(out), target :: buf(*)
    integer, intent(out) :: n
    integer(c_int), intent(out) :: status
    integer(c_int) :: rc
    if (buflen == 0_c_size_t) then
      rc = rkrdb_pack_frame(id, traj_id, frame_idx, c_null_ptr, 0_c_size_t)
    else
      rc = rkrdb_pack_frame(id, traj_id, frame_idx, c_loc(buf(1)), buflen)
    end if
    if (rc < 0) then
      status = rc
      n = 0
    else
      status = rkrdb_ok
      n = int(rc)
    end if
  end subroutine

  subroutine db_pack_frames(id, traj_ids, frame_idxs, nkeys, buf, buflen, n, status)
    integer(c_size_t), intent(in) :: id, buflen
    integer(c_int64_t), intent(in) :: traj_ids(*)
    integer(c_int32_t), intent(in) :: frame_idxs(*)
    integer(c_int32_t), intent(in) :: nkeys
    integer(c_int8_t), intent(out), target :: buf(*)
    integer, intent(out) :: n
    integer(c_int), intent(out) :: status
    integer(c_int) :: rc
    if (buflen == 0_c_size_t) then
      rc = rkrdb_pack_frames(id, traj_ids, frame_idxs, nkeys, c_null_ptr, 0_c_size_t)
    else
      rc = rkrdb_pack_frames(id, traj_ids, frame_idxs, nkeys, c_loc(buf(1)), buflen)
    end if
    if (rc < 0) then
      status = rc
      n = 0
    else
      status = rkrdb_ok
      n = int(rc)
    end if
  end subroutine

  subroutine db_unpack_batch_nframes(buf, buflen, nframes, status)
    integer(c_int8_t), intent(in) :: buf(*)
    integer(c_size_t), intent(in) :: buflen
    integer(c_int32_t), intent(out) :: nframes
    integer(c_int), intent(out) :: status
    status = rkrdb_unpack_batch_nframes(buf, buflen, nframes)
  end subroutine

  subroutine db_unpack_batch_item(buf, buflen, idx, xyz, cap, natoms, status)
    integer(c_int8_t), intent(in) :: buf(*)
    integer(c_size_t), intent(in) :: buflen
    integer(c_int32_t), intent(in) :: idx, cap
    real(c_double), intent(out) :: xyz(*)
    integer(c_int32_t), intent(out) :: natoms
    integer(c_int), intent(out) :: status
    status = rkrdb_unpack_batch_item(buf, buflen, idx, xyz, cap, natoms)
  end subroutine

  subroutine db_unpack_positions(buf, buflen, xyz, cap, natoms, status)
    integer(c_int8_t), intent(in) :: buf(*)
    integer(c_size_t), intent(in) :: buflen
    real(c_double), intent(out) :: xyz(*)
    integer(c_int32_t), intent(in) :: cap
    integer(c_int32_t), intent(out) :: natoms
    integer(c_int), intent(out) :: status
    status = rkrdb_unpack_positions(buf, buflen, xyz, cap, natoms)
  end subroutine

  subroutine db_close(id, status)
    integer(c_size_t), intent(in) :: id
    integer(c_int), intent(out) :: status
    status = rkrdb_close(id)
  end subroutine

  subroutine db_append(id, traj_id, path, n_frames, status)
    integer(c_size_t), intent(in) :: id
    integer(c_int64_t), intent(in) :: traj_id
    character(len=*), intent(in) :: path
    integer(c_int32_t), intent(out) :: n_frames
    integer(c_int), intent(out) :: status
    character(kind=c_char), allocatable :: cp(:)
    cp = f_c_string(path)
    status = rkrdb_append_trajectory(id, traj_id, cp, n_frames)
  end subroutine

  subroutine db_append_units(id, traj_id, path, units_json, n_frames, status)
    integer(c_size_t), intent(in) :: id
    integer(c_int64_t), intent(in) :: traj_id
    character(len=*), intent(in) :: path, units_json
    integer(c_int32_t), intent(out) :: n_frames
    integer(c_int), intent(out) :: status
    character(kind=c_char), allocatable :: cp(:), cu(:)
    cp = f_c_string(path)
    cu = f_c_string(units_json)
    status = rkrdb_append_trajectory_units(id, traj_id, cp, cu, n_frames)
  end subroutine

  subroutine db_extend(id, traj_id, path, n_frames, status)
    integer(c_size_t), intent(in) :: id
    integer(c_int64_t), intent(in) :: traj_id
    character(len=*), intent(in) :: path
    integer(c_int32_t), intent(out) :: n_frames
    integer(c_int), intent(out) :: status
    character(kind=c_char), allocatable :: cp(:)
    cp = f_c_string(path)
    status = rkrdb_extend_trajectory(id, traj_id, cp, n_frames)
  end subroutine

  subroutine db_extend_units(id, traj_id, path, units_json, n_frames, status)
    integer(c_size_t), intent(in) :: id
    integer(c_int64_t), intent(in) :: traj_id
    character(len=*), intent(in) :: path, units_json
    integer(c_int32_t), intent(out) :: n_frames
    integer(c_int), intent(out) :: status
    character(kind=c_char), allocatable :: cp(:), cu(:)
    cp = f_c_string(path)
    cu = f_c_string(units_json)
    status = rkrdb_extend_trajectory_units(id, traj_id, cp, cu, n_frames)
  end subroutine

  subroutine db_set_units(id, traj_id, units_json, n_frames, status)
    integer(c_size_t), intent(in) :: id
    integer(c_int64_t), intent(in) :: traj_id
    character(len=*), intent(in) :: units_json
    integer(c_int32_t), intent(out) :: n_frames
    integer(c_int), intent(out) :: status
    character(kind=c_char), allocatable :: cu(:)
    cu = f_c_string(units_json)
    status = rkrdb_set_units(id, traj_id, cu, n_frames)
  end subroutine

  subroutine db_h5md_times(id, traj_id, times, cap, n, status)
    integer(c_size_t), intent(in) :: id, cap
    integer(c_int64_t), intent(in) :: traj_id
    real(c_double), intent(out) :: times(*)
    integer(c_int32_t), intent(out) :: n
    integer(c_int), intent(out) :: status
    status = rkrdb_h5md_times(id, traj_id, times, cap, n)
  end subroutine

  subroutine db_h5md_shape(id, traj_id, nframes, natoms, status)
    integer(c_size_t), intent(in) :: id
    integer(c_int64_t), intent(in) :: traj_id
    integer(c_int32_t), intent(out) :: nframes, natoms
    integer(c_int), intent(out) :: status
    status = rkrdb_h5md_shape(id, traj_id, nframes, natoms)
  end subroutine

  subroutine db_h5md_positions(id, traj_id, xyz, cap, nframes, natoms, status)
    integer(c_size_t), intent(in) :: id, cap
    integer(c_int64_t), intent(in) :: traj_id
    real(c_double), intent(out) :: xyz(*)
    integer(c_int32_t), intent(out) :: nframes, natoms
    integer(c_int), intent(out) :: status
    status = rkrdb_h5md_positions(id, traj_id, xyz, cap, nframes, natoms)
  end subroutine

  subroutine db_h5md_edges(id, traj_id, edges, cap, status)
    integer(c_size_t), intent(in) :: id, cap
    integer(c_int64_t), intent(in) :: traj_id
    real(c_double), intent(out) :: edges(*)
    integer(c_int), intent(out) :: status
    status = rkrdb_h5md_edges(id, traj_id, edges, cap)
  end subroutine

  subroutine db_h5md_forces(id, traj_id, forces, cap, status)
    integer(c_size_t), intent(in) :: id, cap
    integer(c_int64_t), intent(in) :: traj_id
    real(c_double), intent(out) :: forces(*)
    integer(c_int), intent(out) :: status
    status = rkrdb_h5md_forces(id, traj_id, forces, cap)
  end subroutine

  subroutine db_h5md_velocities(id, traj_id, vel, cap, status)
    integer(c_size_t), intent(in) :: id, cap
    integer(c_int64_t), intent(in) :: traj_id
    real(c_double), intent(out) :: vel(*)
    integer(c_int), intent(out) :: status
    status = rkrdb_h5md_velocities(id, traj_id, vel, cap)
  end subroutine

  subroutine db_h5md_species(id, traj_id, z, cap, natoms, status)
    integer(c_size_t), intent(in) :: id, cap
    integer(c_int64_t), intent(in) :: traj_id
    integer(c_int32_t), intent(out) :: z(*)
    integer(c_int32_t), intent(out) :: natoms
    integer(c_int), intent(out) :: status
    status = rkrdb_h5md_species(id, traj_id, z, cap, natoms)
  end subroutine

  subroutine db_get_positions(id, traj_id, frame_idx, xyz, capacity_atoms, natoms, status)
    integer(c_size_t), intent(in) :: id
    integer(c_int64_t), intent(in) :: traj_id
    integer(c_int32_t), intent(in) :: frame_idx, capacity_atoms
    real(c_double), intent(out) :: xyz(*)
    integer(c_int32_t), intent(out) :: natoms
    integer(c_int), intent(out) :: status
    status = rkrdb_get_positions(id, traj_id, frame_idx, xyz, capacity_atoms, natoms)
  end subroutine

  subroutine db_get_forces(id, traj_id, frame_idx, xyz, capacity_atoms, natoms, has_frc, status)
    integer(c_size_t), intent(in) :: id
    integer(c_int64_t), intent(in) :: traj_id
    integer(c_int32_t), intent(in) :: frame_idx, capacity_atoms
    real(c_double), intent(out) :: xyz(*)
    integer(c_int32_t), intent(out) :: natoms
    logical, intent(out) :: has_frc
    integer(c_int), intent(out) :: status
    integer(c_int8_t) :: hf
    hf = 0_c_int8_t
    status = rkrdb_get_forces(id, traj_id, frame_idx, xyz, capacity_atoms, natoms, hf)
    has_frc = (hf /= 0_c_int8_t)
  end subroutine

  subroutine db_get_velocities(id, traj_id, frame_idx, xyz, capacity_atoms, natoms, has_vel, status)
    integer(c_size_t), intent(in) :: id
    integer(c_int64_t), intent(in) :: traj_id
    integer(c_int32_t), intent(in) :: frame_idx, capacity_atoms
    real(c_double), intent(out) :: xyz(*)
    integer(c_int32_t), intent(out) :: natoms
    logical, intent(out) :: has_vel
    integer(c_int), intent(out) :: status
    integer(c_int8_t) :: hv
    hv = 0_c_int8_t
    status = rkrdb_get_velocities(id, traj_id, frame_idx, xyz, capacity_atoms, natoms, hv)
    has_vel = (hv /= 0_c_int8_t)
  end subroutine

  subroutine db_frame_units(id, traj_id, frame_idx, buf, status)
    integer(c_size_t), intent(in) :: id
    integer(c_int64_t), intent(in) :: traj_id
    integer(c_int32_t), intent(in) :: frame_idx
    character(len=*), intent(out) :: buf
    integer(c_int), intent(out) :: status
    character(kind=c_char) :: tmp(len(buf)+1)
    integer :: i, n
    tmp = c_null_char
    status = rkrdb_frame_units(id, traj_id, frame_idx, tmp, int(size(tmp), c_size_t))
    buf = ' '
    if (status /= rkrdb_ok) return
    n = 0
    do i = 1, size(tmp)
      if (tmp(i) == c_null_char) exit
      n = i
    end do
    if (n > 0) then
      do i = 1, min(n, len(buf))
        buf(i:i) = tmp(i)
      end do
    end if
  end subroutine

  subroutine db_select_basic(id, traj_id, symbol, nmin, nmax, limit, status)
    integer(c_size_t), intent(in) :: id
    integer(c_int64_t), intent(in) :: traj_id
    character(len=*), intent(in) :: symbol
    integer(c_int32_t), intent(in) :: nmin, nmax, limit
    integer(c_int), intent(out) :: status
    character(kind=c_char), allocatable :: cs(:)
    cs = f_c_string(symbol)
    status = rkrdb_select_basic(id, traj_id, cs, nmin, nmax, limit)
  end subroutine

  function db_result_count(id) result(n)
    integer(c_size_t), intent(in) :: id
    integer(c_int) :: n
    n = rkrdb_result_count(id)
  end function

  subroutine db_result_key(id, i, traj, frame, status)
    integer(c_size_t), intent(in) :: id, i
    integer(c_int64_t), intent(out) :: traj
    integer(c_int32_t), intent(out) :: frame
    integer(c_int), intent(out) :: status
    status = rkrdb_result_key(id, i, traj, frame)
  end subroutine

  subroutine db_frame_hash(id, traj_id, frame_idx, hash16, status)
    integer(c_size_t), intent(in) :: id
    integer(c_int64_t), intent(in) :: traj_id
    integer(c_int32_t), intent(in) :: frame_idx
    integer(c_int8_t), intent(out) :: hash16(16)
    integer(c_int), intent(out) :: status
    status = rkrdb_frame_hash(id, traj_id, frame_idx, hash16)
  end subroutine

  subroutine db_xxh3_128(data, n, hash16, status)
    integer(c_int8_t), intent(in) :: data(*)
    integer(c_size_t), intent(in) :: n
    integer(c_int8_t), intent(out) :: hash16(16)
    integer(c_int), intent(out) :: status
    status = rkrdb_xxh3_128(data, n, hash16)
  end subroutine

  subroutine db_frame_formula(id, traj_id, frame_idx, formula, status)
    integer(c_size_t), intent(in) :: id
    integer(c_int64_t), intent(in) :: traj_id
    integer(c_int32_t), intent(in) :: frame_idx
    character(len=*), intent(out) :: formula
    integer(c_int), intent(out) :: status
    character(kind=c_char) :: buf(512)
    integer :: i, n
    buf = c_null_char
    status = rkrdb_frame_formula(id, traj_id, frame_idx, buf, int(512, c_size_t))
    formula = ""
    if (status /= rkrdb_ok) return
    n = 0
    do i = 1, 512
      if (buf(i) == c_null_char) exit
      n = n + 1
    end do
    if (n > 0) formula = transfer(buf(1:n), formula(1:n))
  end subroutine


  subroutine db_cook_frame(id, traj_id, frame_idx, status)
    integer(c_size_t), intent(in) :: id
    integer(c_int64_t), intent(in) :: traj_id
    integer(c_int32_t), intent(in) :: frame_idx
    integer(c_int), intent(out) :: status
    status = rkrdb_cook_frame(id, traj_id, frame_idx)
  end subroutine

  subroutine db_delete_cooked(id, traj_id, frame_idx, status)
    integer(c_size_t), intent(in) :: id
    integer(c_int64_t), intent(in) :: traj_id
    integer(c_int32_t), intent(in) :: frame_idx
    integer(c_int), intent(out) :: status
    status = rkrdb_delete_cooked(id, traj_id, frame_idx)
  end subroutine

  subroutine db_has_valid_cooked(id, traj_id, frame_idx, valid, status)
    integer(c_size_t), intent(in) :: id
    integer(c_int64_t), intent(in) :: traj_id
    integer(c_int32_t), intent(in) :: frame_idx
    logical, intent(out) :: valid
    integer(c_int), intent(out) :: status
    integer(c_int) :: v
    v = rkrdb_has_valid_cooked(id, traj_id, frame_idx)
    if (v < 0) then
      status = v
      valid = .false.
    else
      status = rkrdb_ok
      valid = (v == 1)
    end if
  end subroutine

  function db_get_frame(id, traj_id, frame_idx) result(p)
    ! RKRConFrame*; free with free_rkr_frame from readcon-core.
    integer(c_size_t), intent(in) :: id
    integer(c_int64_t), intent(in) :: traj_id
    integer(c_int32_t), intent(in) :: frame_idx
    type(c_ptr) :: p
    p = rkrdb_get_frame(id, traj_id, frame_idx)
  end function

  subroutine db_recook_all(id, status)
    integer(c_size_t), intent(in) :: id
    integer(c_int), intent(out) :: status
    status = rkrdb_recook_all(id)
  end subroutine

end module readcon_db