147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
tdbs)
(db:safely-close-sqlite3-db mtdbdat (dbr:dbdat-stmt-cache (dbr:subdb-mtdbdat subdb))))
(mutex-unlock! (dbr:subdb-stack-mutex subdb)))
subdbs)))
;; close opened run-id dbs that haven't been used in age seconds
(define (db:close-old dbstruct #!key (age 30)) ;; close dbs older than this age
(assert (dbr:dbstruct? dbstruct) "FATAL: db:close-all called with non-dbstruct "dbstruct)
(let* ((subdbs (hash-table-values (dbr:dbstruct-subdbs dbstruct))))
(for-each
(lambda (subdb)
(mutex-lock! (dbr:subdb-stack-mutex subdb))
(let* ((tdbs (stack->list (dbr:subdb-dbstack subdb)))
(mtdbdat (dbr:dbdat-dbh (dbr:subdb-mtdbdat subdb))))
(dbr:subdb-dbstack-set! subdb (make-stack)) ;; replace the stack with a new one
(map (lambda (dbdat)
(assert (dbr:dbdat-in-use dbdat) "FATAL: dbdat in stack was in use "(dbr:dbdat-dbfile dbdat))
(if (< (- (current-seconds)
(dbr:dbdat-last-used dbdat))
age)
(stack-push! (dbr:subdb-dbstack subdb) dbdat) ;; keep it
(let* ((stmt-cache (dbr:dbdat-stmt-cache dbdat)) ;; close and discard
(dbh (dbr:dbdat-dbh dbdat)))
(dbfile:print-err "INFO: closing unused dbdat for "(dbr:dbdat-dbfile dbdat))
|
|
|
|
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
tdbs)
(db:safely-close-sqlite3-db mtdbdat (dbr:dbdat-stmt-cache (dbr:subdb-mtdbdat subdb))))
(mutex-unlock! (dbr:subdb-stack-mutex subdb)))
subdbs)))
;; close opened run-id dbs that haven't been used in age seconds
(define (db:close-old dbstruct #!key (age 30)) ;; close dbs older than this age
(assert (dbr:dbstruct? dbstruct) "FATAL: db:close-old called with non-dbstruct "dbstruct)
(let* ((subdbs (hash-table-values (dbr:dbstruct-subdbs dbstruct))))
(for-each
(lambda (subdb)
(mutex-lock! (dbr:subdb-stack-mutex subdb))
(let* ((tdbs (stack->list (dbr:subdb-dbstack subdb)))
(mtdbdat (dbr:dbdat-dbh (dbr:subdb-mtdbdat subdb))))
(dbr:subdb-dbstack-set! subdb (make-stack)) ;; replace the stack with a new one
(map (lambda (dbdat)
(assert (not (dbr:dbdat-in-use dbdat)) "FATAL: dbdat in stack was in use "(dbr:dbdat-dbfile dbdat) " in use" (dbr:dbdat-in-use dbdat) "Stack length " (length tdbs) "time diff " (- (current-seconds) (dbr:dbdat-last-used dbdat)))
(if (< (- (current-seconds)
(dbr:dbdat-last-used dbdat))
age)
(stack-push! (dbr:subdb-dbstack subdb) dbdat) ;; keep it
(let* ((stmt-cache (dbr:dbdat-stmt-cache dbdat)) ;; close and discard
(dbh (dbr:dbdat-dbh dbdat)))
(dbfile:print-err "INFO: closing unused dbdat for "(dbr:dbdat-dbfile dbdat))
|
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
(dbfile:open-db dbstruct run-id init-proc))
(let* ((dbdat (dbfile:get-dbdat dbstruct run-id)))
(if dbdat
dbdat
(let* ((tmppath (dbr:dbstruct-tmppath dbstruct))
(tmpdbpath (dbfile:run-id->path tmppath run-id))
(dbdat (dbfile:open-sqlite3-db tmpdbpath init-proc sync-mode: 0 journal-mode: "WAL")))
;; the following line short-circuits the "one db handle per thread" model
;;
;; (dbfile:add-dbdat dbstruct run-id dbdat)
;;
dbdat))))))
;; COMBINE dbfile:open-sqlite-db and dbfile:lock-create-open
|
>
|
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
(dbfile:open-db dbstruct run-id init-proc))
(let* ((dbdat (dbfile:get-dbdat dbstruct run-id)))
(if dbdat
dbdat
(let* ((tmppath (dbr:dbstruct-tmppath dbstruct))
(tmpdbpath (dbfile:run-id->path tmppath run-id))
(dbdat (dbfile:open-sqlite3-db tmpdbpath init-proc sync-mode: 0 journal-mode: "WAL")))
(dbr:dbdat-in-use-set! dbdat #t)
;; the following line short-circuits the "one db handle per thread" model
;;
;; (dbfile:add-dbdat dbstruct run-id dbdat)
;;
dbdat))))))
;; COMBINE dbfile:open-sqlite-db and dbfile:lock-create-open
|