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