[gambit-list] reading a serialized value from an offset in a file

mikel evins mevins at me.com
Sun Apr 10 13:18:23 EDT 2011


Marc,

Suppose I have a file containing data serialized by calling object->u8vector and then written to a file. Suppose, further, that I happen to know that an object of interest to me starts at a specific byte offset within the file.

What's the right way to read that object from the file?

Of course, I could read the whole file and convert its contents to Scheme objects, continuing until I get the one I want. Suppose, though, that the file might grow very large, and I want to be able to read just a particular indicated object from a particular offset.

The idea, by the way, is that I'm going to serialize values into a log-structured file and use an index to fetch them back again on-demand. If I serialize the values using object->u8vector, then it's very easy to deserialize them using u8vector->object, provided that I have the correct sequence of bytes to feed to u8vector->object.

Given that my index is correct, I have the start of the bytes that make up the object I'm trying to read. Now what's the right way to know how many bytes to read into a u8vector that I can then pass to u8vector->object?

It would be even cooler if there were a sort of stream version of u8vector->object, so that I could open the file, seek to the indicated offset, and then tell u8vector->object to read from the bytes starting at that offset--but I don't particularly expect to find that such a facility exists. 

No matter; if I know how to infer the bytecount of a valid serialized value that starts at a given offset, then I'm home free.

Thanks,

--me




More information about the Gambit-list mailing list