Megatest

Diff
Login

Differences From Artifact [0f7631a253]:

To Artifact [ff07e9b5e3]:


406
407
408
409
410
411
412


413
414
415
416
417
418
419





420

421
422
423
424
425
426
427
428
429
430
431
432

433
434
435
436
437
438
439
440
441
442
443
444
445


446

447

448


449
450
451

452
453
454
455
456
457
458
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426

427
428
429
430
431
432
433
434
435
436
437
438

439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454

455
456
457

458
459
460
461
462
463
464
465
466
467
468
469
470







+
+







+
+
+
+
+
-
+











-
+













+
+
-
+

+
-
+
+



+







		    "cp "backupfname" "fname)))
    (dbfile:print-err "WARNING: attempting recovery of file "fname" by running commands:\n"
		      "  "cmd)
    (system cmd)))

;; opens and returns handle and nothing else
;;
;; NOTE: this is already protected by mutex *no-sync-db-mutex*
;;
(define (dbfile:raw-open-no-sync-db dbpath)
  (if (not (file-exists? dbpath))
      (create-directory dbpath #t))
  (debug:print-info 0 *default-log-port* "Opening "dbpath"/no-sync.db")
  (let* ((dbname    (conc dbpath "/no-sync.db"))
	 (db-exists (file-exists? dbname))
	 (init-proc (lambda (db)
		      (sqlite3:with-transaction
		       db
		       (lambda ()
			 ;; I have been having trouble with init of no-sync.db so
			 ;; doing the init in a transaction every time (no gating
		      (if (not db-exists)
			 ;; on file existance.
			  (for-each
			   (lambda (stmt)
			     (sqlite3:execute db stmt))
			   (list
			    "CREATE TABLE IF NOT EXISTS no_sync_metadat
                                (var TEXT,
                                 val TEXT,
                                   CONSTRAINT no_sync_metadat_constraint UNIQUE (var));"
			    "CREATE TABLE IF NOT EXISTS no_sync_locks 
                                (key TEXT,
                                 val TEXT,
                                   CONSTRAINT no_sync_metadat_constraint UNIQUE (var));")))))
                                   CONSTRAINT no_sync_metadat_constraint UNIQUE (key));"))))))
	 (on-tmp      (equal? (car (string-split dbpath "/")) "tmp"))
	 (db        (if on-tmp
			(dbfile:cautious-open-database dbname init-proc 0 "WAL")
			(dbfile:cautious-open-database dbname init-proc 0 #f)
			;; (sqlite3:open-database dbname)
			)))
    (if on-tmp	      ;; done in cautious-open-database
	(begin
	  (sqlite3:execute db "PRAGMA synchronous = 0;")
	  (sqlite3:set-busy-handler! db (sqlite3:make-busy-timeout 136000))))
    db))

(define (dbfile:with-no-sync-db dbpath proc)
  (mutex-lock! *no-sync-db-mutex*)
  (let* ((already-open *no-sync-db*)
  (let* ((db  (dbfile:raw-open-no-sync-db dbpath))
	 (db  (or already-open (dbfile:raw-open-no-sync-db dbpath)))
	 (res (proc db)))
    (if (not already-open)
    (sqlite3:finalize! db)
	(sqlite3:finalize! db))
    (mutex-unlock! *no-sync-db-mutex*)
    res))

(define *no-sync-db-mutex* (make-mutex))

(define (dbfile:open-no-sync-db dbpath)
  (mutex-lock! *no-sync-db-mutex*)
  (let* ((res (if *no-sync-db*
		  *no-sync-db*
		  (let* ((db (dbfile:raw-open-no-sync-db dbpath)))
		    (set! *no-sync-db* db)
		    db))))