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