Skip to content
This repository has been archived by the owner on May 4, 2023. It is now read-only.

Commit

Permalink
remove registry and indexkey, change output paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Newson committed Nov 24, 2009
1 parent 78b4430 commit cbfe065
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 262 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,25 @@ You will now have a zip file in the target/ directory. This contains all the cou
os_process_timeout=60000 ; increase the timeout from 5 seconds.

[external]
fti=/path/to/python /usr/lib/couchdb/couchdb-lucene/couchdb-external-hook.py --host=localhost --port=5985
fti=/path/to/python /usr/lib/couchdb/couchdb-lucene/couchdb-external-hook.py

[httpd_db_handlers]
_fti = {couch_httpd_external, handle_external_req, <<"fti">>}
</pre>

<h1>Configure couchdb-lucene</h1>
<h2>Hook options</h2>

<table>
<tr><th>Option</th><th>Meaning</th><th>Default Value</th></tr>
<tr><td>--remote-host</td><td>The hostname of the couchdb-lucene server</td><td>localhost</td></tr>
<tr><td>--remote-port</td><td>The port of the couchdb-lucene server</td><td>5985</td></tr>
<tr><td>--local-host</td><td>The hostname of the couchdb server</td><td>localhost</td></tr>
<tr><td>--local-port</td><td>The port of the couchdb server</td><td>5984</td></tr>
</table>

couchdb-lucene runs in a single, standalone JVM. As such, you can choose to locate your couchdb-lucene server on a different machine to couchdb if you wish, or keep it on the same machine, it's your call.
<h1>Configure couchdb-lucene</h1>

<ol>
<li>Unzip the couchdb-lucene zip file.
<li>Open the conf/couchdb-lucene.properties file.
<li>Edit the couchdb.url to point to your couchdb installation.
couchdb-lucene runs in a single, standalone JVM. As such, you can choose to locate your couchdb-lucene server on a different machine to couchdb if you wish, or keep it on the same machine, it's your call.

<h1>Start couchdb-lucene</h1>

Expand Down
27 changes: 15 additions & 12 deletions couchdb-external-hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@

def options():
return [
op.make_option('--host', dest='host',
default="127.0.0.1",
op.make_option('--remote-host', dest='remote_host',
default="localhost",
help="Hostname of the couchdb-lucene server. [%default]"),
op.make_option('--port', dest='port', type='int',
op.make_option('--remote-port', dest='remote_port', type='int',
default=5985,
help="Port of the couchdb-lucene server. [%default]"),
op.make_option('--key', dest='key',
default="local",
help="Host key of this couchdb instance. [%default]")
op.make_option('--local-host', dest='local_host',
default="localhost",
help="Hostname of this couchdb instance. [%default]"),
op.make_option('--local-port', dest='local_port', type='int',
default=5984,
help="Port of this couchdb instance. [%default]"),
]

def main():
Expand All @@ -32,14 +35,14 @@ def main():

if len(args):
parser.error("Unrecognized arguments: %s" % ' '.join(args))
res = httplib.HTTPConnection(opts.host, opts.port)
res = httplib.HTTPConnection(opts.remote_host, opts.remote_port)
for req in requests():
try:
resp = respond(res, req, opts.key)
resp = respond(res, req, opts.local_host, opts.local_port)
except Exception, e:
body = traceback.format_exc()
resp = mkresp(500, body, {"Content-Type": "text/plain"})
res = httplib.HTTPConnection(opts.host, opts.port)
res = httplib.HTTPConnection(opts.remote_host, opts.remote_port)

sys.stdout.write(json.dumps(resp))
sys.stdout.write("\n")
Expand All @@ -51,18 +54,18 @@ def requests():
yield json.loads(line)
line = sys.stdin.readline()

def respond(res, req, host_key):
def respond(res, req, host, port):
path = req.get("path", [])

if len(path) != 4:
body = "\n".join([
"Invalid path: %s" % '/'.join([''] + path),
"Paths should be: /host_key/db_name/_fti/docid/index_name?q=...",
"Paths should be: /db_name/_fti/docid/index_name?q=...",
"'docid' is from the '_design/docid' that defines index_name"
])
return mkresp(400, body, {"Content-Type": "text/plain"})

path = '/'.join(['', 'search', host_key, path[0], path[2], path[3]])
path = '/'.join(['', 'search', host, str(port), path[0], path[2], path[3]])
path = '?'.join([path, urllib.urlencode(req["query"])])

req_headers = {}
Expand Down
3 changes: 0 additions & 3 deletions src/main/conf/couchdb-lucene.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# The location of CouchDB.
couchdb.url.local=http://localhost:5984

# The output directory for Lucene indexes.
lucene.dir=indexes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ public void setLucene(final Lucene lucene) {

@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
final IndexKey key = new IndexKey(req);

final String command = req.getParameter("cmd");

if ("expunge".equals(command)) {
lucene.withWriter(key, new WriterCallback() {
lucene.withWriter(req.getPathInfo(), new WriterCallback() {
public boolean callback(final IndexWriter writer) throws IOException {
writer.expungeDeletes(false);
return false;
Expand All @@ -56,7 +54,7 @@ public void onMissing() throws IOException {
}

if ("optimize".equals(command)) {
lucene.withWriter(key, new WriterCallback() {
lucene.withWriter(req.getPathInfo(), new WriterCallback() {
public boolean callback(final IndexWriter writer) throws IOException {
writer.optimize(false);
return false;
Expand Down

This file was deleted.

104 changes: 0 additions & 104 deletions src/main/java/com/github/rnewson/couchdb/lucene/IndexKey.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ public void setLucene(final Lucene lucene) {

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
final IndexKey key = new IndexKey(req);

lucene.withReader(key, Utils.getStaleOk(req), new ReaderCallback() {
lucene.withReader(req.getPathInfo(), Utils.getStaleOk(req), new ReaderCallback() {
public void callback(final IndexReader reader) throws IOException {
final JSONObject result = new JSONObject();
result.put("current", reader.isCurrent());
Expand Down
34 changes: 16 additions & 18 deletions src/main/java/com/github/rnewson/couchdb/lucene/Lucene.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
public final class Lucene {

private final File root;
private final CouchDbRegistry registry;
private final IdempotentExecutor<IndexKey> executor = new IdempotentExecutor<IndexKey>();
private final Map<IndexKey, Tuple> map = new HashMap<IndexKey, Tuple>();
private final IdempotentExecutor<String> executor = new IdempotentExecutor<String>();
private final Map<String, Tuple> map = new HashMap<String, Tuple>();

private static class Tuple {
private String version;
Expand Down Expand Up @@ -66,19 +65,18 @@ public interface WriterCallback {
public void onMissing() throws IOException;
}

public Lucene(final File root, final CouchDbRegistry registry) {
public Lucene(final File root) {
this.root = root;
this.registry = registry;
}

public void startIndexing(final IndexKey indexKey) {
executor.submit(indexKey, new ViewIndexer(this, registry, indexKey));
public void startIndexing(final String path) {
executor.submit(path, new ViewIndexer(this, path));
}

public void withReader(final IndexKey key, final boolean staleOk, final ReaderCallback callback) throws IOException {
public void withReader(final String path, final boolean staleOk, final ReaderCallback callback) throws IOException {
final Tuple tuple;
synchronized (map) {
tuple = map.get(key);
tuple = map.get(path);
}
if (tuple == null) {
callback.onMissing();
Expand Down Expand Up @@ -114,11 +112,11 @@ public void withReader(final IndexKey key, final boolean staleOk, final ReaderCa
}
}

public void withSearcher(final IndexKey key, final boolean staleOk, final SearcherCallback callback) throws IOException {
withReader(key, staleOk, new ReaderCallback() {
public void withSearcher(final String path, final boolean staleOk, final SearcherCallback callback) throws IOException {
withReader(path, staleOk, new ReaderCallback() {

public void callback(final IndexReader reader) throws IOException {
callback.callback(new IndexSearcher(reader), map.get(key).version);
callback.callback(new IndexSearcher(reader), map.get(path).version);
}

public void onMissing() throws IOException {
Expand All @@ -127,10 +125,10 @@ public void onMissing() throws IOException {
});
}

public void withWriter(final IndexKey key, final WriterCallback callback) throws IOException {
public void withWriter(final String path, final WriterCallback callback) throws IOException {
final Tuple tuple;
synchronized (map) {
tuple = map.get(key);
tuple = map.get(path);
}

if (tuple == null) {
Expand All @@ -144,24 +142,24 @@ public void withWriter(final IndexKey key, final WriterCallback callback) throws
tuple.dirty = dirty;
}
} catch (final OutOfMemoryError e) {
map.remove(key).writer.rollback();
map.remove(path).writer.rollback();
throw e;
}
}

public void createWriter(final IndexKey key, final UUID uuid, final String function) throws IOException {
public void createWriter(final String path, final UUID uuid, final String function) throws IOException {
final String digest = digest(function);
final File dir = new File(new File(root, uuid.toString()), digest);
dir.mkdirs();

synchronized (map) {
Tuple tuple = map.remove(key);
Tuple tuple = map.remove(path);
if (tuple != null) {
tuple.close();
}
final Directory d = FSDirectory.open(dir);
tuple = new Tuple(newWriter(d));
map.put(key, tuple);
map.put(path, tuple);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/github/rnewson/couchdb/lucene/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public static void main(String[] args) throws Exception {
}
LOG.info("Index output goes to: " + dir.getCanonicalPath());

final CouchDbRegistry registry = new CouchDbRegistry(properties, "couchdb.url.");
final Lucene lucene = new Lucene(dir, registry);
final Lucene lucene = new Lucene(dir);
final int port = Integer.parseInt(properties.getProperty("lucene.port", "5985"));
final Server jetty = jetty(lucene, port);

Expand Down
Loading

0 comments on commit cbfe065

Please sign in to comment.