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.store.Directory;
029    
030    /**
031     * @author Shuyang Zhou
032     */
033    public class SynchronizedIndexAccessorImpl implements IndexAccessor {
034    
035            public SynchronizedIndexAccessorImpl(IndexAccessor indexAccessor) {
036                    _indexAccessor = indexAccessor;
037    
038                    ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
039    
040                    _readLock = readWriteLock.readLock();
041                    _writeLock = readWriteLock.writeLock();
042            }
043    
044            @Override
045            public void addDocument(Document document) throws IOException {
046                    _readLock.lock();
047    
048                    try {
049                            _indexAccessor.addDocument(document);
050                    }
051                    finally {
052                            _readLock.unlock();
053                    }
054            }
055    
056            @Override
057            public void addDocuments(Collection<Document> documents)
058                    throws IOException {
059    
060                    _readLock.lock();
061    
062                    try {
063                            _indexAccessor.addDocuments(documents);
064                    }
065                    finally {
066                            _readLock.unlock();
067                    }
068            }
069    
070            @Override
071            public void close() {
072                    _readLock.lock();
073    
074                    try {
075                            _indexAccessor.close();
076                    }
077                    finally {
078                            _readLock.unlock();
079                    }
080            }
081    
082            @Override
083            public void delete() {
084                    _writeLock.lock();
085    
086                    try {
087                            _indexAccessor.delete();
088                    }
089                    finally {
090                            _writeLock.unlock();
091                    }
092            }
093    
094            @Override
095            public void deleteDocuments(Term term) throws IOException {
096                    _readLock.lock();
097    
098                    try {
099                            _indexAccessor.deleteDocuments(term);
100                    }
101                    finally {
102                            _readLock.unlock();
103                    }
104            }
105    
106            @Override
107            public void dumpIndex(OutputStream outputStream) throws IOException {
108                    _readLock.lock();
109    
110                    try {
111                            _indexAccessor.dumpIndex(outputStream);
112                    }
113                    finally {
114                            _readLock.unlock();
115                    }
116            }
117    
118            @Override
119            public long getCompanyId() {
120                    return _indexAccessor.getCompanyId();
121            }
122    
123            @Override
124            public long getLastGeneration() {
125                    return _indexAccessor.getLastGeneration();
126            }
127    
128            @Override
129            public Directory getLuceneDir() {
130                    return _indexAccessor.getLuceneDir();
131            }
132    
133            @Override
134            public void loadIndex(InputStream inputStream) throws IOException {
135                    _writeLock.lock();
136    
137                    try {
138                            _indexAccessor.loadIndex(inputStream);
139                    }
140                    finally {
141                            _writeLock.unlock();
142                    }
143            }
144    
145            @Override
146            public void updateDocument(Term term, Document document)
147                    throws IOException {
148    
149                    _readLock.lock();
150    
151                    try {
152                            _indexAccessor.updateDocument(term, document);
153                    }
154                    finally {
155                            _readLock.unlock();
156                    }
157            }
158    
159            private IndexAccessor _indexAccessor;
160            private Lock _readLock;
161            private Lock _writeLock;
162    
163    }