(sc-comment "this file contains declarations and macros to include for using sph-dg as a shared library.") (pre-include-once lmdb-h "lmdb.h") (sc-include-once sph "foreign/sph" sph-dg-status "main/lib/status" sph-dg-config "main/config") (sc-comment "data size is limited by lmdb max keysize, which is 511 byte by lmdb default but can be adjusted with recompiling lmdb") (pre-define-if-not-defined dg-id-t b64 dg-ordinal-t b32 dg-id-max UINT64_MAX dg-size-octets-data-max 511 dg-size-octets-data-min 1 dg-size-octets-id (sizeof dg-id-t) dg-size-octets-ordinal (sizeof dg-ordinal-t)) (pre-define-if-not-defined (dg-id-equal? a b) (= a b)) (pre-define-if-not-defined (dg-id-compare a b) (if* (< a b) -1 (> a b))) (pre-define dg-ordinal-compare dg-id-compare) (pre-define-if-not-defined (dg-pointer->id a index) (deref (+ index (convert-type a dg-id-t*)))) (pre-define imht-set-key-t dg-id-t dg-type-id 0 dg-type-intern 1 dg-type-extern 2 dg-type-intern-small 3 dg-type-mask 3 dg-id-type-step 4 dg-size-octets-relation-data (+ dg-size-octets-ordinal dg-size-octets-id) dg-size-octets-relation-key (* 2 dg-size-octets-id) dg-read-option-skip 1 dg-read-option-is-set-left 2 dg-read-option-is-set-right 4 dg-read-option-initialised 8 dg-null 0 ; dg-type-bit-* are used as filter arguments for dg-node-select for example dg-type-bit-id 1 dg-type-bit-intern 2 dg-type-bit-extern 4 dg-type-bit-intern-small 8) (sc-include "foreign/sph/imht-set") (define-type dg-data-t (struct (data b0*) (size size-t))) (define-type dg-data-record-t (struct (id dg-id-t) (size size-t) (data b0*))) (define-type dg-relation-record-t (struct (left dg-id-t) (right dg-id-t) (label dg-id-t) (ordinal dg-ordinal-t))) (pre-define mi-list-name-prefix dg-ids mi-list-element-t dg-id-t) (sc-include "foreign/sph/mi-list") (pre-define mi-list-name-prefix dg-data-list mi-list-element-t dg-data-t) (sc-include "foreign/sph/mi-list") (pre-define mi-list-name-prefix dg-data-records mi-list-element-t dg-data-record-t) (sc-include "foreign/sph/mi-list") (pre-define mi-list-name-prefix dg-relation-records mi-list-element-t dg-relation-record-t) (sc-include "foreign/sph/mi-list") (pre-define dg-ids-first mi-list-first dg-ids-first-address mi-list-first-address dg-ids-rest mi-list-rest dg-data-list-first mi-list-first dg-data-list-first-address mi-list-first-address dg-data-list-rest mi-list-rest dg-data-records-first mi-list-first dg-data-records-first-address mi-list-first-address dg-data-records-rest mi-list-rest dg-relation-records-first mi-list-first dg-relation-records-first-address mi-list-first-address dg-relation-records-rest mi-list-rest) (define-type dg-txn-t MDB-txn) (pre-define dg-txn-introduce (define dg-txn dg-txn-t* 0)) (pre-define dg-txn-begin (dg-mdb-status-require! (mdb-txn-begin dg-mdb-env 0 MDB-RDONLY (address-of dg-txn)))) (pre-define dg-txn-abort (mdb-txn-abort dg-txn) (set dg-txn 0)) (pre-define dg-txn-commit (dg-mdb-status-require! (mdb-txn-commit dg-txn)) (set dg-txn 0)) (pre-define dg-txn-write-begin (dg-mdb-status-require! (mdb-txn-begin dg-mdb-env 0 0 (address-of dg-txn)))) (pre-define (dg-intern-small-id->data id) ;dg-id-t -> dg-id-t (bit-shift-right id 2)) (pre-define (dg-intern-small-data->id data) ;dg-id-t -> dg-id-t (bit-and dg-type-intern-small (bit-shift-left data 2))) (define-type dg-statistics-t (struct (id->data MDB-stat) (data-intern->id MDB-stat) (data-extern->extern MDB-stat) (left->right MDB-stat) (right->left MDB-stat) (label->left MDB-stat))) (define-type dg-init-options-t (struct (read-only? b8) (maximum-size-octets size-t) (maximum-reader-count b32) (filesystem-has-ordered-writes? b8) (env-open-flags b32) (file-permissions b16))) (define-type dg-index-errors-relation-t (struct (errors? boolean) (missing-right-left dg-relation-records-t*) (missing-label-left dg-relation-records-t*) (excess-right-left dg-relation-records-t*) (excess-label-left dg-relation-records-t*))) (define-type dg-index-errors-intern-t (struct (errors? boolean) (different-data-id dg-ids-t*) (excess-data-id dg-ids-t*) (different-id-data dg-ids-t*) (missing-id-data dg-ids-t*))) (define-type dg-index-errors-extern-t (struct (errors? boolean) (different-data-extern dg-ids-t*) (excess-data-extern dg-ids-t*) (different-id-data dg-ids-t*) (missing-id-data dg-ids-t*))) (define dg-index-errors-relation-null dg-index-errors-relation-t (struct-literal 0 0 0 0 0)) (define dg-index-errors-intern-null dg-index-errors-intern-t (struct-literal 0 0 0 0 0)) (define dg-index-errors-extern-null dg-index-errors-extern-t (struct-literal 0 0 0 0 0)) (define-type dg-ordinal-match-data-t (struct (min dg-ordinal-t) (max dg-ordinal-t))) (define-type dg-node-read-state-t (struct (status status-t) (cursor (MDB-cursor* restrict)) (types b8) (options b8))) (define-type dg-intern-read-state-t (struct (status status-t) (cursor (MDB-cursor* restrict)) (options b8))) (define-type dg-relation-read-state-t (struct (status status-t) (cursor (MDB-cursor* restrict)) (cursor-2 (MDB-cursor* restrict)) (left b0*) (right b0*) (label b0*) (left-first dg-ids-t*) (right-first dg-ids-t*) (ordinal dg-ordinal-match-data-t*) (options b8) (reader b0*))) (define-type dg-relation-reader-t (function-pointer status-t dg-relation-read-state-t* b32 dg-relation-records-t**)) (define-type dg-relation-ordinal-generator-t (function-pointer dg-ordinal-t b0*)) (pre-define (dg-type? dg-type-name id) (= dg-type-name (bit-and id dg-type-mask))) (define (dg-node-read state count result) (status-t dg-node-read-state-t* b32 dg-data-records-t**)) (define (dg-node-select txn types offset state) (status-t dg-txn-t* b8 b32 dg-node-read-state-t*)) (define (dg-node-selection-destroy state) (b0 dg-node-read-state-t*)) (define (dg-relation-selection-destroy state) (b0 dg-relation-read-state-t*)) (define (dg-intern-data->id txn data every? result) (status-t dg-txn-t* dg-data-list-t* boolean dg-ids-t**)) (define (dg-intern-ensure txn data result) (status-t dg-txn-t* dg-data-list-t* dg-ids-t**)) (define (dg-intern-update txn id data) (status-t dg-txn-t* dg-id-t dg-data-t)) (define (dg-extern-update txn id data) (status-t dg-txn-t* dg-id-t dg-data-t)) (define (dg-intern-id->data txn ids every? result) (status-t dg-txn-t* dg-ids-t* boolean dg-data-list-t**)) (define (dg-exit) b0) (define (dg-extern-create txn count data result) (status-t dg-txn-t* b32 dg-data-t* dg-ids-t**)) (define (dg-extern-id->data txn ids every? result) (status-t dg-txn-t* dg-ids-t* boolean dg-data-list-t**)) (define (dg-extern-data->id txn data result) (status-t dg-txn-t* dg-data-t dg-ids-t**)) (define (dg-id-create txn count result) (status-t dg-txn-t* b32 dg-ids-t**)) (define (dg-exists? txn ids result) (status-t dg-txn-t* dg-ids-t* boolean*)) (define (dg-extern? id) (boolean dg-id-t)) (define (dg-id? id) (boolean dg-id-t)) (define (dg-intern-small? id) (boolean dg-id-t)) (define (dg-identify txn ids result) (status-t dg-txn-t* dg-ids-t* dg-ids-t**)) (define (dg-intern? id) (boolean dg-id-t)) (define (dg-relation? id) (boolean dg-id-t)) (define (dg-index-errors-extern txn result) (status-t dg-txn-t* dg-index-errors-extern-t*)) (define (dg-index-errors-intern txn result) (status-t dg-txn-t* dg-index-errors-intern-t*)) (define (dg-index-errors-relation txn result) (status-t dg-txn-t* dg-index-errors-relation-t*)) (define (dg-index-recreate-extern) status-t) (define (dg-index-recreate-intern) status-t) (define (dg-index-recreate-relation) status-t) (define (dg-init dg-root-argument options) (status-t b8* dg-init-options-t*)) (define (dg-init-options-set-defaults a) (dg-init-options-t dg-init-options-t*)) (define (dg-relation-ensure txn left right label ordinal-generator ordinal-generator-state) (status-t dg-txn-t* dg-ids-t* dg-ids-t* dg-ids-t* dg-relation-ordinal-generator-t b0*)) (define (dg-statistics txn result) (status-t dg-txn-t* dg-statistics-t*)) (define (dg-delete txn ids) (status-t dg-txn-t* dg-ids-t*)) (define (dg-relation-delete txn left right label ordinal) (status-t dg-txn-t* dg-ids-t* dg-ids-t* dg-ids-t* dg-ordinal-match-data-t*)) (define (dg-status-description a) (b8* status-t)) (define (dg-status-name a) (b8* status-t)) (define (dg-status-group-id->name a) (b8* status-i-t)) (define (dg-relation-select txn left right label ordinal offset result) (status-t dg-txn-t* dg-ids-t* dg-ids-t* dg-ids-t* dg-ordinal-match-data-t* b32 dg-relation-read-state-t*)) (define (dg-relation-read state count result) (status-t dg-relation-read-state-t* b32 dg-relation-records-t**)) (define dg-root b8* dg-initialised boolean dg-mdb-env MDB-env*) (pre-define (dg-pointer-allocation-set result expression result-temp) (set result-temp expression) (if result-temp (set result result-temp) (dg-status-set-id-goto dg-status-id-memory))) (pre-define (dg-ids-add! target source ids-temp) (dg-pointer-allocation-set target (dg-ids-add target source) ids-temp)) (pre-define (dg-define-ids name) (define name dg-ids-t* 0)) (pre-define (dg-define-ids-2 name-1 name-2) (dg-define-ids name-1) (dg-define-ids name-2)) (pre-define (dg-define-ids-3 name-1 name-2 name-3) (dg-define-ids-2 name-1 name-2) (dg-define-ids name-3)) (pre-define (dg-relation-data->id a) (dg-pointer->id (+ 1 (convert-type a dg-ordinal-t*)) 0)) (pre-define (dg-relation-data->ordinal a) (deref (convert-type a dg-ordinal-t*))) (pre-define (dg-relation-data-set-id a value) (set (dg-relation-data->id a) value)) (pre-define (dg-relation-data-set-ordinal a value) (set (dg-relation-data->ordinal a) value)) (pre-define (dg-relation-data-set-both a ordinal id) (dg-relation-data-set-ordinal ordinal) (dg-relation-data-set-id id)) (define (dg-debug-log-ids a) (b0 dg-ids-t*)) (define (dg-debug-log-ids-set a) (b0 imht-set-t)) (define (dg-debug-display-relation-records records) (b0 dg-relation-records-t*)) (define (dg-debug-count-all-btree-entries txn result) (status-t MDB-txn* b32*)) (define (dg-debug-display-btree-counts txn) (status-t MDB-txn*)) (define (dg-debug-display-content-left->right txn) (status-t dg-txn-t*)) (define (dg-debug-display-content-right->left txn) (status-t dg-txn-t*))