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 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
import edu.ucsb.nceas.metacat.shared.ServiceException;
17
import edu.ucsb.nceas.utilities.FileUtil;
18
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
19

    
20

    
21
/**
22
 * MapLoader implementation for a hazelcast hzObjectPath.  This class is called
23
 * when the IMap get methods needs to refresh against the persistent data-store.
24
 * 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
 * 
27
 * d1_indexer will get Identifiers from elsewhere, but use this class to get
28
 * the paths to their associated files.  The getAllKeys() method will
29
 * return null in a live setting, to avoid possibly expensive preloading
30
 * 
31
 * @author rnahf
32
 */
33
public class ObjectPathMap implements MapLoader<String, String> {
34
	private static IdentifierManager im;
35
	private static String dataPath;
36
	private static String metadataPath;
37
	
38
	
39
	/**
40
	 * creates an ObjectPathMap
41
	 */
42
	public ObjectPathMap() {
43
		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
		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
	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
	}
72

    
73
	
74
	/**
75
	 *  Implementation of hazelcast MapLoader interface method.
76
	 *  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
	 */
80
	@Override
81
	public String load(String key) 
82
	{
83

    
84
		String docid = null;
85
		String path = null;
86
		try {
87
			docid = im.getLocalId(key);
88
			path = pathToDocid(docid);			
89
		} catch (McdbDocNotFoundException e) {
90
			// TODO Auto-generated catch block
91
			e.printStackTrace();
92
			return null;
93
		}
94
		return path;
95
	}
96
	
97
	
98
	/**
99
	 *  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
	 */
103
	@Override
104
	public Map<String, String> loadAll(Collection<String> identifiers) {
105
		
106
		
107
		Hashtable<String,String> map = new Hashtable<String,String>();
108
		for (String id : identifiers) {
109
			try {
110
				String docid = im.getLocalId(id);
111
				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
			}
118
		}
119
		return map;
120
	}
121

    
122
	
123
	/**
124
	 * Return the full set of guids in the local metacat repository as
125
	 * dataone Identifiers.
126
	 * 
127
	 * (Hazelcast allows avoiding pre-loading by returning NULL, so will
128
	 * do this to avoid pre-loading a very long list unnecessarily)
129
	 */
130
	@Override
131
	public Set<String> loadAllKeys() 
132
	{
133
		return null;
134
		
135
//		List<String> guids = im.getAllGUIDs();
136
//
137
//		Set<String> set = Collections.synchronizedSet(new HashSet<String>());
138
//		for (String guid : guids) {
139
//			set.add(guid);
140
//		}
141
//		return set;
142
	}
143
}
(3-3/4)