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

    
22

    
23
/**
24
 * MapLoader implementation for a hazelcast hzObjectPath.  This class is called
25
 * when the ObjectPathMap needs to refresh against the persistent data-store.
26
 * 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
 * 
29
 * d1_indexer will get Identifiers from elsewhere, but use this class to get
30
 * the paths to their associated files.  The getAllKeys() method can (and should)
31
 * return null in a live setting.  For unit testing, it may be useful to use
32
 * it to get some 
33
 * 
34
 * @author rnahf
35
 *
36
 */
37
public class ObjectPathMap implements MapLoader<Identifier, String> {
38
	private static IdentifierManager im;
39
	private static String dataPath;
40
	private static String metadataPath;
41

    
42

    
43
	
44
	/**
45
	 * creates an ObjectPathMapLoader
46
	 */
47
	public ObjectPathMap() {
48
//		try {
49
//			PropertyService ps = PropertyService.getInstance();
50
//			dataPath = PropertyService.getProperty("application.datafilepath");
51
//			metadataPath = PropertyService.getProperty("application.documentfilepath");
52
//		} catch (PropertyNotFoundException e) {
53
//			// TODO Auto-generated catch block
54
//			e.printStackTrace();
55
//		} catch (ServiceException e) {
56
//			// TODO Auto-generated catch block
57
//			e.printStackTrace();
58
//		}
59
		dataPath = "/data/";
60
		metadataPath = "/metadata/";
61
		im = IdentifierManager.getInstance();
62
	}
63

    
64
	
65
	/*
66
	 * Metadata is stored in a different place on the filesystem than
67
	 * the data.  The doctype value for metadata can vary, but for data
68
	 * is always 'BIN', so using a simple if-then-else to separate
69
	 */
70
	private String pathToDocid(String localid) throws McdbDocNotFoundException  {
71
		
72
		Hashtable<String, Object> ht = im.getDocumentInfo(localid);
73
		if (ht.get("doctype").equals("BIN")) {
74
			return dataPath + FileUtil.getFS() + localid;
75
		} else {
76
			return metadataPath + FileUtil.getFS() + localid;
77
		}		
78
	}
79

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

    
127
	
128
	/**
129
	 * Return the full set of guids in the local metacat repository as
130
	 * dataone Identifiers.
131
	 * 
132
	 * (Hazelcast allows avoiding pre-loading by returning NULL, so consider
133
	 * re-implementing if that serves the purpose of this class better)
134
	 */
135
	@Override
136
	public Set<Identifier> loadAllKeys() {
137
		
138
		List<String> guids = im.getAllGUIDs();
139
		
140
		Set<Identifier> set = Collections.synchronizedSet(new HashSet<Identifier>());
141
		for (String guid : guids) {
142
			Identifier id = new Identifier();
143
			id.setValue(guid);
144
			set.add(id);
145
		}
146
		return set;
147
	}
148
}
(2-2/3)