superwaba.ext.xplat.io.search
Class CatalogSearch
java.lang.Object
|
+--superwaba.ext.xplat.io.search.CatalogSearch
- public class CatalogSearch
- extends Object
With this class, you can do searches in a Catalog 5 to 10 times faster than in Java.
This class is stored in the SWCatSearch.pdb and is used with the SWCatSearch.prc file.
Here is an example:
if (!Settings.onDevice || Vm.attachNativeLibrary("SWCatSearch"))
{
Catalog cat = new Catalog("TestDB."+Settings.appCreatorId+".DATA", Catalog.READ_ONLY);
CatalogSearch catSearch = new CatalogSearch(cat);
String text = "a9999"; // string to search
int offset = 0; // offset in record
int startRec = 10; // start searching from record 10
cat.setRecordPos(startRec);
// now we convert the String to a byte array
// Note that we're searching for the exact string,
// case sensitive and whole word only.
int length = 0;
ByteArrayStream bas = new ByteArrayStream(16);
DataStream ds = new DataStream(bas);
length += ds.writeString(text);
byte []toSearch = bas.getBuffer(); // the buffer may be greater than length
int start = Vm.getTimeStamp();
int recIndex = catSearch.searchBytes(toSearch, length, offset);
int end = Vm.getTimeStamp();
Vm.debug("Returned record: "+recIndex);
Vm.debug("Elapsed: "+Vm.getTimeElapsed(end,start)+" ms");
}
Some notes:
. The library SWCatSearch.pdb is automaticaly loaded.
. The recordOffset is useful when you write something before of
what you wanna search. For example, if your record contains an
int before a string, you may set recordOffset to 4 (the int is
stored in the first 4 bytes).
. Be careful: to search for a substring inside a String, you must
remember to skip the first two bytes where the String length is
stored.
. The search starts from the current positioned record in the Catalog.
. This class can be used in desktop.
This is not free software. This library can be purchased at the SuperWaba site.
Field Summary |
protected Catalog |
cat
The underlying catalog |
Constructor Summary |
CatalogSearch(Catalog anopenedAndPositionedCatalog)
Creates a CatalogSearch object with the given parameter. |
Method Summary |
int |
searchBytes(byte[] toSearch,
int length,
int offsetInRec)
Searches the underlying Catalog for the given byte array. |
cat
protected Catalog cat
- The underlying catalog
CatalogSearch
public CatalogSearch(Catalog anopenedAndPositionedCatalog)
- Creates a CatalogSearch object with the given parameter. Note
that the catalog must be opened. If you want to start
the search from a specific record index, just do a setRecordPos
in the catalog before calling
searchBytes
.
searchBytes
public int searchBytes(byte[] toSearch,
int length,
int offsetInRec)
- Searches the underlying Catalog for the given byte array. Calls the
native method in SWCatSearch.prc (or a java code if running in desktop).
- Parameters:
toSearch
- The byte array used to compare with the contents
of each record.length
- How many bytes will be searched inside toSearch
(may be smaller than oSearch.length
)offsetInRec
- How many bytes to skip from the record start.- Returns:
- the record index if found, or -1 if toSearch is null, or
the Catalog is null or closed or empty, or length <= 0, or
length > toSearch.length.