001    /**
002     * Copyright (c) 2000-2012 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            public void addDocument(Document document) throws IOException {
044                    _readLock.lock();
045    
046                    try {
047                            _indexAccessor.addDocument(document);
048                    }
049                    finally {
050                            _readLock.unlock();
051                    }
052            }
053    
054            public void close() {
055                    _readLock.lock();
056    
057                    try {
058                            _indexAccessor.close();
059                    }
060                    finally {
061                            _readLock.unlock();
062                    }
063            }
064    
065            public void delete() {
066                    _writeLock.lock();
067    
068                    try {
069                            _indexAccessor.delete();
070                    }
071                    finally {
072                            _writeLock.unlock();
073                    }
074            }
075    
076            public void deleteDocuments(Term term) throws IOException {
077                    _readLock.lock();
078    
079                    try {
080                            _indexAccessor.deleteDocuments(term);
081                    }
082                    finally {
083                            _readLock.unlock();
084                    }
085            }
086    
087            public void dumpIndex(OutputStream outputStream) throws IOException {
088                    _readLock.lock();
089    
090                    try {
091                            _indexAccessor.dumpIndex(outputStream);
092                    }
093                    finally {
094                            _readLock.unlock();
095                    }
096            }
097    
098            public long getCompanyId() {
099                    return _indexAccessor.getCompanyId();
100            }
101    
102            public long getLastGeneration() {
103                    return _indexAccessor.getLastGeneration();
104            }
105    
106            public Directory getLuceneDir() {
107                    return _indexAccessor.getLuceneDir();
108            }
109    
110            public void loadIndex(InputStream inputStream) throws IOException {
111                    _writeLock.lock();
112    
113                    try {
114                            _indexAccessor.loadIndex(inputStream);
115                    }
116                    finally {
117                            _writeLock.unlock();
118                    }
119            }
120    
121            public void updateDocument(Term term, Document document)
122                    throws IOException {
123    
124                    _readLock.lock();
125    
126                    try {
127                            _indexAccessor.updateDocument(term, document);
128                    }
129                    finally {
130                            _readLock.unlock();
131                    }
132            }
133    
134            private IndexAccessor _indexAccessor;
135            private Lock _readLock;
136            private Lock _writeLock;
137    
138    }