Revision 6440
Added by rnahf over 12 years ago
ObjectPathMap.java | ||
---|---|---|
22 | 22 |
|
23 | 23 |
/** |
24 | 24 |
* MapLoader implementation for a hazelcast hzObjectPath. This class is called |
25 |
* when the ObjectPathMap needs to refresh against the persistent data-store.
|
|
25 |
* when the IMap get methods needs to refresh against the persistent data-store.
|
|
26 | 26 |
* The use case for this class is to communicate the filepath between JVMs on |
27 | 27 |
* the same machine, specifically between the metacat instance and the d1_indexer. |
28 | 28 |
* |
29 | 29 |
* d1_indexer will get Identifiers from elsewhere, but use this class to get |
30 |
* the paths to their associated files. The getAllKeys() method can (and should) |
|
31 |
* return null in a live setting. For unit testing, it may be useful to use |
|
32 |
* it to get some |
|
30 |
* the paths to their associated files. The getAllKeys() method will |
|
31 |
* return null in a live setting, to avoid possibly expensive preloading |
|
33 | 32 |
* |
34 | 33 |
* @author rnahf |
35 |
* |
|
36 | 34 |
*/ |
37 | 35 |
public class ObjectPathMap implements MapLoader<Identifier, String> { |
38 | 36 |
private static IdentifierManager im; |
39 | 37 |
private static String dataPath; |
40 | 38 |
private static String metadataPath; |
41 | 39 |
|
42 |
private static boolean hasDataStore; |
|
43 |
|
|
44 |
|
|
45 | 40 |
|
46 | 41 |
/** |
47 | 42 |
* creates an ObjectPathMap |
48 | 43 |
*/ |
49 | 44 |
public ObjectPathMap() { |
50 |
// try { |
|
51 |
// PropertyService ps = PropertyService.getInstance(); |
|
52 |
// dataPath = PropertyService.getProperty("application.datafilepath"); |
|
53 |
// metadataPath = PropertyService.getProperty("application.documentfilepath"); |
|
54 |
// } catch (PropertyNotFoundException e) { |
|
55 |
// // TODO Auto-generated catch block |
|
56 |
// e.printStackTrace(); |
|
57 |
// } catch (ServiceException e) { |
|
58 |
// // TODO Auto-generated catch block |
|
59 |
// e.printStackTrace(); |
|
60 |
// } |
|
61 |
dataPath = "/data/"; |
|
62 |
metadataPath = "/metadata/"; |
|
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 |
} |
|
63 | 56 |
im = IdentifierManager.getInstance(); |
64 |
String role = System.getProperties().getProperty("hazelcast.hzObjectPathRole"); |
|
65 |
if (role != null && role.equals("consumer")) { |
|
66 |
hasDataStore = false; |
|
67 |
System.out.println("new instance of ObjectPathMap: role = " + role); |
|
68 |
} else { |
|
69 |
hasDataStore = true; |
|
70 |
System.out.println("new instance of ObjectPathMap: role = " + role); |
|
71 |
} |
|
72 | 57 |
} |
73 | 58 |
|
74 | 59 |
|
... | ... | |
77 | 62 |
* the data. The doctype value for metadata can vary, but for data |
78 | 63 |
* is always 'BIN', so using a simple if-then-else to separate |
79 | 64 |
*/ |
80 |
private String pathToDocid(String localid) throws McdbDocNotFoundException { |
|
81 |
|
|
82 |
return "/some/path/" + localid; |
|
83 |
// Hashtable<String, Object> ht = im.getDocumentInfo(localid); |
|
84 |
// if (ht.get("doctype").equals("BIN")) { |
|
85 |
// return dataPath + FileUtil.getFS() + localid; |
|
86 |
// } else { |
|
87 |
// return metadataPath + FileUtil.getFS() + localid; |
|
88 |
// } |
|
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 |
} |
|
89 | 73 |
} |
90 | 74 |
|
91 | 75 |
|
... | ... | |
98 | 82 |
@Override |
99 | 83 |
public String load(Identifier key) |
100 | 84 |
{ |
101 |
if (hasDataStore) { |
|
102 |
String docid = null; |
|
103 |
String path = null; |
|
104 |
// try { |
|
105 |
// docid = im.getLocalId(key.getValue()); |
|
106 |
// path = pathToDocid(docid); |
|
107 |
// } catch (McdbDocNotFoundException e) { |
|
108 |
// // TODO Auto-generated catch block |
|
109 |
// e.printStackTrace(); |
|
110 |
// return null; |
|
111 |
// } |
|
112 |
path = "/path/" + key.getValue(); |
|
113 |
return path; |
|
114 |
} else { |
|
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(); |
|
115 | 94 |
return null; |
116 | 95 |
} |
96 |
return path; |
|
117 | 97 |
} |
118 | 98 |
|
119 | 99 |
|
... | ... | |
125 | 105 |
@Override |
126 | 106 |
public Map<Identifier, String> loadAll(Collection<Identifier> identifiers) { |
127 | 107 |
|
128 |
if (hasDataStore) { |
|
129 |
Hashtable<Identifier,String> map = new Hashtable<Identifier,String>(); |
|
130 |
for (Identifier id : identifiers) { |
|
131 |
map.put(id, "/path/" + id.getValue()); |
|
132 |
// try { |
|
133 |
// String docid = im.getLocalId(id.getValue()); |
|
134 |
// map.put(id, pathToDocid(docid)); |
|
135 |
// |
|
136 |
// } catch (McdbDocNotFoundException e) { |
|
137 |
// // TODO should the map load an empty path instead of |
|
138 |
// // leaving out the entire entry? |
|
139 |
// e.printStackTrace(); |
|
140 |
// } |
|
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(); |
|
141 | 119 |
} |
142 |
return map; |
|
143 |
} else { |
|
144 |
return null; |
|
145 | 120 |
} |
121 |
return map; |
|
146 | 122 |
} |
147 | 123 |
|
148 | 124 |
|
... | ... | |
150 | 126 |
* Return the full set of guids in the local metacat repository as |
151 | 127 |
* dataone Identifiers. |
152 | 128 |
* |
153 |
* (Hazelcast allows avoiding pre-loading by returning NULL, so consider
|
|
154 |
* re-implementing if that serves the purpose of this class better)
|
|
129 |
* (Hazelcast allows avoiding pre-loading by returning NULL, so will
|
|
130 |
* do this to avoid pre-loading a very long list unnecessarily)
|
|
155 | 131 |
*/ |
156 | 132 |
@Override |
157 |
public Set<Identifier> loadAllKeys() { |
|
158 |
|
|
159 |
if (hasDataStore) { |
|
160 |
// List<String> guids = im.getAllGUIDs(); |
|
161 |
// |
|
162 |
Set<Identifier> set = Collections.synchronizedSet(new HashSet<Identifier>()); |
|
163 |
// for (String guid : guids) { |
|
164 |
// Identifier id = new Identifier(); |
|
165 |
// id.setValue(guid); |
|
166 |
// set.add(id); |
|
167 |
// } |
|
168 |
|
|
169 |
for (int i=1; i< 100; i++) { |
|
170 |
Identifier id = new Identifier(); |
|
171 |
id.setValue("testID." + i); |
|
172 |
set.add(id); |
|
173 |
} |
|
174 |
return set; |
|
175 |
} else { |
|
176 |
return null; |
|
177 |
} |
|
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; |
|
178 | 146 |
} |
179 | 147 |
} |
Also available in: Unified diff
cleaned up mock tests on hzObjectPathMap. split out code for mocking a datastore into MockObjectPathMap.