Java Reference
In-Depth Information
Performing RecordStore Queries
The real power of a database is being able to pull out just the record or records you want. In a
larger database, this is called
performing a query
. In the
RecordStore
world, you use the
enumerateRecords()
method:
public RecordEnumeration enumerateRecords(RecordFilter filter,
RecordComparator comparator, boolean keepUpdated)
throws RecordStoreNotOpenException
This single method in
RecordStore
involves three different interfaces that you've never
seen before. Let's start with the big picture first, and then drill down into the new interfaces.
The
enumerateRecords()
method returns a sorted subset of the records in a
RecordStore
.
The
RecordFilter
determines which records will be included in the subset, while the
RecordComparator
is used to sort them. The returned
RecordEnumeration
allows you to navigate
through the returned records.
RecordFilter
The simplest interface is
RecordFilter
. When you call
enumerateRecords()
on a
RecordStore
, each
record's data is retrieved.
RecordFilter
has a single method,
matches()
, which is called for
each record. A record filter should examine the record data and return
true
if the record should
be included in the results returned from
enumerateRecords()
.
Here's a simple
RecordFilter
implementation that will only select records whose first byte
of data is 7:
public class SevenFilter
implements javax.microedition.rms.RecordFilter {
public boolean matches(byte[] candidate) {
if (candidate.length == 0) return false;
return (candidate[0] == 7);
}
}
RecordComparator
The job of a
RecordComparator
implementation is to determine the order of two sets of record
data.
RecordComparator
is similar to the
java.util.Comparator
interface in J2SE.
To implement the
RecordComparator
interface, you just need to define one method:
public int compare(byte[] rec1, byte[] rec2)
This method examines the data contained in
rec1
and
rec2
and determines which of them
should come first in a sorted list. It should return one of the following constants defined in
RecordComparator
:
•
PRECEDES
indicates that
rec1
should come before
rec2
.
•
FOLLOWS
indicates that
rec1
should come after
rec2
.
•
EQUIVALENT
signals that
rec1
and
rec2
are the same, at least as far as sorting is concerned.
Search WWH ::
Custom Search