KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > nativequery > example > SimpleMain


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.nativequery.example;
22
23 import java.io.*;
24 import java.util.*;
25
26 import com.db4o.*;
27 import com.db4o.config.*;
28 import com.db4o.inside.query.*;
29 import com.db4o.query.*;
30
31
32 public class SimpleMain {
33     private static final String JavaDoc FILENAME = "simple.yap";
34
35     public static void main(String JavaDoc[] args) {
36         System.setProperty("db4o.dynamicnq","true");
37         new File(FILENAME).delete();
38         ObjectClass classConfig=Db4o.configure().objectClass(Student.class);
39         classConfig.objectField("name").indexed(true);
40         classConfig.objectField("age").indexed(true);
41         ObjectContainer db=Db4o.openFile(FILENAME);
42         try {
43             Student mumon = new Student(100,"Mumon",1.50f);
44             Student tortoise = new Student(101,"Tortoise",0.85f,mumon);
45             Student achilles = new Student(30,"Achilles",1.80f,tortoise);
46             db.set(mumon);
47             db.set(tortoise);
48             db.set(achilles);
49 // for(int i=0;i<100000;i++) {
50
// db.set(new Student(1,"noone",achilles));
51
// }
52
db.commit();
53             db.close();
54             
55             db=Db4o.openFile(FILENAME);
56             final String JavaDoc protoName="Achilles";
57             Predicate filter=new Predicate() {
58                 private int protoAge=203;
59                 
60                 public boolean match(Student candidate) {
61                     return candidate.tortue!=null&&candidate.getTortue().getAge()>=protoAge/2
62                             ||candidate.getName().equals(protoName)
63                             ||candidate.getSize()<1;
64                 }
65             };
66             ((YapStream)db).getNativeQueryHandler().addListener(new Db4oQueryExecutionListener() {
67                 public void notifyQueryExecuted(NQOptimizationInfo info) {
68                     System.err.println(info.message());
69                 }
70             });
71             long startTime=System.currentTimeMillis();
72             List filtered=db.query(filter);
73             for (Iterator resultIter = filtered.iterator(); resultIter.hasNext();) {
74                 Student student = (Student) resultIter.next();
75                 System.out.println(student);
76             }
77             System.out.println("Took "+(System.currentTimeMillis()-startTime)+" ms");
78         }
79         finally {
80             db.close();
81         }
82     }
83 }
84
Popular Tags