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