From 94d522e778a2f78fa0051febb3a0155a98b444b8 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Fri, 14 Oct 2022 17:48:48 +0200 Subject: [PATCH] README + prevent data inconsistency on db/add. --- README.md | 6 ++++++ src/dodb.cl | 36 +++++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index a6d109f..04d292e 100644 --- a/README.md +++ b/README.md @@ -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]. diff --git a/src/dodb.cl b/src/dodb.cl index 667f23d..e05602b 100644 --- a/src/dodb.cl +++ b/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")