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