--- freem/doc/freem.texi 2025/04/18 22:12:29 1.30 +++ freem/doc/freem.texi 2025/04/20 15:43:32 1.33 @@ -4,7 +4,7 @@ @settitle The FreeM Manual @copying -This manual is for FreeM, (version 0.64.0-rc0), which is a free and open-source implementation of the M programming language. +This manual is for FreeM, (version 0.64.0-rc1), which is a free and open-source implementation of the M programming language. Copyright @copyright{} 2014-2025 Coherent Logic Development LLC @@ -18,7 +18,7 @@ Permission is granted to copy, distribut @title The FreeM Manual @subtitle @sc{The Official Manual of FreeM} -@subtitle Version 0.64.0-rc0 +@subtitle Version 0.64.0-rc1 @c@vskip 10pt @c@center @image{freem-logo-sm,,,,.png} @author Serena Willis @@ -73,7 +73,6 @@ This is the official manual for the Free * Global Aliasing:: Defining alternate names for globals. * Global Mappings:: Mapping global names to non-default namespaces. -* Transaction Processing:: Transactions in FreeM. * Asynchronous Event Handling:: Handling asynchronous events in FreeM. * Global Triggers:: Responding to global accesses in M code. * Synchronous Event Handling:: Synchronous events in FreeM. @@ -371,7 +370,7 @@ Attempting to start a FreeM interpreter The FreeM direct-mode environment is the mode entered when FreeM is invoked without the use of @option{-r @emph{}} or @option{--routine=@emph{}}: @example -Coherent Logic Development FreeM version 0.64.0-rc0 (x86_64-pc-linux-gnu) +Coherent Logic Development FreeM version 0.64.0-rc1 (x86_64-pc-linux-gnu) Copyright (C) 2014, 2020, 2021 Coherent Logic Development LLC @@ -2515,7 +2514,7 @@ In the above @emph{inclusive} form, @cod @cartouche @quotation @emph{Note} -The below @emph{argumentless} and @emph{exclusive} forms of @code{KSUBSCRIPTS} are not implemented in FreeM, as of version 0.64.0-rc0, but are planned for a future release. +The below @emph{argumentless} and @emph{exclusive} forms of @code{KSUBSCRIPTS} are not implemented in FreeM, as of version 0.64.0-rc1, but are planned for a future release. @end quotation @end cartouche @@ -2550,7 +2549,7 @@ In the above @emph{inclusive} form, @cod @cartouche @quotation @emph{Note} -The below @emph{argumentless} and @emph{exclusive} forms of @code{KVALUE} are not implemented in FreeM, as of version 0.64.0-rc0, but are planned for a future release. +The below @emph{argumentless} and @emph{exclusive} forms of @code{KVALUE} are not implemented in FreeM, as of version 0.64.0-rc1, but are planned for a future release. @end quotation @end cartouche @@ -4428,12 +4427,190 @@ See @ref{Classes}. @cindex variables, global @cindex data +@menu +* Globals Overview:: Basics of FreeM persistent storage. +* Creating Globals:: How to create globals. +* Removing Globals:: How to remove globals. +* Global Storage:: How globals are stored. +@end menu + +@node Globals Overview +@section Globals Overview + +FreeM supports typical M globals, which are often described as persistent, hierachical sparse arrays. Globals make it relatively simple to include persistent data in an application without requiring the developer to use an external database management system, and offer syntax and semantics so similar to M local variables and structured system variables that moving from one to the other is seamless. + +Each global comprises three elements: + +@itemize @bullet +@item +An alphabetic name beginning with a caret (@code{^}) or a caret and a percent sign (@code{^%}) +@item +Optionally, one or more comma-delimited subscripts, enclosed in parentheses +@item +A value of up to 255 characters in length +@end itemize + +The percent sign will force the named global into the @code{SYSTEM} namespace of the current FreeM environment. + +@node Creating Globals +@section Creating Globals +@cindex globals, creating + +To create a global, you can use the @code{SET} command: + +@example +SET ^MYGLOBAL("foo","bar")="this is the data value" +@end example + +@node Removing Globals +@section Removing Globals +@cindex globals, removing + +To remove an entire global, you can use the @code{KILL} command with the unsubscripted name of the global: + +@example +KILL ^MYGLOBAL +@end example + +If you only want to remove part of a global, i.e., beginning at a certain subscript level, use the @code{KILL} command with a subscripted name: + +@example +KILL ^MYGLOBAL("foo") +@end example + +This will remove only the @code{"foo"} subscript and all of its children. + +If you only want to remove the data value at a specific subscript level, leaving the subscript itself intact, use @code{KVALUE}: + +@example +KVALUE ^MYGLOBAL("foo") +@end example + +@node Global Storage +@section Global Storage +@cindex globals, storage + +FreeM globals are stored in @code{$PREFIX/var/freem/@emph{}/@emph{}/globals} in a binary format. + +Global files have a header of the following format: + +@verbatim +typedef struct global_header { + + char magic[5]; /* FRMGL */ + int format_version; + char host_triplet[40]; + char host_id[256]; + + unsigned long block_size; + unsigned long last_transaction_id; + + long created; + long last_backup; + +} global_header; +@end verbatim + @node Concurrency Control @chapter Concurrency Control @cindex concurrency control @cindex locking @cindex transaction processing +@menu +* Concurrency Control Overview:: Basics of concurrency control. +* Advisory Locks:: Coordinating access voluntarily. +* Transaction Processing:: Ensuring logical consistency. +@end menu + +@node Concurrency Control Overview +@section Concurrency Control Overview + +Multitasking, multi-user FreeM applications must concern themselves with concurrent access to globals in order to maintain logical consistency and prevent concurrent writes from interleaving. + +In FreeM, there are two mechanisms provided for managing concurrent global access: advisory locks, and transaction processing. + +@node Advisory Locks +@section Advisory Locks + +@node Transaction Processing +@section Transaction Processing +@cindex transaction processing + +FreeM implements a significant subset of the transaction processing features from @emph{ANSI X11.1-1995}. This allows a series of global operations to be conducted all at once, either in batch mode (where concurrency is not disturbed), or in serial mode (where writes are guaranteed to be atomic, consistent, isolated, and durable). + +@subsection Theory of Operation + +FreeM uses a pessimistic concurrency control mechanism for @code{SERIAL} transactions, meaning that any @code{TSTART} command that includes the @code{SERIAL} transaction parameter will cause the process to acquire the transaction processing mutex, which prevents any process but the one holding the mutex from performing any data access (read or write) until either @code{TCOMMIT} or @code{TROLLBACK} is called, either committing or rolling back the transaction, respectively. + +Any transaction in between its @code{TSTART} and @code{TCOMMIT}/@code{TROLLBACK} is said to be @emph{in-flight}. During the in-flight stage, pending global operations are held only in memory and after-image journals. + +FreeM maintains a list of all globals affected during a transaction in-flight. When a @code{TCOMMIT} is reached, FreeM will generate a @emph{checkpoint} of each global data file to be changed by the transaction. These checkpoints allow all FreeM globals to be restored to their pre-transaction state if a @code{TCOMMIT} should fail part of the way through its operation. + +Checkpoints can have one of two modes: + +@table @asis + +@item @code{CP_REMOVE} +Used for globals that did not exist prior to the beginning of this transaction. Simply marks the entire global data file for deletion in case of @code{TCOMMIT} failure. + +@item @code{CP_RESTORE} +Used for globals that @emph{did} exist prior to the beginning of this transaction. In this case, the entire global data file is copied to a new file with a @code{.chk} extension. In cases of @code{TCOMMIT} failure, @code{CP_RESTORE} checkpoint files will be restored over the partially-modified live data file. + +@end table + +The below example shows a few global operations and checkpoints for a transaction in-flight using the @code{trantab} direct-mode command: + +@verbatim +TL1:DEFAULT.USER> trantab + $TLEVEL 1* + Operations for Transaction ID: 6ea14aad-b8f1-47f9-9f52-4f513f892bc0 [RESTARTABLE SERIAL] + + OP. NO. ACTION KEY/DATA + ------- ------ -------- + 1 SET ^FOO=3 + 2 KILL ^FOO + 3 SET ^snw=10 + 4 SET ^BRANDNEW=6 + + Global checkpoints: + + GLOBAL MODE FILES + ------ ---- ----- + ^BRANDNEW CP_REMOVE IN: /usr/local/var/freem/USER/globals/^BRANDNEW + ^snw CP_RESTORE IN: /usr/local/var/freem/USER/globals/^snw + OUT: /usr/local/var/freem/USER/globals/^snw.23390.1.chk + ^FOO CP_RESTORE IN: /usr/local/var/freem/USER/globals/^FOO + OUT: /usr/local/var/freem/USER/globals/^FOO.23390.1.chk +@end verbatim + +In the above example, @code{IN} files are the live data file that will be overwritten or removed, and @code{OUT} files are the checkpoints themselves. Note that @code{OUT} files are only used for @code{CP_RESTORE} checkpoints. + +@subsection Using Transaction Processing + +To use transactions in FreeM, you need to be familiar with three commands: + +@itemize @bullet +@item +@code{TSTART} +@item +@code{TCOMMIT} +@item +@code{TROLLBACK} +@end itemize + +With transaction processing, global variable operations occurring between @code{TSTART} and @code{TCOMMIT} commands will be contained within the transaction. + +The atomicity, consistency, isolation, and durability facets of FreeM transaction hinge on the transaction mode. + +@subsubsection BATCH Transactions +@code{BATCH} transactions offer higher performance, and allow other applications aside from the one doing the transaction to continue normal operations until the transaction is committed with @code{TCOMMIT}. In batch mode, other processes are only locked out of normal operation during the commit phase of the transaction. + +The effect of this is that the operations within the batch transaction will not be interleaved with global writes from other applications, but the entire lifetime of the transaction is not guaranteed to be serialized with respect to the transaction processing activities of other running applications in the environment. + +@subsubsection SERIAL Transactions +@code{SERIAL} transactions offer full ACID compliance at the expense of multiprocessing performance. In serial mode, a @code{TSTART} blocks all activity from all other FreeM processes in the environment, and this blocking effect is not released until the transaction is committed with @code{TCOMMIT} or rolled back with @code{TROLLBACK} (or due to abnormal conditions in the environment that preclude the successful completion of the transaction). + @node Local Variables @chapter Local Variables @cindex variables, local @@ -4490,11 +4667,23 @@ See @ref{Classes}. * Inheritance:: Basing one class upon another. * Methods:: Attaching code to a class. * Public and Private Variables:: Managing class member access. +* Instantiating Objects:: Creating instances of classes. +* Determining Object Class:: Getting object information at runtime. @end menu @node Classes @section Classes +@menu +* Class Overview:: Class basics. +* Constructors:: Managing object creation. +* Destructors:: Cleaning up. +* Runtime Polymorphism:: Selecting methods at runtime. +@end menu + +@node Class Overview +@subsection Class Overview + A @emph{class} is the primary organizing concept of FreeM support for object-oriented programming, and in FreeM, is simply an M routine with a few special properties: @example @@ -4584,6 +4773,14 @@ MYMETHOD(THIS) ; Q "VALUE" @end example +The dot operator is used to invoke class methods: + +@example +DEFAULT.USER> N MYOBJ=$#^%MYCLASS("") +DEFAULT.USER> W MYOBJ.MYMETHOD() +VALUE +@end example + @node Public and Private Variables @section Public and Private Variables @@ -4607,6 +4804,29 @@ Either of the following commands will cr Attempting to access private fields from outside of the class will raise error condition @code{ZOBJFLDACCV}. +@node Instantiating Objects +@section Instantiating Objects + +To instantiate an object (i.e., create an object from a certain class), you will use the @code{NEW} command as follows: + +@example +NEW MYSTR=$#^%STRING("myString") +@end example + +This will create a local variable called MYSTR of type STRING, and initialize it with the value myString. + +@node Determining Object Class +@section Determining Object Class + +To determine the class of any FreeM local variable, you will use the @code{$$TYPE()} method: + +@example +USER> W MYSTR.$$TYPE() +^%STRING +@end example + +The @code{$$TYPE()} method is a member of the @code{OBJECT} class. + @node Libraries @chapter Libraries @cindex libraries @@ -4713,58 +4933,6 @@ To remove the above mapping, any of the KILL ^$SYSTEM("MAPPINGS","GLOBAL","^FOO") @end example -@node Transaction Processing -@chapter Transaction Processing -@cindex transaction processing - -FreeM implements a significant subset of the transaction processing features from @emph{ANSI X11.1-1995}. This allows a series of global operations to be conducted all at once, either in batch mode (where concurrency is not disturbed), or in serial mode (where writes are guaranteed to be atomic, consistent, isolated, and durable). - -@section Theory of Operation - -FreeM uses a pessimistic concurrency control mechanism for @code{SERIAL} transactions, meaning that any @code{TSTART} command that includes the @code{SERIAL} transaction parameter will cause the process to acquire the transaction processing mutex, which prevents any process but the one holding the mutex from performing any data access (read or write) until either @code{TCOMMIT} or @code{TROLLBACK} is called, either committing or rolling back the transaction, respectively. - -Any transaction in between its @code{TSTART} and @code{TCOMMIT}/@code{TROLLBACK} is said to be @emph{in-flight}. During the in-flight stage, pending global operations are held only in memory and after-image journals. - -FreeM maintains a list of all globals affected during a transaction in-flight. When a @code{TCOMMIT} is reached, FreeM will generate a @emph{checkpoint} of each global data file to be changed by the transaction. These checkpoints allow all FreeM globals to be restored to their pre-transaction state if a @code{TCOMMIT} should fail part of the way through its operation. - -Checkpoints can have one of two modes: - -@table @asis - -@item @code{CP_REMOVE} -Used for globals that did not exist prior to the beginning of this transaction. Simply marks the entire global data file for deletion in case of @code{TCOMMIT} failure. - -@item @code{CP_RESTORE} -Used for globals that @emph{did} exist prior to the beginning of this transaction. In this case, the entire global data file is copied to a new file with a @code{.chk} extension. In cases of @code{TCOMMIT} failure, @code{CP_RESTORE} checkpoint files will be restored over the partially-modified live data file. - -@end table - -The below example shows a few global operations and checkpoints for a transaction in-flight using the @code{trantab} direct-mode command: - -@verbatim -TL1:DEFAULT.USER> trantab - $TLEVEL 1* - Operations for Transaction ID: 6ea14aad-b8f1-47f9-9f52-4f513f892bc0 [RESTARTABLE SERIAL] - - OP. NO. ACTION KEY/DATA - ------- ------ -------- - 1 SET ^FOO=3 - 2 KILL ^FOO - 3 SET ^snw=10 - 4 SET ^BRANDNEW=6 - - Global checkpoints: - - GLOBAL MODE FILES - ------ ---- ----- - ^BRANDNEW CP_REMOVE IN: /usr/local/var/freem/USER/globals/^BRANDNEW - ^snw CP_RESTORE IN: /usr/local/var/freem/USER/globals/^snw - OUT: /usr/local/var/freem/USER/globals/^snw.23390.1.chk - ^FOO CP_RESTORE IN: /usr/local/var/freem/USER/globals/^FOO - OUT: /usr/local/var/freem/USER/globals/^FOO.23390.1.chk -@end verbatim - -In the above example, @code{IN} files are the live data file that will be overwritten or removed, and @code{OUT} files are the checkpoints themselves. Note that @code{OUT} files are only used for @code{CP_RESTORE} checkpoints. @node Asynchronous Event Handling