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