com.jaeksoft.searchlib.index.IndexDirectory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opensearchserver Show documentation
Show all versions of opensearchserver Show documentation
OpenSearchServer is a powerful, enterprise-class, search engine program. Using the web user interface, the crawlers (web, file, database, ...) and the REST/RESTFul API you will be able to integrate quickly and easily advanced full-text search capabilities in your application. OpenSearchServer runs on Windows and Linux/Unix/BSD.
The newest version!
/**
* License Agreement for OpenSearchServer
*
* Copyright (C) 2008-2016 Emmanuel Keller / Jaeksoft
*
* http://www.open-search-server.com
*
* This file is part of OpenSearchServer.
*
* OpenSearchServer is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSearchServer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSearchServer.
* If not, see .
**/
package com.jaeksoft.searchlib.index;
import com.jaeksoft.searchlib.Logging;
import com.jaeksoft.searchlib.SearchLibException;
import com.jaeksoft.searchlib.crawler.file.process.fileInstances.swift.SwiftToken;
import com.jaeksoft.searchlib.crawler.file.process.fileInstances.swift.SwiftToken.AuthType;
import com.jaeksoft.searchlib.crawler.web.spider.HttpDownloader;
import com.jaeksoft.searchlib.util.LinkUtils;
import com.jaeksoft.searchlib.util.ReadWriteLock;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.NoSuchDirectoryException;
import org.json.JSONException;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
public class IndexDirectory {
private Directory directory;
private final ReadWriteLock rwl = new ReadWriteLock();
protected IndexDirectory(File indexDir) throws IOException {
directory = FSDirectory.open(indexDir);
}
/**
* Create an index directory using an index remotely stored using the Object
* Storage API.
*
* The URI should be created like that:
* SWIFT://localhost?tenant=&container=&user=&password=&url=
*
* The parameters must be URL encoded as UTF-8
*
* @param uri
* @throws IOException
* @throws URISyntaxException
* @throws JSONException
* @throws SearchLibException
*/
protected IndexDirectory(final URI uri) throws IOException, URISyntaxException, JSONException, SearchLibException {
if ("SWIFT".equals(uri.getScheme())) {
HttpDownloader httpDownloader = new HttpDownloader(null, true, null, 600);
Map parameters = LinkUtils.getUniqueQueryParameters(uri, "UTF-8");
String tenant = parameters.get("tenant");
String container = parameters.get("container");
String user = parameters.get("user");
String password = parameters.get("password");
String url = parameters.get("url");
SwiftToken token = new SwiftToken(httpDownloader, url, user, password, AuthType.KEYSTONE, tenant);
directory = new ObjectStorageDirectory(httpDownloader, token, container);
return;
}
throw new IOException("Unsupported protocol: " + uri);
}
public Directory getDirectory() {
rwl.r.lock();
try {
return directory;
} finally {
rwl.r.unlock();
}
}
public boolean isEmpty() throws IOException {
rwl.r.lock();
try {
return ArrayUtils.isEmpty(directory.listAll());
} catch (NoSuchDirectoryException e) {
return true;
} finally {
rwl.r.unlock();
}
}
public void unlock() {
rwl.w.lock();
try {
if (directory == null)
return;
if (!IndexWriter.isLocked(directory))
return;
IndexWriter.unlock(directory);
} catch (Exception e) {
Logging.warn(e);
} finally {
rwl.w.unlock();
}
}
public void close() {
rwl.w.lock();
try {
if (directory == null)
return;
try {
directory.close();
} catch (IOException e) {
Logging.warn(e.getMessage(), e);
}
directory = null;
} finally {
rwl.w.unlock();
}
}
}