On Mon, Jul 08, 2002 at 12:03:59AM -0300, R�gis Daniel de Oliveira wrote:
> Hy Marco,
> 
> First of all, I want to thank you for the attention gave while you was
> reading my question.
> 
> You wrote me that i dont need to create a secondary db like a index db, but
> i don't urdestood how can i create a index without creating another db.
> Below there is a sample of my db structure. Can you example how my db will
> be after using that technique that you told me?
> 
> 
> Code        Product           Pack        Price
> 0001        Computer        1 und        1250,00
> 0003        Notebook        1 und        3000,00
> 0025        Mouse            1 und        8,00
> 0027        Keyboard        1 und        200,00
> 0054        Book              1 und        35,00
> 
> My app shoud find using the Code, and another timer, using the Product
> field.
> Please reply me with one example of how can I index the Db showed  above
> without creating another db. Thaks a lot!!!

Hi R�gis,
the point is that palm databases are not really "databases" in the
classical sense. They are simply collection of records, and records
can be arbitrary in either contents and length. In other words you
are not forced to map the records of your database table to the
records of the Palm database.
This said, supposing you want to perform your searches on two
distinct columns, you'll need two indexes, and you can thus allocate
two records at the beginning of the palm database to store these
indexes, say records 0 and 1, then the records of your table will
have records numbers starting at 2 in the palm db.
The index records would list record numbers in the palm db, sorted
on the desired column.
So the layout of the palm db would be the following:


 Palm db record #   Record contents
                     -------------
              0     | Index on    |
                    |    "Code"   |
                     -------------
              1     | Index on    |
                    |  "Product"  |
                     -------------
              2     | 1st table   |
                    |    row      |
                     -------------
              3     | 2nd table   |
                    |    row      |
                     -------------
                    |             |
                          ...
                    |             |
                     -------------
             N+1    | N-th table  |
                    |    row      |
                     -------------

If your data is:

(Palm db rec.#)   Code        Product         Pack        Price
           2      0001        Computer        1 und       1250,00
           3      0003        Notebook        1 und       3000,00
           4      0025        Mouse           1 und       8,00
           5      0027        Keyboard        1 und       200,00
           6      0054        Book            1 und       35,00

(the palm db record number is reported only for clarity, but it's not
stored in the database itself, obviously)
then the "Code" index, stored in record 0, would be:

          2,3,4,5,6

(as these rows are already sorted by Code),
while the "Product" index, stored in record 1, would be:

          6,2,5,4,3

Note that, although I've used an ascii representation here, these record
indexes can be stored in binary form, and simply store two bytes per record
(as record indexes are 16 bit unsigned integers).
You then will load the index records in two separate arrays of record
numbers, and when you'll need to find a record by Code or Product, you'll
simply pick up the proper array and perform a binary search there, using
the array as an indirection to the obtain the actual record index to load.
This way you'll need log2(N) record loads and comparisons, instead of N.

I hope this can clarify a bit the technique. Otherwise please tell me
where I've not been clear enough.

Tchau,
Marco

-- 
========================================================================
Marco Pantaleoni                                  [EMAIL PROTECTED]
Padova, Italy                                              [EMAIL PROTECTED]
elastiC language developer                   http://www.elasticworld.org


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

Reply via email to