3.4 Opening a database
3.4.3 Recursive opening of databases
When a database has been opened, the CPOMS has access to the address mappings for all the objects within the database. However, the database may contain the addresses of objects
in unopened databases. That is, the CPOMS may encounter persistent store addresses for which it has no valid address mappings. There are two alternative approaches to this problem: raise an error or open every reachable database.
The simplest solution is to indicate that an error has occurred when such an address is dereferenced. However, when the CPOMS was designed, this was unsatisfactory for two reasons. Firstly, PS-algol contains no means of identifying where an object is stored. That is, the databases only provide knowledge of the access path to an object. Consequently, it is not possible for a PS-algol program to determine in which database an object resides and therefore it cannot anticipate the error.
The second problem with raising an error is that PS-algol contained no exception handling mechanism that could deal with it. As a result, such an error would have caused a program to crash. The combination of these two problems meant that it would not always have been possible to anticipate if a program might crash, particularly if the programmer was not responsible for the databases the program used.
To avoid these problems, the CPOMS opens every reachable database. This operation is performed once all the address mappings for a database have been constructed, by recursively opening each database referenced by the list of reachable databases.
When a database is opened by a recursive open, the database name and password are not used. Instead, a direct reference is used to a database's entry in the database directory. The choice of lock for a recursive open is usually a lock for reading. However, when an unnamed database is opened, a lock for updating is requested. The reason for this is that every other database can be explicitly opened for updating by a user, whereas an unnamed database cannot.
3.4.4 Accessing objects in a database
The act of opening a database has so far only described how the address mappings are constructed. In addition to this, an open must provide a mechanism to allow a program to access the objects in a database. This is achieved by returning the address of a database's root object to the calling PS-algol program. The root object contains an address field that can be used by a PS-algol program to address a user's data. For convenience the root object contains the address of a PS-algol table with which a user can organise his data. In fact, the
open.database procedure used by a PS-algol programmer is a PS-algol procedure which calls the CPOMS open.database and then returns the address of the table. This allows the table to be treated as the database's root object.
3.4.5 Summary of opening a database
When a database is opened, the following three parameters must be specified: a) the name of the database to be opened,
b) the database's password and
c) whether or not the database is to be updated.
A summary of the steps involved in opening a database can now be given. The steps involved are as follows:
a) Search the database directory.
The first step is to search the database directory for an entry with the specified database name. The open fails if there is no database with the specified name.
b) Check the password.
The database's password is then compared with the specified password. The open fails if the passwords are not the same.
c) Lock the database.
A lock is then requested on the database. This is an exclusive lock if the database is to be updated, otherwise it is a shared lock. The open fails if the requested lock cannot be granted.
d) Construct the first partition's address mapping.
The address mapping for the database's first partition is then constructed using the information held in the database directory. The mapping is recorded in the PTODI so that the database's root object can be addressed.
e) Construct an address mapping to access the list of partitions.
The database contains an object that records the partitions allocated to the database. The address mapping for the partition containing this object is calculated using information held in the root object and entered into the PTODI.
f) Construct the remaining address mappings for the database.
An address mapping is then constructed for each partition allocated to the database and entered into the PTODI. Every object in the database can now be addressed. g) Open aU potentially reachable databases.
To ensure no addresses are encountered for which no address mappings are known, every database that may contain a reachable object is recursively opened. The initial open fails if any of the recursive opens fail.
h) Return the address of the user's data.
The final step is to return the address of the database's root object to the calling PS-algol program. A user's data can then be accessed by dereferencing the root object.
3.5 Creating a database
When a PS-algol/ CPOMS system is first initialised, it does not contain any databases and therefore does not provide any roots of persistence. The CPOMS includes a mechanism to create a new database and thereby introduce a new root of persistence. This operation
involves creating the database, allocating an address partition to the database and constructing an entry in the database directory.
3.5.1 The database creation
When a database is created, the following two parameters must be specified: a) the name of the database to be created and
b) the database's password.
The first step in creating a database is to search for a free entry in the database directory. As part of the search, a check is made to ensure that the new database name is unique. If it is not, then the create fails. Once a free entry has been found, the database's name and password are entered into it.
Next, a free address partition is allocated to the new database and the new partition is recorded in the database's directory entry. Since this is the first partition allocated to the database, its first object is the database's root object. Hence, recording the partition in the database directory allows the root object's address to be calculated when the database is opened.
The next step is to create the physical database. This is performed by allocating an area of private storage for the new database and then creating two objects within the storage. The first object is the new database's root object and the second object contains a record of the new partition. To support open.database, the root object contains the address of the second object. The second object's address corresponds to the second object number in the new partition.
The final step in creating the database is to enter a reference to the physical database in the database directory. The new database and its entry in the database directory are then available for use by open.database,