continuing to work on the dbcache
This commit is contained in:
parent
51be2041db
commit
59f58f9421
|
@ -3,7 +3,8 @@
|
||||||
(require (planet ryanc/db)
|
(require (planet ryanc/db)
|
||||||
(prefix-in whalesong: "../version.rkt")
|
(prefix-in whalesong: "../version.rkt")
|
||||||
racket/file
|
racket/file
|
||||||
racket/path)
|
racket/path
|
||||||
|
file/md5)
|
||||||
|
|
||||||
(define cache-directory-path
|
(define cache-directory-path
|
||||||
(build-path (find-system-path 'pref-dir)
|
(build-path (find-system-path 'pref-dir)
|
||||||
|
@ -16,7 +17,28 @@
|
||||||
(unless (directory-exists? cache-directory-path)
|
(unless (directory-exists? cache-directory-path)
|
||||||
(make-directory* cache-directory-path)))
|
(make-directory* cache-directory-path)))
|
||||||
|
|
||||||
(create-cache-directory!)
|
|
||||||
|
(define (ensure-cache-db-structure!)
|
||||||
|
(when (not (file-exists? whalesong-cache.sqlite3))
|
||||||
|
;; Clear existing cache files: they're obsolete.
|
||||||
|
(for ([file (directory-list cache-directory-path)])
|
||||||
|
(when (file-exists? (build-path cache-directory-path file))
|
||||||
|
(with-handlers ([exn:fail? void])
|
||||||
|
(delete-file (build-path cache-directory-path file)))))
|
||||||
|
|
||||||
|
(define conn
|
||||||
|
(sqlite3-connect #:database whalesong-cache.sqlite3
|
||||||
|
#:mode 'create))
|
||||||
|
(query-exec conn
|
||||||
|
(string-append
|
||||||
|
"create table cache(path string not null primary key, "
|
||||||
|
" md5sum string not null, "
|
||||||
|
"data blob not null);"))
|
||||||
|
(query-exec conn
|
||||||
|
"CREATE INDEX cache_md5sum_idx ON cache (md5sum);")
|
||||||
|
(disconnect conn)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(define whalesong-cache.sqlite3
|
(define whalesong-cache.sqlite3
|
||||||
(build-path cache-directory-path
|
(build-path cache-directory-path
|
||||||
|
@ -24,24 +46,43 @@
|
||||||
whalesong:version)))
|
whalesong:version)))
|
||||||
|
|
||||||
|
|
||||||
(when (not (file-exists? whalesong-cache.sqlite3))
|
(create-cache-directory!)
|
||||||
(define conn
|
(ensure-cache-db-structure!)
|
||||||
(sqlite3-connect #:database whalesong-cache.sqlite3
|
|
||||||
#:mode 'create))
|
|
||||||
(query-exec conn
|
|
||||||
(string-append
|
|
||||||
"create table cache(path string not null primary key, "
|
|
||||||
" md5sum string not null, "
|
|
||||||
"data blob not null);"))
|
|
||||||
(disconnect conn))
|
|
||||||
|
|
||||||
|
|
||||||
(define conn
|
(define conn
|
||||||
(sqlite3-connect #:database whalesong-cache.sqlite3))
|
(sqlite3-connect #:database whalesong-cache.sqlite3))
|
||||||
|
|
||||||
|
|
||||||
(define (cached? path)
|
(define lookup-cache-stmt
|
||||||
#f)
|
(prepare conn (string-append "select path, md5sum, data "
|
||||||
|
"from cache "
|
||||||
|
"where path=? and md5sum=?")))
|
||||||
|
(define insert-cache-stmt
|
||||||
|
(prepare conn (string-append "insert into cache(path, md5sum, data)"
|
||||||
|
" values (?, ?, ?);")))
|
||||||
|
|
||||||
(define (save-in-cache! path)
|
;; cached?:
|
||||||
(void))
|
(define (cached? path)
|
||||||
|
(cond
|
||||||
|
[(file-exists? path)
|
||||||
|
(query-maybe-row conn
|
||||||
|
lookup-cache-stmt
|
||||||
|
(path->string path)
|
||||||
|
(call-with-input-file* path md5))]
|
||||||
|
[else
|
||||||
|
#f]))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
;; save-in-cache!: path string -> void
|
||||||
|
;; Saves a record.
|
||||||
|
(define (save-in-cache! path data)
|
||||||
|
(cond
|
||||||
|
[(file-exists? path)
|
||||||
|
(query-exec conn
|
||||||
|
insert-cache-stmt
|
||||||
|
(path->string path)
|
||||||
|
(call-with-input-file* path md5)
|
||||||
|
data)]
|
||||||
|
[else
|
||||||
|
(error 'save-in-cache! "File ~e does not exist" path)]))
|
Loading…
Reference in New Issue
Block a user