Add types.

draft
kimory 2022-10-24 12:33:25 +02:00
parent 8fc2a645c4
commit 10ba9a1a72
1 changed files with 36 additions and 7 deletions

View File

@ -66,6 +66,7 @@
; db/new ensures directory '<path>/data/' exist,
; then loads values from existing files, if any, and puts them into db-data.
(defun db/new (path)
(declare (type string path))
(let ((data (make-hash-table))
(data-dir-path (concatenate 'string path "/data/"))
(current-index 0))
@ -92,12 +93,15 @@
(concatenate 'string dbpath "/indexes/by_" index-name "/"))
; Example: returns "./storage/cars/partitions/by_color/Red/".
(declaim (ftype (function (string string string) string) db/partition/get-directory-path))
(defun db/partition/get-directory-path (dbpath name object-attribute)
(let ((value (value->safe-string object-attribute)))
(concatenate 'string dbpath "/partitions/by_" name "/" value "/")))
(declare (type string dbpath name object-attribute))
(concatenate 'string dbpath "/partitions/by_" name "/" object-attribute "/"))
; Example: "./storage/cars/" "name" #'vehicle-name object -> "./storage/cars/indexes/by_name/Corvet".
(defun db/index/get-symlink-path (dbpath index-name fsymbol object)
(declare (type string dbpath index-name))
(declare (type symbol fsymbol))
(let* ((value (funcall fsymbol object))
(symlink-basename (value->safe-string value)))
(concatenate 'string
@ -109,7 +113,10 @@
; :target "../../data/0000000015"
; :hard nil)
(defun db/index/new (dbpath index-name fsymbol object file-name)
(declare (type string dbpath index-name file-name))
(declare (type symbol fsymbol))
(let ((symlink-path (db/index/get-symlink-path dbpath index-name fsymbol object)))
(declare (type string symlink-path))
; works even when the database directory is moved
(osicat:make-link symlink-path
@ -117,22 +124,28 @@
:hard nil)))
(defun db/index/del (dbpath index-name fsymbol object)
(declare (type string dbpath index-name))
(declare (type symbol fsymbol))
(delete-file (db/index/get-symlink-path dbpath index-name fsymbol object)))
; Example: "./storage/cars/" "color" #'vehicle-color object "0000000015".
; -> "./storage/cars/partitions/by_color/Red/0000000015".
(defun db/partition/get-symlink-path (dbpath partition-name fsymbol object file-name)
(concatenate 'string
; example: "./storage/cars/partitions/by_color/Red/"
(db/partition/get-directory-path dbpath partition-name (funcall fsymbol object))
; example: "0000000015"
file-name))
(declare (type string dbpath partition-name file-name))
(declare (type symbol fsymbol))
(let* ((object-attribute (funcall fsymbol object))
(safe-value (value->safe-string object-attribute))
; example: "./storage/cars/partitions/by_color/Red/"
(dirpath (db/partition/get-directory-path dbpath partition-name safe-value)))
(concatenate 'string dirpath file-name)))
; Example: "./storage/cars/" "color" #'vehicle-color object "0000000015"
; -> (osicat:make-link "./storage/cars/partitions/by_color/Red/0000000015"
; :target "../../../data/0000000015"
; :hard nil)
(defun db/partition/new (dbpath partition-name fsymbol object file-name)
(declare (type string dbpath partition-name file-name))
(declare (type symbol fsymbol))
(let ((symlink-path
(db/partition/get-symlink-path dbpath partition-name fsymbol object file-name)))
@ -144,9 +157,13 @@
:hard nil)))
(defun db/partition/del (dbpath partition-name fsymbol object file-basename)
(declare (type string dbpath partition-name file-basename))
(declare (type symbol fsymbol))
(delete-file (db/partition/get-symlink-path dbpath partition-name fsymbol object file-basename)))
(defun db/new-index (database index-name fsymbol)
(declare (type string index-name))
(declare (type symbol fsymbol))
(let ((dbpath (db-path database))
(index-name (value->safe-string index-name)))
; create a directory for the indexes
@ -164,6 +181,8 @@
(db-data database))))
(defun db/partition/update (dbpath partition-name fsymbol object file-name old-object)
(declare (type string dbpath partition-name file-name))
(declare (type symbol fsymbol))
(let ((new-value (funcall fsymbol object))
(old-value (funcall fsymbol old-object)))
(if (not (equal new-value old-value))
@ -174,6 +193,8 @@
(db/partition/new dbpath partition-name fsymbol object file-name)))))
(defun db/new-partition (database partition-name fsymbol)
(declare (type string partition-name))
(declare (type symbol fsymbol))
(let ((dbpath (db-path database))
(partition-name (value->safe-string partition-name)))
; create a directory for the partitions
@ -191,7 +212,9 @@
(db-data database))))
; Example: "./storage/cars/data/000000000000018".
(declaim (ftype (function (string string) string) get-filepath))
(defun get-filepath (dbpath file-name)
(declare (type string dbpath file-name))
(concatenate 'string dbpath "/data/" file-name))
(defun db/add (database object)
@ -223,6 +246,7 @@
(db-current-index database))
(defun db/del (database object-index)
(declare (type integer object-index))
(let ((file-basename (number->filename object-index))
(dbpath (db-path database))
(object (gethash object-index (db-data database))))
@ -244,6 +268,7 @@
; Search for the data from the FS.
(defun db/get-by-index (database index-name attribute-value)
(declare (type string index-name))
(let ((value (value->safe-string attribute-value)))
(filename->integer
(osicat:read-link
@ -252,12 +277,15 @@
; Search for the data from the FS.
(defun db/get-by-partition (database name attribute-value)
(declare (type string name))
(let ((value (value->safe-string attribute-value))
(dbpath (db-path database)))
(loop for filename in (osicat:list-directory (db/partition/get-directory-path dbpath name value))
collect (filename->integer filename))))
(defun db/index/update (dbpath index-name fsymbol object file-name old-object)
(declare (type string dbpath index-name file-name))
(declare (type symbol fsymbol))
(let ((new-value (funcall fsymbol object))
(old-value (funcall fsymbol old-object))
(symlink-path (db/index/get-symlink-path dbpath index-name fsymbol object)))
@ -274,6 +302,7 @@
; TODO: check database integrity (redundancy)
; TODO: locking
(defun db/update (database object-index)
(declare (type integer object-index))
(let* ((object (gethash object-index (db-data database)))
(dbpath (db-path database))
(file-basename (number->filename object-index))