source: branches/items/PERL2ARB/DOC.txt

Last change on this file was 18746, checked in by westram, 3 years ago
  • adds a script which generates a list of perl interface functions
    • call during arb build
  • update DOC.txt
    • mention that list
    • very basic description of interface
    • describe the ancient information which already has been in this document.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.9 KB
Line 
1
2Perl interface to ARB database library
3--------------------------------------
4
51. provides a low-level interface to access arb databases.
6
7   This works as standalone feature.
8
92. this interface is also used by the macro functionality integrated in arb.
10
11   This works only with an instance of arb running.
12   That instance will be remote controlled using these methods:
13
14        BIO::remote_action      clicks a named button or menu entry
15        BIO::remote_awar        writes or
16        BIO::remote_read_awar   reads the value on a GUI-toggle or -inputfield.
17
18
19While building the ARB perl module (make perl),
20the following list will be generated:
21    ./perl-interface-function-list.txt
22
23This contains an up-to-date list of all available function and
24pointers to available documentation, if any.
25
26
27Some example scripts are available in the directory
28     ../PERL_SCRIPTS
29
30The easiest way to execute these scripts is from a shell
31started from inside arb using the menu
32        Tools/Start XTERM
33That shell will already have the correct setup to run the arb perl.
34
35
36
37
38--------------------------------------------------------------------------------
39Rest below is an older document describing howto use the arb perl interface
40by examples. It is probably out-of-date, but might nevertheless contains
41some general information you might find useful.
42--------------------------------------------------------------------------------
43
44
45
46
47ARB_DB
48
49Basic idea:
50
51        All data is stored hierarchically in database elements.
52
53        A database element is a small structure which stores:
54                - key
55                - type  (integer/string...)
56                - it's value
57                - last modification time
58                - access rights
59                ...
60
61        A value itself might be a set of database elements.
62
63        There are 10 basic data types:
64        Type            C/C++ enum      PERL_NAME
65        ----------------------------------------------------------------
66        String          GB_STRING       STRING  // Null terminated string
67        Integer         GB_INT          INT     // 32 bit integer
68        Float           GB_FLOAT        FLOAT   // real number
69        BitString       GB_BITS         BITS    // any bitstring
70        Integer String  GB_INTS         INTS    // array of integer
71        Container       GB_DB           CONTAINER // a set of other elements
72
73
74        ****************************** Example 1 ***************
75
76        Let's say we want to store the following key value pairs in the
77        database:
78                name    niels
79                tel     37543345
80                sex     male
81                age     40
82
83        So all we have to do is to create 4 database elements of type
84        STRING and store the data into it:
85
86                $gb_main = ARB::open("new_db.arb","wc");        // create new db
87                                // we are operating
88                                // in a multi user env., so we have to
89                                // get exclusive rights to the server:
90                ARB::begin_transaction($gb_main);
91                                // create a dbelem key = "name"
92                                //                      type = "STRING"
93                $gb_name = ARB::create($gb_main,"name","STRING");
94                                // and store a value in the dbelem
95                $error = ARB::write_string($gb_name,"niels");
96                        ...
97                        ...
98                ARB::commit_transaction($gb_main);      // everything is ok
99                $error = ARB::save($gb_main,"new_db.arb", "a"); // save in ascii format
100
101        ***************************** END 1 *********************
102
103
104        What has been done:
105
106                The first argument of nearly all ARB-Database (ARBDB) functions
107                is an existing database element.
108                You may change or read this element or use it to find or
109                create other elements.
110
111                First we create a new database "new_db.arb".
112                ARB::open returns the root element of the database.
113                Before we can actually read or write to it we have to
114                get exclusive rights by calling
115                        GB_begin_transaction( root_element_of_database );
116                To make all changes to the database persistent we have
117                to call GB_commit_transaction, or if we want to undo all
118                recent changes GB_abort_transaction.
119                Because the root node of the database is always a container
120                we can simply add new items to it.
121
122
123        ****************************** Example 2 ***************
124
125        We have the same situation as in Example 1 but we want to store
126        the name, tel .. of different persons in the database.
127
128        So we simply store the person's data not in the root container,
129        but in a person container.
130
131                $gb_main = ....
132                ARB::begin_transaction($gb_main);
133
134                for all persons do {
135                        $gb_person = ARB::create_container($gb_main,"person");
136                        $gb_name = ARB::create($gb_person,"name","STRING");
137                        $error = ARB::write_string($gb_name,"name of p");
138                        if ($error) .....
139                        $gb_tel = ARB::create($gb_person,"tel", STRING);
140                        $error = ARB::write_string($gb_tel,"telnr of person");
141                }
142                commit and save
143
144        ******************************* END 2 ***************************
145
146        What's new in the last example:
147
148                We created sub containers for each person. All this
149                have the key 'person', so in this case the key is really
150                useless. Some people might think that it would be much
151                better to give each container the name of the person.
152                But that would limit you to search for only one field.
153                So we decided not to index the keys of the database
154                elements but their values. So you should not use the
155                key for indexing.
156
157
158Searching the database:
159        There are three main ways to find your data:
160
161                1. Use the key of the elements:
162                        This is good if all your keys are unique in
163                        a container and if there are only a few elements
164                        in a container.
165
166                2. Use the value of the elements:
167                        This works like grep in a filesystem. Go through
168                        all elements and  inspect the value.
169                        This can be indexed, and is therefor much faster
170                        and more flexible than the search by key method.
171
172                3. Walk through the database by hand:
173                        All elements within one container are stored as
174                        a linked list. A small loop allows you to create
175                        any user defined query
176
177        All searching is done by two functions:
178                find and search
179        Both functions start the search at a user defined database elem
180        (first argument).
181
182        ARB::search is used, if all keys in a container are unique.
183        example:
184
185        $gb_searched = ARB::search($gb_start,"key1","none");
186                searches in the container $gb_start for an element with
187                the key 'key1'
188
189        $gb_searched = ARB::search($gb_start,"key1/key2","none");
190                searches a container $gb_h with key 'key1' in $gb_start
191                and if found continues searching in $gb_h for an element
192                with key 'key2'
193
194        The last parameter enables ARB::search to create an element if it
195        has not found any. To do this simply enter the type of the new
196        element as the third argument. This is really usefull, if you want
197        to store information in an existing database and you do not know
198        whether an element exists already or not.
199
200
201        ARB::find is used if not all keys are unique
202
203        Like ARB::search it starts the search at a given element.
204        It's basic syntax looks like this:
205
206        ARB::find($gb_start_point, "[opt key]", "[opt value]", "mode");
207                where mode can be:      "this"
208                                        "down"
209                                        "down_2"
210                                        "this_next"
211                                        "down_next"
212
213        if "[opt key]" != "" than look only for items with that key
214        if "[opt value]" != "" than return only those items which value
215                        mach the "[opt value]"
216                Any '*' maches any number of characters
217                Any '?' maches exactly one character
218                the rest maches itself
219
220        If opt key is the searched key and opt value == "" and mode ==
221        "down" than ARB::find will behave like ARB::search.
222
223        mode indicates the direction of the search:
224        "down"   means that $gb_start_point is a container and it
225                looks for an element in that container
226        "down_2" means that $gb_start_point is a container and all its
227                elements are containers and it looks for elements in
228                the subcontainers of $gb_start_point
229        "this"  forces ARB::search to look for a brother
230                of $gb_start_point.
231        "this_next" continues a search started by mode == "down",
232                The first parameter should be the last hit
233        "down_next" continues a search started by mode == "down_2",
234                The first parameter should be the father of the last hit.
235                To get the father of an element call
236                        ARB::get_father($gb_elem);
237
238
239
240        ****************************** Example 3 ***************
241        Let's find Niels in our database from example 2
242        and print his data on the screen
243
244
245        ....
246                        // find an element which holds niels name
247        $gb_niels_name = ARB::find($gb_main,"name","niels","down_2");
248                        // We want the container for niels not just his name
249        $gb_niels = ARB::get_father($gb_niels_name);
250                        // lets loop through everything
251        $gb_any = ARB::find($gb_niels,"","",down_level);
252        while ($gb_any) {
253                        // We know that we have only strings, so this is
254save
255                        // Just read the value
256                $value = ARB::read_string($gb_any);
257                        // and we need the key
258                $key = ARB::read_key($gb_any);
259                print "$key:    $value\n";
260                        // get the next element
261                $gb_any = ARB::find($gb_any,"","","this_next");
262        }
263        ...
264
265
266***************************** All the functions ***************
267
268Now the rest is really trivial: only a lot of functions
269
270        Some name conventions:
271                $gb_xxx indicates are variable which is a database
272                        element
273                $gb_cxxx is any container
274                $gb_main is always the root element
275                $error is an error string
276
277
278
279$gb_main = ARB::open("path of existing or new database.arb",flags)
280        if "path of existing or new database.arb" contains a ':'
281        character, then ARB looks for a server instead of a file.
282                path syntax:    hostname:tcpnr          // network mode
283                                :path_of_unix_socket    // unix socket
284                                :                       // default socket
285
286        flags is a string of characters:
287                r               read existing database
288                w               database should be writeable
289                c               create a new one if read failed
290                -> r            reads a database
291                -> rwc          reads or creates a database
292                -> wc           always creates a new one
293
294
295$error = ARB::save($gb_main,"path of the database",mode)
296        mode
297                a               ascii format
298                b               binary format
299        Only if you have opened a database on a file you are allowed to
300        save it, otherwise the server has to do it.
301        You may not change the path of the database, it is used
302        for security. If you want to save the database to a new
303        file use:
304
305$error = ARB::save_as($gb_main,"path of the database",mode)
306
307
308$error = ARB::save_quick($gb_main,"existing path of the database");
309        Save all changes to a file "xxxx.quickX" where X is an
310        autoincremented number between 0 and 7
311
312$error = ARB::save_quick_as($gb_main,"new path");
313        Fake an existing database by create a symbolic link and
314        then call ARB::save_quick
315
316$error = ARB::close($gb_main);
317        You should be polite to your server and say good bye, so
318        please call this function just before you exit.
319        Nothing serious will happen if you forget to call it.
320
321
322$error = ARB::begin_transaction($gb_main);
323        Get the exclusive read and write rights from the server
324$error = ARB::abort_transaction($gb_main);
325        Undo everything since the last begin_transaction
326$error = ARB::commit_transaction($gb_main);
327        Write all data to the server and give the database rights
328        back to other users
329
330$error = ARB::push_transaction($gb_main);
331        Increments an internal counter. If the counter was previously
332        0 then calls begin_transaction. This is usefull when working
333        in a subroutine and not knowing whether your calling function
334        had opened a transaction already.
335$error = ARB::pop_transaction($gb_main);
336        Decrements the internal counter. If the counter gets zero,
337        commit a transaction.
338
339
340****************** Now the following functions assume that you have
341        began a transaction ***************************
342
343*************************** error handling ********************
344Most of the functions return an optional error string if there have
345been a problem. If a function do not return a error string but
346for example a database element, you may get the error string by
347        calling ARB::get_error()
348
349example:
350        $gb_main = ARB::open("Idonotexist","rw");
351        if (! $gb_main ) $error = ARB::get_error();
352
353**************************** find and search entries ***********
354
355$gb_xxx = ARB::search($gb_start_search_point,"path/in/the/database",
356                "TYPE_OF_ELEMENT_YOU_WANT_CREATE/none"
357        search a database element if all keys are unique
358
359$gb_xxx = ARB::find($gb_start_search_point, "[key]", "[value]",
360        "search_mode"
361
362
363************************** create new entries ***************
364
365$gb_new_entry = ARB::create($gb_cxx, "key", "type");
366        creates a new element of type "type" within $gb_cxx
367
368$gb_new_container = ARB::create_container($gb_cxx,"key");
369        create a new container within $gb_cxx
370
371************************* read entries *******************
372        // simple types
373$val = ARB::read_byte($gb_xxx);
374$val = ARB::read_int($gb_xxx);
375$val = ARB::read_float($gb_xxx);
376        // array types
377$val = ARB::read_string($gb_xxx);       // read the value
378$val = ARB::read_bytes($gb_xxx);        // may contain \0 characters
379$val = ARB::read_ints_using_index($gb_xx, "index");
380                                        // read the integer #index of an
381                                        // integer array
382$val = ARB::read_floats_using_index($gb_xxx, "index");
383
384$val = ARB::read_count($gb_xxx);        // get the size of the array
385
386$val = ARB::read_as_string($gb_xxx);    // read and convert to string
387
388$val = ARB::read_bits($gb_xxx,char_0,char_1);
389        convert a bit array into a string,
390        a '0' will be converted into char_0
391        a '1' into char_1
392
393        eg: ARB::read_bits($gb_xxx, "-", "+" ); returns a '-' '+' string
394
395
396************************* write entries *******************
397
398$error = ARB::write_int($gb_xxx, "value");      // write the value
399$error = ARB::write_float($gb_xxx, "value");    // write the value
400$error = ARB::write_byte($gb_xxx, "value");     // write the value
401
402$error = ARB::write_bytes($gb_xxx, "value", len);// write array of bytes
403$error = ARB::write_string($gb_xxx, "value");   // write the value
404$error = ARB::write_bits($gb_xxx, "value", char_0); // convert a string
405                                // into bitarray, all characters !=
406                                // char_0 will converted to 1 else 0
407
408$error = ARB::write_as_string($gb_xxx); try to interpret the value and
409                                // convert it automatically to the right
410                                // format
411
412If you write to an element and you do not change the value,
413the element thinks, that you have done nothing, and no
414element pending events are called, and the modification time is not
415changed. If you want to force to change the element, call
416
417$error = ARB::touch($gb_xxx);
418
419******************************* etc entries ****************
420
421$error = ARB::delete($gb_xxx);
422        deletes an element or a container and it's subelements
423
424$error = ARB::copy($gb_destination, $gb_source);
425        copy's the value from source to destination.
426        does not create a new element.
427        the type of $gb_destination must be the type of $gb_source
428        if type == "CONTAINER" than all subelements are copied
429
430************************ security levels **********************
431
432each database element has three security levels:
433        read write and delete
434each level's value is between 0 and 7
435
436$val = ARB::read_security_read($gb_xxx);
437$val = ARB::read_security_write($gb_xxx);
438$val = ARB::read_security_delete($gb_xxx);
439
440$error = ARB::write_security_read($gb_xxx, "val");
441$error = ARB::write_security_write($gb_xxx, "val");
442$error = ARB::write_security_delete($gb_xxx, "val");
443
444to change any element, the users security level must be greater or
445equal the elements level. To change the users level call:
446
447$error        = ARB::change_my_security($gb_main,"level","passwd");
448        passwd is not in use yet
449
450
451*********************** additional element information ***********
452
453$type = ARB::read_type($gb_xxx);
454        gets the type of a database elem: "INT" "STRING" "CONTAINER" ....
455
456$key = ARB::read_key($gb_xxx);
457        read the key of a database elem:
458
459$clock = ARB::read_clock($gb_xxx);
460        gets the last modification time of an entry.
461        The modification time of a container is always greater or equal
462        then the modification time of it's subelements.
463        The clock counts transactions, not seconds
464
465
466$error = ARB::set_temporary($gb_xxx);
467        Marks a field in the database as a temporary field.
468        That means that this field is never saved to a file.
469$error = ARB::clear_temporary($gb_xxx);
470        Clears tmp flag
471
472$error = ARB::write_flag($gb_xxx, bool);
473        Sets or clears a flag in the database,
474        There are functions to quickly find all flagged == marked
475        entries
476
477$gb_xxx = ARB::first_marked($gb_cxx);
478        finds the first flagged element in a container
479
480$gb_xxx = ARB::next_marked($gb_xxx);
481        gets the next flagged item
482
483$count = ARB::number_of_marked_subentries($gb_cxxx);
484        adds all flags of direct subentries within $gb_cxxx
485
486$error = ARB::write_usr_public($gb_xxx, value);
487        Each element has 8 free public bits
488        public means that all clients share the same bits
489
490$value = ARB::read_usr_public($gb_xxx);
491
492
493$error = ARB::write_usr_private($gb_xxx, value);
494        Each element has 8 free private bits
495        private means each database client has it's own bits
496$value = ARB::read_usr_private($gb_xxx);
497
498
499************************** ETC ************************
500
501$error = ARB::release($gb_xxx);
502        By default all clients cache all data which has been searched,
503        read or modified.
504        If you want to get rid of some cached items you may call
505        ARB::release, which frees all data below $gb_xxx.
506        Warning: All database elements, which are a subentry of $gb_xxx
507        will be temporary lost. You have to search or find them again.
508
509
510$error = ARB::add_callback($gb_xxx, function_name, clientdata);
511        Adds a callback to a database element.
512        Every time you call begin_transaction and any other client
513        has changed this database element, your function_name is called
514        Every time you call commit_transaction and you have made
515        changed to this item, your function_name is called,
516
517        function_name is a function with the following parameters:
518
519        func($gb_changed_element, clientdata, "DELETED/CHANGED"
520                A "DELETED" value in the third argument means that
521                the element had been deleted
522                A "CHANGED" value indicate only a change.
523
524$error = ARB::remove_callback($gb_xxx, function_name);
525        remove a callback
526
527
528$error = ARB::create_index($gb_cxx, "key", estimated_size);
529        If you call ARB::find with mode "down_2" or "this_next"
530        very often on a huge database you may create an index.
531        estimated_size is the estimated number of items which should
532        be indexed.
533        In the current version we are using a hash mechanism to index
534        the elements. Only strings can be indexed.
535
536$error = ARB::undo($gb_main, "UNDO"/"REDO");
537        undo oder redo the database.
538        Should not be called within a running transaction
539
540$info = ARB::undo_info($gb_main, "UNDO"/"REDO");
541        Should give you a short information about the next undo/redo
542
543$error = ARB::set_undo_mem($gb_main, size_in_bytes);
544        sets the amount of memory used to store undo values
545
546$time = ARB::last_saved_clock($gb_main);
547        transaction number when the database was last saved
548        Can only be called from the server program
549
550$time = ARB::last_saved_time($gb_main);
551        the same but in seconds after 1 Jan 0:00 1970
552
553$error = ARB::set_cache_size($gb_main, "size_in_bytes");
554        ARB uses datacompression for long strings. If some strings
555        are used very intensely the program may slow down.
556        Therefor a small cache is used and it's size can be set
557        by this function. If you are working with sequences, a value
558        of 1 or 2 meg seems reasonable
559
560
561$error = ARB::check_key("key");
562        checks "key" is a valid key
563
564************************* Creating a server ******************
565
566$error = ARB::sopen("socketid",timeout_in_millisec, $gb_main);
567        serves $gb_main for other clients.
568        syntax of "socketid":
569                host:tcpnr
570                :/path/of/unix/socket
571                :                       // default unix socket
572
573        prepares the database to be served. timeout is not used now
574        but later in:
575
576$error = ARB::accept_calls($gb_main);
577        Watch your socket timeout_in_millisec for any clients, who need
578        help
579
580$error = ARB::shutdown($gb_main);
581        shutdown the server
582
583$clients = ARB::read_clients($gb_main)
584        Get the number of clients using this server.
585        if $clients == -1 than I'm a client no server
586        So this can be used to check wether I'm a client or server
587
Note: See TracBrowser for help on using the repository browser.