forked from rnewson/couchdb-lucene
/
UpdateSequence.java
214 lines (173 loc) · 5.81 KB
/
UpdateSequence.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.github.rnewson.couchdb.lucene.couchdb;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
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;
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;
/**
* This class represents a point-in-time for a couchdb or bigcouch database.
*
* @author robertnewson
*
*/
public abstract class UpdateSequence {
private static class BigCouchUpdateSequence extends UpdateSequence {
private final String since;
private final Map<String, Long> vector = new HashMap<String, Long>();
private BigCouchUpdateSequence(final String encodedVector, final String packedSeqs) {
this.since = encodedVector;
final byte[] bytes = new Base64(true).decode(packedSeqs);
final OtpInputStream stream = new OtpInputStream(bytes);
try {
final OtpErlangList list = (OtpErlangList) stream.read_any();
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(encodedVector + " not valid.");
}
}
@Override
public String appendSince(final String url) {
try {
return url + "&since=" + URLEncoder.encode(since, "US-ASCII");
} catch (UnsupportedEncodingException e) {
throw new Error("US-ASCII inexplicably missing.");
}
}
@Override
public boolean isEarlierThan(final UpdateSequence other) {
if (other == START) {
return false;
}
if (other instanceof BigCouchUpdateSequence) {
final BigCouchUpdateSequence otherBigCouch = (BigCouchUpdateSequence) other;
final Iterator<Entry<String, Long>> it = this.vector.entrySet().iterator();
while (it.hasNext()) {
final Entry<String, Long> entry = it.next();
final Long otherValue = otherBigCouch.vector.get(entry.getKey());
if (otherValue != null && entry.getValue() < otherValue) {
return true;
}
}
return false;
}
throw new IllegalArgumentException(other + " is not compatible.");
}
@Override
public boolean isLaterThan(final UpdateSequence other) {
if (other == START) {
return true;
}
if (other instanceof BigCouchUpdateSequence) {
final BigCouchUpdateSequence otherBigCouch = (BigCouchUpdateSequence) other;
final Iterator<Entry<String, Long>> it = this.vector.entrySet().iterator();
while (it.hasNext()) {
final Entry<String, Long> entry = it.next();
final Long otherValue = otherBigCouch.vector.get(entry.getKey());
if (otherValue != null && entry.getValue() > otherValue) {
return true;
}
}
return false;
}
throw new IllegalArgumentException(other + " is not compatible.");
}
@Override
public String toString() {
return since;
}
}
private static class CouchDbUpdateSequence extends UpdateSequence {
private final long seq;
private CouchDbUpdateSequence(final String encodedIntegral) {
this.seq = Long.parseLong(encodedIntegral);
}
@Override
public String appendSince(final String url) {
return url + "&since=" + seq;
}
@Override
public boolean isEarlierThan(final UpdateSequence other) {
if (other == START) {
return false;
}
if (other instanceof CouchDbUpdateSequence) {
return this.seq < ((CouchDbUpdateSequence) other).seq;
}
throw new IllegalArgumentException(other + " is not compatible.");
}
@Override
public boolean isLaterThan(final UpdateSequence other) {
if (other == START) {
return true;
}
if (other instanceof CouchDbUpdateSequence) {
return this.seq > ((CouchDbUpdateSequence) other).seq;
}
throw new IllegalArgumentException(other + " is not compatible.");
}
@Override
public String toString() {
return Long.toString(seq);
}
}
private static class StartOfUpdateSequence extends UpdateSequence {
@Override
public String appendSince(final String url) {
return url;
}
@Override
public boolean isEarlierThan(final UpdateSequence other) {
return true;
}
@Override
public boolean isLaterThan(final UpdateSequence other) {
return false;
}
@Override
public String toString() {
return "start";
}
}
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);
}
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);
public abstract boolean isLaterThan(final UpdateSequence other);
}