001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.io.storage.local; 020 021import org.apache.reef.io.storage.ScratchSpace; 022 023import java.io.File; 024import java.io.IOException; 025import java.util.Set; 026import java.util.concurrent.ConcurrentSkipListSet; 027 028public class LocalScratchSpace implements ScratchSpace { 029 030 private final String jobName; 031 private final String evaluatorName; 032 private final Set<File> tempFiles = new ConcurrentSkipListSet<File>(); 033 /** 034 * Zero denotes "unlimited" 035 */ 036 private long quota; 037 038 public LocalScratchSpace(String jobName, String evaluatorName) { 039 this.jobName = jobName; 040 this.evaluatorName = evaluatorName; 041 this.quota = 0; 042 } 043 044 public LocalScratchSpace(String jobName, String evaluatorName, long quota) { 045 this.jobName = jobName; 046 this.evaluatorName = evaluatorName; 047 this.quota = quota; 048 } 049 050 public File newFile() { 051 final File ret; 052 try { 053 ret = File.createTempFile("reef-" + jobName + "-" + evaluatorName, 054 "tmp"); 055 } catch (IOException e) { 056 throw new RuntimeException(e); 057 } 058 tempFiles.add(ret); 059 return ret; 060 } 061 062 @Override 063 public long availableSpace() { 064 return quota; 065 } 066 067 @Override 068 public long usedSpace() { 069 long ret = 0; 070 for (File f : tempFiles) { 071 // TODO: Error handling... 072 ret += f.length(); 073 } 074 return ret; 075 } 076 077 @Override 078 public void delete() { 079 // TODO: Error handling. Files.delete() would give us an exception. We 080 // should pass a set of Exceptions into a ReefRuntimeException. 081 for (File f : tempFiles) { 082 f.delete(); 083 } 084 tempFiles.clear(); 085 } 086 087}