Get by index and partition: done.
parent
b561984dcc
commit
49e155248a
79
src/dodb.cl
79
src/dodb.cl
|
@ -1,7 +1,4 @@
|
||||||
#!/usr/local/bin/clisp
|
(ql:quickload "osicat")
|
||||||
|
|
||||||
;(load "~/.quicklisp/setup.lisp")
|
|
||||||
;(ql:quickload "osicat")
|
|
||||||
|
|
||||||
(load "./util.cl")
|
(load "./util.cl")
|
||||||
(defpackage :dodb
|
(defpackage :dodb
|
||||||
|
@ -15,14 +12,17 @@
|
||||||
(in-package dodb)
|
(in-package dodb)
|
||||||
|
|
||||||
;(defun ln (path &key target hard)
|
;(defun ln (path &key target hard)
|
||||||
; (format t "SYMLINK: ~A -> ~A~&" path target))
|
; (format t "LN: ~A -> ~A~&" path target))
|
||||||
; ; (osicat:make-link symlink-path
|
;(defun ls (path)
|
||||||
; ; :target (concatenate 'string "../../data/" file-name)
|
; (format t "LS: ~A~&" path target))
|
||||||
; ; :hard nil))
|
; (osicat:make-link symlink-path
|
||||||
|
; :target (concatenate 'string "../../data/" file-name)
|
||||||
|
; :hard nil))
|
||||||
|
|
||||||
;(defmacro alias (to fn)
|
(defmacro alias (to fn)
|
||||||
; `(setf (fdefinition ',to) #',fn))
|
`(setf (fdefinition ',to) #',fn))
|
||||||
;(alias ln osicat:make-link)
|
(alias ln osicat:make-link)
|
||||||
|
(alias ls osicat:list-directory)
|
||||||
|
|
||||||
; TODO: equivalent in Crystal
|
; TODO: equivalent in Crystal
|
||||||
; cars = DODB::DataBase(Car).new "./storage"
|
; cars = DODB::DataBase(Car).new "./storage"
|
||||||
|
@ -41,7 +41,10 @@
|
||||||
; index = 1-1
|
; index = 1-1
|
||||||
; partition = 1-n
|
; partition = 1-n
|
||||||
; String Path [String] [String] Int
|
; 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)
|
(defun db/new (struct-name path)
|
||||||
(ensure-directories-exist (concatenate 'string path "/data/"))
|
(ensure-directories-exist (concatenate 'string path "/data/"))
|
||||||
|
@ -72,14 +75,21 @@
|
||||||
(concatenate 'string (db-struct-name database) "-" attribute-name)))
|
(concatenate 'string (db-struct-name database) "-" attribute-name)))
|
||||||
object))
|
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)
|
(defun db/index/get-filename (database index-name object)
|
||||||
(get-object-attribute database index-name object))
|
(get-object-attribute database index-name object))
|
||||||
|
|
||||||
(defun db/index/get-symlink-path (database index-name object)
|
(defun db/index/get-symlink-path (database index-name object)
|
||||||
(concatenate 'string
|
(concatenate 'string
|
||||||
(db-path database)
|
(db/index/get-directory-path database index-name)
|
||||||
"/indexes/by_" index-name "/"
|
|
||||||
(db/index/get-filename database index-name object)))
|
(db/index/get-filename database index-name object)))
|
||||||
|
|
||||||
(defun db/index/new (database index-name object file-name)
|
(defun db/index/new (database index-name object file-name)
|
||||||
|
@ -92,15 +102,12 @@
|
||||||
:hard nil)
|
: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)
|
(defun db/partition/get-symlink-path (database partition-name object file-name)
|
||||||
(concatenate 'string
|
(concatenate 'string
|
||||||
(db-path database)
|
(db/partition/get-directory-path database partition-name
|
||||||
"/partitions/by_" partition-name "/"
|
; example: "Red"
|
||||||
(db/partition/get-filename database partition-name object) "/"
|
(get-object-attribute database partition-name object))
|
||||||
|
; example: "000000000000018.data"
|
||||||
file-name
|
file-name
|
||||||
))
|
))
|
||||||
|
|
||||||
|
@ -135,24 +142,46 @@
|
||||||
(loop for index in (db-indexes database)
|
(loop for index in (db-indexes database)
|
||||||
do (db/index/new database index object file-name))
|
do (db/index/new database index object file-name))
|
||||||
|
|
||||||
; TODO: handle partitions
|
; handle partitions
|
||||||
(loop for partition in (db-partitions database)
|
(loop for partition in (db-partitions database)
|
||||||
do (db/partition/new database partition object file-name))
|
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
|
; test structure
|
||||||
(defstruct vehicle name color)
|
(defstruct vehicle name color)
|
||||||
|
|
||||||
; TODO
|
(defparameter cars (db/new "vehicle" "./storage/cars/"))
|
||||||
(setf cars (db/new "vehicle" "./storage/cars/"))
|
|
||||||
|
|
||||||
(db/new-index cars "name")
|
(db/new-index cars "name")
|
||||||
(db/new-partition cars "color")
|
(db/new-partition cars "color")
|
||||||
|
|
||||||
(db/add cars (make-vehicle :name "Corvet" :color "Red"))
|
(db/add cars (make-vehicle :name "Corvet" :color "Red"))
|
||||||
|
(db/add cars (make-vehicle :name "Deudeuch" :color "Beige"))
|
||||||
|
|
||||||
(format t "~&~S~&" cars)
|
(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"))
|
; (setf cars_by_name (db/new-index cars "name"))
|
||||||
; (db/get-by-index cars "name" "foo"))
|
; (db/get-by-index cars "name" "foo"))
|
||||||
; (setf cars_by_name (db/new-index cars "name" (optional: (lambda (x) (vehicle-name x)))))
|
; (setf cars_by_name (db/new-index cars "name" (optional: (lambda (x) (vehicle-name x)))))
|
||||||
|
|
Loading…
Reference in New Issue