2.5. Hello world III


The final cut.

How (explicit) "rollback" works? To discover this kind of magic we're cooking hello_world3.c example.

Example 2-3. hello_world2.c

     1	#include <jf_file.h>
     2	int main()
     3	{
     4	        int rc;
     5	        jf_file_t jf;
     6	        size_t write;
     7	        rc = jf_file_open(&jf, NULL, "jf_tut_foo", "w", NULL);
     8	        if (JF_RC_OK != rc)
     9	                return 1;
    10	        rc = jf_file_printf(&jf, &write, "%s", "Hello");
    11	        if (JF_RC_OK != rc)
    12	                return 1;
    13	        rc = jf_file_rollback(&jf);
    14	        if (JF_RC_OK != rc)
    15	                return 1;
    16	        rc = jf_file_printf(&jf, &write, "%s", " world!\n");
    17	        if (JF_RC_OK != rc)
    18	                return 1;
    19	        rc = jf_file_commit(&jf);
    20	        if (JF_RC_OK != rc)
    21	                return 1;
    22	        rc = jf_file_close(&jf);
    23	        if (JF_RC_OK != rc)
    24	                return 1;
    25	        printf("Hello world III program is OK!\n");
    26	        return 0;
    27	}

Hello world III code explanation

Row 1

to use libjf a program must include at least jf_file.h header file

Row 5

declare object jf of type jf_file_t: jf is a "journaled file object"

Row 7

open (create & open) for write ("w") a journaled file of name jf_tut_foo and associate it to object jf; second argument (NULL) indicates a private journal must be used, fifth argument (NULL) tells default parameters must be used

Row 10

write to journaled file string "Hello"; second argument is used to retrieve the number of bytes stored to journaled file, other arguments mimic fprintf function

Row 13

rollback changes previously operated on journaled file jf: write of string "Hello" will be discarded (undone)

Row 16

write to journaled file different stuff (string " world!\n")

Row 19

commit changes previously operated on journaled file jf

Row 22

close journaled file

2.5.1. Hello world III compile & run

Use a command like this to compile hello_world3.c source code:

libtool --mode=link gcc -Wall -I/opt/libjf/include -L/opt/libjf/lib -ljf \
        -o hello_world3 hello_world3.c
remove temporary files wrote by first and/or second episode of hello world saga:
tiian@linux:~/tutorial> rm -f jf_tut_foo*
and run hello_world3 program:
tiian@linux:~/tutorial> ./hello_world3
Hello world III program is OK!
Take a look to journaled file and associated private journal:
tiian@linux:~/tutorial> ls -la jf_tut_foo*
-rw-r--r--  1 tiian users    8 2005-08-11 14:11 jf_tut_foo
-rw-r--r--  1 tiian users 8306 2005-08-11 14:11 jf_tut_foo.jf
inspect journaled file jf_tut_foo:
tiian@linux:~/tutorial> cat jf_tut_foo
The first string ("Hello") was not stored in journaled file because an explicit rollback was performed; inspect journal content:
tiian@linux:~/tutorial> jf_report -df -j jf_tut_foo.jf
<?xml version="1.0" encoding="UTF-8"?>
  <header magic_number='0x41524153' version='1' file_id_mask='0x8' 
      file_id_mask_shift='3' size_mask='0xfffffff0' size_mask_shift='4' 
      file_size='4194304' file_num='3' rotation_threshold='0.800' 
      ctrl_recs='36' journal_recs='8278' />
  <journaled_file_table max_files='2' number_of_files='2' 
    <file id='0' name='jf_tut_foo.jf' last_pos='8306' last_size='0' 
        status='0' last_uc_pos='0' last_uc_size='8306' stream='0x804b008' />
    <file id='1' name='jf_tut_foo' last_pos='8' last_size='8' status='2' 
        last_uc_pos='0' last_uc_size='0' stream='(nil)' />
    <append jrn_rec_off='8278' file_id='1' size='8' offset='0'>
      <data type='redo' format='hex'>20 77 6f 72 6c 64 21 0a </data>
      <data type='redo' format='text'> world! </data>
    <commit jrn_rec_off='8302' file_id='1'/>