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 ObjectPathMapCustomer implements MapLoader<Identifier, String> {
38
	private static IdentifierManager im;
39
	private static String dataPath;
40
	private static String metadataPath;
41
	
42
	private static boolean hasDataStore;
43

    
44

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

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

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

    
131
	
132
	/**
133
	 * Return the full set of guids in the local metacat repository as
134
	 * dataone Identifiers.
135
	 * 
136
	 * (Hazelcast allows avoiding pre-loading by returning NULL, so consider
137
	 * re-implementing if that serves the purpose of this class better)
138
	 */
139
	@Override
140
	public Set<Identifier> loadAllKeys() {
141
		
142
//		List<String> guids = im.getAllGUIDs();
143
//		
144
		Set<Identifier> set = Collections.synchronizedSet(new HashSet<Identifier>());
145
//		for (String guid : guids) {
146
//			Identifier id = new Identifier();
147
//			id.setValue(guid);
148
//			set.add(id);
149
//		}
150
		
151
		for (int i=1; i< 100; i++) {
152
			Identifier id = new Identifier();
153
			id.setValue("testID." + i);
154
			set.add(id);
155
		}
156
		return set;
157
	}
158
}
(3-3/4)