Marcus Daniels swarm-support Sept. 13, 1999 Here's an example of how to handle Collections. There are six possible configurations of the code below: 1. The compile-time flag USE_HDF5 determines whether Archiving is done with the HDF5 or Lisp serialization backends. 2. The compile-time flag USE_MAP determines whether or not the data is stored as a Map or a List. In the Map configuration your "value" is keyed to the "date". In the List configuration, the "date" and "value" are stored in the same object. 3. The runtime mode flag ("-m create") determines whether or not data is loaded or created. In the Lisp backend, the data you describe would look like this as a List: (list (cons 'myCollection (make-instance 'List (make-instance 'Data #:date "1999-01-01" #:value 1.000000D0) (make-instance 'Data #:date "1999-01-02" #:value 2.000000D0) (make-instance 'Data #:date "1999-12-31" #:value 3.000000D0)))) ..or like this as a Map: (list (cons 'myCollection (make-instance 'Map (cons "1999-01-01" (make-instance 'Data #:value 1.000000D0)) (cons "1999-01-02" (make-instance 'Data #:value 2.000000D0)) (cons "1999-12-31" (make-instance 'Data #:value 3.000000D0)) #:compare-function #:compare-c-strings))) #import // initSwarm #import // Archivers #import // CreateDrop #import // getZone @interface Data: CreateDrop { #ifndef USE_MAP const char *date; #endif double value; } #ifndef USE_MAP - setDate: (const char *)date; - (void)describe: stream; #else - (double)getValue; #endif - setValue: (double)value; @end @implementation Data #ifndef USE_MAP - setDate: (const char *)theDate { date = STRDUP (theDate); return self; } #endif - setValue: (double)theValue { value = theValue; return self; } #ifndef USE_MAP - (void)describe: stream { [stream catC: "Date: "]; [stream catC: date]; [stream catC: " Value: "]; [stream catDouble: value]; [stream catC: "\n"]; } #else - (double)getValue { return value; } #endif @end @interface Controller: CreateDrop { id collection; } #ifdef USE_MAP - (void)createMap; #else - (void)createList; #endif @end #define NAME "myCollection" @implementation Controller #ifdef USE_MAP - (void)createMap { #define DATA(value) [[Data createBegin: getZone (self)] setValue: value] collection = [[[Map createBegin: getZone (self)] setCompareCStrings] createEnd]; [collection at: (id) "1999-01-01" insert: DATA(1.0)]; [collection at: (id) "1999-01-02" insert: DATA(2.0)]; [collection at: (id) "1999-12-31" insert: DATA(3.0)]; } #else #define DATA(date,value) \ [[[Data createBegin: getZone (self)] setDate: date] setValue: value] - (void)createList { collection = [List create: getZone (self)]; [collection addLast: DATA ("1999-01-01", 1.0)]; [collection addLast: DATA ("1999-01-02", 2.0)]; [collection addLast: DATA ("1999-12-31", 3.0)]; } #endif - updateArchiver: archiver { [archiver putShallow: NAME object: collection]; return self; } @end int main (int argc, const char **argv) { initSwarmBatch (argc, argv); { id archiver; id controller; #ifdef USE_HDF5 archiver = [[[HDF5Archiver createBegin: globalZone] setPath: "collection.hdf"] createEnd]; #else archiver = [[[LispArchiver createBegin: globalZone] setPath: "collection.scm"] createEnd]; #endif controller = [Controller create: globalZone]; if (strcmp ([arguments getAppModeString], "create") == 0) { #ifdef USE_MAP [controller createMap]; #else [controller createList]; #endif [archiver registerClient: controller]; [archiver save]; } else { #ifdef USE_MAP id collection, index, obj, key; collection = [archiver getObject: NAME]; index = [collection begin: scratchZone]; for (obj = [index next: &key]; [index getLoc] == Member; obj = [index next: &key]) { printf ("Date: %s Value: %f\n", (const char *) key, [obj getValue]); } [index drop]; #else xfprint ([archiver getObject: NAME]); #endif } } return 0; } /* Local Variables: compile-command: "$SWARMHOME/bin/libtool-swarm --mode=link gcc -DUSE_MAP -DUSE_HDF5 -o collectionArchiving -g -Wno-import -I$SWARMHOME/include -L$SWARMHOME/lib collectionArchiving.m -lswarm -lobjc" End: */