[redland-dev] Perl API Serializer, File Only?

Richard Newman r.newman at reading.ac.uk
Mon Oct 18 12:06:21 BST 2004


In my own Objective-C store class, which links to Redland, I wrote a 
(naive) method to return all triples in the store. It does this by 
iterating over each, and appending them to a mutable string (ouch). The 
point? It's quite easy to write it in a client app, and should be even 
simpler in Perl. No need for Dave to lose weekends :D (assuming, of 
course, that this is going to perform well enough for you! I've only 
tested in the thousands of triples, not millions.)

Code below.

// Returns an autoreleased string (actually a NSMutableString).
- (NSMutableString *) getTriplesAsString {
     unsigned char *stat = NULL;

     NSMutableString *output;

     librdf_stream *sst = librdf_model_as_stream(model);

     if (sst == NULL) {
         NSLog(@"Error serialising model.");
         return nil;
     }
	// allocate the output string.
	output = [[NSMutableString alloc] init];

     while (!librdf_stream_end(sst)) {
         stat = 
librdf_statement_to_string(librdf_stream_get_object(sst));
         [output appendFormat:@"%s\n", stat];
         free(stat);
         librdf_stream_next(sst);
     }
	stat = NULL;
     librdf_free_stream(sst); sst = NULL;
     return [output autorelease];
}

On Oct 18, 2004, at 11:51, Dave Beckett wrote:

> On Mon, 18 Oct 2004 02:21:28 -0700, Kip Hampton 
> <khampton at totalcinema.com> wrote:
>
>> Howdy Redlanders,
>>
>> I'm just digging into evaluating redland for an upcoming (perl-based)
>> project. I see that RDF::Redland::Serializer implements
>> serialize_model_to_file() btu my app will be returning content
>> dynamically so writing out to a file isn't waht i need.
>>
>> How does one go about serializing a model to a string? Am I missing
>> something obvious?
>
> No you aren't.
>
> Iit doesn't exist yet for a variety of reasons ;)
>
> This is one of those cases where the resulting
> end user functionality looks quite small:
>   $foo=$serializer->model_as_string($model)
> but the code to do the internals is much larger.
>
> The main blocker is that I need to create a cross-language I/O
> abstraction (aka stream in java, I/O stream in C++) so that instead
> of doing fprintf(fh, ...) I use iostream->printf(...) and then the
> right magic happens so that it goes to a stream or string, as 
> appropriate.
>
>   Aside:
>   Perl is actually one of the problems here, since it has a PerlIO
>   abstraction that is not C standard I/O.  So I already can't use
>   FILE* across languages for serializing to already existing open 
> files.
>
>   I know some systems have smarter libc I/O that can present FILE* to
>   in-memory buffers, but it isn't portable or usable across languages
>   (see above).
>
> So I have to refactor all existing serializing I/O code to use that
> new code by adding wrappers to make it work as before, and then go on
> to add the as_string() support.  I also will be creating a
> raptor_serializer class and methods to do the work and move all
> syntax writing code there.
>
> I do have some code for the beginings of the I/O work and
> raptor_serializer classes but it's not anywhere complete.
>
> It could be one of those cases where this is actually easier than it
> looks and some weekend I'll just be able to do it all :)  No promises!
>
> Dave
>
>
> _______________________________________________
> redland-dev mailing list
> redland-dev at lists.librdf.org
> http://lists.usefulinc.com/mailman/listinfo/redland-dev
>




More information about the redland-dev mailing list