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