1
|
package edu.ucsb.nceas.metacat.dataone.hazelcast;
|
2
|
|
3
|
import java.sql.SQLException;
|
4
|
import java.util.Collection;
|
5
|
import java.util.HashMap;
|
6
|
import java.util.Map;
|
7
|
import java.util.Set;
|
8
|
|
9
|
import org.apache.log4j.Logger;
|
10
|
import org.dataone.service.types.v1.Identifier;
|
11
|
import org.dataone.service.types.v1.ObjectInfo;
|
12
|
import org.dataone.service.types.v1.ObjectList;
|
13
|
import org.dataone.service.types.v1.SystemMetadata;
|
14
|
|
15
|
import com.hazelcast.core.Hazelcast;
|
16
|
import com.hazelcast.core.MapLoader;
|
17
|
import com.hazelcast.core.MapStore;
|
18
|
|
19
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
20
|
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
|
21
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
22
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
23
|
|
24
|
/**
|
25
|
* Storage implementation for Hazelcast System Metadata
|
26
|
* @author leinfelder
|
27
|
*
|
28
|
*/
|
29
|
public class SystemMetadataMap
|
30
|
implements MapStore<Identifier, SystemMetadata>, MapLoader<Identifier, SystemMetadata> {
|
31
|
|
32
|
private Logger logMetacat = null;
|
33
|
|
34
|
@Override
|
35
|
public void delete(Identifier arg0) {
|
36
|
// we do not delete system metadata
|
37
|
}
|
38
|
|
39
|
@Override
|
40
|
public void deleteAll(Collection<Identifier> arg0) {
|
41
|
// we do not delete system metadata
|
42
|
}
|
43
|
|
44
|
@Override
|
45
|
public void store(Identifier pid, SystemMetadata sm) {
|
46
|
try {
|
47
|
//System.out.println("Storing System Metadata to store: " + pid.getValue());
|
48
|
if (!IdentifierManager.getInstance().systemMetadataExists(pid.getValue())) {
|
49
|
IdentifierManager.getInstance().createSystemMetadata(sm);
|
50
|
|
51
|
} else {
|
52
|
IdentifierManager.getInstance().updateSystemMetadata(sm);
|
53
|
|
54
|
}
|
55
|
|
56
|
} catch (McdbDocNotFoundException e) {
|
57
|
throw new RuntimeException(e.getMessage(), e);
|
58
|
|
59
|
} catch (SQLException e) {
|
60
|
throw new RuntimeException(e.getMessage(), e);
|
61
|
|
62
|
}
|
63
|
}
|
64
|
|
65
|
@Override
|
66
|
public void storeAll(Map<Identifier, SystemMetadata> map) {
|
67
|
for (Identifier key: map.keySet()) {
|
68
|
store(key, map.get(key));
|
69
|
}
|
70
|
}
|
71
|
|
72
|
@Override
|
73
|
public SystemMetadata load(Identifier pid) {
|
74
|
SystemMetadata sm = null;
|
75
|
logMetacat = Logger.getLogger(SystemMetadataMap.class);
|
76
|
|
77
|
try {
|
78
|
logMetacat.debug("loading from store: " + pid.getValue());
|
79
|
sm = IdentifierManager.getInstance().getSystemMetadata(pid.getValue());
|
80
|
} catch (McdbDocNotFoundException e) {
|
81
|
//throw new RuntimeException(e.getMessage(), e);
|
82
|
// not found => null
|
83
|
return null;
|
84
|
}
|
85
|
catch (Exception e) {
|
86
|
throw new RuntimeException(e.getMessage(), e);
|
87
|
}
|
88
|
return sm;
|
89
|
}
|
90
|
|
91
|
@Override
|
92
|
public Map<Identifier, SystemMetadata> loadAll(Collection<Identifier> keys) {
|
93
|
Map<Identifier, SystemMetadata> map = new HashMap<Identifier, SystemMetadata>();
|
94
|
for (Identifier key: keys) {
|
95
|
SystemMetadata value = load(key);
|
96
|
map.put(key, value);
|
97
|
}
|
98
|
return map;
|
99
|
}
|
100
|
|
101
|
@Override
|
102
|
public Set<Identifier> loadAllKeys() {
|
103
|
|
104
|
//Set<Identifier> pids = new HashSet<Identifier>();
|
105
|
String identifiersSet;
|
106
|
try {
|
107
|
identifiersSet =
|
108
|
PropertyService.getProperty("dataone.hazelcast.storageCluster.identifiersSet");
|
109
|
|
110
|
} catch (PropertyNotFoundException e1) {
|
111
|
identifiersSet = "hzIdentifiers";
|
112
|
|
113
|
}
|
114
|
Set<Identifier> pids = Hazelcast.getSet(identifiersSet);
|
115
|
|
116
|
try {
|
117
|
ObjectList ol = IdentifierManager.getInstance().querySystemMetadata(
|
118
|
null, //startTime,
|
119
|
null, //endTime,
|
120
|
null, //objectFormatId,
|
121
|
false, //replicaStatus,
|
122
|
0, //start,
|
123
|
-1 //count
|
124
|
);
|
125
|
for (ObjectInfo o: ol.getObjectInfoList()) {
|
126
|
Identifier pid = o.getIdentifier();
|
127
|
if ( !pids.contains(pid) ) {
|
128
|
pids.add(pid);
|
129
|
|
130
|
}
|
131
|
}
|
132
|
|
133
|
} catch (Exception e) {
|
134
|
throw new RuntimeException(e.getMessage(), e);
|
135
|
|
136
|
}
|
137
|
|
138
|
return pids;
|
139
|
}
|
140
|
|
141
|
}
|