Since both UserInfo and ContactList records for a user are variable length, the simplest method of storing this data on disk is in a text-format file, with each record terminated by a newline. In order to be able to locate a user, without parsing every XML string, the following format was chosen:
userid1 <UserInfo><UserID>userid1</UserID>...</UserInfo>\n userid2 <UserInfo><UserID>userid2</UserID>...</UserInfo>\nThe space between the userid and the XML string make the line easy to split up into the two pieces using C++'s string stream object.
The contact list database needs to parsed by the server far more often than the personal information database since the server needs to lookup which clients it should forward UserInfo messages to. Since the data for a contact list is much simpler, slow-to-parse XML was abandoned in favour of an efficient space separated list of UserIDs.
userid1 eventcount contact1 contact2 ... \n userid2 eventcount contact1 contact2 ... \n
Records will be
stored in sorted order so lookups can be performed using a binary-chop
method in
time.
Unfortunately, as the number of users grows, eventually there will be too much
information to store in memory at one time and even storing all the
information in one file may cause the file to reach a system limit for
file size.
The interim solution is to split the database into multiple
files. UserIDs are stored in numeric order in a filename given by
(UserID
128). The database module works by checking the
currently loaded file for the required UserID. If the UserID is found,
the operation is carried out, otherwise the current file is saved to
disk, and the correct file loaded. This is not much better than carrying out
all operations to disk since it is not likely that consecutive
operations will be on UserIDs in the same file.
The optimal data structure for the database would be one that allowed
recently used records to be kept in memory and inactive ones on
disk. This could be implemented using a splay tree[CLR90]
which has an amortised cost of
for any operation. Because
the most recently used node of the tree is moved to the top of the
tree, caching the most recently used records in memory could be
achieved by keeping the upper few levels of the tree in memory and
the remaining levels on disk. Unfortunately there was not sufficient time to
implement this so the current implementation uses the system outlined
in the previous paragraph.