FileDocCategorySizeDatePackage
UserDBDBM.javaAPI DocExample1391Fri Oct 12 20:02:16 BST 2001jabadot

UserDBDBM

public class UserDBDBM extends UserDB
A trivial "database" for User objects, stored in a flat file.

Since this is exected to be used heavily, and to avoid the overhead of re-reading the file, the "Singleton" Design Pattern is used to ensure that there is only ever one instance of this class.

Fields Summary
protected static final String
DEF_NAME
protected DBM
db
Constructors Summary
protected UserDBDBM()
Default Constructor


	   
	    
		this(DEF_NAME);
	
protected UserDBDBM(String fn)
Constructor

		super();
		
		db = new DBM(fn);
		String k;
		Object o;

		// Iterate through contents of DBM, adding into list.
		for (o=db.firstkeyObject(); o!=null; o=db.nextkey(o)) {
			// firstkey/nextkey give Key as Object, cast to String.
			k = (String)o;
			o = db.fetch(k);	// Get corresponding Value (a User)
			users.add((User)o);	// Add to list.
		}
	
Methods Summary
public synchronized voidaddUser(User nu)
Add one user to the list, both in-memory and on disk.

		// Add it to the in-memory list
		super.addUser(nu);

		// Add it to the on-disk version: store in DB with
		// key = nickname, value = object.
		db.store(nu.getName(), nu);