README + prevent data inconsistency on db/add.
This commit is contained in:
parent
b29badbad0
commit
94d522e778
@ -6,6 +6,12 @@ When storing simple files directly on the file-system is enough.
|
||||
|
||||
This project is a work in progress.
|
||||
|
||||
# Constraints (WIP)
|
||||
|
||||
TODO:
|
||||
- parallelism: run dodb on several threads (solved by a locking system)
|
||||
- fs data integrity: in case of a crash, dodb won't corrupt data on disk (solved by a temp file on the same filesystem, then a simple mv)
|
||||
|
||||
# Crystal version
|
||||
|
||||
Follow [this link for the Crystal version of DODB][dodbcr].
|
||||
|
36
src/dodb.cl
36
src/dodb.cl
@ -62,8 +62,6 @@
|
||||
(setf current-index id))
|
||||
(setf (gethash id data) value)))
|
||||
|
||||
(format t "Max current index: ~A~&" current-index)
|
||||
|
||||
(make-db
|
||||
:struct-name struct-name
|
||||
:path path
|
||||
@ -172,7 +170,7 @@
|
||||
(db-data database)))
|
||||
|
||||
; Example: database -> "000000000000018".
|
||||
(defun db/add/new-data-filename (database)
|
||||
(defun db/add/new-data-basename (database)
|
||||
(number->filename (db-current-index database)))
|
||||
|
||||
; Example: {db-path}/data/000000000000018
|
||||
@ -181,20 +179,23 @@
|
||||
|
||||
(defun db/add (database object)
|
||||
(incf (db-current-index database))
|
||||
(let ((file-name (db/add/new-data-filename database)))
|
||||
(let* ((file-basename (db/add/new-data-basename database))
|
||||
(tmp-file-basename (concatenate 'string file-basename "_tmp"))
|
||||
(tmp-file-path (get-filepath database tmp-file-basename)))
|
||||
|
||||
; write object to file
|
||||
(util:write-object-to-file
|
||||
object
|
||||
(get-filepath database file-name))
|
||||
; write object to temporary file
|
||||
(util:write-object-to-file object tmp-file-path)
|
||||
|
||||
; rename the temporary file
|
||||
(rename-file tmp-file-path file-basename)
|
||||
|
||||
; handle indexes
|
||||
(loop for index in (db-indexes database)
|
||||
do (db/index/new database index object file-name))
|
||||
do (db/index/new database index object file-basename))
|
||||
|
||||
; handle partitions
|
||||
(loop for partition in (db-partitions database)
|
||||
do (db/partition/new database partition object file-name)))
|
||||
do (db/partition/new database partition object file-basename)))
|
||||
|
||||
; store new in-memory data
|
||||
; database.data[database.current-index] = object
|
||||
@ -249,6 +250,11 @@
|
||||
(alias db/get-by-index db/search/fs/get-by-index)
|
||||
(alias db/get-by-partition db/search/fs/get-by-partition)
|
||||
|
||||
;(defun db/update (database object-index)
|
||||
; (let ((object (gethash object-index (db-data database))))
|
||||
; ; TODO: write the object on the FS
|
||||
; ))
|
||||
|
||||
;
|
||||
; TEST SCENARIO
|
||||
;
|
||||
@ -257,11 +263,11 @@
|
||||
|
||||
(defparameter cars (db/new "vehicle" "./storage/cars/"))
|
||||
|
||||
;(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/add cars (make-vehicle :name "BMW" :color "Blue"))
|
||||
;(db/add cars (make-vehicle :name "Suzuki Wagon" :color "Blue"))
|
||||
(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/add cars (make-vehicle :name "BMW" :color "Blue"))
|
||||
(db/add cars (make-vehicle :name "Suzuki Wagon" :color "Blue"))
|
||||
|
||||
(db/new-index cars "name")
|
||||
(db/new-partition cars "color")
|
||||
|
Loading…
Reference in New Issue
Block a user