Project

General

Profile

1
package edu.ucsb.nceas.metacat.dataone.hazelcast;
2

    
3
import java.util.Collection;
4
import java.util.Collections;
5
import java.util.HashSet;
6
import java.util.Hashtable;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Set;
10

    
11
import org.dataone.service.types.v1.Identifier;
12

    
13
import com.hazelcast.core.MapLoader;
14

    
15
import edu.ucsb.nceas.metacat.IdentifierManager;
16
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
17
import edu.ucsb.nceas.metacat.properties.PropertyService;
18
import edu.ucsb.nceas.utilities.FileUtil;
19
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
20

    
21

    
22
/**
23
 * MapLoader implementation for a hazelcast ObjectPath.  This class is called
24
 * when the ObjectPathMap needs to refresh against the persistent data-store.
25
 * 
26
 * @author rnahf
27
 *
28
 */
29
public class ObjectPathMapLoader implements MapLoader<Identifier, String> {
30
	private static IdentifierManager im;
31
	private static String dataPath;
32
	private static String metadataPath;
33

    
34

    
35
	
36
	/**
37
	 * creates an ObjectPathMapLoader
38
	 */
39
	public ObjectPathMapLoader() {
40
		try {
41
			dataPath = PropertyService.getProperty("application.datafilepath");
42
			metadataPath = PropertyService.getProperty("application.documentfilepath");
43
		} catch (PropertyNotFoundException e) {
44
			// TODO Auto-generated catch block
45
			e.printStackTrace();
46
		}
47
		im = IdentifierManager.getInstance();
48
	}
49

    
50
	
51
	/*
52
	 * Metadata is stored in a different place on the filesystem than
53
	 * the data.  The doctype value for metadata can vary, but for data
54
	 * is always 'BIN', so using a simple if-then-else to separate
55
	 */
56
	private String pathToDocid(String localid) throws McdbDocNotFoundException  {
57
		
58
		Hashtable<String, Object> ht = im.getDocumentInfo(localid);
59
		if (ht.get("doctype").equals("BIN")) {
60
			return dataPath + FileUtil.getFS() + localid;
61
		} else {
62
			return metadataPath + FileUtil.getFS() + localid;
63
		}		
64
	}
65

    
66
	
67
	/**
68
	 *  Implementation of hazelcast MapLoader interface method.
69
	 *  For the provided Identifier (as key), returns the path to the
70
	 *  document on the local filesystem.  Returns null if it can't 
71
	 *  create the path. 
72
	 */
73
	@Override
74
	public String load(Identifier key) 
75
	{
76
		String docid = null;
77
		String path = null;
78
		try {
79
			docid = im.getLocalId(key.getValue());
80
			path = pathToDocid(docid);
81
		} catch (McdbDocNotFoundException e) {
82
			// TODO Auto-generated catch block
83
			e.printStackTrace();
84
			return null;
85
		}
86
		return path;
87
	}
88
	
89
	
90
	/**
91
	 *  Implementation of hazelcast MapLoader interface method.  This method loads
92
	 *  mappings for all Identifiers in the parameters.  Any Identifier not found
93
	 *  is not included in the resulting map.
94
	 */
95
	@Override
96
	public Map<Identifier, String> loadAll(Collection<Identifier> identifiers) {
97
		
98
		Hashtable<Identifier,String> map = new Hashtable<Identifier,String>();
99
		for (Identifier id : identifiers) {
100
			try {
101
				String docid = im.getLocalId(id.getValue());
102
				map.put(id, pathToDocid(docid));
103
				
104
			} catch (McdbDocNotFoundException e) {
105
				// TODO should the map load an empty path instead of
106
				// leaving out the entire entry?
107
				e.printStackTrace();
108
			}
109
		}
110
		return map;
111
	}
112

    
113
	
114
	/**
115
	 * Return the full set of guids in the local metacat repository as
116
	 * dataone Identifiers.
117
	 * 
118
	 * (Hazelcast allows avoiding pre-loading by returning NULL, so consider
119
	 * re-implementing if that serves the purpose of this class better)
120
	 */
121
	@Override
122
	public Set<Identifier> loadAllKeys() {
123
		
124
		List<String> guids = im.getAllGUIDs();
125
		
126
		Set<Identifier> set = Collections.synchronizedSet(new HashSet<Identifier>());
127
		for (String guid : guids) {
128
			Identifier id = new Identifier();
129
			id.setValue(guid);
130
			set.add(id);
131
		}
132
		return set;
133
	}
134
}
(2-2/3)