1
|
package edu.ucsb.nceas.metacat.dataone.hazelcast;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.Collection;
|
5
|
import java.util.Collections;
|
6
|
import java.util.HashSet;
|
7
|
import java.util.Hashtable;
|
8
|
import java.util.List;
|
9
|
import java.util.Map;
|
10
|
import java.util.Set;
|
11
|
|
12
|
import org.dataone.service.types.v1.Identifier;
|
13
|
import org.jibx.runtime.JiBXException;
|
14
|
import org.xml.sax.SAXException;
|
15
|
|
16
|
import com.hazelcast.core.MapLoader;
|
17
|
|
18
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
19
|
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
|
20
|
import edu.ucsb.nceas.metacat.McdbException;
|
21
|
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlException;
|
22
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
23
|
import edu.ucsb.nceas.metacat.replication.ReplicationService;
|
24
|
import edu.ucsb.nceas.metacat.shared.HandlerException;
|
25
|
import edu.ucsb.nceas.metacat.shared.ServiceException;
|
26
|
import edu.ucsb.nceas.utilities.FileUtil;
|
27
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
28
|
|
29
|
|
30
|
/**
|
31
|
* MapLoader implementation for a hazelcast hzObjectPath. This class is called
|
32
|
* when the IMap get methods needs to refresh against the persistent data-store.
|
33
|
* The use case for this class is to communicate the filepath between JVMs on
|
34
|
* the same machine, specifically between the metacat instance and the d1_indexer.
|
35
|
*
|
36
|
* d1_indexer will get Identifiers from elsewhere, but use this class to get
|
37
|
* the paths to their associated files. The getAllKeys() method will
|
38
|
* return null in a live setting, to avoid possibly expensive preloading
|
39
|
*
|
40
|
* @author rnahf
|
41
|
*/
|
42
|
public class ObjectPathMap implements MapLoader<Identifier, String> {
|
43
|
private static IdentifierManager im;
|
44
|
private static String dataPath;
|
45
|
private static String metadataPath;
|
46
|
|
47
|
|
48
|
/**
|
49
|
* creates an ObjectPathMap
|
50
|
*/
|
51
|
public ObjectPathMap() {
|
52
|
try {
|
53
|
PropertyService ps = PropertyService.getInstance();
|
54
|
dataPath = PropertyService.getProperty("application.datafilepath");
|
55
|
metadataPath = PropertyService.getProperty("application.documentfilepath");
|
56
|
} catch (PropertyNotFoundException e) {
|
57
|
// TODO Auto-generated catch block
|
58
|
e.printStackTrace();
|
59
|
} catch (ServiceException e) {
|
60
|
// TODO Auto-generated catch block
|
61
|
e.printStackTrace();
|
62
|
}
|
63
|
im = IdentifierManager.getInstance();
|
64
|
}
|
65
|
|
66
|
|
67
|
/*
|
68
|
* Metadata is stored in a different place on the filesystem than
|
69
|
* the data. The doctype value for metadata can vary, but for data
|
70
|
* is always 'BIN', so using a simple if-then-else to separate
|
71
|
*/
|
72
|
private String pathToDocid(String localid) throws AccessControlException, HandlerException, JiBXException, IOException, McdbException, SAXException
|
73
|
{
|
74
|
Hashtable<String, String> ht = ReplicationService.getDocumentInfoMap(localid);
|
75
|
if (ht.get("doctype").equals("BIN")) {
|
76
|
return dataPath + FileUtil.getFS() + localid;
|
77
|
} else {
|
78
|
return metadataPath + FileUtil.getFS() + localid;
|
79
|
}
|
80
|
}
|
81
|
|
82
|
|
83
|
/**
|
84
|
* Implementation of hazelcast MapLoader interface method.
|
85
|
* For the provided Identifier (as key), returns the path to the
|
86
|
* document on the local filesystem. Returns null if it can't
|
87
|
* create the path.
|
88
|
*/
|
89
|
@Override
|
90
|
public String load(Identifier key)
|
91
|
{
|
92
|
|
93
|
String docid = null;
|
94
|
String path = null;
|
95
|
try {
|
96
|
docid = im.getLocalId(key.getValue());
|
97
|
path = pathToDocid(docid);
|
98
|
} catch (Exception e) {
|
99
|
// TODO Auto-generated catch block
|
100
|
e.printStackTrace();
|
101
|
return null;
|
102
|
}
|
103
|
return path;
|
104
|
}
|
105
|
|
106
|
|
107
|
/**
|
108
|
* Implementation of hazelcast MapLoader interface method. This method loads
|
109
|
* mappings for all Identifiers in the parameters. Any Identifier not found
|
110
|
* is not included in the resulting map.
|
111
|
*/
|
112
|
@Override
|
113
|
public Map<Identifier, String> loadAll(Collection<Identifier> identifiers) {
|
114
|
|
115
|
|
116
|
Hashtable<Identifier,String> map = new Hashtable<Identifier,String>();
|
117
|
for (Identifier id : identifiers) {
|
118
|
try {
|
119
|
String docid = im.getLocalId(id.getValue());
|
120
|
map.put(id, pathToDocid(docid));
|
121
|
|
122
|
} catch (Exception e) {
|
123
|
// TODO should the map load an empty path instead of
|
124
|
// leaving out the entire entry?
|
125
|
e.printStackTrace();
|
126
|
}
|
127
|
}
|
128
|
return map;
|
129
|
}
|
130
|
|
131
|
|
132
|
/**
|
133
|
* Return the full set of guids in the local metacat repository as
|
134
|
* dataone Identifiers.
|
135
|
*
|
136
|
* (Hazelcast allows avoiding pre-loading by returning NULL, so will
|
137
|
* do this to avoid pre-loading a very long list unnecessarily)
|
138
|
*/
|
139
|
@Override
|
140
|
public Set<Identifier> loadAllKeys()
|
141
|
{
|
142
|
return null;
|
143
|
|
144
|
// List<String> guids = im.getAllGUIDs();
|
145
|
//
|
146
|
// Set<Identifier> set = Collections.synchronizedSet(new HashSet<Identifier>());
|
147
|
// for (String guid : guids) {
|
148
|
// Identifier id = new Identifier();
|
149
|
// id.setValue(guid);
|
150
|
// set.add(id);
|
151
|
// }
|
152
|
// return set;
|
153
|
}
|
154
|
}
|