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.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.v2.SystemMetadata;
13

    
14
import com.hazelcast.core.MapLoader;
15
import com.hazelcast.core.MapStore;
16

    
17
import edu.ucsb.nceas.metacat.IdentifierManager;
18
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
19

    
20
/**
21
 * Storage implementation for Hazelcast System Metadata
22
 * @author leinfelder
23
 *
24
 */
25
public class SystemMetadataMap 
26
    implements MapStore<Identifier, SystemMetadata>, MapLoader<Identifier, SystemMetadata> {
27

    
28
  private Logger logMetacat = Logger.getLogger(SystemMetadataMap.class);
29

    
30
	@Override
31
	public void delete(Identifier arg0) {
32
		if(arg0!= null) {
33
			logMetacat.debug("delete the identifier"+arg0.getValue());
34
			boolean success = IdentifierManager.getInstance().deleteSystemMetadata(arg0.getValue());
35
			if(!success) {
36
				throw new RuntimeException("SystemMetadataMap.delete - the system metadata of guid - "+arg0.getValue()+" can't be removed successfully.");
37
			}
38
		}
39
		
40
	}
41

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

    
47
	@Override
48
	public void store(Identifier pid, SystemMetadata sm) {
49
		try {
50
			logMetacat.debug("Storing System Metadata to store: " + pid.getValue());
51
			IdentifierManager.getInstance().insertOrUpdateSystemMetadata(sm);
52
		} catch (McdbDocNotFoundException e) {
53
			throw new RuntimeException(e.getMessage(), e);
54
			
55
		} catch (SQLException e) {
56
	      throw new RuntimeException(e.getMessage(), e);
57
	      
58
    } catch (InvalidSystemMetadata e) {
59
        throw new RuntimeException(e.getMessage(), e);
60
        }
61
	}
62

    
63
	@Override
64
	public void storeAll(Map<Identifier, SystemMetadata> map) {
65
		for (Identifier key: map.keySet()) {
66
			store(key, map.get(key));
67
		}
68
	}
69

    
70
	@Override
71
	public SystemMetadata load(Identifier pid) {
72
		SystemMetadata sm = null;
73
    
74
    
75
    try {
76
			logMetacat.debug("loading from store: " + pid.getValue());
77
			sm = IdentifierManager.getInstance().getSystemMetadata(pid.getValue());
78
		} catch (McdbDocNotFoundException e) {
79
			//throw new RuntimeException(e.getMessage(), e);
80
			// not found => null
81
			logMetacat.warn("could not load system metadata for: " +  pid.getValue(), e);
82
			return null;
83
		}
84
		catch (Exception e) {
85
			throw new RuntimeException(e.getMessage(), e);
86
		}
87
		return sm;
88
	}
89

    
90
	@Override
91
	public Map<Identifier, SystemMetadata> loadAll(Collection<Identifier> keys) {
92
		Map<Identifier, SystemMetadata> map = new HashMap<Identifier, SystemMetadata>();
93
		for (Identifier key: keys) {
94
			SystemMetadata value = load(key);
95
			map.put(key, value);
96
		}
97
		return map;
98
	}
99

    
100
	/**
101
	 * Returning null so that no entries are loaded on map initialization
102
	 * 
103
	 * @see http://www.hazelcast.com/docs/1.9.4/manual/single_html/#Map
104
	 * 
105
	 * As of 1.9.3 MapLoader has the new MapLoader.loadAllKeys API. 
106
	 * It is used for pre-populating the in-memory map when the map is first touched/used. 
107
	 * If MapLoader.loadAllKeys returns NULL then nothing will be loaded. 
108
	 * Your MapLoader.loadAllKeys implementation can return all or some of the keys. 
109
	 * You may select and return only the hot keys, for instance. 
110
	 * Also note that this is the fastest way of pre-populating the map as 
111
	 * Hazelcast will optimize the loading process by having each node loading owned portion of the entries.
112
	 */
113
	@Override
114
	public Set<Identifier> loadAllKeys() {
115
		return null;
116
	}
117

    
118
}
(3-3/3)