Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support new BigCouch 0.4 update sequence format

  • Loading branch information...
commit ed0aa352c80cb25d424393aea344f30f96329779 1 parent 0a03bd3
Robert Newson authored
View
24 src/main/java/com/github/rnewson/couchdb/lucene/couchdb/UpdateSequence.java
@@ -4,6 +4,8 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.apache.commons.codec.binary.Base64;
@@ -27,10 +29,9 @@
private final String since;
private final Map<String, Long> vector = new HashMap<String, Long>();
- private BigCouchUpdateSequence(final String encodedVector) {
+ private BigCouchUpdateSequence(final String encodedVector, final String packedSeqs) {
this.since = encodedVector;
- final String packedSeqs = encodedVector.split("-", 2)[1];
final byte[] bytes = new Base64(true).decode(packedSeqs);
final OtpInputStream stream = new OtpInputStream(bytes);
try {
@@ -173,16 +174,31 @@ public String toString() {
public static final UpdateSequence START = new StartOfUpdateSequence();
+ private static Pattern BC3 = Pattern.compile("[0-9]+-([0-9a-zA-Z_-]+)");
+ private static Pattern BC4 = Pattern.compile("\\[[0-9]+\\s*,\\s*\"([0-9a-zA-Z_-]+)\"\\]");
+
public static UpdateSequence parseUpdateSequence(final String str) {
if (str.matches("[0-9]+")) {
return new CouchDbUpdateSequence(str);
}
- if (str.matches("[0-9]+-[0-9a-zA-Z_-]+")) {
- return new BigCouchUpdateSequence(str);
+ String packedSeqs;
+ if ((packedSeqs = extractPackedSeqs(BC3, str)) != null) {
+ return new BigCouchUpdateSequence(str, packedSeqs);
+ }
+ if ((packedSeqs = extractPackedSeqs(BC4, str)) != null) {
+ return new BigCouchUpdateSequence(str, packedSeqs);
}
throw new IllegalArgumentException(str + " not recognized.");
}
+ private static String extractPackedSeqs(final Pattern p, final String str) {
+ final Matcher m = p.matcher(str);
+ if (m.matches()) {
+ return m.group(1);
+ }
+ return null;
+ }
+
public abstract String appendSince(final String url);
public abstract boolean isEarlierThan(final UpdateSequence other);
View
34 src/test/java/com/github/rnewson/couchdb/lucene/couchdb/UpdateSequenceTest.java
@@ -0,0 +1,34 @@
+package com.github.rnewson.couchdb.lucene.couchdb;
+
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+
+public class UpdateSequenceTest {
+
+ @Test
+ public void couchdbSequence() {
+ assertThat(UpdateSequence.parseUpdateSequence("1234"), notNullValue());
+ }
+
+ @Test
+ public void bigcouch3Sequence() {
+ assertThat(
+ UpdateSequence
+ .parseUpdateSequence("79521-g1AAAAGbeJzLYWBg4MhgTmEQT8pMT84vTc5wMDQ30jM00zO0BG"
+ + "JjgxygAqZEhiT5____ZyUxMKi1EVSdpAAkk-yhGtRdCWtwAGmIh9lwi7CGBJCGepgN0gQ"
+ + "15LEASYYGIAXUMx-syYlITQsgmvaDneZDpKYDEE33wZpOE6npAUQTJBA6sgABPG9K"),
+ notNullValue());
+ }
+
+ @Test
+ public void bigcouch4Sequence() {
+ assertThat(
+ UpdateSequence
+ .parseUpdateSequence("[79521,\"g1AAAAGbeJzLYWBg4MhgTmEQT8pMT84vTc5wMDQ30jM00zO0BG"
+ + "JjgxygAqZEhiT5____ZyUxMKi1EVSdpAAkk-yhGtRdCWtwAGmIh9lwi7CGBJCGepgN0gQ"
+ + "15LEASYYGIAXUMx-syYlITQsgmvaDneZDpKYDEE33wZpOE6npAUQTJBA6sgABPG9K\"]"),
+ notNullValue());
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.