fortran-tcdb

September 9, 2020 · View on GitHub

A collection of ISO C binding interfaces to the Tokyo Cabinet key-value database for Fortran 2008. At the moment, only hash databases are supported.

Tokyo Cabinet

The interfaces are split across several modules:

  • tchdb.f90: File-based hash database API.
  • tcmdb.f90: On-memory hash database API.
  • tclist.f90: Array List API.
  • tcmisc.f90: Miscellaneous utilities API (date & time, MD5).
  • tcutil.f90: Includes hack to access Tokyo Cabinet version number.

Tested with Tokyo Cabinet 1.4.48 on FreeBSD 12 with GNU Fortran 9, but should be compatible to other Unix-like operating systems and Fortran 2008 compilers.

String Arguments

In some cases, convenience routines are used to add c_null_char to string arguments automatically. Performance is therefore slightly decreased, as an additional function call is necessary. To avoid these wrappers, affected interfaces are exposed directly, with a trailing underscore in their name, but null termination must be handled manually:

! Calling the wrapper function that does null termination for us:
err = tc_hdb_put2(hdb, 'foo', 'bar')

! Calling the interface directly:
err = tc_hdb_put2_(hdb, 'foo' // c_null_char, 'bar' // c_null_char)

Build

At first, install Tokyo Cabinet. On FreeBSD, run:

# pkg install databases/tokyocabinet

Development headers may be required on Linux. Then, compile the static library libfortran-tcdb.a with:

$ make static

Make sure that PREFIX points to the correct path. Link Tokyo Cabinet with libfortran-tcdb.a and -ltokyocabinet -lz -lbz2 -lrt -lpthread -lm -lc.

Example

! example.f90
program main
    use, intrinsic :: iso_c_binding, only: c_ptr
    use :: tchdb
    implicit none
    type(c_ptr)                   :: hdb
    character(len=:), allocatable :: value
    logical                       :: err

    hdb = tc_hdb_new()
    err = tc_hdb_open(hdb, 'casket.tch', ior(HDB_OWRITER, HDB_OCREAT))
    err = tc_hdb_put2(hdb, 'foo', 'bar')

    value = tc_hdb_get2(hdb, 'foo')
    print '(2a)', 'value: ', value

    err = tc_hdb_close(hdb)
    call tc_hdb_del(hdb)
end program main

Compile and run the example with:

$ gfortran -I/usr/local/include/ -L/usr/local/lib/ -o example example.f90 libfortran-tcdb.a -ltokyocabinet
$ ./example

Further Examples

Additional examples can be found in examples/:

  • hdb opens a hash database and does read/write operations.
  • mdb creates an on-memory hash database to store strings.
  • list pushes strings to an array list.
  • tcv prints the Tokyo Cabinet version number (in case you really need this info …).

Build them with:

$ make examples

Coverage

Array List API

C Function NameFortran Interface NameBound
tclistbsearchtc_list_bsearch
tclistcleartc_list_clear
tclistdeltc_list_del
tclistdumptc_list_dump
tclistduptc_list_dup
tclistinserttc_list_insert
tclistinsert2tc_list_insert2, tc_list_insert2_
tclistloadtc_list_load
tclistlsearchtc_list_lsearch
tclistnewtc_list_new
tclistnew2tc_list_new2
tclistnew3
tclistnumtc_list_num
tclistovertc_list_over
tclistover2tc_list_over2, tc_list_over2_
tclistpoptc_list_pop
tclistpop2tc_list_pop2
tclistpushtc_list_push
tclistpush2tc_list_push2, tc_list_push2_
tclistremovetc_list_remove
tclistremove2tc_list_remove2, tc_list_remove2_
tclistshifttc_list_shift
tclistshift2tc_list_shift2, tc_list_shift2_
tclistsorttc_list_sort
tclistunshifttc_list_unshift
tclistunshift2tc_list_unshift2, tc_list_unshift2_
tclistvaltc_list_val
tclistval2tc_list_val2, tc_list_val2_

On-Memory Hash Database API

C Function NameFortran Interface NameBound
tcmdbadddoubletc_mdb_add_double
tcmdbaddinttc_mdb_add_int
tcmdbcutfronttc_mdb_cut_front
tcmdbdeltc_mdb_del
tcmdbfwmkeystc_mdb_fwm_keys
tcmdbfwmkeys2tc_mdb_fwm_keys2, tc_mdb_fwm_keys2_
tcmdbgettc_mdb_get
tcmdbget2tc_mdb_get2, tc_mdb_get2_
tcmdbiterinittc_mdb_iter_init
tcmdbiternexttc_mdb_iter_next
tcmdbiternext2tc_mdb_iter_next2
tcmdbmsiztc_mdb_msiz
tcmdbnewtc_mdb_new
tcmdbnew2tc_mdb_new2
tcmdbouttc_mdb_out
tcmdbout2tc_mdb_out2, tc_mdb_out2_
tcmdbputtc_mdb_put
tcmdbput2tc_mdb_put2, tc_mdb_put2_
tcmdbputcattc_mdb_put_cat
tcmdbputcat2tc_mdb_put_cat2, tc_mdb_put_cat2_
tcmdbputkeeptc_mdb_put_keep
tcmdbputkeep2tc_mdb_put_keep2, tc_mdb_put_keep2_
tcmdbrnumtc_mdb_rnum
tcmdbvanishtc_mdb_vanish
tcmdbvsiztc_mdb_vsiz
tcmdbvsiz2tc_mdb_vsiz2, tc_mdb_vsiz2_

Hash Database API

C Function NameFortran Interface NameBound
tchdbadddoubletc_hdb_add_double
tchdbaddinttc_hdb_add_int
tchdbclosetc_hdb_close
tchdbcopytc_hdb_copy, tc_hdb_copy_
tchdbdeltc_hdb_del
tchdbecodetc_hdb_ecode
tchdberrmsgtc_hdb_err_msg
tchdbfsiztc_hdb_fsiz
tchdbfwmkeystc_hdb_fwm_keys
tchdbfwmkeys2tc_hdb_fwm_keys2, tc_hdb_fwm_keys2_
tchdbgettc_hdb_get
tchdbget2tc_hdb_get2, tc_hdb_get2_
tchdbget3tc_hdb_get3
tchdbiterinittc_hdb_iter_init
tchdbiternexttc_hdb_iter_next
tchdbiternext2tc_hdb_iter_next2
tchdbiternext3tc_hdb_iter_next3
tchdbnewtc_hdb_new
tchdbopentc_hdb_open, tc_hdb_open_
tchdboptimizetc_hdb_optimize
tchdbouttc_hdb_out
tchdbout2tc_hdb_out2, tc_hdb_out2_
tchdbpathtc_hdb_path
tchdbputtc_hdb_put
tchdbput2tc_hdb_put2, tc_hdb_put2_
tchdbputasynctc_hdb_put_async
tchdbputasync2tc_hdb_put_async2, tc_hdb_put_async2_
tchdbputcattc_hdb_put_cat
tchdbputcat2tc_hdb_put_cat2, tc_hdb_put_cat2_
tchdbputkeeptc_hdb_put_keep
tchdbputkeep2tc_hdb_put_keep2, tc_hdb_put_keep2_
tchdbrnumtc_hdb_rnum
tchdbsetdfunittc_hdb_set_dfunit
tchdbsetcachetc_hdb_set_cache
tchdbsetmutextc_hdb_set_mutex
tchdbsetxmsiztc_hdb_set_xmsiz
tchdbsynctc_hdb_sync
tchdbtranaborttc_hdb_tran_abort
tchdbtranbegintc_hdb_tran_begin
tchdbtrancommittc_hdb_tran_commit
tchdbtunetc_hdb_tune
tchdbvanishtc_hdb_vanish
tchdbvsiztc_hdb_vsiz
tchdbvsiz2tc_hdb_vsiz2, tc_hdb_vsiz2_

Basic Utilities API

C Variable NameFortran Interface NameBound
*tcversiontc_version

Miscellaneous Utilities API

C Function NameFortran Interface NameBound
tcdatestrwwwtc_date_str_www
tcmd5hashtc_md5_hash
tcstrmktimetc_str_mk_time

Licence

ISC