r53 - in trunk/diswork-fs/src/main/java/org/nuiton/disworkfs: . storage
Author: bleny Date: 2010-05-26 10:44:36 +0200 (Wed, 26 May 2010) New Revision: 53 Url: http://nuiton.org/repositories/revision/diswork/53 Log: exceptions, suppression de donnees retardees Modified: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DisworkFileSystem.java trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/storage/Storage.java Modified: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DisworkFileSystem.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DisworkFileSystem.java 2010-05-25 16:24:08 UTC (rev 52) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DisworkFileSystem.java 2010-05-26 08:44:36 UTC (rev 53) @@ -1,9 +1,11 @@ package org.nuiton.disworkfs; +import java.io.Closeable; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; +import java.util.ConcurrentModificationException; import java.util.List; import org.apache.commons.logging.Log; @@ -24,7 +26,7 @@ * on directories, files and symlinks</li> * </ul> */ -public class DisworkFileSystem { +public class DisworkFileSystem implements Closeable { /** * storage will permit to save and read directories, files and links @@ -104,8 +106,14 @@ * @param path * @param source * @throws IOException if file already exists + * @throws ConcurrentModificationException if file is already being written */ - public void write(String path, InputStream source) throws IOException { + public void write(String path, InputStream source) + throws IOException, + ConcurrentModificationException { + if (source == null) { + throw new IOException("source is not readable (null stream)"); + } String parent = EntryUtil.getParentFromPath(path); String name = EntryUtil.getNameFromPath(path); write(parent, name, source); @@ -149,13 +157,16 @@ * @param path the absolute path (ending by the name) of the directory * @throws IOException if parent path is not correct */ - public void createDirectory(String path) throws IOException { + public void createDirectory(String path) + throws IOException, + ConcurrentModificationException { String parent = EntryUtil.getParentFromPath(path); String dirName = EntryUtil.getNameFromPath(path); createDirectory(parent, dirName); } - protected void createDirectory(String parent, String dirName) throws IOException { + protected void createDirectory(String parent, String dirName) + throws IOException { log.info("trying to create directory " + dirName + " in " + parent); @@ -190,7 +201,8 @@ * @throws IOException if parent path is not correct */ public void createSymbolicLink(String path, String target) - throws IOException { + throws IOException, + ConcurrentModificationException { String parent = EntryUtil.getParentFromPath(path); String name = EntryUtil.getNameFromPath(path); createSymbolicLink(parent, name, target); @@ -244,7 +256,8 @@ * @param path the complete path to the entity to remove * @throws IOException if path is incorrect or directory not empty */ - public void delete(String path) throws IOException { + public void delete(String path) throws IOException, + ConcurrentModificationException { String parent = EntryUtil.getParentFromPath(path); String name = EntryUtil.getNameFromPath(path); log.info("trying to remove " + path); @@ -352,10 +365,9 @@ * @return null if path is not valid or the entry corresponding to path * @throws IOException */ - - // FIXME 20105021 bleny works fine but is not understandable protected String walk(String path, String current, String content) throws IOException { + // FIXME 20105021 bleny works fine but is not understandable String result = null; if (path.equals("/")) { @@ -437,5 +449,10 @@ } return result; } + + @Override + public void close() throws IOException { + storage.close(); + } } Modified: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/storage/Storage.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/storage/Storage.java 2010-05-25 16:24:08 UTC (rev 52) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/storage/Storage.java 2010-05-26 08:44:36 UTC (rev 53) @@ -1,9 +1,12 @@ package org.nuiton.disworkfs.storage; import java.io.ByteArrayInputStream; +import java.io.Closeable; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; import java.util.ConcurrentModificationException; +import java.util.List; import java.util.Map; import org.apache.commons.io.IOUtils; @@ -14,10 +17,14 @@ /** * */ -public class Storage { +public class Storage implements Closeable { private static final Log log = LogFactory.getLog(Storage.class); + + protected static final long LOCK_VALID_TIME = 60 * 60 * 1000; + protected List<String> idsToBeRemoved = new ArrayList<String>(); + /** * This class is a InputStream used to read data from the map. When * someone read from the filesytem, he will bre returned a @@ -147,6 +154,8 @@ } public void putFile(String id, InputStream content) throws IOException { + log.debug("putFile(\"" + id + "\", stream of "+ content.available() + + " bytes)"); put(id, content); } @@ -171,11 +180,9 @@ * @param value * @throws IOException */ - protected void put(String key, InputStream value) throws IOException { - // TODO 20100519 bleny deal with null value properly - if (value == null) { - value = new ByteArrayInputStream(new byte[0]); - } + protected void put(String key, InputStream value) + throws IOException, + ConcurrentModificationException { String lockKey = key + "_lock"; String newDataKey = key + "_newData"; @@ -188,19 +195,20 @@ // file is locked, check date Long currentLock = Long.parseLong(EntryUtil.bytesToString(lock)); - if (currentDate - currentLock > 60 * 60 * 1000) { - // this lock is out dated, let's erase it - String obsoleteMetaBlock = EntryUtil.bytesToString(map.get(newDataKey)); + if (currentDate - currentLock > LOCK_VALID_TIME) { + // this lock is out-dated, let's erase it + String obsoleteMetaBlock = + EntryUtil.bytesToString(map.get(newDataKey)); if (obsoleteMetaBlock != null) { - String[] obsoleteBlocksIds = EntryUtil.getBlockIdsFromMetaBlock(obsoleteMetaBlock); + String[] obsoleteBlocksIds = + EntryUtil.getBlockIdsFromMetaBlock(obsoleteMetaBlock); for (String obsoleteBlockId : obsoleteBlocksIds) { map.remove(obsoleteBlockId); } } } else { - // file is currently written, stopping + // file is currently written, stopping operation map.put(lockKey, lock); - // FIXME 20100525 bleny should be declared in throws throw new ConcurrentModificationException("can't write due to concurrency"); } } @@ -253,9 +261,28 @@ map.remove(lockKey); } - public void remove(Object key) { - // TODO poussin 20100518 mark bloque to remove - map.remove(key); + /** + * due to concurrency, key should not be removed immediately. Someone may + * read it now. It will be removed later, using {@link #clean()}. + * @param key the key to be removed from the map + */ + public void remove(String key) { + idsToBeRemoved.add(key); } + + /** + * this method clean the map by removing all the blocks that are not + * referenced (ie, their key is not in any meta-block or in entries) + */ + public void clean() { + for (String key : idsToBeRemoved) { + map.remove(key); + } + } + @Override + public void close() throws IOException { + clean(); + } + } \ No newline at end of file
participants (1)
-
bleny@users.nuiton.org