Permalink
Browse files

update atomically (and sequentially) with new addDocuments method.

  • Loading branch information...
1 parent 36ec57b commit 0feac67b6edcae0357d3992555ffdd443485abce Robert Newson committed Jun 11, 2011
@@ -9,6 +9,7 @@
import java.io.InputStreamReader;
import java.io.Writer;
import java.net.SocketException;
+import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -363,7 +364,7 @@ public Void handleResponse(final HttpResponse response)
final IndexState state = entry.getValue();
if (seq.isLaterThan(state.pending_seq)) {
- final Document[] docs;
+ final Collection<Document> docs;
try {
docs = state.converter.convert(doc, view
.getDefaultSettings(), database);
@@ -372,10 +373,8 @@ public Void handleResponse(final HttpResponse response)
continue loop;
}
- state.writer.deleteDocuments(new Term("_id", id));
- for (final Document d : docs) {
- state.writer.addDocument(d, view.getAnalyzer());
- }
+ state.writer.updateDocuments(new Term("_id", id), docs,
+ view.getAnalyzer());
state.setPendingSequence(seq);
state.readerDirty = true;
}
@@ -18,6 +18,9 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
import java.util.Iterator;
import org.apache.log4j.Logger;
@@ -43,7 +46,7 @@
public final class DocumentConverter {
- private static final Document[] NO_DOCUMENTS = new Document[0];
+ private static final Collection<Document> NO_DOCUMENTS = Collections.emptyList();
private static final Logger LOG = Logger.getLogger(DocumentConverter.class);
private final Context context;
@@ -72,7 +75,7 @@ public DocumentConverter(final Context context, final View view) throws IOExcept
viewFun = view.compileFunction(context, scope);
}
- public Document[] convert(
+ public Collection<Document> convert(
final CouchDocument doc,
final ViewSettings defaults,
final Database database) throws IOException, ParseException, JSONException {
@@ -93,20 +96,20 @@ public DocumentConverter(final Context context, final View view) throws IOExcept
if (result instanceof RhinoDocument) {
final RhinoDocument rhinoDocument = (RhinoDocument) result;
final Document document = rhinoDocument.toDocument(doc.getId(), defaults, database);
- return new Document[]{document};
+ return Collections.singleton(document);
}
if (result instanceof NativeArray) {
final NativeArray nativeArray = (NativeArray) result;
- final Document[] arrayResult = new Document[(int) nativeArray.getLength()];
+ final Collection<Document> arrayResult = new ArrayList<Document>((int) nativeArray.getLength());
for (int i = 0; i < (int) nativeArray.getLength(); i++) {
if (nativeArray.get(i, null) instanceof RhinoDocument) {
final RhinoDocument rhinoDocument = (RhinoDocument) nativeArray.get(i, null);
final Document document = rhinoDocument.toDocument(
doc.getId(),
defaults,
database);
- arrayResult[i] = document;
+ arrayResult.add(document);
}
}
return arrayResult;
@@ -4,6 +4,7 @@
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
+import java.util.Collection;
import java.util.TimeZone;
import org.apache.lucene.document.Document;
@@ -45,64 +46,64 @@ public void testSingleDocumentReturn() throws Exception {
final DocumentConverter converter = new DocumentConverter(
context,
view("function(doc) {return new Document();}"));
- final Document[] result = converter.convert(doc("{_id:\"hello\"}"), settings(), null);
- assertThat(result.length, is(1));
- assertThat(result[0].get("_id"), is("hello"));
+ final Collection<Document> result = converter.convert(doc("{_id:\"hello\"}"), settings(), null);
+ assertThat(result.size(), is(1));
+ assertThat(result.iterator().next().get("_id"), is("hello"));
}
@Test
public void testMultipleDocumentReturn() throws Exception {
final DocumentConverter converter = new DocumentConverter(
context,
view("function(doc) {var ret = new Array(); ret.push(new Document()); ret.push(new Document()); return ret;}"));
- final Document[] result = converter.convert(doc("{_id:\"hello\"}"), settings(), null);
- assertThat(result.length, is(2));
- assertThat(result[0].get("_id"), is("hello"));
+ final Collection<Document> result = converter.convert(doc("{_id:\"hello\"}"), settings(), null);
+ assertThat(result.size(), is(2));
+ assertThat(result.iterator().next().get("_id"), is("hello"));
}
@Test
public void testAdd() throws Exception {
final DocumentConverter converter = new DocumentConverter(
context,
view("function(doc) {var ret=new Document(); ret.add(doc.key); return ret;}"));
- final Document[] result = converter.convert(
+ final Collection<Document> result = converter.convert(
doc("{_id:\"hello\", key:\"value\"}"),
settings(),
null);
- assertThat(result.length, is(1));
- assertThat(result[0].get(Constants.DEFAULT_FIELD), is("value"));
+ assertThat(result.size(), is(1));
+ assertThat(result.iterator().next().get(Constants.DEFAULT_FIELD), is("value"));
}
@Test
public void testForLoopOverObject() throws Exception {
final DocumentConverter converter = new DocumentConverter(
context,
view("function(doc) {var ret=new Document(); for (var key in doc) { ret.add(doc[key]); } return ret; }"));
- final Document[] result = converter.convert(
+ final Collection<Document> result = converter.convert(
doc("{_id:\"hello\", key:\"value\"}"),
settings(),
null);
- assertThat(result.length, is(1));
- assertThat(result[0].get("_id"), is("hello"));
- assertThat(result[0].getValues(Constants.DEFAULT_FIELD)[0], is("hello"));
- assertThat(result[0].getValues(Constants.DEFAULT_FIELD)[1], is("value"));
+ assertThat(result.size(), is(1));
+ assertThat(result.iterator().next().get("_id"), is("hello"));
+ assertThat(result.iterator().next().getValues(Constants.DEFAULT_FIELD)[0], is("hello"));
+ assertThat(result.iterator().next().getValues(Constants.DEFAULT_FIELD)[1], is("value"));
}
@Test
public void testForLoopOverArray() throws Exception {
final DocumentConverter converter = new DocumentConverter(
context,
view("function(doc) {var ret=new Document(); for (var key in doc.arr) {ret.add(doc.arr[key]); } return ret; }"));
- final Document[] result = converter.convert(
+ final Collection<Document> result = converter.convert(
doc("{_id:\"hello\", arr:[0,1,2,3]}"),
settings(),
null);
- assertThat(result.length, is(1));
- assertThat(result[0].get("_id"), is("hello"));
- assertThat(result[0].getValues(Constants.DEFAULT_FIELD)[0], is("0"));
- assertThat(result[0].getValues(Constants.DEFAULT_FIELD)[1], is("1"));
- assertThat(result[0].getValues(Constants.DEFAULT_FIELD)[2], is("2"));
- assertThat(result[0].getValues(Constants.DEFAULT_FIELD)[3], is("3"));
+ assertThat(result.size(), is(1));
+ assertThat(result.iterator().next().get("_id"), is("hello"));
+ assertThat(result.iterator().next().getValues(Constants.DEFAULT_FIELD)[0], is("0"));
+ assertThat(result.iterator().next().getValues(Constants.DEFAULT_FIELD)[1], is("1"));
+ assertThat(result.iterator().next().getValues(Constants.DEFAULT_FIELD)[2], is("2"));
+ assertThat(result.iterator().next().getValues(Constants.DEFAULT_FIELD)[3], is("3"));
}
@Test
@@ -114,40 +115,40 @@ public void testForEverything() throws Exception {
+ "{switch (typeof obj[key]) {case 'object':idx(obj[key]); break; "
+ "case 'function': break; default: ret.add(obj[key]); break;} } }; idx(doc); return ret; }"));
- final Document[] result = converter.convert(
+ final Collection<Document> result = converter.convert(
doc("{_id:\"hello\", l1: { l2: {l3:[\"v3\", \"v4\"]}}}"),
settings(),
null);
- assertThat(result[0].getValues(Constants.DEFAULT_FIELD)[0], is("hello"));
- assertThat(result[0].getValues(Constants.DEFAULT_FIELD)[1], is("v3"));
- assertThat(result[0].getValues(Constants.DEFAULT_FIELD)[2], is("v4"));
+ assertThat(result.iterator().next().getValues(Constants.DEFAULT_FIELD)[0], is("hello"));
+ assertThat(result.iterator().next().getValues(Constants.DEFAULT_FIELD)[1], is("v3"));
+ assertThat(result.iterator().next().getValues(Constants.DEFAULT_FIELD)[2], is("v4"));
}
@Test
public void testNullReturn() throws Exception {
final DocumentConverter converter = new DocumentConverter(
context,
view("function(doc) {return null;}"));
- final Document[] result = converter.convert(doc("{_id:\"hello\"}"), settings(), null);
- assertThat(result.length, is(0));
+ final Collection<Document> result = converter.convert(doc("{_id:\"hello\"}"), settings(), null);
+ assertThat(result.size(), is(0));
}
@Test
public void testUndefinedReturn() throws Exception {
final DocumentConverter converter = new DocumentConverter(
context,
view("function(doc) {return doc.nope;}"));
- final Document[] result = converter.convert(doc("{_id:\"hello\"}"), settings(), null);
- assertThat(result.length, is(0));
+ final Collection<Document> result = converter.convert(doc("{_id:\"hello\"}"), settings(), null);
+ assertThat(result.size(), is(0));
}
@Test
public void testRuntimeException() throws Exception {
final DocumentConverter converter = new DocumentConverter(
context,
view("function(doc) {throw {bad : \"stuff\"}}"));
- final Document[] result = converter.convert(doc("{_id:\"hello\"}"), settings(), null);
- assertThat(result.length, is(0));
+ final Collection<Document> result = converter.convert(doc("{_id:\"hello\"}"), settings(), null);
+ assertThat(result.size(), is(0));
}
@Test(expected=EvaluatorException.class)
@@ -163,122 +164,122 @@ public void testNullAddsAreIgnored() throws Exception {
final DocumentConverter converter = new DocumentConverter(
context,
view("function(doc) {var ret=new Document(); ret.add(doc.nope); return ret;}"));
- final Document[] result = converter.convert(doc("{_id:\"hello\"}"), settings(), null);
- assertThat(result.length, is(1));
+ final Collection<Document> result = converter.convert(doc("{_id:\"hello\"}"), settings(), null);
+ assertThat(result.size(), is(1));
}
@Test
public void testQuoteRemoval() throws Exception {
final DocumentConverter converter = new DocumentConverter(
context,
view("\"function(doc) {return new Document();}\""));
- final Document[] result = converter.convert(doc("{_id:\"hello\"}"), settings(), null);
- assertThat(result.length, is(1));
- assertThat(result[0].get("_id"), is("hello"));
+ final Collection<Document> result = converter.convert(doc("{_id:\"hello\"}"), settings(), null);
+ assertThat(result.size(), is(1));
+ assertThat(result.iterator().next().get("_id"), is("hello"));
}
@Test
public void testNoReturnValue() throws Exception {
final String fun = "function(doc) { }";
final DocumentConverter converter = new DocumentConverter(context, view(fun));
- final Document[] result = converter.convert(doc("{_id:\"hi\"}"), settings(), null);
- assertThat(result.length, is(0));
+ final Collection<Document> result = converter.convert(doc("{_id:\"hi\"}"), settings(), null);
+ assertThat(result.size(), is(0));
}
@Test
public void testDefaultValue() throws Exception {
final String fun = "function(doc) { var ret=new Document(); ret.add(doc['arr'].join(' ')); return ret; }";
final DocumentConverter converter = new DocumentConverter(context, view(fun));
- final Document[] result = converter.convert(
+ final Collection<Document> result = converter.convert(
doc("{_id:\"hi\", arr:[\"1\",\"2\"]}"),
settings(),
null);
- assertThat(result.length, is(1));
- assertThat(result[0].get("default"), is("1 2"));
+ assertThat(result.size(), is(1));
+ assertThat(result.iterator().next().get("default"), is("1 2"));
}
@Test
public void testNullValue() throws Exception {
final String fun = "function(doc) { var ret=new Document(); ret.add(doc.foo); return ret; }";
final DocumentConverter converter = new DocumentConverter(context, view(fun));
- final Document[] result = converter.convert(
+ final Collection<Document> result = converter.convert(
doc("{_id:\"hi\", foo:null}"),
settings(),
null);
- assertThat(result.length, is(1));
- assertThat(result[0].get("foo"), is(nullValue()));
+ assertThat(result.size(), is(1));
+ assertThat(result.iterator().next().get("foo"), is(nullValue()));
}
@Test
public void testLongValue() throws Exception {
final String fun = "function(doc) { var ret=new Document(); ret.add(12, {type:\"long\", field:\"num\"}); return ret; }";
final DocumentConverter converter = new DocumentConverter(context, view(fun));
- final Document[] result = converter.convert(
+ final Collection<Document> result = converter.convert(
doc("{_id:\"hi\"}"),
settings(),
null);
- assertThat(result.length, is(1));
- assertThat(result[0].getFieldable("num"), is(NumericField.class));
+ assertThat(result.size(), is(1));
+ assertThat(result.iterator().next().getFieldable("num"), is(NumericField.class));
}
@Test
public void testDateString() throws Exception {
final String fun = "function(doc) { var ret=new Document(); ret.add(\"2009-01-01\", {type:\"date\", field:\"num\"}); return ret; }";
final DocumentConverter converter = new DocumentConverter(context, view(fun));
- final Document[] result = converter.convert(
+ final Collection<Document> result = converter.convert(
doc("{_id:\"hi\"}"),
settings(),
null);
- assertThat(result.length, is(1));
- assertThat(result[0].getFieldable("num"), is(NumericField.class));
+ assertThat(result.size(), is(1));
+ assertThat(result.iterator().next().getFieldable("num"), is(NumericField.class));
}
@Test
public void testDateObject() throws Exception {
final String fun = "function(doc) { var ret=new Document(); ret.add(new Date(2010,8,13), {type:\"date\", field:\"num\"}); return ret; }";
final DocumentConverter converter = new DocumentConverter(context, view(fun));
- final Document[] result = converter.convert(
+ final Collection<Document> result = converter.convert(
doc("{_id:\"hi\"}"),
settings(),
null);
- assertThat(result.length, is(1));
- assertThat(result[0].getFieldable("num"), is(NumericField.class));
- assertThat((Long)((NumericField)result[0].getFieldable("num")).getNumericValue(), is(1284332400000L));
+ assertThat(result.size(), is(1));
+ assertThat(result.iterator().next().getFieldable("num"), is(NumericField.class));
+ assertThat((Long)((NumericField)result.iterator().next().getFieldable("num")).getNumericValue(), is(1284332400000L));
}
@Test
public void testDateObject2() throws Exception {
final String fun = "function(doc) { var ret=new Document(); ret.add(new Date(\"January 6, 1972 16:05:00\"), {type:\"date\", field:\"num\"}); return ret; }";
final DocumentConverter converter = new DocumentConverter(context, view(fun));
- final Document[] result = converter.convert(
+ final Collection<Document> result = converter.convert(
doc("{_id:\"hi\"}"),
settings(),
null);
- assertThat(result.length, is(1));
- assertThat(result[0].getFieldable("num"), is(NumericField.class));
- assertThat((Long)((NumericField)result[0].getFieldable("num")).getNumericValue(), is(63561900000L));
+ assertThat(result.size(), is(1));
+ assertThat(result.iterator().next().getFieldable("num"), is(NumericField.class));
+ assertThat((Long)((NumericField)result.iterator().next().getFieldable("num")).getNumericValue(), is(63561900000L));
}
@Test
public void testParseInt() throws Exception {
final String fun = "function(doc) { var ret=new Document(); ret.add(parseInt(\"12.5\"), {type:\"int\", field:\"num\"}); return ret; }";
final DocumentConverter converter = new DocumentConverter(context, view(fun));
- final Document[] result = converter.convert(
+ final Collection<Document> result = converter.convert(
doc("{_id:\"hi\"}"),
settings(),
null);
- assertThat(result.length, is(1));
- assertThat(result[0].getFieldable("num"), is(NumericField.class));
+ assertThat(result.size(), is(1));
+ assertThat(result.iterator().next().getFieldable("num"), is(NumericField.class));
}
@Test
public void testConditionalOnNulls() throws Exception {
final String fun = "function(doc) { if (doc.foo && doc.bar) { return new Document(); }; return null; }";
final DocumentConverter converter = new DocumentConverter(context,
view(fun));
- final Document[] result = converter.convert(
+ final Collection<Document> result = converter.convert(
doc("{_id:\"hi\", foo: null, bar: null}"), settings(), null);
- assertThat(result.length, is(0));
+ assertThat(result.size(), is(0));
}
private CouchDocument doc(final String json) throws JSONException {

0 comments on commit 0feac67

Please sign in to comment.