FileDictionary: Tiny Squeak Database

FileDictionary ( ) is a super-simple database for Squeak. Basically, you can use it like a Dictionary, except that you (a) must create it with a fileNamed: method and (b) you must explicitly open and close the FileDictionary. The parts of the Dictionary protocol that FileDictionary understands:

FileDictionary actually uses two files. The data (values) will be stored in the filename provided when creating the FileDictionary. The index into the database will be stored in a file with the same name, adding on the suffix ".toc" (for Table Of Contents). The TOC file is what gets read at open and written at close.

There are examples in the class method basicExamples. Here's an example of writing things to the FileDictionary:

| fd newDict |
fd := FileDictionary fileNamed: 'samplefd'.
fd open.
fd at: 'fred' put: 'ethel'.
fd at: 'tim' put: #(0 1 2 3).
newDict _ Dictionary new.
newDict at: 'mary' put: 'poppins'.
newDict at: 'fred' put: 'astaire'.
fd at: 'names' put: newDict.
fd close.

And here's how you might read things back in again:

| fd |
fd := FileDictionary fileNamed: 'samplefd'.
fd open.
Transcript show: (fd at: 'fred'); cr.
fd do: [:anAssoc | Transcript show: anAssoc printString ; cr].
fd close.

Implementation and Caveats

The implementation of FileDictionary is important in that it limits what values can be stored. FileDictionary is implemented on top of a subclass of DataStream. Data is written out onto the database file using DataStream. The index dictionary in FileDictionary tracks the position in the database file where the value object begins. New objects are always written at the end of the database file. DataStream is also used to read in and write out the index dictionary.

The caveats that this implementation introduces:

I wouldn't use FileDictionary for any kind of persistent object implementation. ReferenceStream (or SmartRefStream) are much better for that kind of thing but I can't change the position on the underlying bytestream safely, which is why I subclassed DataStream as PositionableDataStream for FileDictionary.


More About Squeak...
Home Page for How To Squeak

Last modified at 5/8/98; 1:22:56 PM
Other Links of Interest
College of Computing | EduTech Institute | GVU Center
Mark Guzdial | CS2390, Modeling and Design | STABLE