]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexedRelationsMemorySearcher.java
Add logging to indexing & replace File-API with NIO Path-API
[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 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * @author Tuukka Lehtonen
44  * @author Antti Villberg
45  */
46 public class IndexedRelationsMemorySearcher extends IndexedRelationsSearcherBase {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(IndexedRelationsMemorySearcher.class);
49     
50     final IndexedRelationsSearcher backend;
51     final GenericRelation r;
52     
53     TLongHashSet changed = new TLongHashSet();
54     
55     IndexedRelationsMemorySearcher(RequestProcessor session, IndexedRelationsSearcher backend, Resource relation, Resource input, GenericRelation r) throws DatabaseException {
56         super(session, relation, input);
57         this.backend = backend;
58         this.r = r;
59         setReady();
60     }
61
62     @Override
63     String getDescriptor() {
64         return "MEM: ";
65     }
66     
67     @Override
68     void insertIndex(IProgressMonitor monitor, GenericRelation r, int boundLength, Collection<Object[]> documentsData) throws CorruptIndexException, IOException, DatabaseException {
69         for(Object[] o : documentsData) {
70             Long resource = (Long)o[1];
71             changed.add(resource);
72         }
73         super.insertIndex(monitor, r, boundLength, documentsData);
74     }
75     
76     @Override
77     boolean replaceIndex(IProgressMonitor monitor, String key, Collection<Object> keyValues, GenericRelation r,
78             int boundLength, Collection<Object[]> documentsData) throws CorruptIndexException, IOException,
79             DatabaseException {
80         for(Object[] o : documentsData) {
81             Long resource = (Long)o[1];
82             changed.add(resource);
83         }
84         return super.replaceIndex(monitor, key, keyValues, r, boundLength, documentsData);
85     }
86     
87     @Override
88     void removeIndex(IProgressMonitor monitor, GenericRelation r, RequestProcessor processor, String key,
89             Collection<Object> keyValues) throws DatabaseException, CorruptIndexException, IOException {
90         for(Object o : keyValues) {
91             Resource resource= (Resource)o;
92             changed.add(resource.getResourceId());
93         }
94         super.removeIndex(monitor, r, processor, key, keyValues);
95     }
96     
97     public List<Object[]> allDocs(IProgressMonitor monitor, Session session) throws ParseException, IOException,
98     DatabaseException {
99         
100         Query query = new MatchAllDocsQuery(); 
101
102         startAccess(null, session, false);
103         
104         TopDocs td = searcher.search(query, Integer.MAX_VALUE);
105         
106         ScoreDoc[ ] scoreDocs = td.scoreDocs; 
107         List<Object[]> result = new ArrayList<Object[]>(scoreDocs.length);
108
109         final Map<String, String> classMap = new THashMap<String, String>();
110         for (Pair<String, String> field : r.getFields()) {
111             classMap.put(field.first, field.second);
112         }
113
114         for(ScoreDoc scoreDoc:scoreDocs) {
115
116             try {
117
118                 Document doc = reader.document(scoreDoc.doc);
119                 List<IndexableField> fs = doc.getFields();
120                 Object[] o = new Object[fs.size()];
121                 int index = 0; 
122                 for (IndexableField f : fs) {
123                     String clazz = classMap.get(f.name());
124                     if ("Long".equals(clazz)) {
125                         o[index++] = Long.parseLong(f.stringValue());
126                     } else {
127                         o[index++] = f.stringValue();
128                     }
129                 }
130                 result.add(o);
131             } catch (CorruptIndexException e) {
132                 throw new DatabaseException(e);
133             } catch (IOException e) {
134                 throw new DatabaseException(e);
135             }
136
137         }
138
139         changeState(monitor, session, State.READY);
140         
141 //        closeInternal();
142         
143         return result;
144
145     }
146     
147     public void commit() {
148         try {
149             if(writer != null)
150                 writer.commit();
151         } catch (CorruptIndexException e) {
152             e.printStackTrace();
153         } catch (IOException e) {
154             e.printStackTrace();
155         }
156     }
157
158 //    public static String cacheReport() {
159 //        StringBuilder sb = new StringBuilder();
160 //        sb.append("Directories: ").append(directories.size()).append("\n");
161 //        for (String key : directories.keySet()) {
162 //            RAMDirectory dir = directories.get(key);
163 //            if (dir != null) {
164 //                sb.append("\t").append(dir).append("\n");
165 //            }
166 //        }
167 //        sb.append("Searchers: ").append(searchers.size()).append("\n");
168 //        for (String key : searchers.keySet()) {
169 //            IndexedRelationsMemorySearcher s = searchers.get(key);
170 //            if (s != null) {
171 //                sb.append("\t").append(s.getClass()).append(": ").append(s.getIndexPath()).append("\n");
172 //            }
173 //        }
174 //        return sb.toString();
175 //    }
176
177     @Override
178     Directory getDirectory(Session session) throws IOException {
179         MemoryIndexing mem = MemoryIndexing.getInstance(session);
180         String path = indexPath.toAbsolutePath().toString();
181         return mem.getDirectory(path, Queries.getAnalyzer());
182     }
183     
184     @Override
185     Throwable bestEffortClear(IProgressMonitor monitor, Session session) {
186         MemoryIndexing mem = MemoryIndexing.getInstance(session);
187         changed.clear();
188         
189         String path = indexPath.toAbsolutePath().toString();
190         mem.remove(path);
191         
192         return null;
193         
194     }
195     
196     @Override
197     protected boolean requireChangeInfoOnReplace() {
198         return false;
199     }
200
201     @Override
202     protected Logger getLogger() {
203         return LOGGER;
204     }
205     
206 }