Permalink
Browse files

No good, try again. :)

Revert "Add support for BigCouch update sequences. (EXPERIMENTAL)"

This reverts commit 467a549.
  • Loading branch information...
1 parent 695c686 commit 4233e12fa84afbf783da536150bd561ecfc5a7c3 Robert Newson committed Jun 4, 2011
View
@@ -38,11 +38,6 @@
<version>1.6</version>
</dependency>
<dependency>
- <groupId>commons-codec</groupId>
- <artifactId>commons-codec</artifactId>
- <version>1.5</version>
- </dependency>
- <dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
@@ -78,11 +73,6 @@
<version>${tika-version}</version>
</dependency>
<dependency>
- <groupId>org.erlang.otp</groupId>
- <artifactId>jinterface</artifactId>
- <version>1.5.3.2</version>
- </dependency>
- <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
@@ -63,7 +63,6 @@
import com.github.rnewson.couchdb.lucene.couchdb.CouchDocument;
import com.github.rnewson.couchdb.lucene.couchdb.Database;
import com.github.rnewson.couchdb.lucene.couchdb.DesignDocument;
-import com.github.rnewson.couchdb.lucene.couchdb.UpdateSequence;
import com.github.rnewson.couchdb.lucene.couchdb.View;
import com.github.rnewson.couchdb.lucene.util.Analyzers;
import com.github.rnewson.couchdb.lucene.util.Constants;
@@ -80,7 +79,7 @@
private String etag;
private final Analyzer analyzer;
- private UpdateSequence pending_seq;
+ private long pending_seq;
private IndexReader reader;
private final IndexWriter writer;
private final Database database;
@@ -162,10 +161,10 @@ private void blockForLatest(final boolean staleOk) throws IOException, JSONExcep
if (staleOk) {
return;
}
- final UpdateSequence latest = database.getInfo().getUpdateSequence();
+ final long latest = database.getInfo().getUpdateSequence();
synchronized (this) {
long timeout = getSearchTimeout();
- while (pending_seq.isEarlierThan(latest)) {
+ while (pending_seq < latest) {
try {
final long start = System.currentTimeMillis();
wait(timeout);
@@ -180,8 +179,8 @@ private void blockForLatest(final boolean staleOk) throws IOException, JSONExcep
}
}
- private synchronized void setPendingSequence(final UpdateSequence seq) {
- pending_seq = seq;
+ private synchronized void setPendingSequence(final long newSequence) {
+ pending_seq = newSequence;
notifyAll();
}
@@ -224,7 +223,7 @@ private static long now() {
private final Database database;
- private UpdateSequence ddoc_seq;
+ private long ddoc_seq;
private long lastCommit;
@@ -238,7 +237,7 @@ private static long now() {
private final File root;
- private UpdateSequence since;
+ private long since;
private final Map<View, IndexState> states = Collections
.synchronizedMap(new HashMap<View, IndexState>());
@@ -320,7 +319,7 @@ public Void handleResponse(final HttpResponse response)
break loop;
}
- final UpdateSequence seq = new UpdateSequence(json.getString("seq"));
+ final long seq = json.getLong("seq");
final String id = json.getString("id");
CouchDocument doc;
if (!json.isNull("doc")) {
@@ -342,7 +341,7 @@ public Void handleResponse(final HttpResponse response)
}
if (id.startsWith("_design")) {
- if (ddoc_seq.isEarlierThan(seq)) {
+ if (seq > ddoc_seq) {
logger.info("Exiting due to design document change.");
break loop;
}
@@ -359,7 +358,7 @@ public Void handleResponse(final HttpResponse response)
final View view = entry.getKey();
final IndexState state = entry.getValue();
- if (state.pending_seq.isEarlierThan(seq)) {
+ if (seq > state.pending_seq) {
final Document[] docs;
try {
docs = state.converter.convert(doc, view
@@ -671,9 +670,9 @@ private void commitAll() throws IOException {
final View view = entry.getKey();
final IndexState state = entry.getValue();
- if (getUpdateSequence(state.writer).isEarlierThan(state.pending_seq)) {
+ if (state.pending_seq > getUpdateSequence(state.writer)) {
final Map<String, String> userData = new HashMap<String, String>();
- userData.put("last_seq", state.pending_seq.toString());
+ userData.put("last_seq", Long.toString(state.pending_seq));
state.writer.commit(userData);
logger.info(view + " now at update_seq " + state.pending_seq);
}
@@ -707,22 +706,22 @@ private IndexState getState(final HttpServletRequest req,
return result;
}
- private UpdateSequence getUpdateSequence(final Directory dir) throws IOException {
+ private long getUpdateSequence(final Directory dir) throws IOException {
if (!IndexReader.indexExists(dir)) {
- return UpdateSequence.BOTTOM;
+ return 0L;
}
return getUpdateSequence(IndexReader.getCommitUserData(dir));
}
- private UpdateSequence getUpdateSequence(final IndexWriter writer) throws IOException {
+ private long getUpdateSequence(final IndexWriter writer) throws IOException {
return getUpdateSequence(writer.getDirectory());
}
- private UpdateSequence getUpdateSequence(final Map<String, String> userData) {
+ private long getUpdateSequence(final Map<String, String> userData) {
if (userData != null && userData.containsKey("last_seq")) {
- return new UpdateSequence(userData.get("last_seq"));
+ return Long.parseLong(userData.get("last_seq"));
}
- return UpdateSequence.BOTTOM;
+ return 0L;
}
private void init() throws IOException, JSONException {
@@ -733,7 +732,7 @@ private void init() throws IOException, JSONException {
context.setOptimizationLevel(9);
this.ddoc_seq = database.getInfo().getUpdateSequence();
- this.since = null;
+ this.since = -1L;
for (final DesignDocument ddoc : database.getAllDesignDocuments()) {
for (final Entry<String, View> entry : ddoc.getAllViews()
@@ -745,13 +744,11 @@ private void init() throws IOException, JSONException {
if (!states.containsKey(view)) {
final Directory dir = FSDirectory.open(viewDir(view, true),
new SingleInstanceLockFactory());
- final UpdateSequence seq = getUpdateSequence(dir);
- if (since == null) {
- since = seq;
- }
- if (seq.isEarlierThan(since)) {
+ final long seq = getUpdateSequence(dir);
+ if (since == -1) {
since = seq;
}
+ since = Math.min(since, seq);
logger.debug(dir + " bumped since to " + since);
final DocumentConverter converter = new DocumentConverter(
@@ -105,7 +105,7 @@ public DatabaseInfo getInfo() throws IOException, JSONException {
return httpClient.execute(get, handler);
}
- public HttpUriRequest getChangesRequest(final UpdateSequence since)
+ public HttpUriRequest getChangesRequest(final long since)
throws IOException {
return new HttpGet(
url
@@ -11,8 +11,8 @@ public DatabaseInfo(final JSONObject json) {
this.json = json;
}
- public UpdateSequence getUpdateSequence() throws JSONException {
- return new UpdateSequence(json.getString("update_seq"));
+ public long getUpdateSequence() throws JSONException {
+ return json.getLong("update_seq");
}
public String getName() throws JSONException {
@@ -1,84 +0,0 @@
-package com.github.rnewson.couchdb.lucene.couchdb;
-
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Map.Entry;
-
-import org.apache.commons.codec.binary.Base64;
-
-import com.ericsson.otp.erlang.OtpErlangDecodeException;
-import com.ericsson.otp.erlang.OtpErlangList;
-import com.ericsson.otp.erlang.OtpErlangLong;
-import com.ericsson.otp.erlang.OtpErlangObject;
-import com.ericsson.otp.erlang.OtpErlangTuple;
-import com.ericsson.otp.erlang.OtpInputStream;
-
-public final class UpdateSequence {
-
- public static final UpdateSequence BOTTOM = new UpdateSequence("0");
-
- private long seq;
- private Map<String, Long> vector;
- private final String asString;
-
- public UpdateSequence(final String seq) {
- this.asString = seq;
-
- if (seq.matches("[0-9]+")) {
- this.seq = Long.parseLong(seq);
- return;
- }
-
- if (seq.matches("[0-9]+-[0-9a-zA-Z_-]+")) {
- final String packedSeqs = seq.split("-", 2)[1];
- final byte[] bytes = new Base64(true).decode(packedSeqs);
- final OtpInputStream stream = new OtpInputStream(bytes);
- try {
- final OtpErlangList list = (OtpErlangList) stream.read_any();
- this.vector = new HashMap<String, Long>();
- for (int i = 0, arity = list.arity(); i < arity; i++) {
- final OtpErlangTuple tuple = (OtpErlangTuple) list
- .elementAt(i);
- final OtpErlangObject node = tuple.elementAt(0);
- final OtpErlangObject range = tuple.elementAt(1);
- final OtpErlangLong node_seq = (OtpErlangLong) tuple
- .elementAt(2);
- vector.put(node + "-" + range, node_seq.longValue());
- }
- } catch (final OtpErlangDecodeException e) {
- throw new IllegalArgumentException(seq + " not valid.");
- }
- return;
- }
-
- throw new IllegalArgumentException(seq + " not recognized.");
- }
-
- public boolean isEarlierThan(final UpdateSequence other) {
- if (this == BOTTOM) {
- return true;
- }
-
- if (vector == null && other.vector == null) {
- return this.seq < other.seq;
- } else if (vector != null && other.vector != null) {
- final Iterator<Entry<String, Long>> it = this.vector.entrySet()
- .iterator();
- while (it.hasNext()) {
- final Entry<String, Long> entry = it.next();
- final Long otherValue = other.vector.get(entry.getKey());
- if (otherValue != null && otherValue >= entry.getValue()) {
- return false;
- }
- }
- return true;
- }
- throw new IllegalArgumentException(other + " is not compatible.");
- }
-
- public String toString() {
- return asString;
- }
-
-}

0 comments on commit 4233e12

Please sign in to comment.