Clobber icon indicating copy to clipboard operation
Clobber copied to clipboard

Add a WITH-TRANSACTION macro

Open phoe opened this issue 3 years ago • 1 comments

https://github.com/robert-strandh/Clobber/blob/de32591d2f7cb0181d6623ba2505946e280937ea/demo.lisp#L73-L82

Maybe it would be good to give Clobber some love and add some sort of with-transaction macro that automatically opens and closes a log file via a dynamic variable binding unwind-protect and is idempotent (as in, it should detect when a transaction log is already open and then do nothing, deferring closing it to the outer forms or the user if at all).

@contrapunctus-1 - does it sound like something that you'd like to do, too?

phoe avatar Jan 19 '22 16:01 phoe

How to use with-transaction-log

Examples:

;;; first clone the latest version of clobber from github
(ql:quickload :clobber) 
#+(or)(in-package :clobber-demo/demo1) ; works too
#+(or)(in-package :clobber-demo/demo2) ; works too
(in-package :clobber-demo/demo3)

;;; Example 1. Logging to *standard-output* after executing the transactions from an input stream
(with-input-from-string (in "#2!(CLOBBER-DEMO/COMMON:NEW-BANK . #3!(#4![CLOBBER-DEMO/COMMON:BANK] . COMMON-LISP:NIL))")
  (clobber:with-transaction-log (log (cons in *standard-output*)
				     (lambda (just-read-transaction)
				       (format t "This is the object resulted from reading the transaction from the input stream:~%~A~%The type of the object is:~A~%~%" just-read-transaction (type-of just-read-transaction))))
    ;; we'll use (do-things) from the demos to see how with-transaction-log is supposed to be used
    (let ((*transaction-log* log)) ; (do-things) relies on *transaction-log*
      (format t "Executing transactions... The resulted log is:~%")
      (do-things))))

;;; Example 2. Logging to a file after executing the transactions already existent in that file
(clobber:with-transaction-log (log #p"my-database"
				   (lambda (revived-object)
				     ;; print just the type of the revived-object because the current print-object methods do not ignore unbound slots
				     (format t "The object resulted from reading the transaction from the input has the type:~A~%" (type-of revived-object))))
  ;; we'll use (do-things) from the demos to see how with-transaction-log is supposed to be used
  (let ((*transaction-log* log)) ; (do-things) relies on *transaction-log*
    (format t "Executing transactions... The resulted log is in the file my-database.~%")
    (do-things)))

Example 3. See it in action: test-serialize-hash-tables

rainthree avatar Jul 04 '24 13:07 rainthree