Predicate Query - Java

Click here to view in C++

This is an example on how to use predicate scans. In this example, the database has 4 "Car" objects.

This class has a field String VIN;

The value of value in the four "Car" objects are:

  • 158L3X
  • 462A2H
  • 158P7D
  • 472G1Q

After ingesting some data, the application does a predicate scan on the container that has these four objects for 158*. That is: "VIN=~ \"158.*.\""

Program Output



SM: adding new class to schema named Car as Car
Found Cars with VIN starting with 158
158L3X
158P7D


Car.java


import com.objy.db.app.*;
class Car extends ooObj
{
    public String VIN;
    public void setVin(String vinNum)
    {
        markModified(); //to mark object for update
        VIN = vinNum;
    }
    public String returnVin()
    {
        fetch(); //mark object for read
        return VIN;
    }
};

createObj.java


import com.objy.db.*;
import com.objy.db.app.*;

public class createObj
{
	public static void main(String[] args)
	{
		String bootfile = "test.boot";		//insert bootfile name here
		String dbName = "testDB";		//insert db name here
		String contName = "testCont";		//insert cont name here

		Connection connection = null;
		Session session = null;
		try
		{
			connection = Connection.open(bootfile, oo.openReadWrite);
			connection.loadSchemaClasses(true);
			session = new Session();
			ooFDObj fd = session.getFD();
			session.begin();
			ooDBObj db = null;
			if(fd.hasDB(dbName))
			{
				db = fd.lookupDB(dbName);
			}
			else
			{
				db = fd.newDB(dbName);
			}
			ooContObj cont = null;
			if(db.hasContainer(contName))
			{
				cont = db.lookupContainer(contName);
			}
			else
			{
				cont = new ooContObj();
				db.addContainer(cont, 0, contName, 0, 0);
			}

			Car obj;
			obj = new Car();
			cont.cluster(obj);
			obj.setVin("158L3X");
			obj = new Car();
			cont.cluster(obj);
			obj.setVin("462A2H");
			obj = new Car();
			cont.cluster(obj);
			obj.setVin("158P7D");
			obj = new Car();
			cont.cluster(obj);
			obj.setVin("472G1Q");

			session.commit();

			session.begin();

			Iterator carItr;
			carItr = cont.scan("Car", "VIN=~ \"158.*.\"");
			System.out.println("Found Cars with VIN starting with 158");
			while(carItr.hasNext())
			{
				System.out.println(((Car)carItr.next()).returnVin());
			}
			session.commit();

		}
		catch(Exception e)
		{
			e.printStackTrace();
			session.abort();
		}
	}
}