swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: InFile


From: Marcus G. Daniels
Subject: Re: InFile
Date: 13 Sep 1999 19:34:25 -0700
User-agent: Gnus/5.070084 (Pterodactyl Gnus v0.84) Emacs/20.4

>>>>> "B" == Benedikt Stefánsson <address@hidden> writes:

B> The code example you showed for LispArchiver assumes that the user
B> knows the key value to pass to the archiver to retrieve the
B> MyObject instance. 

The keys in an archive are, well, keys.  They must be unique or
overwrites will occur.  If you have set of things you'd refer
to the name of the set instead of anonymously to the members.

B> What if you want to deserialize a collection, is
B> it possible to put in the Scheme list a series of key,value pairs
B> and use a while() loop to iterate through the list, i.e.. have a
B> file like this:

B> (list
B>  (cons 'myObject
B>       (make-instance 'MyObject #:date "1/1/99" #:value 1.0)
B>  )
B> (cons 'myObject
B>       (make-instance 'MyObject #:date "1/2/99" #:value 2.0)
B> )
B> ...
B>  (cons 'myObject
B>       (make-instance 'MyObject #:date "12/31/99" #:value 365.0)
B> )
B> )

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 <simtools.h> // initSwarm
#import <defobj.h> // Archivers
#import <defobj/Create.h> // CreateDrop
#import <defobj/defalloc.h> // 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:
*/

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]