17 Using the Persistence Library

The persistence library can be used to save your data on the disk for later use in another program execution. Any object created can be saved using the persistence library. The patterns defining the objects do not have to be extended in any way before the objects can be saved. Imagine that we like to save the character count in the previous example for usage in another program. The pattern definition of the directoryList can be described in a separate file (called DirList.bet) as follows:

Program 21: DirList.bet

ORIGIN '~beta/containers/list
--- lib: Attributes ---
   directoryList: List
     (# ...
     #);

Notice, that we do not define a program fragment in this file, instead we define attributes only. A file describing simple pattern declarations only can use the slot called lib defined in the betaenv environment (see section 19 below about the fragment system, for more details). The declarations in the DirList file can be used by including the file in the program. Thus the program listed Program 13.1 can be changed like:

Program 22: SaveListDir2.bet

ORIGIN '~beta/basiclib/file';
INCLUDE 'DirList'
---- program: descriptor ----
(# dir: @directory;
   dirList: ^directoryList;
do &directoryList[] -> dirList[];
   ...
#)

We can now save DirList using the persistent store, The persistent store is available as a library in the file ~beta/persistentstore/persistentstore. By including this file we can use the persistentstore pattern to save the list. persistentstore has the following useful operations:

persistentstore.create: given a text create a persistent store with that name

persistentstore.openWrite: given a name opens the persistent store with read and write permission. openRead opens a store with read permission only

persistentstore.get: given a name and a pattern variable, returns an object in the storage with that type

persistentstore.put: given a name and an object, stores that object in the persistent store

persistentstore.close: closes the persistent store

The following program is similar to the one above, except that it stores the dirList in a persistent store.

Program 23: SaveListDir3.bet

ORIGIN '~beta/basiclib/file';
INCLUDE '~beta/persistentstore/persistentstore;
INCLUDE 'DirList'
---- program: descriptor ----

(# (* Saving the file names in a persistent store *)

   dir: @directory;
   theStore: @persistentstore;
   dirList: ^directoryList;
do &directoryList[] -> dirList[];
   
   ... (* Program 13.1 *)

   'fileStore'->theStore.create;
   (dirList[],'myList')->theStore.put;
   theStore.close;
#)

The persistent store is now located in the file directory: fileStore.

Finally, we can make a program that reads the list, and examines the data:

Program 24: GetListDir.bet

ORIGIN '~beta/basiclib/file';
INCLUDE '~beta/persistentstore/persistentstore;
INCLUDE 'DirList'
---- PROGRAM: descriptor ----

(# (* Reading counted occurrences of characters 
    * from a persistent store
    *)
   theStore: @persistentstore;
   dirList: ^directoryList;
do 
   'fileStore'->theStore.openWrite;
   ('myList', directoryList##)->theStore.get->dirList[];
   dirList.scan(# ... #);
   ...
   theStore.close;
#)

A complete description of the facilities in the persistent store library can be found in [MIA 91-20].


Libraries Tutorial
© 1994-2004 Mjølner Informatics
[Modified: Thursday October 19th 2000 at 14:10]