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