Revision 6414
Added by rnahf over 13 years ago
src/edu/ucsb/nceas/metacat/dataone/hazelcast/ObjectPathMapLoader.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.utilities.FileUtil; |
|
19 |
import edu.ucsb.nceas.utilities.PropertyNotFoundException; |
|
20 |
|
|
21 |
|
|
22 |
/** |
|
23 |
* MapLoader implementation for a hazelcast ObjectPath. This class is called |
|
24 |
* when the ObjectPathMap needs to refresh against the persistent data-store. |
|
25 |
* |
|
26 |
* @author rnahf |
|
27 |
* |
|
28 |
*/ |
|
29 |
public class ObjectPathMapLoader implements MapLoader<Identifier, String> { |
|
30 |
private static IdentifierManager im; |
|
31 |
private static String dataPath; |
|
32 |
private static String metadataPath; |
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
/** |
|
37 |
* creates an ObjectPathMapLoader |
|
38 |
*/ |
|
39 |
public ObjectPathMapLoader() { |
|
40 |
try { |
|
41 |
dataPath = PropertyService.getProperty("application.datafilepath"); |
|
42 |
metadataPath = PropertyService.getProperty("application.documentfilepath"); |
|
43 |
} catch (PropertyNotFoundException e) { |
|
44 |
// TODO Auto-generated catch block |
|
45 |
e.printStackTrace(); |
|
46 |
} |
|
47 |
im = IdentifierManager.getInstance(); |
|
48 |
} |
|
49 |
|
|
50 |
|
|
51 |
/* |
|
52 |
* Metadata is stored in a different place on the filesystem than |
|
53 |
* the data. The doctype value for metadata can vary, but for data |
|
54 |
* is always 'BIN', so using a simple if-then-else to separate |
|
55 |
*/ |
|
56 |
private String pathToDocid(String docid, String doctype) { |
|
57 |
|
|
58 |
if (doctype.equals("BIN")) { |
|
59 |
return dataPath + FileUtil.getFS() + docid; |
|
60 |
} else { |
|
61 |
return metadataPath + FileUtil.getFS() + docid; |
|
62 |
} |
|
63 |
} |
|
64 |
|
|
65 |
|
|
66 |
/** |
|
67 |
* Implementation of hazelcast MapLoader interface method. |
|
68 |
*/ |
|
69 |
|
|
70 |
public String load(Identifier key) { |
|
71 |
|
|
72 |
String docid = null; |
|
73 |
Hashtable<String,Object> docinfo = null; |
|
74 |
try { |
|
75 |
docid = im.getLocalId(key.getValue()); |
|
76 |
docinfo = im.getDocumentInfo(docid); |
|
77 |
|
|
78 |
} catch (McdbDocNotFoundException e) { |
|
79 |
// TODO Auto-generated catch block |
|
80 |
e.printStackTrace(); |
|
81 |
return null; |
|
82 |
} |
|
83 |
return pathToDocid(docid,(String)docinfo.get("doctype")); |
|
84 |
} |
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
/** |
|
91 |
* Implementation of hazelcast MapLoader interface method. |
|
92 |
*/ |
|
93 |
@Override |
|
94 |
public Map<Identifier, String> loadAll(Collection<Identifier> identifiers) { |
|
95 |
|
|
96 |
Hashtable<Identifier,String> map = new Hashtable<Identifier,String>(); |
|
97 |
for (Identifier id : identifiers) { |
|
98 |
try { |
|
99 |
String docid = im.getLocalId(id.getValue()); |
|
100 |
Hashtable<String,Object> docinfo = im.getDocumentInfo(docid); |
|
101 |
map.put(id, pathToDocid(docid, (String)docinfo.get("doctype"))); |
|
102 |
|
|
103 |
} catch (McdbDocNotFoundException e) { |
|
104 |
// TODO Auto-generated catch block |
|
105 |
e.printStackTrace(); |
|
106 |
} |
|
107 |
} |
|
108 |
return map; |
|
109 |
} |
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
@Override |
|
114 |
public Set<Identifier> loadAllKeys() { |
|
115 |
Set<Identifier> set = Collections.synchronizedSet(new HashSet<Identifier>()); |
|
116 |
|
|
117 |
List<String> docids = null; |
|
118 |
try { |
|
119 |
docids = im.getAllLocalIds(); |
|
120 |
for(String docid : docids) { |
|
121 |
Identifier id = new Identifier(); |
|
122 |
id.setValue(im.getGUID(docid, im.getLatestRevForLocalId(docid))); |
|
123 |
set.add(id); |
|
124 |
} |
|
125 |
} catch (Exception e) { |
|
126 |
// TODO Auto-generated catch block |
|
127 |
e.printStackTrace(); |
|
128 |
} |
|
129 |
return set; |
|
130 |
} |
|
131 |
} |
Also available in: Unified diff
new class for refreshing the hazelcast map with metacat. Initial commit.