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.v1.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
		// we do not delete system metadata
33
	}
34

    
35
	@Override
36
	public void deleteAll(Collection<Identifier> arg0) {
37
		// we do not delete system metadata	
38
	}
39

    
40
	@Override
41
	public void store(Identifier pid, SystemMetadata sm) {
42
		try {
43
			logMetacat.debug("Storing System Metadata to store: " + pid.getValue());
44
			IdentifierManager.getInstance().insertOrUpdateSystemMetadata(sm);
45
		} catch (McdbDocNotFoundException e) {
46
			throw new RuntimeException(e.getMessage(), e);
47
			
48
		} catch (SQLException e) {
49
	      throw new RuntimeException(e.getMessage(), e);
50
	      
51
    } catch (InvalidSystemMetadata e) {
52
        throw new RuntimeException(e.getMessage(), e);
53
        }
54
	}
55

    
56
	@Override
57
	public void storeAll(Map<Identifier, SystemMetadata> map) {
58
		for (Identifier key: map.keySet()) {
59
			store(key, map.get(key));
60
		}
61
	}
62

    
63
	@Override
64
	public SystemMetadata load(Identifier pid) {
65
		SystemMetadata sm = null;
66
    
67
    
68
    try {
69
			logMetacat.debug("loading from store: " + pid.getValue());
70
			sm = IdentifierManager.getInstance().getSystemMetadata(pid.getValue());
71
		} catch (McdbDocNotFoundException e) {
72
			//throw new RuntimeException(e.getMessage(), e);
73
			// not found => null
74
			return null;
75
		}
76
		catch (Exception e) {
77
			throw new RuntimeException(e.getMessage(), e);
78
		}
79
		return sm;
80
	}
81

    
82
	@Override
83
	public Map<Identifier, SystemMetadata> loadAll(Collection<Identifier> keys) {
84
		Map<Identifier, SystemMetadata> map = new HashMap<Identifier, SystemMetadata>();
85
		for (Identifier key: keys) {
86
			SystemMetadata value = load(key);
87
			map.put(key, value);
88
		}
89
		return map;
90
	}
91

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

    
110
}
(3-3/3)