> 
> I got that far, Neil ... but what I don't understand
> is this: if the ptrs in the unpacked customer record
> are merely pointing into the packed customer record;
> how can you change the city from "Taos" to "Dallas"
> in the form without tromping all over the phone #?
> If there is code to move the phone # up when changing
> the city, I didn't see it.  Did I just miss it, or is
> there some magic being performed by the OS to do this?

Here's what happens when we change the city from "Taos" to "Dallas". We have
an unpacked customer record (containing four character pointers), and we
change one of the pointers from pointing to "Taos" (the bytes of which are
in the packed record in the DB) to "Dallas" (the bytes of which are
somewhere in the dynamic heap).  When we save the record, we convert it from
unpacked to packed. It's at this point that we'll be repacking.

Neil
>
>
>>
>>
>>
>> A packed customer (in a database record) is stored as a
>> 4-byte ID, followed
>> by 4 null-terminated strings (name, address, city, phone).
>>
>> Here's the declaration
>>
>> typedef struct {
>>     SDWord customerID;
>>     char    name[1];    // actually longer than 1
>> } PackedCustomer.
>>
>> Here's an example of the bytes of a (small) packed customer:
>>     0x00 0x00 0x00 0x52 'N' 'e' 'i' 'l' '\0' 'M' 'a' 'i' 'n'
>> '\0' 'R' 'e'
>> 'd' 'l' 'a' 'n' 'd' 's' '\0' '7' '9' '3' '-' '5' '9' '9' '5' '\0'
>>
>> Notice that the name will always start at offset 4 within the
>> packed record.
>>
>> An unpacked customer is used in memory, to actually access the name,
>> address, city, and phone.
>>
>> typedef struct {
>>     SDWord    customerID;
>>     const char *name;
>>     const char *address;
>>     const char *city;
>>     const char *phone;
>> } Customer;
>>
>> It has four character pointers which point into the
>> appropriate offsets
>> within the associated (locked) PackedCustomer.
>>
>> If the example packed customer above were at memory address
>> 0x1230, then the
>> Customer would be
>>     0x00000052        // id
>>     0x1234            // offset 4 within packed customer
>>     0x1239            // offset 9 within packed customer
>>     0x123e            // offset 14 within packed customer
>>     0x1247            // offset 23 within packed customer
>>
>> The authors should have made it more clear in the book, but I
>> think they
>> (incorrectly) assumed this was a well-known, or obvious technique.
>>
>>
>> Neil
>>
>>
>
>

Reply via email to