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 = null;
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
			if (!IdentifierManager.getInstance().systemMetadataExists(pid.getValue())) {
45
				IdentifierManager.getInstance().insertSystemMetadata(sm);
46
				
47
			} else {
48
				IdentifierManager.getInstance().updateSystemMetadata(sm);
49
				
50
			}
51
			
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
    logMetacat = Logger.getLogger(SystemMetadataMap.class);
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
			return null;
82
		}
83
		catch (Exception e) {
84
			throw new RuntimeException(e.getMessage(), e);
85
		}
86
		return sm;
87
	}
88

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

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

    
117
}
(3-3/3)