On Friday 11 June 2004 04:08 pm, E F wrote:

> In the application I have to cycle through records and display the
> apporpriate one based on certain criteria.  I am assumng when cycling
> through them now, I would have to unpack it, read it and continue.  This
> seems like it would slow down the application pretty well.
>
> Am I missing something?  Or is there a better way to go?

I started using numbered fields when I found how useful they were on the  
BlackBerry.  I use a number (tag) to represent a field, and my record chunk 
is several fields concatenated in no particular order.

[len][tag][data][len][tag][data]..

When I want a field, I lock the record chunk and then skip through the 
fields until I find the field's tag.  I make a function such as 
len=get_field(recp, NAME_TAG, &ptr).  Often I will read-only access the 
field via the ptr and len, for convenience and speed.

The advantages are that you can shrink or grow a field since it isn't 
positional, and it's quick to get to a field since you are just skipping 
[len] then comparing [tag] and so on until [tag] matches what you want.  
It's also nice to be able to obsolete a particular field or have a different 
set of fields in each record.  You no longer have to migrate your data to 
fit a new schema, you simply begin supporting some newer field tags.

The PalmOS typical pack/unpack is not so good.  Lots of times a record has 
back-to-back strings and unpacking always involves copying it into RAM 
structures.

The RIM way is to deref each field, getting a ptr and len, within a locked 
record for read-only access.  For writing, they have a load() save() 
symantic which uses RAM.  For example, you would load() the record, make 
field changes, and when you save() it would commit the changes.

So that's what I would suggest.  While you are cycling through records, you 
deref only the fields you need in a record with some type of get_field() 
function.  And if you need to change the record, you make a copy into RAM, 
change the fields with individual set_field() delete_field() etc, and then 
replace the existing record.  For improved speed, you may hack the record 
directly without making a RAM copy.. but you'd loose the transactional 
safety of replacing the record as a whole.

-- 
/* Chris Faherty <[EMAIL PROTECTED]> */

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to