KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > FulltextIndex


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.test;
22
23 import java.util.*;
24
25 import com.db4o.*;
26 import com.db4o.ext.*;
27 import com.db4o.messaging.*;
28 import com.db4o.query.*;
29
30
31 /**
32  *
33  */

34 public class FulltextIndex implements MessageRecipient{
35     
36     public String JavaDoc toIndex;
37     private List indexEntries;
38     
39     public void configure(){
40         Db4o.configure().setMessageRecipient(new FulltextIndex());
41         Db4o.configure().objectClass(FullTextIndexEntry.class).objectField("text").indexed(true);
42     }
43     
44     public void storeOne(){
45         toIndex = "This sentence no verb";
46     }
47     
48     public void test(){
49         Query q = Test.query();
50         q.constrain(FullTextIndexEntry.class);
51         q.descend("text").constrain("sentence");
52         boolean found = false;
53         ObjectSet objectSet = q.execute();
54         while(objectSet.hasNext()){
55             FullTextIndexEntry ftie = (FullTextIndexEntry) objectSet.next();
56             Iterator i = ftie.objects.iterator();
57             while(i.hasNext()){
58                 FulltextIndex fti = (FulltextIndex)i.next();
59                 if(fti.toIndex.indexOf("sentence") > -1){
60                     found = true;
61                 }
62             }
63         }
64         Test.ensure(found);
65     }
66     
67     public void objectOnNew(ObjectContainer objectContainer){
68         objectOnUpdate(objectContainer);
69     }
70     
71     public void objectOnUpdate(ObjectContainer objectContainer){
72         ensureServerMessageRecipient();
73         if(objectContainer instanceof ExtClient){
74             MessageSender sender = objectContainer.ext().configure().getMessageSender();
75             sender.send(new IDMessage(objectContainer.ext().getID(this)));
76         }else{
77             updateIndex(objectContainer);
78         }
79     }
80     
81     private void updateIndex(ObjectContainer objectContainer){
82         if(indexEntries != null){
83             Iterator i = indexEntries.iterator();
84             while(i.hasNext()){
85                 FullTextIndexEntry entry = (FullTextIndexEntry)i.next();
86                 entry.objects.remove(this);
87             }
88             indexEntries.clear();
89         }else{
90             indexEntries = objectContainer.ext().collections().newLinkedList();
91         }
92         String JavaDoc[] strings = toIndex.split(" ");
93         for (int i = 0; i < strings.length; i++) {
94             Query q = objectContainer.query();
95             q.constrain(FullTextIndexEntry.class);
96             q.descend("text").constrain(strings[i]);
97             ObjectSet objectSet = q.execute();
98             if(objectSet.size() == 1){
99                 FullTextIndexEntry ftie = (FullTextIndexEntry)objectSet.next();
100                 ftie.objects.add(this);
101             }else{
102                 FullTextIndexEntry ftie = new FullTextIndexEntry();
103                 ftie.text = strings[i];
104                 ftie.objects = objectContainer.ext().collections().newLinkedList();
105                 ftie.objects.add(this);
106                 objectContainer.set(ftie);
107             }
108         }
109         objectContainer.commit();
110         
111     }
112     
113     public void processMessage(ObjectContainer objectContainer, Object JavaDoc message) {
114         FulltextIndex fti = (FulltextIndex)objectContainer.ext().getByID(((IDMessage)message).id);
115         objectContainer.activate(fti, 1);
116         fti.updateIndex(objectContainer);
117     }
118     
119     /**
120      * There are side effects from other test cases that set different message recipients
121      * on the server. We want to make sure that we set the last one.
122      */

123     private void ensureServerMessageRecipient(){
124         ObjectServer server = Test.server();
125         if(server != null){
126             server.ext().configure().setMessageRecipient(new FulltextIndex());
127         }
128     }
129     
130     public static class FullTextIndexEntry {
131         public String JavaDoc text;
132         public List objects;
133     }
134     
135     public static class IDMessage{
136         public long id;
137         public IDMessage(){
138         }
139         public IDMessage(long id){
140             this.id = id;
141         }
142     }
143 }
144
Popular Tags