beautypg.com

Apple Newton Programmer’s Newton 2.0 (for Newton 2.0) User Manual

Page 481

background image

C H A P T E R 1 1

Data Storage and Retrieval

Using Newton Data Storage Objects

11-49

A multiple-slot index solves this problem by sorting entries according to multiple
key values. The key values are extracted from up to six index paths specified by the

path

array of the index specification frame. For example, the following code

fragment specifies a multiple-slot index that sorts entries according to the values
each holds in its

'last

,

'first

, and

'num

slots:

// multiple-slot index on data from three slots

myMultiSlotSpec :=

{structure:'multiSlot,

path: ['last,'first,'num],

type: ['string, 'string, 'int }

The first key in the

path

array is called the primary key; subsequent lower-order

keys, if they are present, are the secondary key, tertiary key, and so on, up to a total
of six keys per array.

The primary key specifies a minimum criterion for inclusion in the index and
provides a value used to sort the indexed entries initially. In the example, only
entries having a

last

slot are indexed, and the value of the

last

slot is used to

impose an initial ordering on the indexed entries. Thus, the multiple-slot index in
the previous example sorts the

"Bates"

entry before all of the

"Perry"

entries.

The secondary key, if it is present, is used to sort entries having identical primary
keys. In the previous example, the multiple-slot index imposes a secondary
ordering on all

"Perry"

entries, according to the value each holds in its

first

slot. Similarly, the tertiary key, if present, is used to sort further any entries having
identical secondary key values. Because none of the entries in the example have
identical secondary key values (none of the

first

slots hold identical values), the

value of each entry’s

num

slot has no effect on how the index sorts the entries.

Thus, the multiple-slot index shown previously sorts the set of sample entries in the
following order:

{last: "Bates", first: "Carol", num: 5}

{last: "Perry", first: "Barbara", num: 3}

{last: "Perry", first: "Bruce", num: 1}

{last: "Perry", first: "Daphne", num: 7}

{last: "Perry", first: "John", num: 4}

{last: "Perry", first: "Ralph", num: 2}

Now that you’re familiar with the manner in which multiple-slot indexes sort entries,
let’s look at the way the

Query

method uses a multiple-slot index to select entries.

Missing slots in a multiple-slot query spec are treated as

nil

values, just as they

are when querying on single-slot indexes. For example, if the query spec is missing
an

endKey

slot, the upper end of the range of entries examined by the query is

unbounded, just as it would be for a query on a single-slot index.