db/new: loads old data. Grooming.

draft
Karchnu 2022-10-12 19:54:00 +02:00
parent 16fb640d2f
commit 080fcf426d
1 changed files with 20 additions and 11 deletions

View File

@ -16,6 +16,10 @@
(alias ln osicat:make-link)
(alias ls osicat:list-directory)
(alias basename pathname-name)
(defun filename->integer (filename)
(parse-integer (basename filename)))
(defun to-upper-case (some-string)
(format nil "~@:(~a~)" some-string))
@ -44,7 +48,14 @@
(defun db/new (struct-name path)
(ensure-directories-exist (concatenate 'string path "/data/"))
(make-db :struct-name struct-name :path path))
(let ((data (make-hash-table)))
(loop for filename in (ls (concatenate 'string path "/data/"))
do (let ((id (filename->integer filename))
(value (util:read-object-from-file filename)))
(setf (gethash id data) value)))
(make-db :struct-name struct-name :path path :data data)))
; Example: ./storage/cars 18 -> ./storage/cars/data/000000000000018
; USAGE: when dealing with hash keys from the in-memory db.
@ -165,23 +176,20 @@
; database.data[database.current-index] = object
(setf (gethash (db-current-index database) (db-data database)) object))
(alias basename pathname-name)
; Search for the data from the FS.
; TODO: get the hash index, too.
(defun db/search/fs/get-by-index (database index-name value)
(parse-integer
(basename
(osicat:read-link
(concatenate 'string
(db/index/get-directory-path database index-name) "/" value)))))
(filename->integer
(osicat:read-link
(concatenate 'string
(db/index/get-directory-path database index-name) "/" value))))
; (util:read-object-from-file (concatenate 'string
; (db/index/get-directory-path database index-name) "/" value)))
; Search for the data from the FS.
(defun db/search/fs/get-by-partition (database name value)
(loop for filename in (ls (db/partition/get-directory-path database name value))
collect (parse-integer (basename filename))))
collect (filename->integer filename)))
;; Search for the data from the in-memory data.
;; TODO: get the hash index, too.
@ -206,8 +214,9 @@
(defparameter cars (db/new "vehicle" "./storage/cars/"))
(db/add cars (make-vehicle :name "Corvet" :color "Red"))
(db/add cars (make-vehicle :name "Deudeuch" :color "Beige"))
;(db/add cars (make-vehicle :name "Corvet" :color "Red"))
;(db/add cars (make-vehicle :name "Ferrari" :color "Red"))
;(db/add cars (make-vehicle :name "Deudeuch" :color "Beige"))
(db/new-index cars "name")
(db/new-partition cars "color")