Db4o Learning (4)-NQ Query

Source: Internet
Author: User

1. NQ in Java

The Java version of db4o also has a predicate class, but here you can write the Query Class As
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.

In Java, db4o has a predicate class. You can write an anonymous Query Class here, but in Java, the match method must be implemented.

 
List <person> Persons = dB. Query (NewPredicate <person> (){Public BooleanMatch (person ){ReturnPerson. getage ()> 60 ;}});

 

 
For(Person: Persons) system. Out. println (person );
 
 

C # syntax is more concise:

/// C #2.0Ilist <person> Persons = dB. query <person> (Delegate(Person ){ReturnPerson. age> 60 ;});
 
Foreach(PersonInPersons) console. writeline (person );
 
 

2. More syntaxes

(1) Range Query

 
List <person> Persons = dB. Query (NewPredicate <person> (){Public BooleanMatch (person ){ReturnPerson. getage () <60 | person. getage ()> 80 ;}});
 
 
 
(2) composite Query
List <person> Persons = dB. Query (NewPredicate <person> (){Public BooleanMatch (person ){ReturnPerson. getage ()> 80 & person. getname () ="Mandela";}});

 

(3) sorting

To sort the results of a native query, you add a comparison (C #) or comparator (Java) object as an additional parameter to
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.

To achieve sorting, you must add a comparator parameter to the query method.

// ComparatorComparator <person> personcmp =NewComparator <person> (){Public IntCompare (person P1, person P2 ){ReturnP1.getname (). compareto (p2.getname ());}};
 
// QueryList <person> result = dB. Query (NewPredicate <person> (){Public BooleanMatch (person ){Return True;}},Personcmp);

 

3. Handling status

• If a query is optimized, then none of the Code in the query method is ever executed.

If the query is reasonably optimized to soda, the query method is not executed.

• 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.

If the query cannot be optimized, the method in the query will be executed on each object.

 

4. Check whether the listener query is optimized.

A db4oqueryexecutionlistener object is a listener with a method policyqueryexecuted 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
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 (NewDb4oqueryexecutionlistener (){Public VoidNotifyqueryexecuted (nqoptimizationinfo) {system. Out. println (info. Message ());}});
 
 
 
Note:

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
This! Bloat is a Java Byte Code optimizer that was developed at Purdue University and is used by db4o.

Java Optimization requires a jar package of bloat. jar and db4o-5.0-nqopt.jar, otherwise it will not be optimized.

 

5. Use a query Template

Eclipse-> Windows-> preference-> JAVA-> Editor-> template-> New NQ selected for automatic insertion

 
List <$ {extent}> List = dB. Query (NewPredicate <$ {extent}> (){Public BooleanMatch ($ {extent} candidate ){Return True;}});
 
 
 
Set shortcuts
 
Eclipse-> Windows-> preference-> General-> keys-> content assist-> set the shortcut key shift + TAB when editing Java source.
 
 
 
Usage:
 
Enter NQ and SHIFT + TAB. Select NQ to automatically replace the template.
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.