[exo-jcr-commits] exo-jcr SVN: r4698 - jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs.
do-not-reply at jboss.org
do-not-reply at jboss.org
Mon Aug 8 05:48:49 EDT 2011
Author: dkuleshov
Date: 2011-08-08 05:48:49 -0400 (Mon, 08 Aug 2011)
New Revision: 4698
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/TreeFileIOChannel.java
Log:
EXOJCR-1406: changed locking mechanism for mkdirs method
Modified: jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/TreeFileIOChannel.java
===================================================================
--- jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/TreeFileIOChannel.java 2011-08-05 16:12:33 UTC (rev 4697)
+++ jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/TreeFileIOChannel.java 2011-08-08 09:48:49 UTC (rev 4698)
@@ -23,7 +23,12 @@
import java.io.File;
import java.io.IOException;
-import java.util.concurrent.Semaphore;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
/**
* Created by The eXo Platform SAS
@@ -36,7 +41,7 @@
public class TreeFileIOChannel extends FileIOChannel
{
- private static Semaphore mkdirsLock = new Semaphore(1);
+ private static final ConcurrentMap<String, Lock> locks = new ConcurrentHashMap<String, Lock>(64, 0.75f, 64);
TreeFileIOChannel(File rootDir, FileCleaner cleaner, String storageId, ValueDataResourceHolder resources)
{
@@ -152,23 +157,51 @@
return path;
}
- static private boolean mkdirs(final File dir)
+ private static void mkdirs(File dir)
{
- // TODO issue on JIRA syncronized method removed and semaphore use
- try
+ if (dir.exists())
{
- mkdirsLock.acquire();
- return dir.mkdirs();
+ return;
}
- catch (InterruptedException e)
+ List<File> dir2Create = new ArrayList<File>();
+ dir2Create.add(dir);
+ dir = dir.getParentFile();
+ while (dir != null && !dir.exists())
{
- // chLog.error("mkdirs error on " + dir.getAbsolutePath() + ". " + e, e);
- // return false;
- throw new IllegalStateException("mkdirs error on " + dir.getAbsolutePath() + " due to " + e, e);
+ dir2Create.add(0, dir);
+ dir = dir.getParentFile();
}
+ for (int i = 0, length = dir2Create.size(); i < length; i++)
+ {
+ mkdir(dir2Create.get(i));
+ }
+ }
+
+ private static void mkdir(File dir)
+ {
+ String path = dir.getAbsolutePath();
+ Lock lock = locks.get(path);
+ if (lock == null)
+ {
+ lock = new ReentrantLock();
+ Lock prevLock = locks.putIfAbsent(path, lock);
+ if (prevLock != null)
+ {
+ lock = prevLock;
+ }
+ }
+ lock.lock();
+ try
+ {
+ if (!dir.exists())
+ {
+ dir.mkdir();
+ }
+ }
finally
{
- mkdirsLock.release();
+ lock.unlock();
+ locks.remove(path, lock);
}
}
}
More information about the exo-jcr-commits
mailing list