001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.search.lucene;
016    
017    import java.io.IOException;
018    import java.io.InputStream;
019    import java.io.OutputStream;
020    
021    import java.util.Collection;
022    import java.util.concurrent.locks.Lock;
023    import java.util.concurrent.locks.ReadWriteLock;
024    import java.util.concurrent.locks.ReentrantReadWriteLock;
025    
026    import org.apache.lucene.document.Document;
027    import org.apache.lucene.index.Term;
028    import org.apache.lucene.search.IndexSearcher;
029    import org.apache.lucene.store.Directory;
030    
031    /**
032     * @author Shuyang Zhou
033     */
034    public class SynchronizedIndexAccessorImpl implements IndexAccessor {
035    
036            public SynchronizedIndexAccessorImpl(IndexAccessor indexAccessor) {
037                    _indexAccessor = indexAccessor;
038    
039                    ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
040    
041                    _readLock = readWriteLock.readLock();
042                    _writeLock = readWriteLock.writeLock();
043            }
044    
045            @Override
046            public IndexSearcher acquireIndexSearcher() throws IOException {
047                    _readLock.lock();
048    
049                    try {
050                            return _indexAccessor.acquireIndexSearcher();
051                    }
052                    finally {
053                            _readLock.unlock();
054                    }
055            }
056    
057            @Override
058            public void addDocument(Document document) throws IOException {
059                    _readLock.lock();
060    
061                    try {
062                            _indexAccessor.addDocument(document);
063                    }
064                    finally {
065                            _readLock.unlock();
066                    }
067            }
068    
069            @Override
070            public void addDocuments(Collection<Document> documents)
071                    throws IOException {
072    
073                    _readLock.lock();
074    
075                    try {
076                            _indexAccessor.addDocuments(documents);
077                    }
078                    finally {
079                            _readLock.unlock();
080                    }
081            }
082    
083            @Override
084            public void close() {
085                    _readLock.lock();
086    
087                    try {
088                            _indexAccessor.close();
089                    }
090                    finally {
091                            _readLock.unlock();
092                    }
093            }
094    
095            @Override
096            public void delete() {
097                    _writeLock.lock();
098    
099                    try {
100                            _indexAccessor.delete();
101                    }
102                    finally {
103                            _writeLock.unlock();
104                    }
105            }
106    
107            @Override
108            public void deleteDocuments(Term term) throws IOException {
109                    _readLock.lock();
110    
111                    try {
112                            _indexAccessor.deleteDocuments(term);
113                    }
114                    finally {
115                            _readLock.unlock();
116                    }
117            }
118    
119            @Override
120            public void dumpIndex(OutputStream outputStream) throws IOException {
121                    _readLock.lock();
122    
123                    try {
124                            _indexAccessor.dumpIndex(outputStream);
125                    }
126                    finally {
127                            _readLock.unlock();
128                    }
129            }
130    
131            @Override
132            public long getCompanyId() {
133                    return _indexAccessor.getCompanyId();
134            }
135    
136            @Override
137            public long getLastGeneration() {
138                    return _indexAccessor.getLastGeneration();
139            }
140    
141            @Override
142            public Directory getLuceneDir() {
143                    return _indexAccessor.getLuceneDir();
144            }
145    
146            @Override
147            public void invalidate() {
148                    _indexAccessor.invalidate();
149            }
150    
151            @Override
152            public void loadIndex(InputStream inputStream) throws IOException {
153                    _writeLock.lock();
154    
155                    try {
156                            _indexAccessor.loadIndex(inputStream);
157                    }
158                    finally {
159                            _writeLock.unlock();
160                    }
161            }
162    
163            @Override
164            public void releaseIndexSearcher(IndexSearcher indexSearcher)
165                    throws IOException {
166    
167                    _readLock.lock();
168    
169                    try {
170                            _indexAccessor.releaseIndexSearcher(indexSearcher);
171                    }
172                    finally {
173                            _readLock.unlock();
174                    }
175            }
176    
177            @Override
178            public void updateDocument(Term term, Document document)
179                    throws IOException {
180    
181                    _readLock.lock();
182    
183                    try {
184                            _indexAccessor.updateDocument(term, document);
185                    }
186                    finally {
187                            _readLock.unlock();
188                    }
189            }
190    
191            private IndexAccessor _indexAccessor;
192            private Lock _readLock;
193            private Lock _writeLock;
194    
195    }