Project

General

Profile

1
package edu.ucsb.nceas.metacat.dataone.hazelcast;
2

    
3
import java.sql.SQLException;
4
import java.util.Collection;
5
import java.util.Collections;
6
import java.util.HashSet;
7
import java.util.Hashtable;
8
import java.util.List;
9
import java.util.Map;
10
import java.util.Set;
11

    
12
import org.dataone.service.types.v1.Identifier;
13

    
14
import com.hazelcast.core.MapLoader;
15

    
16
import edu.ucsb.nceas.metacat.DBUtil;
17
import edu.ucsb.nceas.metacat.IdentifierManager;
18
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
19
import edu.ucsb.nceas.metacat.properties.PropertyService;
20
import edu.ucsb.nceas.utilities.FileUtil;
21
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
22

    
23

    
24
/**
25
 * MapLoader implementation for a hazelcast ObjectPath.  This class is called
26
 * when the ObjectPathMap needs to refresh against the persistent data-store.
27
 * 
28
 * @author rnahf
29
 *
30
 */
31
public class ObjectPathMapLoader implements MapLoader<Identifier, String> {
32
	private static IdentifierManager im;
33
	private static DBUtil dbUtil;
34
	private static String dataPath;
35
	private static String metadataPath;
36

    
37

    
38
	
39
	/**
40
	 * creates an ObjectPathMapLoader
41
	 */
42
	public ObjectPathMapLoader() {
43
		try {
44
			dataPath = PropertyService.getProperty("application.datafilepath");
45
			metadataPath = PropertyService.getProperty("application.documentfilepath");
46
		} catch (PropertyNotFoundException e) {
47
			// TODO Auto-generated catch block
48
			e.printStackTrace();
49
		}
50
		im = IdentifierManager.getInstance();
51
		dbUtil = new DBUtil();
52
	}
53

    
54
	
55
	/*
56
	 * Metadata is stored in a different place on the filesystem than
57
	 * the data.  The doctype value for metadata can vary, but for data
58
	 * is always 'BIN', so using a simple if-then-else to separate
59
	 */
60
	private String pathToDocid(String docid) throws SQLException {
61
		
62
		String revType = dbUtil.getCurrentRevisionAndDocTypeForGivenDocument(docid);
63
		String[] rt = revType.split(";");
64
		String docType = rt[1];
65
		String revision = rt[0];
66
		if (docType.equals("BIN")) {
67
			return dataPath + FileUtil.getFS() + docid + "." + revision;
68
		} else {
69
			return metadataPath + FileUtil.getFS() + docid + "." + revision;
70
		}		
71
	}
72

    
73
	
74
	/**
75
	 *  Implementation of hazelcast MapLoader interface method.
76
	 *  for the provided Identifier (as key), looks up the localID
77
	 *  and revision
78
	 */
79
	@Override
80
	public String load(Identifier key) 
81
	{
82
		String docid = null;
83
		String path = null;
84
		try {
85
			docid = im.getLocalId(key.getValue());
86
			path = pathToDocid(docid);
87
		} catch (McdbDocNotFoundException e) {
88
			// TODO Auto-generated catch block
89
			e.printStackTrace();
90
			return null;
91
		} catch (SQLException e) {
92
			// TODO Auto-generated catch block
93
			e.printStackTrace();
94
		}
95
		return path;
96
	}
97
	
98
	
99
	/**
100
	 *  Implementation of hazelcast MapLoader interface method.
101
	 */
102
	@Override
103
	public Map<Identifier, String> loadAll(Collection<Identifier> identifiers) {
104
		
105
		Hashtable<Identifier,String> map = new Hashtable<Identifier,String>();
106
		for (Identifier id : identifiers) {
107
			try {
108
				String docid = im.getLocalId(id.getValue());
109
				map.put(id, pathToDocid(docid));
110
				
111
			} catch (McdbDocNotFoundException e) {
112
				// TODO Auto-generated catch block
113
				e.printStackTrace();
114
			} catch (SQLException e) {
115
				// TODO Auto-generated catch block
116
				e.printStackTrace();
117
			}
118
		}
119
		return map;
120
	}
121

    
122
	
123
	/**
124
	 * Hazelcast documents a few ways to implement this method, depending on specific
125
	 * usage.  Preloading all of the GUIDs may be an unnecessarily long operation,
126
	 * so this implementation returns NULL, and loading will be shifted to the other
127
	 * method calls.
128
	 */
129
	@Override
130
	public Set<Identifier> loadAllKeys() {
131
		return null;
132

    
133
		/*  ---- this would be another way to implement, using existing methods
134
		 *  ---- in IdentifierManager, and DBUtil.  but it is still problematic
135
		 *  ---- in how it goes from docid to GUID wrt revisions
136
		 *  ---- 
137
		 *  ---- better would be a new call to the identifier table listing all GUIDs
138
		 *  ---- (from new method in IdentifierManager)
139
		Set<Identifier> set = Collections.synchronizedSet(new HashSet<Identifier>());
140
		
141
		List<String> docids = null;
142
		try {
143
			docids = im.getAllLocalIds();
144
			for(String docid : docids) {
145
				Identifier id = new Identifier();
146
				// TODO: need new query for going from localID to guid.
147
				id.setValue(im.getGUID(docid, im.getLatestRevForLocalId(docid)));
148
				set.add(id);
149
			}
150
		} catch (Exception e) {
151
			// TODO Auto-generated catch block
152
			e.printStackTrace();
153
		}
154
		return set;
155
		*/
156
	}
157
}
(2-2/3)