Virtual Machinery logo BTree
Home BTree Guide BTree Performance Sample Applications BTree Roadmap Other Products
BTree ME Demo Application


This documentation is supplied as an example of the functionality available with the full distribution. This page describes the main Demo application supplied with the base distribution while the links below will take you to other applications (also provided with the distribution) which illustrate the functionality of the BTrees supplied for use with Suns J2ME Java implementation. These illustrate different ways of working with the J2ME environment depending on the restrictions of the device or the location of the data.The following applications are supplied - (click on links to see further documentation) -

BTree ME Demo using Wireless Toolkit

The BTreeMEDemo sample application class supplied with the Java implementation of the BTreeME is essentially an extension of the demo provided as a sample download from the Virtual Machinery website. It requires a device which supports the J2ME RMS (Record Management Service) - most phones and Palm devices support this to some extent. You can view data in this BTree carry out random access tests on the data and delete the tree. You can also read and write the tree to/from to a host system via a socket connection using utilities supplied as part of the distribution. Although this application has been built and tested on the Wireless Toolkit it will also work on the Nokia and Palm platforms.

Starting the application

When the application first starts you will see the launch screen. Press the 'Launch' button at the bottom right hand side of the screen. The main menu now appears offering eight possible options -

  • View record
  • Create new Tree
  • Run Random Tests
  • Show results
  • Clear results
  • File out current Tree
  • File in external Tree
  • Delete current Tree
You can exit the main menu at any time by pressing the 'Exit' button on the bottom left of the screen.

Creating a new Tree

If you haven't already created a BTree on the device you should do so now by choosing the 'Create new tree' option. After pressing this option you will be prompted for the number of entries that you wish to put in the tree. If you elect to continue by pressing the 'Next' key then a gauge will be displayed which will show the progress as the data is created. When all the data has been created a summary message will be displayed showing how many entries were created, the time taken and the range of keys available. Until you are sure how quickly your device creates keys and how much space is available you should experiment with small numbers of keys (lesss than 20).

The code used to generate the trees is provided in the class GeneraterandomTest. Source code for this class is provided.

Viewing a record

Since we now have some data in the BTree (either loaded from the external sample BTree or created on the device) we can use the up and down arrows on the phone screen to move to the 'View record' option and select it.You can now enter the account number for the account whose details you wish to see. The account number must be in the range 1111111 to 1111111+x where x is the number of accounts that are currently in the BTree. Press 'Show' to view the details associated with that account number.

The code used to access and display a BTree record is very simple -

    if ((lookupKeyBox.getString().length()!=0) && (btree != null)) {
        try { 
            standardTime -= System.currentTimeMillis();
            String res =btree.get(lookupKeyBox.getString());
            standardTime += System.currentTimeMillis();
            standardResults++;
            resultField.setString(res);
        }
	catch (Exception e) {
            clearResults(viewStatus);
            viewStatus.setText("An Error has occurred - perhaps you have not created a tree?");		
        }
   }
Note the collection of the timing data and the simplicity of the get call.

Running a random test

This option allows you to test the speed of access to data in the BTree by accessing random entries in the BTree. After selecting this option you will be prompted for the number of entries that you wish to retrieve from the tree. If you elect to continue by pressing the 'Next' key then a gauge will be displayed which will show the progress as the records are retrievd . When all the data has been created a summary message will be displayed showing how many entries were successfully retrieved and the time taken.

Showing and clearing statistics

Shows statistics relating to the time taken to retrieve records in both Random tests and via the 'View Record' feature.

Resets all the statistics relating to the retrieval of records.

Filing trees to and from a host OS

In each of these cases you will need to start the SocketPoller code.

Filing a tree to the host OS

When you have the poller set up all you have to do is provide the root name for the BTree files on your external OS. Two files will be created XXXX.IND and XXXX.DAT where XXXX is the name that you enter as the external file name. These files will be located in the directory that you ran the VMSocketPoller from.The external files created can be read by other BTree implementations including the BTree toolkit. Note that if your device has not yet been connected to an external data source during this session then you will be asked if it is OK to use airtime. You will need to answer 'Yes' to this to be connected to the socket.

The code in the BTreeMEDemo class that files out the tree is shown below -

/**
 * File Out the Standard Tree
 */
 private void fileOut() {
    if (btree != null) {
        new Thread() {
	    public void run() {
	        RMSBTreeFiler.fileOutBTreeToSocket(STANDARD_TREE, fileName.getString(), btree.getDPageSize(),
btree.getIPageSize(), btree.getLastDBlk(), btree.getLastIBlk(), 5000);
            }
        }.start();
    }
 }
The source code for RMSBTreeFiler is included as part of the distribution

Filing a tree in from the host OS

The code uses port 5000 so you must first run the SocketPoller on that port i.e. by running -

java VMSocketPoller 5000

at the command line. When you have the poller set up all you have to do is provide the file names for the BTree files on your external OS. These files will be located relative to the directory that you ran the VMSocketPoller from.The external files can have been created by other BTree implementations including the J2SE/J2EE BTree implementation. Note that if your device has not yet been connected to an external data source during this session then you will be asked if it is OK to use airtime. You will need to answer 'Yes' to this to be connected to the socket.

The code in the BTreeMEDemo class that files in a tree is shown below -

/**
 * File in an External Tree
 */
private void fileIn() {
    new Thread() {
        public void run() {
            String fName= fileName.getString();
            RMSBTreeFiler.fileInFromSocket(STANDARD_TREE,fName+".DAT",fName+".IND",5000);
            try {
                btree = (BTreeRMSDemo) BTreeRMSDemo.openExistingTree(STANDARD_TREE);
            }
            catch(Exception e) { System.out.println("Error opening existing Tree");e.printStackTrace();}	
	}
    }.start();
}


Deleting the tree

Deletes the current BTree. The code to do this is very simple -

private void cleanTree() {
    try {
        if (btree != null) { btree.closeBTree();}
    }
    catch (Exception e) {e.printStackTrace();}
    if (btree != null) { btree.deleteBTree();}
    btree = null;
} 
Note how the tree is closed prior to deletion and the variable is set to null. These are both good practice. This code deletes all traces of the tree , removing both of the RMS instances used to hold the BTree data and index sets.