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
	private static boolean hasDataStore;
43

    
44

    
45
	
46
	/**
47
	 * creates an ObjectPathMap
48
	 */
49
	public ObjectPathMap() {
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
		String role = System.getProperties().getProperty("hazelcast.hzObjectPathRole");
65
		if (role != null && role.equals("consumer")) {
66
			hasDataStore = false;
67
			System.out.println("new instance of ObjectPathMap: role = " + role);
68
		} else {
69
			hasDataStore = true;
70
			System.out.println("new instance of ObjectPathMap: role = " + role);
71
		}
72
	}
73

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

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

    
148
	
149
	/**
150
	 * Return the full set of guids in the local metacat repository as
151
	 * dataone Identifiers.
152
	 * 
153
	 * (Hazelcast allows avoiding pre-loading by returning NULL, so consider
154
	 * re-implementing if that serves the purpose of this class better)
155
	 */
156
	@Override
157
	public Set<Identifier> loadAllKeys() {
158

    
159
		if (hasDataStore) {
160
			//		List<String> guids = im.getAllGUIDs();
161
			//		
162
			Set<Identifier> set = Collections.synchronizedSet(new HashSet<Identifier>());
163
			//		for (String guid : guids) {
164
			//			Identifier id = new Identifier();
165
			//			id.setValue(guid);
166
			//			set.add(id);
167
			//		}
168

    
169
			for (int i=1; i< 100; i++) {
170
				Identifier id = new Identifier();
171
				id.setValue("testID." + i);
172
				set.add(id);
173
			}
174
			return set;
175
		} else {
176
			return null;
177
		}
178
	}
179
}
(2-2/4)