Project

General

Profile

« Previous | Next » 

Revision 6962

calculate object size using the size on the file system rather than re-reading as an input stream.
Now only EML document bytes will be read twice: once for the checksum and again for parsing out datapackage details

View differences:

src/edu/ucsb/nceas/metacat/dataone/SystemMetadataFactory.java
25 25
 */
26 26
package edu.ucsb.nceas.metacat.dataone;
27 27

  
28
import java.io.BufferedInputStream;
29
import java.io.File;
28 30
import java.io.IOException;
29 31
import java.io.InputStream;
30 32
import java.math.BigInteger;
......
78 80
import edu.ucsb.nceas.metacat.AccessionNumber;
79 81
import edu.ucsb.nceas.metacat.AccessionNumberException;
80 82
import edu.ucsb.nceas.metacat.DBUtil;
83
import edu.ucsb.nceas.metacat.DocumentImpl;
81 84
import edu.ucsb.nceas.metacat.IdentifierManager;
82 85
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
83 86
import edu.ucsb.nceas.metacat.McdbException;
......
169 172
			sysMeta.setSerialVersion(BigInteger.valueOf(1));
170 173
			sysMeta.setArchived(false);
171 174
		}
172
				
173
		// get the data or metadata object
174
		InputStream inputStream;
175
		try {
176
			inputStream = MetacatHandler.read(localId);
177
		} catch (ParseLSIDException ple) {
178
			logMetacat.debug("There was a problem parsing the LSID from "
179
					+ localId + ". The error message was: " + ple.getMessage());
180
			throw ple;
181

  
182
		} catch (PropertyNotFoundException pnfe) {
183
			logMetacat.debug("There was a problem finding a property. "
184
					+ "The error message was: " + pnfe.getMessage());
185
			throw pnfe;
186

  
187
		} catch (McdbException me) {
188
			logMetacat.debug("There was a Metacat problem. "
189
					+ "The error message was: " + me.getMessage());
190
			throw me;
191

  
192
		} catch (SQLException sqle) {
193
			logMetacat.debug("There was a SQL problem. "
194
					+ "The error message was: " + sqle.getMessage());
195
			throw sqle;
196

  
197
		} catch (ClassNotFoundException cnfe) {
198
			logMetacat.debug("There was a problem finding a class. "
199
					+ "The error message was: " + cnfe.getMessage());
200
			throw cnfe;
201

  
202
		} catch (IOException ioe) {
203
			logMetacat.debug("There was an I/O exception. "
204
					+ "The error message was: " + ioe.getMessage());
205
			throw ioe;
206

  
207
		}
208

  
175
		
209 176
		// get additional docinfo
210 177
		Hashtable<String, String> docInfo = ReplicationService.getDocumentInfoMap(localId);
211 178
		// set the default object format
......
240 207
		sysMeta.setFormatId(fmtid);
241 208
		logMetacat.debug("The ObjectFormat for " + localId + " is " + fmtid.getValue());
242 209

  
210
		// for retrieving the actual object
211
		InputStream inputStream = null;
212
		inputStream = MetacatHandler.read(localId);
213

  
243 214
		// create the checksum
244
		inputStream = MetacatHandler.read(localId);
245 215
		String algorithm = "MD5";
246 216
		Checksum checksum = ChecksumUtil.checksum(inputStream, algorithm);
247 217
		sysMeta.setChecksum(checksum);
248 218
		
249
		// set the size
250
		inputStream = MetacatHandler.read(localId);
251
		String sizeStr = new Long(sizeOfStream(inputStream)).toString();
252
		sysMeta.setSize(new BigInteger(sizeStr));
219
		// set the size from file on disk, don't read bytes again
220
		File fileOnDisk = getFileOnDisk(localId);
221
		long fileSize = 0;
222
		if (fileOnDisk.exists()) {
223
			fileSize = fileOnDisk.length();
224
		}
225
		sysMeta.setSize(BigInteger.valueOf(fileSize));
253 226
		
254 227
		// submitter
255 228
		Subject submitter = new Subject();
......
337 310
						"eml://ecoinformatics.org/eml-2.1.1").getFormatId()) {
338 311

  
339 312
			try {
313
				
314
				// get it again to parse the document
315
				logMetacat.debug("Re-reading document inputStream");
340 316
				inputStream = MetacatHandler.read(localId);
341 317
				
342 318
				DataoneEMLParser emlParser = DataoneEMLParser.getInstance();
......
400 376
											}
401 377
										}
402 378
										
403
										// TODO: any other special processing (csv, images, etc)?
404 379
									} else {
405 380
										// if we don't know what it is, should we skip it?
406 381
										dataObject = null;
......
717 692
		return size;
718 693

  
719 694
	}
695
	
696
	private static File getFileOnDisk(String docid) throws McdbException, PropertyNotFoundException {
697
		
698
		DocumentImpl doc = new DocumentImpl(docid, false);
699
		String filepath = null;
700
		String filename = null;
701

  
702
		// deal with data or metadata cases
703
		if (doc.getRootNodeID() == 0) {
704
			// this is a data file
705
			filepath = PropertyService.getProperty("application.datafilepath");
706
		} else {
707
			filepath = PropertyService.getProperty("application.documentfilepath");
708
		}
709
		// ensure it is a directory path
710
		if (!(filepath.endsWith("/"))) {
711
			filepath += "/";
712
		}
713
		filename = filepath + docid;
714
		File documentFile = new File(filename);
715
		
716
		return documentFile;
717
	}
720 718
}

Also available in: Unified diff