<<<<<<<<<<<<<<<<<

ndbm(unix) Unix ndbm(unix)

NAME

ndbm - store key/data pairs using UNIX ndbm facility

DESCRIPTION

The ndbm class stores key/data pairs in a database using the UNIX ndbm facility. Besides methods corresponding to the procedures supported by ndbm, the ndbm object also provides high-level operations for storing and retrieving LYMB objects. For example, it is possible to use this class to create a "persistent" object database.

SUPERCLASS

object

INSTANCE VARIABLES

file the prefix of the ndbm file. Ndbm will create two data files - file.dir and file.pag - to store its data.

key is the access string into the database.

datum is the data (a string) associated with the key.

MESSAGES

file= name set the file prefix.

file? get the file prefix.

key=,@ value set the access key.

key? get the value of the access key.

datum= value set the data value to a string.

datum?,fetch? get the data value for the current key value.

open! open the database using the current file prefix.

close! close the currently opened database.

insert_mode! in this mode, items can be stored in the database only if they do not previously exist. If they do exist, the database is not changed.

replace_mode! in this mode, items will be stored in the database regardless whether they exist in the database or not. This is the default.

store! store the current key/data pair in the database

store: (key_1,data_1, ..., key_n,data_n) store listed pairs of keys and data in the database.

store_object: (object_name_1, ..., object_name_2) store the named objects in the database. Storing objects entails having them perform a "print!" into a string which is then placed into the database (with the object name serving as the key).

fetch! using the current key value, retrieve the data from the database.

fetch_object: object_name retrieves data (usually output from print!) associated with object_name and then passes it to parser. Side effect is to execute the LYMB script (e.g., object instantiation).

delete! deletes the current key and data from the database.

stored? returns a 1 if the current key is stored in the database, 0 otherwise.

firstkey! sets the current key value to the first key value in the database. (Used in conjunction with nextkey to traverse database.)

firstkey? returns the firstkey value on the argument stack. (Used in conjunction with nextkey to traverse database.)

nextkey! sets the current key value to the next key value in the database. (Used in conjunction with firstkey to traverse database.)

nextkey? returns the nextkey value on the argument stack. (Used in conjunction with firstkey to traverse database.)

EXAMPLE

/* * Tests the ndbm object */ scalar new: ascalar = 10 print:value; string new: astring = "This is a test!";

/* * Create database */ ndbm new: dbm file=`test_dbm' open! print:error replace_mode! store: ( `Cathy',`A wonderful person', `Tim', `A hell of a guy', `Joyce', `The jury is still out...' ) store_object: ( `ascalar', `astring' ) close! ; /* * Delete an entry */ dbm file=`test_dbm' open! key=`astring' print: stored delete! print:stored ; /* * extract some data. Notice that a side effect of fetch object is to * re-instantiate ascalar. */ ascalar = 100 print:value; dbm file=`test_dbm' open! print:error @`Cathy' print:datum @`Joyce' print:datum @`Tim' print:datum fetch_object: ( `ascalar' ) ; ascalar print:value;

dbm store: (`Joyce',`She is okay after all') @`Joyce' print:datum ; /* * Now traverse database */ string new: firstkey;

string new: key; string new: null;

forloop new: for start_actions=` for=1; firstkey = [dbm firstkey?] print:value; key = firstkey; ' tick_actions=` dbm @key print:`key'; key = [dbm nextkey?]; for notequal: (key,null); ' start! ;

SEE ALSO

hash_table


Please send comments and suggestions to
consult@rpi.edu