DB4O学习(四)--NQ查询
1.NQ in java
The Java version of db4o also has a Predicate class, but here you can write the query class as an
anonymous inner class, which keeps the query definition inline with the code where the query
is executed. It’s not quite as elegant as the C# 2.0 version as you have to write the match method explicitly.
java版本的db4o有个Predicate类,这里可以写个匿名的查询类,但是java版本必须实现match方法。
List<Person> persons = db.query(new Predicate<Person>() { public boolean match(Person person) { return person.getAge() > 60; } });
for(Person person : persons)
System.out.println(person);C#的语法更简洁:
// C# 2.0 IList<Person> persons = db.Query<Person>(delegate(Person person) { return person.Age > 60; });
foreach(Person person in persons) Console.WriteLine(person);
2.更多语法
(1)范围查询
List<Person> persons = db.query(new Predicate<Person>() { public boolean match(Person person) { return person.getAge() < 60 || person.getAge() > 80; } });
(2)复合查询
List<Person> persons = db.query(new Predicate<Person>() { public boolean match(Person person) { return person.getAge() > 80 && person.getName() == "Mandela"; } });
(3)排序
To sort the results of a native query, you add a Comparison (C#) or Comparator (Java) object as an additional parameter to the
Query method. You can define whatever sorting criteria you like in the Comparison/Comparator,
giving a great deal of flexibility in the way your data is sorted.
为实现排序,必须给查询方法添加一个Comparator参数。
// Comparator Comparator<Person> personCmp = new Comparator<Person>() { public int compare(Person p1, Person p2) { return p1.getName().compareTo(p2.getName()); } };
// Query List<Person> result = db.query(new Predicate<Person>() { public boolean match(Person person) { return true; } },personCmp);
3.处理情况
• If a query is optimized, then none of the code in the query method is ever executed.
如果查询被合理优化为SODA,则查询方法不会被执行。
• If the query is not optimized, all of the code in the query method is executed for every
object of the relevant type in the database.
如果查询没法被优化,则查询里的方法将在每个对象执行。
4.监听查询是否被优化
A Db4oQueryExecutionListener object is a listener with a method notifyQueryExecuted that can
be set to be called when a native query is executed. An NQOptimizationInfo object is automati-
cally passed by db4o as a parameter to this method, with a message indicating whether that
query was optimized (DYNOPTIMIZED) or not (UNOPTIMIZED). You can write code in this method to
do whatever you like—in the example that follows we just print out the message.
// Listener is implemented as an anonymous inner class ((YapStream)db).getNativeQueryHandler().addListener( new Db4oQueryExecutionListener() { public void notifyQueryExecuted(NQOptimizationInfo info) { System.out.println(info.message()); } });
注意:
In Java, NQ optimization requires that the libraries bloat.jar and db4o-5.0-nqopt.jar (or
equivalent) be in the classpath. If they are not, queries will not be optimized. You will NOT get any warning of
this! BLOAT is a Java byte code optimizer that was developed at Purdue University and is used by db4o.
java的优化需要bloat.jar and db4o-5.0-nqopt.jar 的jar包,否则将不被优化。
5.使用查询模板
eclipse—》windows—》preference—》java—》editor—》template—》new nq 选中自动插入
List <${extent}> list = db.query(new Predicate <${extent}> () {
public boolean match(${extent} candidate){
return true;
}
});设置快捷键
eclipse—》windows—》preference-》general-》keys-》content assist-》设置快捷键shift+tab,在editing java source的时候。
使用方法:
输入nq,然后shift+tab就可以了。选择nq自动替换出来模板。
浙公网安备 33010602011771号