Return-Path: Delivered-To: apmail-lucene-commits-archive@www.apache.org Received: (qmail 74267 invoked from network); 29 Nov 2010 15:20:21 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 29 Nov 2010 15:20:21 -0000 Received: (qmail 95692 invoked by uid 500); 29 Nov 2010 15:20:20 -0000 Mailing-List: contact commits-help@lucene.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@lucene.apache.org Delivered-To: mailing list commits@lucene.apache.org Received: (qmail 95675 invoked by uid 99); 29 Nov 2010 15:20:19 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 29 Nov 2010 15:20:19 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 29 Nov 2010 15:20:17 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 4358B238890D; Mon, 29 Nov 2010 15:18:44 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1040145 - /lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/store/RAMDirectory.java Date: Mon, 29 Nov 2010 15:18:43 -0000 To: commits@lucene.apache.org From: shaie@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20101129151844.4358B238890D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: shaie Date: Mon Nov 29 15:18:42 2010 New Revision: 1040145 URL: http://svn.apache.org/viewvc?rev=1040145&view=rev Log: LUCENE-2779: Use ConcurrentHashMap in RAMDirectory (3x) Modified: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/store/RAMDirectory.java Modified: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/store/RAMDirectory.java URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/store/RAMDirectory.java?rev=1040145&r1=1040144&r2=1040145&view=diff ============================================================================== --- lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/store/RAMDirectory.java (original) +++ lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/store/RAMDirectory.java Mon Nov 29 15:18:42 2010 @@ -20,8 +20,8 @@ package org.apache.lucene.store; import java.io.IOException; import java.io.FileNotFoundException; import java.io.Serializable; -import java.util.HashMap; -import java.util.Set; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; import org.apache.lucene.index.IndexFileNameFilter; @@ -36,7 +36,7 @@ public class RAMDirectory extends Direct private static final long serialVersionUID = 1l; - protected HashMap fileMap = new HashMap(); + protected Map fileMap = new ConcurrentHashMap(); protected final AtomicLong sizeInBytes = new AtomicLong(); // ***** @@ -83,25 +83,16 @@ public class RAMDirectory extends Direct } @Override - public synchronized final String[] listAll() { + public final String[] listAll() { ensureOpen(); - Set fileNames = fileMap.keySet(); - String[] result = new String[fileNames.size()]; - int i = 0; - for(final String fileName: fileNames) - result[i++] = fileName; - return result; + return fileMap.keySet().toArray(new String[0]); } /** Returns true iff the named file exists in this directory. */ @Override public final boolean fileExists(String name) { ensureOpen(); - RAMFile file; - synchronized (this) { - file = fileMap.get(name); - } - return file != null; + return fileMap.containsKey(name); } /** Returns the time the named file was last modified. @@ -110,12 +101,10 @@ public class RAMDirectory extends Direct @Override public final long fileModified(String name) throws IOException { ensureOpen(); - RAMFile file; - synchronized (this) { - file = fileMap.get(name); - } - if (file==null) + RAMFile file = fileMap.get(name); + if (file == null) { throw new FileNotFoundException(name); + } return file.getLastModified(); } @@ -125,12 +114,10 @@ public class RAMDirectory extends Direct @Override public void touchFile(String name) throws IOException { ensureOpen(); - RAMFile file; - synchronized (this) { - file = fileMap.get(name); - } - if (file==null) + RAMFile file = fileMap.get(name); + if (file == null) { throw new FileNotFoundException(name); + } long ts2, ts1 = System.currentTimeMillis(); do { @@ -151,19 +138,18 @@ public class RAMDirectory extends Direct @Override public final long fileLength(String name) throws IOException { ensureOpen(); - RAMFile file; - synchronized (this) { - file = fileMap.get(name); - } - if (file==null) + RAMFile file = fileMap.get(name); + if (file == null) { throw new FileNotFoundException(name); + } return file.getLength(); } - /** Return total size in bytes of all files in this - * directory. This is currently quantized to - * RAMOutputStream.BUFFER_SIZE. */ - public synchronized final long sizeInBytes() { + /** + * Return total size in bytes of all files in this directory. This is + * currently quantized to RAMOutputStream.BUFFER_SIZE. + */ + public final long sizeInBytes() { ensureOpen(); return sizeInBytes.get(); } @@ -172,14 +158,15 @@ public class RAMDirectory extends Direct * @throws IOException if the file does not exist */ @Override - public synchronized void deleteFile(String name) throws IOException { + public void deleteFile(String name) throws IOException { ensureOpen(); RAMFile file = fileMap.remove(name); - if (file!=null) { - file.directory = null; - sizeInBytes.addAndGet(-file.sizeInBytes); - } else + if (file != null) { + file.directory = null; + sizeInBytes.addAndGet(-file.sizeInBytes); + } else { throw new FileNotFoundException(name); + } } /** Creates a new, empty file in the directory with the given name. Returns a stream writing this file. */ @@ -187,14 +174,12 @@ public class RAMDirectory extends Direct public IndexOutput createOutput(String name) throws IOException { ensureOpen(); RAMFile file = newRAMFile(); - synchronized (this) { - RAMFile existing = fileMap.get(name); - if (existing!=null) { - sizeInBytes.addAndGet(-existing.sizeInBytes); - existing.directory = null; - } - fileMap.put(name, file); + RAMFile existing = fileMap.remove(name); + if (existing != null) { + sizeInBytes.addAndGet(-existing.sizeInBytes); + existing.directory = null; } + fileMap.put(name, file); return new RAMOutputStream(file); } @@ -211,12 +196,10 @@ public class RAMDirectory extends Direct @Override public IndexInput openInput(String name) throws IOException { ensureOpen(); - RAMFile file; - synchronized (this) { - file = fileMap.get(name); - } - if (file == null) + RAMFile file = fileMap.get(name); + if (file == null) { throw new FileNotFoundException(name); + } return new RAMInputStream(file); }