Get by index and partition: done.

draft
Karchnu 2022-10-12 12:02:08 +02:00
parent b561984dcc
commit 49e155248a
1 changed files with 54 additions and 25 deletions

View File

@ -1,7 +1,4 @@
#!/usr/local/bin/clisp
;(load "~/.quicklisp/setup.lisp")
;(ql:quickload "osicat")
(ql:quickload "osicat")
(load "./util.cl")
(defpackage :dodb
@ -15,14 +12,17 @@
(in-package dodb)
;(defun ln (path &key target hard)
; (format t "SYMLINK: ~A -> ~A~&" path target))
; ; (osicat:make-link symlink-path
; ; :target (concatenate 'string "../../data/" file-name)
; ; :hard nil))
; (format t "LN: ~A -> ~A~&" path target))
;(defun ls (path)
; (format t "LS: ~A~&" path target))
; (osicat:make-link symlink-path
; :target (concatenate 'string "../../data/" file-name)
; :hard nil))
;(defmacro alias (to fn)
; `(setf (fdefinition ',to) #',fn))
;(alias ln osicat:make-link)
(defmacro alias (to fn)
`(setf (fdefinition ',to) #',fn))
(alias ln osicat:make-link)
(alias ls osicat:list-directory)
; TODO: equivalent in Crystal
; cars = DODB::DataBase(Car).new "./storage"
@ -41,7 +41,10 @@
; index = 1-1
; partition = 1-n
; String Path [String] [String] Int
(defstruct db struct-name path (indexes ()) (partitions ()) (current-index 0))
(defstruct db struct-name path (indexes ()) (partitions ()) (current-index 0)
; {Int -> struct}
(data (make-hash-table))
)
(defun db/new (struct-name path)
(ensure-directories-exist (concatenate 'string path "/data/"))
@ -72,14 +75,21 @@
(concatenate 'string (db-struct-name database) "-" attribute-name)))
object))
; Returns "Corvet".
; Example: returns "storage/cars/indexes/by_name/".
(defun db/index/get-directory-path (database name)
(concatenate 'string (db-path database) "/indexes/by_" name "/" ))
; Example: returns "storage/cars/partitions/by_color/".
(defun db/partition/get-directory-path (database name value)
(concatenate 'string (db-path database) "/partitions/by_" name "/" value "/"))
; Example: returns "Corvet".
(defun db/index/get-filename (database index-name object)
(get-object-attribute database index-name object))
(defun db/index/get-symlink-path (database index-name object)
(concatenate 'string
(db-path database)
"/indexes/by_" index-name "/"
(db/index/get-directory-path database index-name)
(db/index/get-filename database index-name object)))
(defun db/index/new (database index-name object file-name)
@ -92,15 +102,12 @@
:hard nil)
))
; Returns "Red".
(defun db/partition/get-filename (database partition-name object)
(get-object-attribute database partition-name object))
(defun db/partition/get-symlink-path (database partition-name object file-name)
(concatenate 'string
(db-path database)
"/partitions/by_" partition-name "/"
(db/partition/get-filename database partition-name object) "/"
(db/partition/get-directory-path database partition-name
; example: "Red"
(get-object-attribute database partition-name object))
; example: "000000000000018.data"
file-name
))
@ -135,24 +142,46 @@
(loop for index in (db-indexes database)
do (db/index/new database index object file-name))
; TODO: handle partitions
; handle partitions
(loop for partition in (db-partitions database)
do (db/partition/new database partition object file-name))
)
; store new in-memory data
(setf (gethash (db-current-index database) (db-data database)) object)
)
; test structure
(defstruct vehicle name color)
; TODO
(setf cars (db/new "vehicle" "./storage/cars/"))
(defparameter cars (db/new "vehicle" "./storage/cars/"))
(db/new-index cars "name")
(db/new-partition cars "color")
(db/add cars (make-vehicle :name "Corvet" :color "Red"))
(db/add cars (make-vehicle :name "Deudeuch" :color "Beige"))
(format t "~&~S~&" cars)
(defun db/get-by-index (database index-name value)
; (format t "db/get-by-index ~A => ~A" index-name value)
; TODO: should we search for the data from the FS or in-memory?
; (ls (db/index/get-directory-path database index-name))
(util:read-object-from-file (concatenate 'string
(db/index/get-directory-path database index-name) "/" value)))
(defun db/get-by-partition (database name value)
; (format t "db/get-by-partition ~A => ~A" name value)
;(ls (db/partition/get-directory-path database name value)))
(loop for car in (ls (db/partition/get-directory-path database name value))
collect (util:read-object-from-file car)))
(format t "db/get-by-index ~S~&" (db/get-by-index cars "name" "Corvet"))
(format t "db/get-by-partition red cars:~&")
(loop for car in (db/get-by-partition cars "color" "Red")
do (format t "- ~S~&" car))
; (setf cars_by_name (db/new-index cars "name"))
; (db/get-by-index cars "name" "foo"))
; (setf cars_by_name (db/new-index cars "name" (optional: (lambda (x) (vehicle-name x)))))