Changeset 3035 for trunk/bknr/datastore/src/data/tutorial.lisp
- Timestamp:
- 04/29/08 23:48:20 (4 months ago)
- Files:
-
- trunk/bknr/datastore/src/data/tutorial.lisp (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/bknr/datastore/src/data/tutorial.lisp
r2609 r3035 113 113 ;;; transaction, and logged to disk. 114 114 115 (PROGN 116 (DEFUN TX-DECF-COUNTER () 117 (UNLESS (IN-TRANSACTION-P) (ERROR 'NOT-IN-TRANSACTION)) 118 (DECF (TUTORIAL-STORE-COUNTER *STORE*))) 119 (DEFUN DECF-COUNTER (&REST #:G3047) 120 (EXECUTE 121 (MAKE-INSTANCE 'TRANSACTION 122 :FUNCTION-SYMBOL 123 'TX-DECF-COUNTER 124 :TIMESTAMP 125 (GET-UNIVERSAL-TIME) 126 :ARGS 127 #:G3047)))) 115 (PROGN (DEFUN TX-DECF-COUNTER () 116 (UNLESS (IN-TRANSACTION-P) (ERROR 'NOT-IN-TRANSACTION)) 117 (DECF (TUTORIAL-STORE-COUNTER *STORE*))) 118 (DEFUN DECF-COUNTER () 119 (EXECUTE (MAKE-INSTANCE 'TRANSACTION 120 :FUNCTION-SYMBOL 'TX-DECF-COUNTER 121 :TIMESTAMP (GET-UNIVERSAL-TIME) 122 :ARGS (LIST))))) 128 123 129 124 ;;; The new datastore only supports a single datastore instance per 130 ;;; LISP session. When creating a `STORE' object, the `*STORE*'125 ;;; LISP instance. When creating a `STORE' object, the `*STORE*' 131 126 ;;; special variable is modified to point to the datastore. Thus, we 132 127 ;;; can create our simple datastore by creating an object of type … … 213 208 ; => T 214 209 (restore-store *store*) 215 ; Warning: restoring #<TUTORIAL-STORE DIR: "/tmp/tutorial-store/"> 216 ; Warning: loading transaction log 217 ; /tmp/tutorial-store/current/transaction-log 218 ; executing transaction #$(TX-INCF-COUNTER) at timestamp 3309258381 219 ; executing transaction #$(TX-INCF-COUNTER) at timestamp 3309258383 220 ; executing transaction #$(TX-DECF-COUNTER) at timestamp 3309258387 221 ; executing transaction #$(TX-INCF-COUNTER) at timestamp 3309258390 222 ; => :NORMAL 210 ; restoring #<TUTORIAL-STORE DIR: "/tmp/tutorial-store/"> 211 ; loading transaction log /tmp/tutorial-store/current/transaction-log 212 ; executing transaction #<TRANSACTION 21.04.2008 07:08:22 TX-INCF-COUNTER > at timestamp 3417743302 213 ; executing transaction #<TRANSACTION 21.04.2008 07:08:25 TX-INCF-COUNTER > at timestamp 3417743305 214 ; executing transaction #<TRANSACTION 21.04.2008 07:08:26 TX-DECF-COUNTER > at timestamp 3417743306 215 ; executing transaction #<TRANSACTION 21.04.2008 07:08:34 TX-INCF-COUNTER > at timestamp 3417743314 216 ; => NIL 223 217 (tutorial-store-counter *store*) 224 218 ; => 2 225 (restore-store *store* :until 3309258387) 226 ; Warning: restoring #<TUTORIAL-STORE DIR: "/tmp/tutorial-store/"> 227 ; Warning: loading transaction log 228 ; /tmp/tutorial-store/current/transaction-log 229 ; executing transaction #$(TX-INCF-COUNTER) at timestamp 3309258381 230 ; executing transaction #$(TX-INCF-COUNTER) at timestamp 3309258383 231 ; executing transaction #$(TX-DECF-COUNTER) at timestamp 3309258387 232 ; => :NORMAL 219 ; !! Update the timestamp below to correspond to the fist transaction executed above !! 220 (restore-store *store* :until 3417743302) ... 221 ; restoring #<TUTORIAL-STORE DIR: "/tmp/tutorial-store/"> 222 ; loading transaction log /tmp/tutorial-store/current/transaction-log 223 ; executing transaction #<TRANSACTION 21.04.2008 07:08:22 TX-INCF-COUNTER > at timestamp 3417743302 224 ; creating log file backup: /tmp/tutorial-store/current/transaction-log.backup 225 ; truncating transaction log at position 42. 233 226 (tutorial-store-counter *store*) 234 227 ; => 1 … … 304 297 ; => #<TUTORIAL-STORE DIR: "/tmp/tutorial-store/"> 305 298 (snapshot) 299 ; Snapshotting subsystem #<COUNTER-SUBSYSTEM #xE65F866> of #<TUTORIAL-STORE DIR: "/tmp/tutorial-store/"> 300 ; Successfully snapshotted #<COUNTER-SUBSYSTEM #xE65F866> of #<TUTORIAL-STORE DIR: "/tmp/tutorial-store/"> 306 301 ; => NIL 307 302 (restore) 308 ; Warning: restoring #<TUTORIAL-STORE DIR: "/tmp/tutorial-store/"> 309 ; => :NORMAL 303 ; restoring #<TUTORIAL-STORE DIR: "/tmp/tutorial-store/"> 304 ; Restoring the subsystem #<COUNTER-SUBSYSTEM #xE65F866> of #<TUTORIAL-STORE DIR: "/tmp/tutorial-store/"> 305 ; => NUL 310 306 311 307 ;;;# An object store example … … 346 342 347 343 ; Warning: restoring #<MP-STORE DIR: "/tmp/object-store/"> 348 ; => #<MP-STORE DIR: "/tmp/object-store/"> 344 ; restoring #<MP-STORE DIR: "/tmp/object-store/"> 345 ; Restoring the subsystem #<STORE-OBJECT-SUBSYSTEM #xE63F866> of #<MP-STORE DIR: "/tmp/object-store/"> 349 346 (all-store-objects) 350 347 ; => NIL … … 371 368 372 369 (make-object 'store-object) 373 ; executing transaction #$(TX-MAKE-OBJECT STORE-OBJECT) 374 ; at timestamp 3309260107 370 ; executing transaction #<TRANSACTION 21.04.2008 08:02:10 TX-MAKE-OBJECT STORE-OBJECT ID 2> at timestamp 3417746530 375 371 ; => #<STORE-OBJECT ID: 12> 376 372 (store-object-with-id 2) 377 373 ; => #<STORE-OBJECT ID: 2> 378 374 (delete-object (store-object-with-id 2)) 379 ; executing transaction #$(TX-DELETE-OBJECT 2) 380 ; at timestamp 3309260112 375 ; executing transaction #<TRANSACTION 21.04.2008 08:52:14 TX-DELETE-OBJECT 2> at timestamp 3417749534 381 376 ; => T 382 377 (store-object-with-id 2) … … 438 433 ; #<TUTORIAL-OBJECT ID: 4> #<TUTORIAL-OBJECT ID: 5>) 439 434 440 ;;; A basic transaction used to work on persistent objects isthe441 ;;; transaction `CHANGE-SLOT-VALUES', which sets the values of slots442 ;;; in an object. The value of a persistent slot can not be changed443 ;;; outside of a transaction, as restoring the datastore would not444 ;;; change the slot value.435 ;;; In order to change the slot values of persistent object, the 436 ;;; application needs to be in a transaction context. This can be 437 ;;; done either by invoking a named transaction as above, or by 438 ;;; creating an anonymous transaction. In an anonymous transaction, 439 ;;; all write accesses to persistent objects are logged. 445 440 446 441 (define-persistent-class tutorial-object2 () … … 448 443 449 444 (make-object 'tutorial-object2 :b 3) 450 ; executing transaction #$(TX-MAKE-OBJECT TUTORIAL-OBJECT2 B 3) 451 ; at timestamp 3309263046 452 ; => #<TUTORIAL-OBJECT2 ID: 16> 453 (setf (slot-value (store-object-with-id 16) 'b) 4) 445 ; executing transaction #<TRANSACTION 21.04.2008 08:03:27 TX-MAKE-OBJECT TUTORIAL-OBJECT2 ID 6 B 3> at timestamp 3417746607 446 ; => #<TUTORIAL-OBJECT2 ID: 6> 447 (setf (slot-value (store-object-with-id 6) 'b) 4) 454 448 ; => Error 455 (change-slot-values (store-object-with-id 16) 'b 4) 456 ; executing transaction #$(TX-CHANGE-SLOT-VALUES 457 ; #<TUTORIAL-OBJECT2 ID: 16> B 4) at timestamp 3309263109 458 ; => NIL459 (tutorial-object2-b (store-object-with-id 16))449 ; Attempt to set persistent slot B of #<TUTORIAL-OBJECT2 ID: 6> outside of a transaction 450 (with-transaction () 451 (setf (slot-value (store-object-with-id 6) 'b) 4)) 452 ; => 4 453 (tutorial-object2-b (store-object-with-id 6)) 460 454 ; => 4 461 455 … … 484 478 ;;; We can modify the slot `A' outside a transaction: 485 479 (make-object 'protocol-object :a 1 :b 2) 486 ; executing transaction #$(TX-MAKE-OBJECT PROTOCOL-OBJECT A 1 B 2) 487 ; at timestamp 3309262613 488 ; => #<PROTOCOL-OBJECT ID: 14> 489 (setf (protocol-object-a *) 2) 480 ; executing transaction #<TRANSACTION 21.04.2008 08:10:49 TX-MAKE-OBJECT PROTOCOL-OBJECT ID 7 A 1 B 2> at timestamp 3417747049 481 ; => #<PROTOCOL-OBJECT ID: 7> 482 (setf (protocol-object-a (store-object-with-id 7)) 2) 490 483 ; => 2 491 484 … … 493 486 ;;; has to be changed inside a transaction. 494 487 495 (setf (protocol-object-b (store-object-with-id 14)) 4)488 (setf (protocol-object-b (store-object-with-id 7)) 4) 496 489 ; => Error 490 ; Attempt to set persistent slot B of #<PROTOCOL-OBJECT ID: 7> outside of a transaction 497 491 498 492 ;;; An object can be removed from the datastore using the transaction … … 507 501 ;;; using `SNAPSHOT'. 508 502 (snapshot) 509 ; Warning: Backup of the datastore in 510 ; /tmp/object-store/20041112T153046/. 511 ; Warning: 512 ; Snapshotting subsystem #<STORE-OBJECT-SUBSYSTEM {49396ED5}> 513 ; of #<MP-STORE DIR: "/tmp/object-store/">... 514 ; Warning: 515 ; Successfully snapshotted #<STORE-OBJECT-SUBSYSTEM {49396ED5}> 516 ; of #<MP-STORE DIR: "/tmp/object-store/">. 517 ; => NIL 503 ; Snapshotting subsystem #<STORE-OBJECT-SUBSYSTEM #xE54991E> of #<MP-STORE DIR: "/tmp/object-store/"> 504 ; Successfully snapshotted #<STORE-OBJECT-SUBSYSTEM #xE54991E> of #<MP-STORE DIR: "/tmp/object-store/"> 518 505 519 506 ;;; This will create a backup directory containing the old transaction … … 522 509 523 510 (directory "/tmp/object-store/**/*.*") 524 ; => (#p"/tmp/object-store/20041112T153046/" 525 ; #p"/tmp/object-store/20041112T153046/transaction-log" 526 ; #p"/tmp/object-store/current/" 527 ; #p"/tmp/object-store/current/store-object-subsystem-snapshot") 511 ; => (#P"/tmp/object-store/20080421T061210/random-state" 512 ; #P"/tmp/object-store/20080421T061210/transaction-log" 513 ; #P"/tmp/object-store/current/random-state" 514 ; #P"/tmp/object-store/current/store-object-subsystem-snapshot" 515 ; #P"/tmp/object-store/current/transaction-log") 528 516 529 517 ;;; The snapshot file contains all persistent objects present at … … 549 537 550 538 (make-object 'gorilla :name "lucy" :mood :aggressive) 551 ; => #<GORILLA ID: 17>539 ; => #<GORILLA ID: 8> 552 540 (make-object 'gorilla :name "john" :mood :playful) 553 ; => #<GORILLA ID: 18>541 ; => #<GORILLA ID: 9> 554 542 (make-object 'gorilla :name "peter" :mood :playful) 555 ; => #<GORILLA ID: 1 9>543 ; => #<GORILLA ID: 10> 556 544 (gorilla-with-name "lucy") 557 ; => #<GORILLA ID: 17>545 ; => #<GORILLA ID: 8> 558 546 (gorillas-with-mood :playful) 559 ; => (#<GORILLA ID: 19> #<GORILLA ID: 18>) 560 561 ;;;## Exporting store objects to XML 562 563 ;;; Exporting store objects to XML is not possible right now, but it 564 ;;; will soon be available in the BKNR Framework. Stay tuned. 547 ; => (#<GORILLA ID: 10> #<GORILLA ID: 9>) 565 548 566 549 ;;;## Adding blobs … … 599 582 ;;; adding the blob subsystem to its list of subsystems. 600 583 584 (close-store) 601 585 (make-instance 'mp-store :directory "/tmp/object-store/" 602 586 :subsystems (list … … 619 603 ;;; object in the blob root directory of the blob subsystem. 620 604 621 (make-blob-from-file "/tmp/bla. png" 'photo :name "foobar"622 :type : png)623 ; => #<PHOTO ID: 1 6, TYPE: png>605 (make-blob-from-file "/tmp/bla.jpg" 'photo :name "foobar" 606 :type :jpg) 607 ; => #<PHOTO ID: 11, TYPE: jpg> 624 608 625 609 ;;; We can work with the photo object in the same way as when we work … … 629 613 ;;; object. 630 614 631 (blob-pathname (store-object-with-id 1 6))632 ; => # p"/tmp/object-store/blob-root/16"615 (blob-pathname (store-object-with-id 11)) 616 ; => #P"/tmp/object-store/blob-root/11" 633 617 634 618 ;;; The method `BLOB-TO-FILE' and `BLOB-TO-STREAM' write the binary … … 657 641 658 642 (make-object 'relaxed-object) 659 ; => #<RELAXED-OBJECT ID: 20>643 ; => #<RELAXED-OBJECT ID: 12> 660 644 (make-object 'relaxed-object) 661 ; => #<RELAXED-OBJECT ID: 21>662 ( change-slot-values (store-object-with-id 19)663 'a (store-object-with-id 20))664 ; => NIL665 (delete-object (store-object-with-id 20))645 ; => #<RELAXED-OBJECT ID: 13> 646 (with-transaction () 647 (setf (slot-value (store-object-with-id 12) 'a) (store-object-with-id 13))) 648 ; => #<RELAXED-OBJECT ID: 13> 649 (delete-object (store-object-with-id 13)) 666 650 ; => T 667 651 (snapshot) 668 ; Warning: 669 ; Encoding reference to destroyed object with ID 20 670 ; from slot A of object RELAXED-OBJECT with ID 19. 652 ; Warning: Backup of the datastore in /tmp/object-store/20080421T064811/. 653 ; While executing: (:INTERNAL (SNAPSHOT-STORE (STORE))), in process worker(1750). 654 ; Snapshotting subsystem #<STORE-OBJECT-SUBSYSTEM #xE6069EE> of #<MP-STORE DIR: "/tmp/object-store/"> 655 ; Warning: Encoding reference to destroyed object with ID 13 from slot A of object RELAXED-OBJECT with ID 12. 656 ; While executing: #<STANDARD-METHOD ENCODE-OBJECT (STORE-OBJECT T)>, in process worker(1750). 657 ; Successfully snapshotted #<STORE-OBJECT-SUBSYSTEM #xE6069EE> of #<MP-STORE DIR: "/tmp/object-store/"> 658 ; Snapshotting subsystem #<BLOB-SUBSYSTEM #xE6069CE> of #<MP-STORE DIR: "/tmp/object-store/"> 659 ; Successfully snapshotted #<BLOB-SUBSYSTEM #xE6069CE> of #<MP-STORE DIR: "/tmp/object-store/"> 671 660 ; => NIL 672 661 (restore) 673 ; Warning: restoring #<MP-STORE DIR: "/tmp/object-store/"> 674 ; Warning: 675 ; loading snapshot file 676 ; /tmp/object-store/current/store-object-subsystem-snapshot 677 ; Warning: 678 ; Reference to inexistent object with id 20 in 679 ; relaxed slot A of object with class RELAXED-OBJECT with ID 19. 680 ; => :NORMAL 681 (relaxed-object-a (store-object-with-id 19)) 662 ; restoring #<MP-STORE DIR: "/tmp/object-store/"> 663 ; Restoring the subsystem #<STORE-OBJECT-SUBSYSTEM #xE6069EE> of #<MP-STORE DIR: "/tmp/object-store/"> 664 ; loading snapshot file /tmp/object-store/current/store-object-subsystem-snapshot 665 ; Warning: internal inconsistency during restore: can't find store object 13 in loaded store 666 ; While executing: %DECODE-STORE-OBJECT, in process worker(1754). 667 ; Warning: Reference to inexistent object with id 13 from unnamed container, returning NIL. 668 ; While executing: %DECODE-STORE-OBJECT, in process worker(1754). 669 ; Restoring the subsystem #<BLOB-SUBSYSTEM #xE6069CE> of #<MP-STORE DIR: "/tmp/object-store/"> 670 ; loading transaction log /tmp/object-store/current/transaction-log 671 ; executing transaction #<ANONYMOUS-TRANSACTION 21.04.2008 08:48:48 PREPARE-FOR-SNAPSHOT NIL> at timestamp 3417749328 672 (relaxed-object-a (store-object-with-id 12)) 682 673 ; => NIL 683 674
