]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexedRelationsMemorySearcher.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.db.indexing / src / org / simantics / db / indexing / IndexedRelationsMemorySearcher.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.indexing;
13
14 import gnu.trove.map.hash.THashMap;
15 import gnu.trove.set.hash.TLongHashSet;
16
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.apache.lucene.document.Document;
24 import org.apache.lucene.index.CorruptIndexException;
25 import org.apache.lucene.index.IndexableField;
26 import org.apache.lucene.queryparser.classic.ParseException;
27 import org.apache.lucene.search.MatchAllDocsQuery;
28 import org.apache.lucene.search.Query;
29 import org.apache.lucene.search.ScoreDoc;
30 import org.apache.lucene.search.TopDocs;
31 import org.apache.lucene.store.Directory;
32 import org.eclipse.core.runtime.IProgressMonitor;
33 import org.simantics.db.RequestProcessor;
34 import org.simantics.db.Resource;
35 import org.simantics.db.Session;
36 import org.simantics.db.exception.DatabaseException;
37 import org.simantics.db.layer0.adapter.GenericRelation;
38 import org.simantics.utils.datastructures.Pair;
39
40 /**
41  * @author Tuukka Lehtonen
42  * @author Antti Villberg
43  */
44 public class IndexedRelationsMemorySearcher extends IndexedRelationsSearcherBase {
45
46     final IndexedRelationsSearcher backend;
47     final GenericRelation r;
48     
49     TLongHashSet changed = new TLongHashSet();
50     
51     IndexedRelationsMemorySearcher(RequestProcessor session, IndexedRelationsSearcher backend, Resource relation, Resource input, GenericRelation r) throws DatabaseException {
52         super(session, relation, input);
53         this.backend = backend;
54         this.r = r;
55         setReady();
56     }
57
58     @Override
59     String getDescriptor() {
60         return "MEM: ";
61     }
62     
63     @Override
64     void insertIndex(IProgressMonitor monitor, GenericRelation r, int boundLength, Collection<Object[]> documentsData) throws CorruptIndexException, IOException, DatabaseException {
65         for(Object[] o : documentsData) {
66             Long resource = (Long)o[1];
67             changed.add(resource);
68         }
69         super.insertIndex(monitor, r, boundLength, documentsData);
70     }
71     
72     @Override
73     boolean replaceIndex(IProgressMonitor monitor, String key, Collection<Object> keyValues, GenericRelation r,
74             int boundLength, Collection<Object[]> documentsData) throws CorruptIndexException, IOException,
75             DatabaseException {
76         for(Object[] o : documentsData) {
77             Long resource = (Long)o[1];
78             changed.add(resource);
79         }
80         return super.replaceIndex(monitor, key, keyValues, r, boundLength, documentsData);
81     }
82     
83     @Override
84     void removeIndex(IProgressMonitor monitor, GenericRelation r, RequestProcessor processor, String key,
85             Collection<Object> keyValues) throws DatabaseException, CorruptIndexException, IOException {
86         for(Object o : keyValues) {
87             Resource resource= (Resource)o;
88             changed.add(resource.getResourceId());
89         }
90         super.removeIndex(monitor, r, processor, key, keyValues);
91     }
92     
93     public List<Object[]> allDocs(IProgressMonitor monitor, Session session) throws ParseException, IOException,
94     DatabaseException {
95         
96         Query query = new MatchAllDocsQuery(); 
97
98         startAccess(null, session, false);
99         
100         TopDocs td = searcher.search(query, Integer.MAX_VALUE);
101         
102         ScoreDoc[ ] scoreDocs = td.scoreDocs; 
103         List<Object[]> result = new ArrayList<Object[]>(scoreDocs.length);
104
105         final Map<String, String> classMap = new THashMap<String, String>();
106         for (Pair<String, String> field : r.getFields()) {
107             classMap.put(field.first, field.second);
108         }
109
110         for(ScoreDoc scoreDoc:scoreDocs) {
111
112             try {
113
114                 Document doc = reader.document(scoreDoc.doc);
115                 List<IndexableField> fs = doc.getFields();
116                 Object[] o = new Object[fs.size()];
117                 int index = 0; 
118                 for (IndexableField f : fs) {
119                     String clazz = classMap.get(f.name());
120                     if ("Long".equals(clazz)) {
121                         o[index++] = Long.parseLong(f.stringValue());
122                     } else {
123                         o[index++] = f.stringValue();
124                     }
125                 }
126                 result.add(o);
127             } catch (CorruptIndexException e) {
128                 throw new DatabaseException(e);
129             } catch (IOException e) {
130                 throw new DatabaseException(e);
131             }
132
133         }
134
135         changeState(monitor, session, State.READY);
136         
137 //        closeInternal();
138         
139         return result;
140
141     }
142     
143     public void commit() {
144         try {
145             if(writer != null)
146                 writer.commit();
147         } catch (CorruptIndexException e) {
148             e.printStackTrace();
149         } catch (IOException e) {
150             e.printStackTrace();
151         }
152     }
153
154 //    public static String cacheReport() {
155 //        StringBuilder sb = new StringBuilder();
156 //        sb.append("Directories: ").append(directories.size()).append("\n");
157 //        for (String key : directories.keySet()) {
158 //            RAMDirectory dir = directories.get(key);
159 //            if (dir != null) {
160 //                sb.append("\t").append(dir).append("\n");
161 //            }
162 //        }
163 //        sb.append("Searchers: ").append(searchers.size()).append("\n");
164 //        for (String key : searchers.keySet()) {
165 //            IndexedRelationsMemorySearcher s = searchers.get(key);
166 //            if (s != null) {
167 //                sb.append("\t").append(s.getClass()).append(": ").append(s.getIndexPath()).append("\n");
168 //            }
169 //        }
170 //        return sb.toString();
171 //    }
172
173     @Override
174     Directory getDirectory(Session session) throws IOException {
175         MemoryIndexing mem = MemoryIndexing.getInstance(session);
176         String path = indexPath.getAbsolutePath();
177         return mem.getDirectory(path, Queries.getAnalyzer());
178     }
179     
180     @Override
181     Throwable bestEffortClear(IProgressMonitor monitor, Session session) {
182
183         setProblem(null);
184         
185         MemoryIndexing mem = MemoryIndexing.getInstance(session);
186         changed.clear();
187         
188         String path = indexPath.getAbsolutePath();
189         mem.remove(path);
190         
191         return null;
192         
193     }
194     
195     @Override
196     protected boolean requireChangeInfoOnReplace() {
197         return false;
198     }
199     
200 }