Introduction
TCQuerymap provides you queryable data structures. I have been trying to find out best ways of querying in-memory data structures. Most of the time we cache data which is single index like Primary Key in database but database offers capability of adding indexes on other properties too so that querying becomes easy. There are other frameworks who provide querying by Iterating all elements which becomes less performant as soon as number of elements increase.
Querymap gives you ability of adding more than one indexes and querying it with simple query API. "TC" here refers to Terracotta. When used with Terracotta it gives you ability of having in-memory database since Terracotta has highly available durable heap where you can store data as well as index and QueryMap makes it queryable.
Details
For details how it works and why it is written you can refer to this blog :
How to start using it.
Suppose you have Domain object which you want to cache and query on mutiple ways.
List<String> list = new ArrayList<String>();
list.add("prop");
list.add("flag");
list.add("inner.property1");
list.add("inner.property2");
TreeIndex index = new TreeIndexImpl(Domain.class,list);
// indexing objects
for(int i=1;i<NUM_OBJECTs;i++){
Domain d = new Domain(i);
// first parameter : Primary Key, Second Param : Bean to index, Third Param : Id
index.index(""+i,d , ""+i);
}
//querying objects
Collection<String> col = from(Domain.class.getName()).where(
//gt("inner.property2", 60),
//lt("inner.property2",89),
//in("inner.property2",10,12,13,14),
//between("inner.property2", 10, 20),
//le("inner.property2", 60),
//ge("inner.property2", 60),
eq("inner.property2",somevalue)
).execute();
Querying
Querying provides following functions :
- eq : Equals like where clause : where object.property="value"
- lt : Less than, < operator of where clause : where object.property < somevalue
- gt : Greater than, >
- le, ge : Less than or equal : >= and <=
- in : finding value in given set e.g. where object.property in (10,20,30)
- between : range query e.g. where object.property between 10 and 20
Putting more than one condition does logical AND of results of two queries.
Where I can use it
You can use is to make your cache "queryable". Suppose you have large number of objects that you want to query on more than one property you can use it.
Real use of querymap is using it with Terracotta where it virtually provides in-memory database with following features
- Transactions
- Query ability
- Persistence
- NO ORM mismatch
How do I use it with Terracotta
For using it as TIM you need to add following lines to tc-config.xml
<modules>
<module group-id="com.google.code" name="querymap-1.0" version="1.0.0"/>
</modules>In code you can use it as queryable map as follows. Here propList is list of properties to be indexed.
TerracottaQueryMap<String, Domain> map = new TerracottaQueryMap(Domain.class,proplist));
map.put(key,domain1);
map.put(key,domain2);
map.put(key,domain3);
To Query.
Collection<Domain> col =map.entrySet(
from(Domain.class.getName()).
where(
eq("inner.property2",somevalue)
)
);
Add your content here. Format your content with:
- Text in bold or italic
- Headings, paragraphs, and lists
- Automatic links to other wiki pages