Megatest

Check-in [2639de81a8]
Login
Overview
Comment:Cleaned up prepared db statements handling.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | v1.80-refactor
Files: files | file ages | folders
SHA1: 2639de81a8e087a3cd7ac4880113ed1c91d7366c
User & Date: matt on 2023-01-16 14:09:15
Other Links: branch diff | manifest | tags
Context
2023-01-16
15:53
Most sqlite3: calls now prepared and cached. check-in: bcf8145611 user: matt tags: v1.80-refactor
14:09
Cleaned up prepared db statements handling. check-in: 2639de81a8 user: matt tags: v1.80-refactor
12:17
last of overt connection issues addressed. TCP connections growing very slowly. However now the db issues are manifesting. check-in: 22f2c33711 user: matt tags: v1.80-transport-cleanish, v1.80-refactor
Changes

Modified db.scm from [97e60b5bf8] to [6457b2d810].

90
91
92
93
94
95
96


97
98
99
100




101
102
103


104
105
106
107
108
109
110
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







+
+

-
-
-
+
+
+
+


-
+
+







	  (db:hoh-set! dat key1 key2 val)))))

(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
;;======================================================================

(define (db:general-sqlite-error-dump exn stmt . params)

Modified dbfile.scm from [5825d84245] to [20e94c199d].

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
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







-

-
-
+








+
+








;; NOTE: Need one dbr:subdb per main.db, 1.db ...
;;
(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)
  (birth-sec   (current-seconds)))

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
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







-
-
+
+
+
+




-
-
-
+




-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
-
+
+
-
-
+
-
-







	  exn
	(begin
	  (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)))
      (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))) 
	     (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
	 subdbs)
        #t)
          )
          #f
      #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))
;; 	 (dbstruct (hash-table-ref/default dbstructs dbname #f)))
;;     (if dbstruct