Revision 6566
Added by ben leinfelder about 13 years ago
test/edu/ucsb/nceas/metacat/dataone/hazelcast/MockObjectPathMap.java | ||
---|---|---|
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 ObjectPathMap 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 can (and should) |
|
31 |
* return null in a live setting. For unit testing, it may be useful to use |
|
32 |
* it to get some |
|
33 |
* |
|
34 |
* @author rnahf |
|
35 |
* |
|
36 |
*/ |
|
37 |
public class MockObjectPathMap implements MapLoader<Identifier, String> { |
|
38 |
private static IdentifierManager im; |
|
39 |
private static String dataPath; |
|
40 |
private static String metadataPath; |
|
41 |
|
|
42 |
|
|
43 |
/** |
|
44 |
* creates an ObjectPathMap |
|
45 |
*/ |
|
46 |
public MockObjectPathMap() { |
|
47 |
// try { |
|
48 |
// PropertyService ps = PropertyService.getInstance(); |
|
49 |
// dataPath = PropertyService.getProperty("application.datafilepath"); |
|
50 |
// metadataPath = PropertyService.getProperty("application.documentfilepath"); |
|
51 |
// } catch (PropertyNotFoundException e) { |
|
52 |
// // TODO Auto-generated catch block |
|
53 |
// e.printStackTrace(); |
|
54 |
// } catch (ServiceException e) { |
|
55 |
// // TODO Auto-generated catch block |
|
56 |
// e.printStackTrace(); |
|
57 |
// } |
|
58 |
dataPath = "/data/"; |
|
59 |
metadataPath = "/metadata/"; |
|
60 |
im = IdentifierManager.getInstance(); |
|
61 |
} |
|
62 |
|
|
63 |
|
|
64 |
/* |
|
65 |
* Metadata is stored in a different place on the filesystem than |
|
66 |
* the data. The doctype value for metadata can vary, but for data |
|
67 |
* is always 'BIN', so using a simple if-then-else to separate |
|
68 |
*/ |
|
69 |
private String pathToDocid(String localid) throws McdbDocNotFoundException { |
|
70 |
|
|
71 |
return "/some/path/" + localid; |
|
72 |
// Hashtable<String, Object> ht = im.getDocumentInfo(localid); |
|
73 |
// if (ht.get("doctype").equals("BIN")) { |
|
74 |
// return dataPath + FileUtil.getFS() + localid; |
|
75 |
// } else { |
|
76 |
// return metadataPath + FileUtil.getFS() + localid; |
|
77 |
// } |
|
78 |
} |
|
79 |
|
|
80 |
|
|
81 |
/** |
|
82 |
* Implementation of hazelcast MapLoader interface method. |
|
83 |
* For the provided Identifier (as key), returns the path to the |
|
84 |
* document on the local filesystem. Returns null if it can't |
|
85 |
* create the path. |
|
86 |
*/ |
|
87 |
@Override |
|
88 |
public String load(Identifier key) |
|
89 |
{ |
|
90 |
|
|
91 |
String docid = null; |
|
92 |
String path = null; |
|
93 |
// try { |
|
94 |
// docid = im.getLocalId(key.getValue()); |
|
95 |
// path = pathToDocid(docid); |
|
96 |
// } catch (McdbDocNotFoundException e) { |
|
97 |
// // TODO Auto-generated catch block |
|
98 |
// e.printStackTrace(); |
|
99 |
// return null; |
|
100 |
// } |
|
101 |
if (key.getValue().startsWith("NO_CREATE")) { |
|
102 |
return null; |
|
103 |
} |
|
104 |
path = "/path/" + key.getValue(); |
|
105 |
return path; |
|
106 |
} |
|
107 |
|
|
108 |
|
|
109 |
/** |
|
110 |
* Implementation of hazelcast MapLoader interface method. This method loads |
|
111 |
* mappings for all Identifiers in the parameters. Any Identifier not found |
|
112 |
* is not included in the resulting map. |
|
113 |
*/ |
|
114 |
@Override |
|
115 |
public Map<Identifier, String> loadAll(Collection<Identifier> identifiers) { |
|
116 |
|
|
117 |
|
|
118 |
Hashtable<Identifier,String> map = new Hashtable<Identifier,String>(); |
|
119 |
for (Identifier id : identifiers) { |
|
120 |
map.put(id, "/path/" + id.getValue()); |
|
121 |
// try { |
|
122 |
// String docid = im.getLocalId(id.getValue()); |
|
123 |
// map.put(id, pathToDocid(docid)); |
|
124 |
// |
|
125 |
// } catch (McdbDocNotFoundException e) { |
|
126 |
// // TODO should the map load an empty path instead of |
|
127 |
// // leaving out the entire entry? |
|
128 |
// e.printStackTrace(); |
|
129 |
// } |
|
130 |
} |
|
131 |
return map; |
|
132 |
} |
|
133 |
|
|
134 |
|
|
135 |
/** |
|
136 |
* Return the full set of guids in the local metacat repository as |
|
137 |
* dataone Identifiers. |
|
138 |
* |
|
139 |
* (Hazelcast allows avoiding pre-loading by returning NULL, so consider |
|
140 |
* re-implementing if that serves the purpose of this class better) |
|
141 |
*/ |
|
142 |
@Override |
|
143 |
public Set<Identifier> loadAllKeys() |
|
144 |
{ |
|
145 |
// List<String> guids = im.getAllGUIDs(); |
|
146 |
// |
|
147 |
Set<Identifier> set = Collections.synchronizedSet(new HashSet<Identifier>()); |
|
148 |
// for (String guid : guids) { |
|
149 |
// Identifier id = new Identifier(); |
|
150 |
// id.setValue(guid); |
|
151 |
// set.add(id); |
|
152 |
// } |
|
153 |
|
|
154 |
for (int i=1; i< 100; i++) { |
|
155 |
Identifier id = new Identifier(); |
|
156 |
id.setValue("testID." + i); |
|
157 |
set.add(id); |
|
158 |
} |
|
159 |
return set; |
|
160 |
|
|
161 |
} |
|
162 |
} |
Also available in: Unified diff
removing non-junit file from the test area