001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.test;
022:
023: import java.util.*;
024:
025: import com.db4o.*;
026: import com.db4o.ext.*;
027: import com.db4o.messaging.*;
028: import com.db4o.query.*;
029:
030: /**
031: *
032: */
033: public class FulltextIndex implements MessageRecipient {
034:
035: public String toIndex;
036: private List indexEntries;
037:
038: public void configure() {
039: Db4o.configure().clientServer().setMessageRecipient(
040: new FulltextIndex());
041: Db4o.configure().objectClass(FullTextIndexEntry.class)
042: .objectField("text").indexed(true);
043: }
044:
045: public void storeOne() {
046: toIndex = "This sentence no verb";
047: }
048:
049: public void test() {
050: Query q = Test.query();
051: q.constrain(FullTextIndexEntry.class);
052: q.descend("text").constrain("sentence");
053: boolean found = false;
054: ObjectSet objectSet = q.execute();
055: while (objectSet.hasNext()) {
056: FullTextIndexEntry ftie = (FullTextIndexEntry) objectSet
057: .next();
058: Iterator i = ftie.objects.iterator();
059: while (i.hasNext()) {
060: FulltextIndex fti = (FulltextIndex) i.next();
061: if (fti.toIndex.indexOf("sentence") > -1) {
062: found = true;
063: }
064: }
065: }
066: Test.ensure(found);
067: }
068:
069: public void objectOnNew(ObjectContainer objectContainer) {
070: objectOnUpdate(objectContainer);
071: }
072:
073: public void objectOnUpdate(ObjectContainer objectContainer) {
074: ensureServerMessageRecipient();
075: if (objectContainer instanceof ExtClient) {
076: MessageSender sender = objectContainer.ext().configure()
077: .clientServer().getMessageSender();
078: sender
079: .send(new IDMessage(objectContainer.ext().getID(
080: this )));
081: } else {
082: updateIndex(objectContainer);
083: }
084: }
085:
086: private void updateIndex(ObjectContainer objectContainer) {
087: if (indexEntries != null) {
088: Iterator i = indexEntries.iterator();
089: while (i.hasNext()) {
090: FullTextIndexEntry entry = (FullTextIndexEntry) i
091: .next();
092: entry.objects.remove(this );
093: }
094: indexEntries.clear();
095: } else {
096: indexEntries = objectContainer.ext().collections()
097: .newLinkedList();
098: }
099: String[] strings = toIndex.split(" ");
100: for (int i = 0; i < strings.length; i++) {
101: Query q = objectContainer.query();
102: q.constrain(FullTextIndexEntry.class);
103: q.descend("text").constrain(strings[i]);
104: ObjectSet objectSet = q.execute();
105: if (objectSet.size() == 1) {
106: FullTextIndexEntry ftie = (FullTextIndexEntry) objectSet
107: .next();
108: ftie.objects.add(this );
109: } else {
110: FullTextIndexEntry ftie = new FullTextIndexEntry();
111: ftie.text = strings[i];
112: ftie.objects = objectContainer.ext().collections()
113: .newLinkedList();
114: ftie.objects.add(this );
115: objectContainer.set(ftie);
116: }
117: }
118: objectContainer.commit();
119:
120: }
121:
122: public void processMessage(ObjectContainer objectContainer,
123: Object message) {
124: FulltextIndex fti = (FulltextIndex) objectContainer.ext()
125: .getByID(((IDMessage) message).id);
126: objectContainer.activate(fti, 1);
127: fti.updateIndex(objectContainer);
128: }
129:
130: /**
131: * There are side effects from other test cases that set different message recipients
132: * on the server. We want to make sure that we set the last one.
133: */
134: private void ensureServerMessageRecipient() {
135: ObjectServer server = Test.server();
136: if (server != null) {
137: server.ext().configure().clientServer()
138: .setMessageRecipient(new FulltextIndex());
139: }
140: }
141:
142: public static class FullTextIndexEntry {
143: public String text;
144: public List objects;
145: }
146:
147: public static class IDMessage {
148: public long id;
149:
150: public IDMessage() {
151: }
152:
153: public IDMessage(long id) {
154: this.id = id;
155: }
156: }
157: }
|