CacheStore features a query system that implements Object Queries, minus joins and aggregation. Below is an overview and guide of how our query system works and the basic syntax it uses.
First, we need to start a CacheStore server and start a client through the CacheStore interactive shell. This can be found in the "Quickstart" section under the corresponding user guides (Remote and Cluster).
Once CacheStore is up and running, connect to a store using the CacheStore shell. For this guide, we will be assuming a few things. "c" will be a variable for our client. The Person class will be our model object that will be used as the format for the values being saved and retrieved. The Person class is defined below:
import java.io.Serializable;
public class Person implements Serializable {
String name;
int age;
boolean male;
Address address;
public Person(String name, int age, boolean male, Address address) {
this.name = name;
this.age = age;
this.male = male;
this.address = address;
}
public static class Address implements Serializable{
String street;
int zip;
public Address(String street, int zip){
this.street = street;
this.zip = zip;
}
public String getStreet(){
return street;
}
public int getZip(){
return zip;
}
@Override
public String toString(){
return "Address{" +
"street='" + street + '\'' +
", zip=" + zip +
'}';
}
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public boolean isMale() {
return male;
}
public Address getAddress() {
return address;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", male=" + male +
", address=" + address +
'}';
}
}
Basic Query query(c, "select name from Person") query(c, "select name, age from Person")
Selecting inner class Address from Person
query(c, "select Address.street from Person")
query(c, "select Address.street, Address.zip from Person")
Select with Where statements
query(c, "select name, age from Person where age > 10")
query(c, "select name, age from Person where name = \"Mickey\" or male = false")
query(c, "select name, from Person where Address.street =\"Main\" and Address.zip = 92808")
query(c, "select name, age, Address.zip from Person where (age >15 or male = true) and (Address.zip = 95604)")
Select by key number
query(c, "select name, age from Person where key# = 1")
query(c, "select name, age from Person where (key# > 5 and key# != 20) and (age <= 50 or male = true)")
Replace (update)
query(c, "replace Person set age = 99 where age = 50")
query(c, "replace Address set zip = 10000")
A few things to take note of:
- "query" is the main query command, but there is also "query4v" and "query4List" which use the same query syntax
- The query syntax is case sensitive; statements are all all lower case and object fields must match case exactly how they are defined in the object
- Primitive fields of the object level that is queried for will always show. In these examples, select age should also return male, but not name or Address while select name will return name, age, male, but not Address and so on
- String quotes must be escaped: \"String\"
- "key#" is a reserved keyword
- Doubles, Floats, and Longs must start with an integer; ex. 0.1 instead of .1