Index: db.scm ================================================================== --- db.scm +++ db.scm @@ -92,17 +92,21 @@ (define (db:hoh-get dat key1 key2) (let* ((subhash (hash-table-ref/default dat key1 #f))) (and subhash (hash-table-ref/default subhash key2 #f)))) +;; the stmt-cache is associated with a single db handle +;; so there is no need for the hoh stuff. (define (db:get-cache-stmth dbdat run-id db stmt) - (let* (;; (dbdat (dbfile:get-dbdat dbstruct run-id)) - (stmt-cache (dbr:dbdat-stmt-cache dbdat)) - (stmth (db:hoh-get stmt-cache db stmt))) + (let* ((stmt-cache (dbr:dbdat-stmt-cache dbdat)) + ;; (stmth (db:hoh-get stmt-cache db stmt)) + (stmth (hash-table-ref/default stmt-cache stmt #f)) + ) (or stmth (let* ((newstmth (sqlite3:prepare db stmt))) - (db:hoh-set! stmt-cache db stmt newstmth) + ;; (db:hoh-set! stmt-cache db stmt newstmth) + (hash-table-set! stmt-cache stmt newstmth) newstmth)))) ;;====================================================================== ;; SQLITE3 HELPERS ;;====================================================================== Index: dbfile.scm ================================================================== --- dbfile.scm +++ dbfile.scm @@ -62,22 +62,22 @@ ;; (defstruct dbr:subdb (dbname #f) ;; .megatest/1.db (mtdbfile #f) ;; mtrah/.megatest/1.db (mtdbdat #f) ;; only need one of these for syncing - ;; (dbdats (make-hash-table)) ;; id => dbdat (tmpdbfile #f) ;; /tmp/.../.megatest/1.db - ;; (refndbfile #f) ;; /tmp/.../.megatest/1.db_ref - (dbstack (make-stack)) ;; stack for tmp dbr:dbdat, + (dbstack (make-stack)) ;; stack for tmp dbr:dbdat, ie. dbdats (homehost #f) ;; not used yet (on-homehost #f) ;; not used yet (read-only #f) (last-sync 0) (last-write (current-seconds)) ) ;; goal is to converge on one struct for an area but for now it is too confusing ;; need to keep dbhandles and cached statements together +;; This is the handle and associated file for a single db +;; (defstruct dbr:dbdat (dbfile #f) (dbh #f) (stmt-cache (make-hash-table)) (read-only #f) @@ -116,50 +116,37 @@ (print "Attempt to safely close sqlite3 db failed. Trying again. exn=" exn) (thread-sleep! 3) (sqlite3:interrupt! db) (db:safely-close-sqlite3-db db stmt-cache try-num: (- try-num 1))) (if (sqlite3:database? db) - (let* ((stmts (and stmt-cache (hash-table-ref/default stmt-cache db #f)))) - (if stmts (map sqlite3:finalize! (hash-table-values stmts))) + ;; (let* ((stmts (and stmt-cache (hash-table-ref/default stmt-cache #f)))) + (begin + (if stmt-cache + (map sqlite3:finalize! (hash-table-values stmt-cache))) (sqlite3:finalize! db) #t) (begin (dbfile:print-err "db:safely-close-sqlite3-db: " db " is not an sqlite3 db") - #f - ) - )))) + #f))))) ;; close all opened run-id dbs (define (db:close-all dbstruct) (if (dbr:dbstruct? dbstruct) -;; (handle-exceptions -;; exn -;; (begin -;; (debug:print 0 *default-log-port* "WARNING: Finalizing failed, " ((condition-property-accessor 'exn 'message) exn) ", note - exn=" exn) -;; (print-call-chain *default-log-port*)) - ;; (db:sync-touched dbstruct 0 force-sync: #t) ;; NO. Do not do this here. Instead we rely on a server to be started when there are writes, even if the server itself is not going to be used as a server. - (let* ((subdbs (hash-table-values (dbr:dbstruct-subdbs dbstruct)))) - (for-each - (lambda (subdb) - (let* ((tdbs (stack->list (dbr:subdb-dbstack subdb))) - (mtdbdat (dbr:dbdat-dbh (dbr:subdb-mtdbdat subdb))) - #;(rdb (dbr:dbdat-dbh (dbr:subdb-refndb subdb)))) - - (map (lambda (dbdat) - (let* ((stmt-cache (dbr:dbdat-stmt-cache dbdat)) - (dbh (dbr:dbdat-dbh dbdat))) - (db:safely-close-sqlite3-db dbh stmt-cache))) - tdbs) - (db:safely-close-sqlite3-db mtdbdat (dbr:dbdat-stmt-cache (dbr:subdb-mtdbdat subdb))) - ;; (if (sqlite3:database? mdb) (sqlite3:finalize! mdb)) - #;(db:safely-close-sqlite3-db rdb #f))) ;; stmt-cache))))) ;; (if (sqlite3:database? rdb) (sqlite3:finalize! rdb)))))) - subdbs) - #t - ) - #f - ) -) + (let* ((subdbs (hash-table-values (dbr:dbstruct-subdbs dbstruct)))) + (for-each + (lambda (subdb) + (let* ((tdbs (stack->list (dbr:subdb-dbstack subdb))) + (mtdbdat (dbr:dbdat-dbh (dbr:subdb-mtdbdat subdb)))) + (map (lambda (dbdat) + (let* ((stmt-cache (dbr:dbdat-stmt-cache dbdat)) + (dbh (dbr:dbdat-dbh dbdat))) + (db:safely-close-sqlite3-db dbh stmt-cache))) + tdbs) + (db:safely-close-sqlite3-db mtdbdat (dbr:dbdat-stmt-cache (dbr:subdb-mtdbdat subdb))))) + subdbs) + #t) + #f)) ;; ;; set up a single db (e.g. main.db, 1.db ... etc.) ;; ;; ;; (define (db:setup-db dbstruct areapath run-id) ;; (let* ((dbname (db:run-id->dbname run-id))