Project

General

Profile

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.List;
7
import java.util.Map;
8
import java.util.Set;
9

    
10
import org.apache.log4j.Logger;
11
import org.dataone.service.exceptions.InvalidSystemMetadata;
12
import org.dataone.service.types.v1.Identifier;
13
import org.dataone.service.types.v1.ObjectInfo;
14
import org.dataone.service.types.v1.ObjectList;
15
import org.dataone.service.types.v1.SystemMetadata;
16

    
17
import com.hazelcast.core.Hazelcast;
18
import com.hazelcast.core.MapLoader;
19
import com.hazelcast.core.MapStore;
20

    
21
import edu.ucsb.nceas.metacat.IdentifierManager;
22
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
23
import edu.ucsb.nceas.metacat.properties.PropertyService;
24
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
25

    
26
/**
27
 * Storage implementation for Hazelcast System Metadata
28
 * @author leinfelder
29
 *
30
 */
31
public class SystemMetadataMap 
32
    implements MapStore<Identifier, SystemMetadata>, MapLoader<Identifier, SystemMetadata> {
33

    
34
  private Logger logMetacat = null;
35

    
36
	@Override
37
	public void delete(Identifier arg0) {
38
		// we do not delete system metadata
39
	}
40

    
41
	@Override
42
	public void deleteAll(Collection<Identifier> arg0) {
43
		// we do not delete system metadata	
44
	}
45

    
46
	@Override
47
	public void store(Identifier pid, SystemMetadata sm) {
48
		try {
49
			logMetacat.debug("Storing System Metadata to store: " + pid.getValue());
50
			if (!IdentifierManager.getInstance().systemMetadataExists(pid.getValue())) {
51
				IdentifierManager.getInstance().insertSystemMetadata(sm);
52
				
53
			} else {
54
				IdentifierManager.getInstance().updateSystemMetadata(sm);
55
				
56
			}
57
			
58
		} catch (McdbDocNotFoundException e) {
59
			throw new RuntimeException(e.getMessage(), e);
60
			
61
		} catch (SQLException e) {
62
	      throw new RuntimeException(e.getMessage(), e);
63
	      
64
    } catch (InvalidSystemMetadata e) {
65
        throw new RuntimeException(e.getMessage(), e);
66
        }
67
	}
68

    
69
	@Override
70
	public void storeAll(Map<Identifier, SystemMetadata> map) {
71
		for (Identifier key: map.keySet()) {
72
			store(key, map.get(key));
73
		}
74
	}
75

    
76
	@Override
77
	public SystemMetadata load(Identifier pid) {
78
		SystemMetadata sm = null;
79
    logMetacat = Logger.getLogger(SystemMetadataMap.class);
80
    
81
    try {
82
			logMetacat.debug("loading from store: " + pid.getValue());
83
			sm = IdentifierManager.getInstance().getSystemMetadata(pid.getValue());
84
		} catch (McdbDocNotFoundException e) {
85
			//throw new RuntimeException(e.getMessage(), e);
86
			// not found => null
87
			return null;
88
		}
89
		catch (Exception e) {
90
			throw new RuntimeException(e.getMessage(), e);
91
		}
92
		return sm;
93
	}
94

    
95
	@Override
96
	public Map<Identifier, SystemMetadata> loadAll(Collection<Identifier> keys) {
97
		Map<Identifier, SystemMetadata> map = new HashMap<Identifier, SystemMetadata>();
98
		for (Identifier key: keys) {
99
			SystemMetadata value = load(key);
100
			map.put(key, value);
101
		}
102
		return map;
103
	}
104

    
105
	@Override
106
	public Set<Identifier> loadAllKeys() {
107
	  
108
		String identifiersSet;
109
		try {
110
			identifiersSet = 
111
				PropertyService.getProperty("dataone.hazelcast.storageCluster.identifiersSet");
112
		} catch (PropertyNotFoundException e1) {
113
		    identifiersSet = "hzIdentifiers";
114
		}
115
		
116
		Set<Identifier> pids = Hazelcast.getSet(identifiersSet);
117
		
118
		try {
119
			
120
			// this has more overhead than just looking at the GUIDs
121
			ObjectList ol = IdentifierManager.getInstance().querySystemMetadata(
122
					null, //startTime, 
123
					null, //endTime, 
124
					null, //objectFormatId, 
125
					false, //replicaStatus, 
126
					0, //start, 
127
					-1 //count
128
					);
129
			for (ObjectInfo o: ol.getObjectInfoList()) {
130
				Identifier pid = o.getIdentifier();
131
				if ( !pids.contains(pid) ) {
132
					pids.add(pid);
133
				}				
134
			}
135
			
136
			// ALTERNATIVE method: look up all the Identifiers from the table
137
//			List<String> guids = IdentifierManager.getInstance().getAllGUIDs();
138
//			for (String guid: guids){
139
//				Identifier pid = new Identifier();
140
//				pid.setValue(guid);
141
//				pids.add(pid);
142
//			}
143
			
144
		} catch (Exception e) {
145
			throw new RuntimeException(e.getMessage(), e);
146
			
147
		}
148
		
149
		return pids;
150
	}
151

    
152
}
(3-3/3)