01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: 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.internal.*;
29: import com.db4o.internal.query.*;
30: import com.db4o.query.*;
31:
32: public class SimpleMain {
33: private static final String FILENAME = "simple.yap";
34:
35: public static void main(String[] args) {
36: System.setProperty("db4o.dynamicnq", "true");
37: new File(FILENAME).delete();
38: ObjectClass classConfig = Db4o.configure().objectClass(
39: Student.class);
40: classConfig.objectField("name").indexed(true);
41: classConfig.objectField("age").indexed(true);
42: ObjectContainer db = Db4o.openFile(FILENAME);
43: try {
44: Student mumon = new Student(100, "Mumon", 1.50f);
45: Student tortoise = new Student(101, "Tortoise", 0.85f,
46: mumon);
47: Student achilles = new Student(30, "Achilles", 1.80f,
48: tortoise);
49: db.set(mumon);
50: db.set(tortoise);
51: db.set(achilles);
52: // for(int i=0;i<100000;i++) {
53: // db.set(new Student(1,"noone",achilles));
54: // }
55: db.commit();
56: db.close();
57:
58: db = Db4o.openFile(FILENAME);
59: final String protoName = "Achilles";
60: Predicate filter = new Predicate() {
61: private int protoAge = 203;
62:
63: public boolean match(Student candidate) {
64: return candidate.tortue != null
65: && candidate.getTortue().getAge() >= protoAge / 2
66: || candidate.getName().equals(protoName)
67: || candidate.getSize() < 1;
68: }
69: };
70: ((ObjectContainerBase) db).getNativeQueryHandler()
71: .addListener(new Db4oQueryExecutionListener() {
72: public void notifyQueryExecuted(
73: NQOptimizationInfo info) {
74: System.err.println(info.message());
75: }
76: });
77: long startTime = System.currentTimeMillis();
78: List filtered = db.query(filter);
79: for (Iterator resultIter = filtered.iterator(); resultIter
80: .hasNext();) {
81: Student student = (Student) resultIter.next();
82: System.out.println(student);
83: }
84: System.out.println("Took "
85: + (System.currentTimeMillis() - startTime) + " ms");
86: } finally {
87: db.close();
88: }
89: }
90: }
|