Project

General

Profile

1 6414 rnahf
package edu.ucsb.nceas.metacat.dataone.hazelcast;
2
3
import java.util.Collection;
4 6471 jones
import java.util.Collections;
5
import java.util.HashSet;
6 6414 rnahf
import java.util.Hashtable;
7 6471 jones
import java.util.List;
8 6414 rnahf
import java.util.Map;
9
import java.util.Set;
10
11
import com.hazelcast.core.MapLoader;
12
13
import edu.ucsb.nceas.metacat.IdentifierManager;
14
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
15
import edu.ucsb.nceas.metacat.properties.PropertyService;
16 6420 rnahf
import edu.ucsb.nceas.metacat.shared.ServiceException;
17 6414 rnahf
import edu.ucsb.nceas.utilities.FileUtil;
18
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
19
20
21
/**
22 6420 rnahf
 * MapLoader implementation for a hazelcast hzObjectPath.  This class is called
23 6440 rnahf
 * when the IMap get methods needs to refresh against the persistent data-store.
24 6420 rnahf
 * The use case for this class is to communicate the filepath between JVMs on
25
 * the same machine, specifically between the metacat instance and the d1_indexer.
26 6414 rnahf
 *
27 6420 rnahf
 * d1_indexer will get Identifiers from elsewhere, but use this class to get
28 6440 rnahf
 * the paths to their associated files.  The getAllKeys() method will
29
 * return null in a live setting, to avoid possibly expensive preloading
30 6420 rnahf
 *
31 6414 rnahf
 * @author rnahf
32
 */
33 6458 rnahf
public class ObjectPathMap implements MapLoader<String, String> {
34 6414 rnahf
	private static IdentifierManager im;
35
	private static String dataPath;
36
	private static String metadataPath;
37 6424 rnahf
38 6414 rnahf
39
	/**
40 6424 rnahf
	 * creates an ObjectPathMap
41 6414 rnahf
	 */
42 6420 rnahf
	public ObjectPathMap() {
43 6440 rnahf
		try {
44
			PropertyService ps = PropertyService.getInstance();
45
			dataPath = PropertyService.getProperty("application.datafilepath");
46
			metadataPath = PropertyService.getProperty("application.documentfilepath");
47
		} catch (PropertyNotFoundException e) {
48
			// TODO Auto-generated catch block
49
			e.printStackTrace();
50
		} catch (ServiceException e) {
51
			// TODO Auto-generated catch block
52
			e.printStackTrace();
53
		}
54 6414 rnahf
		im = IdentifierManager.getInstance();
55
	}
56
57
58
	/*
59
	 * Metadata is stored in a different place on the filesystem than
60
	 * the data.  The doctype value for metadata can vary, but for data
61
	 * is always 'BIN', so using a simple if-then-else to separate
62
	 */
63 6440 rnahf
	private String pathToDocid(String localid) throws McdbDocNotFoundException
64
	{
65
		Hashtable<String, Object> ht = im.getDocumentInfo(localid);
66
		if (ht.get("doctype").equals("BIN")) {
67
			return dataPath + FileUtil.getFS() + localid;
68
		} else {
69
			return metadataPath + FileUtil.getFS() + localid;
70
		}
71 6414 rnahf
	}
72
73
74
	/**
75
	 *  Implementation of hazelcast MapLoader interface method.
76 6416 rnahf
	 *  For the provided Identifier (as key), returns the path to the
77
	 *  document on the local filesystem.  Returns null if it can't
78
	 *  create the path.
79 6414 rnahf
	 */
80 6471 jones
	@Override
81 6458 rnahf
	public String load(String key)
82 6415 rnahf
	{
83 6440 rnahf
84
		String docid = null;
85
		String path = null;
86
		try {
87 6458 rnahf
			docid = im.getLocalId(key);
88 6440 rnahf
			path = pathToDocid(docid);
89
		} catch (McdbDocNotFoundException e) {
90
			// TODO Auto-generated catch block
91
			e.printStackTrace();
92 6414 rnahf
			return null;
93
		}
94 6440 rnahf
		return path;
95 6414 rnahf
	}
96
97
98
	/**
99 6416 rnahf
	 *  Implementation of hazelcast MapLoader interface method.  This method loads
100
	 *  mappings for all Identifiers in the parameters.  Any Identifier not found
101
	 *  is not included in the resulting map.
102 6414 rnahf
	 */
103 6471 jones
	@Override
104 6458 rnahf
	public Map<String, String> loadAll(Collection<String> identifiers) {
105 6414 rnahf
106 6440 rnahf
107 6458 rnahf
		Hashtable<String,String> map = new Hashtable<String,String>();
108
		for (String id : identifiers) {
109 6440 rnahf
			try {
110 6458 rnahf
				String docid = im.getLocalId(id);
111 6440 rnahf
				map.put(id, pathToDocid(docid));
112
113
			} catch (McdbDocNotFoundException e) {
114
				// TODO should the map load an empty path instead of
115
				// leaving out the entire entry?
116
				e.printStackTrace();
117 6414 rnahf
			}
118
		}
119 6440 rnahf
		return map;
120 6414 rnahf
	}
121
122
123 6415 rnahf
	/**
124 6416 rnahf
	 * Return the full set of guids in the local metacat repository as
125
	 * dataone Identifiers.
126
	 *
127 6440 rnahf
	 * (Hazelcast allows avoiding pre-loading by returning NULL, so will
128
	 * do this to avoid pre-loading a very long list unnecessarily)
129 6415 rnahf
	 */
130 6471 jones
	@Override
131 6458 rnahf
	public Set<String> loadAllKeys()
132 6440 rnahf
	{
133
		return null;
134
135
//		List<String> guids = im.getAllGUIDs();
136
//
137 6458 rnahf
//		Set<String> set = Collections.synchronizedSet(new HashSet<String>());
138 6440 rnahf
//		for (String guid : guids) {
139 6458 rnahf
//			set.add(guid);
140 6440 rnahf
//		}
141
//		return set;
142 6414 rnahf
	}
143
}