1 |
7606
|
tao
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Purpose: A class that gets Accession Number, check for uniqueness
|
4 |
|
|
* and register it into db
|
5 |
|
|
* Copyright: 2000 Regents of the University of California and the
|
6 |
|
|
* National Center for Ecological Analysis and Synthesis
|
7 |
|
|
* Authors: Jivka Bojilova, Matt Jones
|
8 |
|
|
*
|
9 |
|
|
* '$Author: leinfelder $'
|
10 |
|
|
* '$Date: 2011-11-02 20:40:12 -0700 (Wed, 02 Nov 2011) $'
|
11 |
|
|
* '$Revision: 6595 $'
|
12 |
|
|
*
|
13 |
|
|
* This program is free software; you can redistribute it and/or modify
|
14 |
|
|
* it under the terms of the GNU General Public License as published by
|
15 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
16 |
|
|
* (at your option) any later version.
|
17 |
|
|
*
|
18 |
|
|
* This program is distributed in the hope that it will be useful,
|
19 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21 |
|
|
* GNU General Public License for more details.
|
22 |
|
|
*
|
23 |
|
|
* You should have received a copy of the GNU General Public License
|
24 |
|
|
* along with this program; if not, write to the Free Software
|
25 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26 |
|
|
*/
|
27 |
|
|
package edu.ucsb.nceas.metacat.index;
|
28 |
|
|
|
29 |
7691
|
tao
|
import java.io.FileInputStream;
|
30 |
|
|
import java.io.FileNotFoundException;
|
31 |
7606
|
tao
|
import java.io.InputStream;
|
32 |
|
|
import java.util.ArrayList;
|
33 |
|
|
import java.util.Date;
|
34 |
|
|
import java.util.List;
|
35 |
7788
|
tao
|
import java.util.TimerTask;
|
36 |
7606
|
tao
|
|
37 |
|
|
import org.apache.commons.logging.Log;
|
38 |
|
|
import org.apache.commons.logging.LogFactory;
|
39 |
|
|
import org.apache.solr.client.solrj.SolrServerException;
|
40 |
|
|
import org.dataone.configuration.Settings;
|
41 |
|
|
import org.dataone.service.exceptions.InvalidRequest;
|
42 |
|
|
import org.dataone.service.exceptions.InvalidToken;
|
43 |
|
|
import org.dataone.service.exceptions.NotAuthorized;
|
44 |
|
|
import org.dataone.service.exceptions.NotImplemented;
|
45 |
|
|
import org.dataone.service.exceptions.ServiceFailure;
|
46 |
|
|
import org.dataone.service.types.v1.Identifier;
|
47 |
7740
|
tao
|
import org.dataone.service.types.v1.ObjectFormatIdentifier;
|
48 |
7606
|
tao
|
import org.dataone.service.types.v1.SystemMetadata;
|
49 |
|
|
|
50 |
7691
|
tao
|
import com.hazelcast.core.IMap;
|
51 |
7793
|
tao
|
import com.hazelcast.core.ISet;
|
52 |
7606
|
tao
|
|
53 |
7828
|
leinfelder
|
import edu.ucsb.nceas.metacat.common.index.event.IndexEvent;
|
54 |
7802
|
tao
|
import edu.ucsb.nceas.metacat.index.event.EventlogFactory;
|
55 |
7806
|
tao
|
import edu.ucsb.nceas.metacat.index.event.IndexEventLogException;
|
56 |
7691
|
tao
|
|
57 |
7802
|
tao
|
|
58 |
7606
|
tao
|
/**
|
59 |
|
|
* A class represents the object to generate massive solr indexes.
|
60 |
|
|
* This can happen during an update of Metacat (generating index for all existing documents)
|
61 |
|
|
* or regenerate index for those documents
|
62 |
|
|
* failing to build index during the insert or update.
|
63 |
|
|
*
|
64 |
|
|
* @author tao
|
65 |
|
|
*
|
66 |
|
|
*/
|
67 |
7788
|
tao
|
public class IndexGenerator extends TimerTask {
|
68 |
7606
|
tao
|
|
69 |
7740
|
tao
|
private static final int FIRST =0;
|
70 |
|
|
private static final int SECOND =1;
|
71 |
7774
|
tao
|
public static final int WAITTIME = 10000;
|
72 |
|
|
public static final int MAXWAITNUMBER = 180;
|
73 |
7606
|
tao
|
private static final String HTTP = "http://";
|
74 |
|
|
private static final String MNAPPENDIX = "/d1/mn";
|
75 |
7740
|
tao
|
private static final String RESOURCEMAPPROPERYNAME = "index.resourcemap.namespace";
|
76 |
7774
|
tao
|
public static final String WAITIMEPOPERTYNAME = "index.regenerate.start.waitingtime";
|
77 |
|
|
public static final String MAXATTEMPTSPROPERTYNAME = "index.regenerate.start.maxattempts";
|
78 |
7748
|
tao
|
|
79 |
|
|
private static int waitingTime = WAITTIME;
|
80 |
|
|
private static int maxAttempts = MAXWAITNUMBER;
|
81 |
|
|
|
82 |
7606
|
tao
|
private SolrIndex solrIndex = null;
|
83 |
7691
|
tao
|
//private SystemMetadataEventListener systemMetadataListener = null;
|
84 |
|
|
private IMap<Identifier, SystemMetadata> systemMetadataMap;
|
85 |
|
|
private IMap<Identifier, String> objectPathMap;
|
86 |
7606
|
tao
|
private Log log = LogFactory.getLog(IndexGenerator.class);
|
87 |
7793
|
tao
|
//private MNode mNode = null;
|
88 |
7786
|
tao
|
private static List<String> resourceMapNamespaces = null;
|
89 |
7606
|
tao
|
|
90 |
|
|
/**
|
91 |
|
|
* Constructor
|
92 |
|
|
* @param solrIndex
|
93 |
|
|
* @param systemMetadataListener
|
94 |
|
|
*/
|
95 |
7691
|
tao
|
public IndexGenerator(SolrIndex solrIndex) {
|
96 |
7606
|
tao
|
this.solrIndex = solrIndex;
|
97 |
7740
|
tao
|
resourceMapNamespaces = Settings.getConfiguration().getList(RESOURCEMAPPROPERYNAME);
|
98 |
7691
|
tao
|
//this.systemMetadataListener = systemMetadataListener;
|
99 |
7793
|
tao
|
//this.mNode = new MNode(buildMNBaseURL());
|
100 |
7748
|
tao
|
try {
|
101 |
|
|
waitingTime = Settings.getConfiguration().getInt(WAITIMEPOPERTYNAME);
|
102 |
|
|
maxAttempts = Settings.getConfiguration().getInt(MAXATTEMPTSPROPERTYNAME);
|
103 |
|
|
} catch (Exception e) {
|
104 |
|
|
log.warn("IndexGenerator.constructor - couldn't read the waiting time or maxattempts from the metacat.properties file since : "+e.getMessage()+". Default values will be used");
|
105 |
|
|
waitingTime = WAITTIME;
|
106 |
|
|
maxAttempts = MAXWAITNUMBER;
|
107 |
|
|
}
|
108 |
7606
|
tao
|
}
|
109 |
|
|
|
110 |
|
|
/**
|
111 |
|
|
* Build the index for all documents in Metacat without overwriting.
|
112 |
|
|
* @throws SolrServerException
|
113 |
|
|
* @throws ServiceFailure
|
114 |
|
|
* @throws NotImplemented
|
115 |
|
|
* @throws NotAuthorized
|
116 |
|
|
* @throws InvalidToken
|
117 |
|
|
* @throws InvalidRequest
|
118 |
7806
|
tao
|
* @throws IndexEventLogException
|
119 |
|
|
* @throws IllegalAccessException
|
120 |
|
|
* @throws InstantiationException
|
121 |
|
|
* @throws ClassNotFoundException
|
122 |
7606
|
tao
|
*/
|
123 |
7806
|
tao
|
/*public void indexAll() throws InvalidRequest, InvalidToken, NotAuthorized,
|
124 |
|
|
NotImplemented, ServiceFailure, SolrServerException, FileNotFoundException, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException {
|
125 |
7606
|
tao
|
boolean force = false;
|
126 |
|
|
indexAll(force);
|
127 |
7806
|
tao
|
}*/
|
128 |
7606
|
tao
|
|
129 |
|
|
/**
|
130 |
7806
|
tao
|
* Build the index for all documents.
|
131 |
7606
|
tao
|
* @throws SolrServerException
|
132 |
|
|
* @throws ServiceFailure
|
133 |
|
|
* @throws NotImplemented
|
134 |
|
|
* @throws NotAuthorized
|
135 |
|
|
* @throws InvalidToken
|
136 |
|
|
* @throws InvalidRequest
|
137 |
7806
|
tao
|
* @throws IndexEventLogException
|
138 |
|
|
* @throws IllegalAccessException
|
139 |
|
|
* @throws InstantiationException
|
140 |
|
|
* @throws ClassNotFoundException
|
141 |
7606
|
tao
|
*/
|
142 |
7806
|
tao
|
public void indexAll() throws InvalidRequest, InvalidToken,
|
143 |
|
|
NotAuthorized, NotImplemented, ServiceFailure, SolrServerException, FileNotFoundException, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException {
|
144 |
7606
|
tao
|
Date since = null;
|
145 |
|
|
Date until = null;
|
146 |
7806
|
tao
|
index(since, until);
|
147 |
7606
|
tao
|
}
|
148 |
|
|
|
149 |
|
|
/**
|
150 |
|
|
* Build the index for the docs which have been modified since the specified date.
|
151 |
|
|
* @param since
|
152 |
|
|
* @throws SolrServerException
|
153 |
|
|
* @throws ServiceFailure
|
154 |
|
|
* @throws NotImplemented
|
155 |
|
|
* @throws NotAuthorized
|
156 |
|
|
* @throws InvalidToken
|
157 |
|
|
* @throws InvalidRequest
|
158 |
7806
|
tao
|
* @throws IndexEventLogException
|
159 |
|
|
* @throws IllegalAccessException
|
160 |
|
|
* @throws InstantiationException
|
161 |
|
|
* @throws ClassNotFoundException
|
162 |
7606
|
tao
|
*/
|
163 |
7806
|
tao
|
public void index(Date since) throws InvalidRequest, InvalidToken,
|
164 |
|
|
NotAuthorized, NotImplemented, ServiceFailure, SolrServerException, FileNotFoundException, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException {
|
165 |
7606
|
tao
|
Date until = null;
|
166 |
7806
|
tao
|
index(since, until);
|
167 |
7606
|
tao
|
}
|
168 |
|
|
|
169 |
|
|
/**
|
170 |
|
|
* Build the index for the docs which have been modified between the specified date.s
|
171 |
|
|
* @param since
|
172 |
|
|
* @param until
|
173 |
|
|
* @throws SolrServerException
|
174 |
|
|
* @throws ServiceFailure
|
175 |
|
|
* @throws NotImplemented
|
176 |
|
|
* @throws NotAuthorized
|
177 |
|
|
* @throws InvalidToken
|
178 |
|
|
* @throws InvalidRequest
|
179 |
7691
|
tao
|
* @throws FileNotFoundException
|
180 |
7806
|
tao
|
* @throws IndexEventLogException
|
181 |
|
|
* @throws IllegalAccessException
|
182 |
|
|
* @throws InstantiationException
|
183 |
|
|
* @throws ClassNotFoundException
|
184 |
7606
|
tao
|
*/
|
185 |
7806
|
tao
|
public void index(Date since, Date until) throws SolrServerException, InvalidRequest,
|
186 |
|
|
InvalidToken, NotAuthorized, NotImplemented, ServiceFailure, FileNotFoundException, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException {
|
187 |
|
|
Date processedDate = null;
|
188 |
7606
|
tao
|
List<String> solrIds = null;
|
189 |
7740
|
tao
|
initSystemMetadataMap();
|
190 |
|
|
initObjectPathMap();
|
191 |
7806
|
tao
|
List[] metacatIds = getMetacatIds(since, until);
|
192 |
|
|
List<String> otherMetacatIds = metacatIds[FIRST];
|
193 |
|
|
List<String> resourceMapIds = metacatIds[SECOND];
|
194 |
|
|
|
195 |
|
|
//figure out the procesedDate by comparing the last element of otherMetacatIds and resourceMapIds.
|
196 |
|
|
Date latestOtherId = null;
|
197 |
|
|
if (otherMetacatIds != null && !otherMetacatIds.isEmpty()) {
|
198 |
|
|
int size = otherMetacatIds.size();
|
199 |
|
|
String id = otherMetacatIds.get(size-1);
|
200 |
|
|
SystemMetadata sysmeta = getSystemMetadata(id);
|
201 |
|
|
latestOtherId = sysmeta.getDateSysMetadataModified();
|
202 |
7606
|
tao
|
}
|
203 |
7806
|
tao
|
Date latestResourceId = null;
|
204 |
|
|
if (resourceMapIds != null && !resourceMapIds.isEmpty()) {
|
205 |
|
|
int size = resourceMapIds.size();
|
206 |
|
|
String id = resourceMapIds.get(size-1);
|
207 |
|
|
SystemMetadata sysmeta = getSystemMetadata(id);
|
208 |
|
|
latestResourceId = sysmeta.getDateSysMetadataModified();
|
209 |
|
|
}
|
210 |
|
|
if(latestOtherId != null && latestResourceId != null && latestOtherId.getTime() > latestResourceId.getTime()) {
|
211 |
|
|
processedDate = latestOtherId;
|
212 |
|
|
} else if (latestOtherId != null && latestResourceId != null && latestOtherId.getTime() <= latestResourceId.getTime()) {
|
213 |
|
|
processedDate = latestResourceId;
|
214 |
|
|
} else if (latestOtherId == null && latestResourceId != null) {
|
215 |
|
|
processedDate = latestResourceId;
|
216 |
|
|
} else if (latestOtherId != null && latestResourceId == null) {
|
217 |
|
|
processedDate = latestOtherId;
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
//add the failedPids
|
221 |
7815
|
leinfelder
|
List<IndexEvent> failedEvents = EventlogFactory.createIndexEventLog().getEvents(null, null, null, null);
|
222 |
7806
|
tao
|
List<String> failedOtherIds = new ArrayList<String>();
|
223 |
|
|
List<String> failedResourceMapIds = new ArrayList<String>();
|
224 |
7815
|
leinfelder
|
if(failedEvents != null) {
|
225 |
|
|
for(IndexEvent event : failedEvents) {
|
226 |
|
|
String id = event.getIdentifier().getValue();
|
227 |
7806
|
tao
|
SystemMetadata sysmeta = getSystemMetadata(id);
|
228 |
|
|
if(sysmeta != null && !sysmeta.getArchived()) {
|
229 |
|
|
ObjectFormatIdentifier formatId =sysmeta.getFormatId();
|
230 |
|
|
if(formatId != null && formatId.getValue() != null && resourceMapNamespaces != null && isResourceMap(formatId)) {
|
231 |
|
|
failedResourceMapIds.add(id);
|
232 |
|
|
} else {
|
233 |
|
|
failedOtherIds.add(id);
|
234 |
|
|
}
|
235 |
|
|
}
|
236 |
|
|
}
|
237 |
|
|
}
|
238 |
|
|
|
239 |
|
|
if(!failedOtherIds.isEmpty()) {
|
240 |
|
|
failedOtherIds.addAll(otherMetacatIds);
|
241 |
|
|
} else {
|
242 |
|
|
failedOtherIds = otherMetacatIds;
|
243 |
|
|
}
|
244 |
|
|
|
245 |
|
|
if(!failedResourceMapIds.isEmpty()) {
|
246 |
|
|
failedResourceMapIds.addAll(resourceMapIds);
|
247 |
|
|
} else {
|
248 |
|
|
failedResourceMapIds = resourceMapIds;
|
249 |
|
|
}
|
250 |
|
|
|
251 |
|
|
log.info("the metacat ids (exception resource map -----------------------------"+failedOtherIds);
|
252 |
|
|
log.info("the metacat resroucemap ids -----------------------------"+failedResourceMapIds);
|
253 |
|
|
index(failedOtherIds);
|
254 |
|
|
index(failedResourceMapIds);
|
255 |
7740
|
tao
|
|
256 |
7806
|
tao
|
//record the timed index.
|
257 |
|
|
if(processedDate != null) {
|
258 |
|
|
EventlogFactory.createIndexEventLog().setLastProcessDate(processedDate);
|
259 |
|
|
}
|
260 |
7734
|
tao
|
|
261 |
7740
|
tao
|
}
|
262 |
|
|
|
263 |
|
|
/*
|
264 |
7806
|
tao
|
* Doing index
|
265 |
7740
|
tao
|
*/
|
266 |
7806
|
tao
|
private void index(List<String> metacatIds) {
|
267 |
7688
|
tao
|
if(metacatIds != null) {
|
268 |
7606
|
tao
|
for(String metacatId : metacatIds) {
|
269 |
|
|
if(metacatId != null) {
|
270 |
|
|
try {
|
271 |
|
|
generateIndex(metacatId);
|
272 |
|
|
} catch (Exception e) {
|
273 |
|
|
log.error("IndexGenerator.index - Metacat Index couldn't generate the index for the id - "+metacatId+" because "+e.getMessage());
|
274 |
|
|
}
|
275 |
|
|
|
276 |
7806
|
tao
|
|
277 |
7606
|
tao
|
}
|
278 |
|
|
}
|
279 |
7688
|
tao
|
}
|
280 |
7606
|
tao
|
}
|
281 |
|
|
|
282 |
7613
|
tao
|
public void run() {
|
283 |
7805
|
tao
|
/*IndexEvent event = new IndexEvent();
|
284 |
7802
|
tao
|
event.setDate(Calendar.getInstance().getTime());
|
285 |
|
|
event.setType(IndexEvent.STARTTIMEDINDEX);
|
286 |
|
|
event.setDescription("Start the timed index job");
|
287 |
7613
|
tao
|
try {
|
288 |
7802
|
tao
|
EventlogFactory.createIndexEventLog().write(event);
|
289 |
|
|
} catch (Exception e) {
|
290 |
|
|
log.error("IndexGenerator.run - IndexEventLog can't log the timed indexing start event :"+e.getMessage());
|
291 |
7805
|
tao
|
}*/
|
292 |
7802
|
tao
|
try {
|
293 |
7806
|
tao
|
Date since = EventlogFactory.createIndexEventLog().getLastProcessDate();
|
294 |
|
|
index(since);
|
295 |
7613
|
tao
|
} catch (InvalidRequest e) {
|
296 |
|
|
// TODO Auto-generated catch block
|
297 |
|
|
//e.printStackTrace();
|
298 |
|
|
log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
|
299 |
|
|
} catch (InvalidToken e) {
|
300 |
|
|
// TODO Auto-generated catch block
|
301 |
|
|
//e.printStackTrace();
|
302 |
|
|
log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
|
303 |
|
|
} catch (NotAuthorized e) {
|
304 |
|
|
// TODO Auto-generated catch block
|
305 |
|
|
//e.printStackTrace();
|
306 |
|
|
} catch (NotImplemented e) {
|
307 |
|
|
// TODO Auto-generated catch block
|
308 |
|
|
//e.printStackTrace();
|
309 |
|
|
log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
|
310 |
|
|
} catch (ServiceFailure e) {
|
311 |
|
|
// TODO Auto-generated catch block
|
312 |
|
|
//e.printStackTrace();
|
313 |
|
|
log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
|
314 |
|
|
} catch (SolrServerException e) {
|
315 |
|
|
// TODO Auto-generated catch block
|
316 |
|
|
//e.printStackTrace();
|
317 |
|
|
log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
|
318 |
7691
|
tao
|
} catch (FileNotFoundException e) {
|
319 |
|
|
log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
|
320 |
7613
|
tao
|
}
|
321 |
7805
|
tao
|
/*event.setDate(Calendar.getInstance().getTime());
|
322 |
7802
|
tao
|
event.setType(IndexEvent.FINISHTIMEDINDEX);
|
323 |
|
|
event.setDescription("Finish the timed index job");
|
324 |
|
|
try {
|
325 |
|
|
EventlogFactory.createIndexEventLog().write(event);
|
326 |
|
|
} catch (Exception e) {
|
327 |
|
|
log.error("IndexGenerator.run - IndexEventLog can't log the timed indexing finish event :"+e.getMessage());
|
328 |
7806
|
tao
|
}*/ catch (ClassNotFoundException e) {
|
329 |
|
|
// TODO Auto-generated catch block
|
330 |
|
|
log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
|
331 |
|
|
} catch (InstantiationException e) {
|
332 |
|
|
// TODO Auto-generated catch block
|
333 |
|
|
log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
|
334 |
|
|
} catch (IllegalAccessException e) {
|
335 |
|
|
// TODO Auto-generated catch block
|
336 |
|
|
log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
|
337 |
|
|
} catch (IndexEventLogException e) {
|
338 |
|
|
// TODO Auto-generated catch block
|
339 |
|
|
log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
|
340 |
|
|
}
|
341 |
7613
|
tao
|
}
|
342 |
|
|
|
343 |
7606
|
tao
|
/*
|
344 |
|
|
* Get the indexed ids list from the solr server.
|
345 |
|
|
* An empty list will be returned if there is no ids.
|
346 |
|
|
*/
|
347 |
|
|
private List<String> getSolrDocIds() throws SolrServerException {
|
348 |
|
|
List<String> ids = solrIndex.getSolrIds();
|
349 |
|
|
return ids;
|
350 |
|
|
}
|
351 |
|
|
|
352 |
|
|
/*
|
353 |
7806
|
tao
|
* Get an array of the list of ids of the metacat which has the systemmetadata modification in the range.
|
354 |
|
|
*
|
355 |
|
|
* If since and util are null, it will return all of them.
|
356 |
7740
|
tao
|
* The first element of the list is the ids except the resource map. The second elements of the list is the ids of the resource map.
|
357 |
7806
|
tao
|
* The reason to split them is when we index the resource map, we need the index of the documents in the resource map ready.
|
358 |
|
|
* The last element in the each list has the latest SystemMetadata modification date. But they are not sorted.
|
359 |
7606
|
tao
|
*/
|
360 |
7793
|
tao
|
private List[] getMetacatIds(Date since, Date until) throws InvalidRequest,
|
361 |
|
|
InvalidToken, NotAuthorized, NotImplemented, ServiceFailure, FileNotFoundException {
|
362 |
7740
|
tao
|
|
363 |
|
|
List<String> resourceMapIds = new ArrayList();
|
364 |
|
|
List<String> otherIds = new ArrayList();
|
365 |
|
|
List[] ids = new List[2];
|
366 |
|
|
ids[FIRST]= otherIds;
|
367 |
|
|
ids[SECOND] = resourceMapIds;
|
368 |
7793
|
tao
|
ISet<Identifier> metacatIds = DistributedMapsFactory.getIdentifiersSet();
|
369 |
7806
|
tao
|
Date otherPreviousDate = null;
|
370 |
|
|
Date resourceMapPreviousDate = null;
|
371 |
7793
|
tao
|
if(metacatIds != null) {
|
372 |
|
|
for(Identifier identifier : metacatIds) {
|
373 |
|
|
if(identifier != null && identifier.getValue() != null && !identifier.getValue().equals("")) {
|
374 |
|
|
SystemMetadata sysmeta = getSystemMetadata(identifier.getValue());
|
375 |
|
|
if(sysmeta != null && !sysmeta.getArchived()) {
|
376 |
|
|
ObjectFormatIdentifier formatId =sysmeta.getFormatId();
|
377 |
|
|
//System.out.println("the object format id is "+formatId.getValue());
|
378 |
|
|
//System.out.println("the ============ resourcMapNamespaces"+resourceMapNamespaces);
|
379 |
|
|
boolean correctTimeRange = false;
|
380 |
|
|
Date sysDate = sysmeta.getDateSysMetadataModified();
|
381 |
|
|
if(since == null && until == null) {
|
382 |
|
|
correctTimeRange = true;
|
383 |
|
|
} else if (since != null && until == null) {
|
384 |
|
|
if(sysDate.getTime() >= since.getTime()) {
|
385 |
|
|
correctTimeRange = true;
|
386 |
|
|
}
|
387 |
|
|
} else if (since == null && until != null) {
|
388 |
|
|
if(sysDate.getTime() <= until.getTime()) {
|
389 |
|
|
correctTimeRange = true;
|
390 |
|
|
}
|
391 |
|
|
} else if (since != null && until != null) {
|
392 |
|
|
if(sysDate.getTime() >= since.getTime() && sysDate.getTime() <= until.getTime()) {
|
393 |
|
|
correctTimeRange = true;
|
394 |
|
|
}
|
395 |
|
|
}
|
396 |
|
|
if(correctTimeRange && formatId != null && formatId.getValue() != null && resourceMapNamespaces != null && isResourceMap(formatId)) {
|
397 |
7806
|
tao
|
//for the resource map
|
398 |
|
|
if(!resourceMapIds.isEmpty()) {
|
399 |
|
|
if(sysDate.getTime() > resourceMapPreviousDate.getTime()) {
|
400 |
|
|
resourceMapIds.add(identifier.getValue());//append to the end of the list if current is later than the previous one
|
401 |
7848
|
tao
|
resourceMapPreviousDate = sysDate;//reset resourceMapPreviousDate to the bigger one
|
402 |
7806
|
tao
|
} else {
|
403 |
|
|
int size = resourceMapIds.size();//
|
404 |
|
|
resourceMapIds.add(size -1, identifier.getValue());//keep the previous one at the end of the list.
|
405 |
|
|
}
|
406 |
|
|
} else {
|
407 |
|
|
resourceMapIds.add(identifier.getValue());
|
408 |
7848
|
tao
|
resourceMapPreviousDate = sysDate;//init resourcemapPreviousDate
|
409 |
7806
|
tao
|
}
|
410 |
7848
|
tao
|
|
411 |
7838
|
tao
|
} else if (correctTimeRange) {
|
412 |
7806
|
tao
|
if(!otherIds.isEmpty()) {
|
413 |
|
|
if(sysDate.getTime() > otherPreviousDate.getTime()) {
|
414 |
|
|
otherIds.add(identifier.getValue());
|
415 |
7848
|
tao
|
otherPreviousDate = sysDate;//reset otherPreviousDate to the bigger one
|
416 |
7740
|
tao
|
} else {
|
417 |
7806
|
tao
|
int size = otherIds.size();
|
418 |
|
|
otherIds.add(size-1, identifier.getValue());
|
419 |
7740
|
tao
|
}
|
420 |
7806
|
tao
|
} else {
|
421 |
|
|
otherIds.add(identifier.getValue());
|
422 |
7848
|
tao
|
otherPreviousDate = sysDate;//init otherPreviousDate
|
423 |
7740
|
tao
|
}
|
424 |
7848
|
tao
|
|
425 |
7607
|
tao
|
}
|
426 |
7806
|
tao
|
|
427 |
7607
|
tao
|
}
|
428 |
|
|
}
|
429 |
|
|
}
|
430 |
7806
|
tao
|
}
|
431 |
7606
|
tao
|
return ids;
|
432 |
|
|
}
|
433 |
|
|
|
434 |
|
|
/*
|
435 |
7740
|
tao
|
* If the specified ObjectFormatIdentifier is a resrouce map namespace.
|
436 |
|
|
*/
|
437 |
7786
|
tao
|
public static boolean isResourceMap(ObjectFormatIdentifier formatId) {
|
438 |
7740
|
tao
|
boolean isResourceMap = false;
|
439 |
|
|
if(formatId != null && resourceMapNamespaces != null) {
|
440 |
|
|
for(String namespace : resourceMapNamespaces) {
|
441 |
|
|
if(namespace != null && formatId.getValue() != null && !formatId.getValue().trim().equals("") && formatId.getValue().equals(namespace)) {
|
442 |
|
|
isResourceMap = true;
|
443 |
|
|
break;
|
444 |
|
|
}
|
445 |
|
|
}
|
446 |
|
|
}
|
447 |
|
|
return isResourceMap;
|
448 |
|
|
}
|
449 |
|
|
|
450 |
7793
|
tao
|
|
451 |
7606
|
tao
|
|
452 |
|
|
/*
|
453 |
|
|
* Generate index for the id.
|
454 |
|
|
*/
|
455 |
|
|
private void generateIndex(String id) throws Exception {
|
456 |
|
|
if(id != null) {
|
457 |
7691
|
tao
|
SystemMetadata sysmeta = getSystemMetadata(id);
|
458 |
|
|
//only update none-archived id.
|
459 |
|
|
if(sysmeta != null && !sysmeta.getArchived()) {
|
460 |
|
|
InputStream data = getDataObject(id);
|
461 |
7606
|
tao
|
Identifier obsolete = sysmeta.getObsoletes();
|
462 |
7627
|
tao
|
List<String> obsoleteChain = null;
|
463 |
7606
|
tao
|
if(obsolete != null) {
|
464 |
7691
|
tao
|
obsoleteChain = getObsoletes(id);
|
465 |
7627
|
tao
|
}
|
466 |
|
|
solrIndex.update(id, obsoleteChain, sysmeta, data);
|
467 |
7606
|
tao
|
} else {
|
468 |
|
|
throw new Exception("IndexGenerator.generate - there is no found SystemMetadata associated with the id "+id);
|
469 |
|
|
}
|
470 |
|
|
|
471 |
|
|
}
|
472 |
|
|
}
|
473 |
7691
|
tao
|
|
474 |
|
|
/*
|
475 |
|
|
* Initialize the system metadata map
|
476 |
|
|
*/
|
477 |
|
|
private void initSystemMetadataMap() throws FileNotFoundException, ServiceFailure{
|
478 |
|
|
int times = 0;
|
479 |
|
|
if(systemMetadataMap == null) {
|
480 |
7774
|
tao
|
systemMetadataMap = DistributedMapsFactory.getSystemMetadataMap();
|
481 |
|
|
/*while(true) {
|
482 |
7691
|
tao
|
try {
|
483 |
|
|
systemMetadataMap = DistributedMapsFactory.getSystemMetadataMap();
|
484 |
|
|
break;
|
485 |
|
|
} catch (FileNotFoundException e) {
|
486 |
|
|
throw e;
|
487 |
|
|
} catch (ServiceFailure e) {
|
488 |
7748
|
tao
|
if(times <= maxAttempts) {
|
489 |
7691
|
tao
|
log.warn("IndexGenerator.initSystemMetadataMap - the hazelcast service is not ready : "
|
490 |
7748
|
tao
|
+e.getMessage()+"\nWe will try to access it "+waitingTime/1000+" seconds later ");
|
491 |
7691
|
tao
|
try {
|
492 |
7748
|
tao
|
Thread.sleep(waitingTime);
|
493 |
7691
|
tao
|
} catch (Exception ee) {
|
494 |
7748
|
tao
|
log.warn("IndexGenerator.initSystemMetadataMap - the thread can't sleep for "+waitingTime/1000+" seconds to wait the hazelcast service");
|
495 |
7691
|
tao
|
}
|
496 |
|
|
|
497 |
|
|
} else {
|
498 |
7748
|
tao
|
throw new ServiceFailure("0000", "IndexGenerator.initSystemMetadataMap - the hazelcast service is not ready even though Metacat-index wailted for "+maxAttempts*waitingTime/1000+" seconds. We can't get the system metadata from it and the building index can't happen this time");
|
499 |
7691
|
tao
|
}
|
500 |
|
|
}
|
501 |
|
|
times++;
|
502 |
7774
|
tao
|
}*/
|
503 |
7691
|
tao
|
}
|
504 |
|
|
}
|
505 |
|
|
|
506 |
|
|
/*
|
507 |
|
|
* We should call this method after calling initSystemMetadataMap since this method doesn't have the mechanism to wait the readiness of the hazelcast service
|
508 |
|
|
*/
|
509 |
|
|
private void initObjectPathMap() throws FileNotFoundException, ServiceFailure {
|
510 |
|
|
if(objectPathMap == null) {
|
511 |
|
|
objectPathMap = DistributedMapsFactory.getObjectPathMap();
|
512 |
|
|
}
|
513 |
|
|
}
|
514 |
|
|
/**
|
515 |
|
|
* Get an InputStream as the data object for the specific pid.
|
516 |
|
|
* @param pid
|
517 |
|
|
* @return
|
518 |
|
|
* @throws FileNotFoundException
|
519 |
|
|
*/
|
520 |
|
|
private InputStream getDataObject(String pid) throws FileNotFoundException {
|
521 |
|
|
Identifier identifier = new Identifier();
|
522 |
|
|
identifier.setValue(pid);
|
523 |
|
|
String objectPath = objectPathMap.get(identifier);
|
524 |
|
|
InputStream data = null;
|
525 |
|
|
data = new FileInputStream(objectPath);
|
526 |
|
|
return data;
|
527 |
|
|
|
528 |
|
|
}
|
529 |
|
|
|
530 |
|
|
/**
|
531 |
|
|
* Get the SystemMetadata for the specified id from the distributed Map.
|
532 |
|
|
* The null maybe is returned if there is no system metadata found.
|
533 |
|
|
* @param id the specified id.
|
534 |
|
|
* @return the SystemMetadata associated with the id.
|
535 |
|
|
*/
|
536 |
|
|
private SystemMetadata getSystemMetadata(String id) {
|
537 |
|
|
SystemMetadata metadata = null;
|
538 |
|
|
if(systemMetadataMap != null && id != null) {
|
539 |
|
|
Identifier identifier = new Identifier();
|
540 |
|
|
identifier.setValue(id);
|
541 |
|
|
metadata = systemMetadataMap.get(identifier);
|
542 |
|
|
}
|
543 |
|
|
return metadata;
|
544 |
|
|
}
|
545 |
|
|
|
546 |
|
|
/**
|
547 |
|
|
* Get the obsoletes chain of the specified id. The returned list doesn't include
|
548 |
|
|
* the specified id itself. The newer version has the lower index number in the list.
|
549 |
|
|
* Empty list will be returned if there is no document to be obsoleted by this id.
|
550 |
|
|
* @param id
|
551 |
|
|
* @return
|
552 |
|
|
*/
|
553 |
|
|
private List<String> getObsoletes(String id) {
|
554 |
|
|
List<String> obsoletes = new ArrayList<String>();
|
555 |
|
|
while (id != null) {
|
556 |
|
|
SystemMetadata metadata = getSystemMetadata(id);
|
557 |
|
|
id = null;//set it to be null in order to stop the while loop if the id can't be assinged to a new value in the following code.
|
558 |
|
|
if(metadata != null) {
|
559 |
|
|
Identifier identifier = metadata.getObsoletes();
|
560 |
|
|
if(identifier != null && identifier.getValue() != null && !identifier.getValue().trim().equals("")) {
|
561 |
|
|
obsoletes.add(identifier.getValue());
|
562 |
|
|
id = identifier.getValue();
|
563 |
|
|
}
|
564 |
|
|
}
|
565 |
|
|
}
|
566 |
|
|
return obsoletes;
|
567 |
|
|
}
|
568 |
7788
|
tao
|
|
569 |
|
|
/**
|
570 |
|
|
* Overwrite and do nothing
|
571 |
|
|
*/
|
572 |
|
|
public boolean cancel() {
|
573 |
|
|
return true;
|
574 |
|
|
}
|
575 |
7691
|
tao
|
|
576 |
7606
|
tao
|
}
|