lucene 4: Moved from FieldSelectors to FieldVisitors. Removed BaseFieldVisitor#reset and changed SourceFieldVisitor and UidFieldVisitor to singleton to prototype.

This commit is contained in:
Martijn van Groningen 2012-10-29 15:11:49 +01:00 committed by Shay Banon
parent d8d7498292
commit b928e74904
11 changed files with 19 additions and 65 deletions

View File

@ -5,9 +5,6 @@ import org.apache.lucene.index.StoredFieldVisitor;
public abstract class BaseFieldVisitor extends StoredFieldVisitor { public abstract class BaseFieldVisitor extends StoredFieldVisitor {
// LUCENE 4 UPGRADE: Some field visitors need to be cleared before they can be reused. Maybe a better way.
public abstract void reset();
// LUCENE 4 UPGRADE: Added for now to make everything work. Want to make use of Document as less as possible. // LUCENE 4 UPGRADE: Added for now to make everything work. Want to make use of Document as less as possible.
public abstract Document createDocument(); public abstract Document createDocument();

View File

@ -73,11 +73,6 @@ public class MultipleFieldsVisitor extends BaseFieldVisitor {
return fieldsToAdd == null || fieldsToAdd.contains(fieldInfo.name) ? Status.YES : Status.NO; return fieldsToAdd == null || fieldsToAdd.contains(fieldInfo.name) ? Status.YES : Status.NO;
} }
@Override
public void reset() {
doc = null;
}
@Override @Override
public Document createDocument() { public Document createDocument() {
return doc; return doc;

View File

@ -81,9 +81,4 @@ public class SingleFieldVisitor extends BaseFieldVisitor {
values.add(value); values.add(value);
} }
} }
@Override
public void reset() {
values = null;
}
} }

View File

@ -213,7 +213,7 @@ public class SourceFieldMapper extends AbstractFieldMapper<byte[]> implements In
} }
public BaseFieldVisitor fieldSelector() { public BaseFieldVisitor fieldSelector() {
return SourceFieldVisitor.INSTANCE; return new SourceFieldVisitor();
} }
@Override @Override

View File

@ -32,39 +32,30 @@ import java.io.IOException;
*/ */
public class SourceFieldVisitor extends BaseFieldVisitor { public class SourceFieldVisitor extends BaseFieldVisitor {
public static final SourceFieldVisitor INSTANCE = new SourceFieldVisitor(); private BytesRef source;
private static ThreadLocal<BytesRef> loadingContext = new ThreadLocal<BytesRef>();
private SourceFieldVisitor() {
}
@Override @Override
public Status needsField(FieldInfo fieldInfo) throws IOException { public Status needsField(FieldInfo fieldInfo) throws IOException {
if (SourceFieldMapper.NAME.equals(fieldInfo.name)) { if (SourceFieldMapper.NAME.equals(fieldInfo.name)) {
return Status.YES; return Status.YES;
} }
return loadingContext.get() != null ? Status.STOP : Status.NO; return source != null ? Status.STOP : Status.NO;
} }
@Override @Override
public void binaryField(FieldInfo fieldInfo, byte[] value) throws IOException { public void binaryField(FieldInfo fieldInfo, byte[] value) throws IOException {
loadingContext.set(new BytesRef(value)); source = new BytesRef(value);
}
@Override
public void reset() {
loadingContext.remove();
} }
@Override @Override
public Document createDocument() { public Document createDocument() {
Document document = new Document(); Document document = new Document();
document.add(new StoredField("_source", loadingContext.get().utf8ToString())); document.add(new StoredField("_source", source));
return document; return document;
} }
public BytesRef source() { public BytesRef source() {
return loadingContext.get(); return source;
} }
@Override @Override

View File

@ -28,7 +28,6 @@ import java.io.IOException;
/** /**
* A field selector that loads all fields except the source field. * A field selector that loads all fields except the source field.
*/ */
// LUCENE 4 UPGRADE: change into singleton
public class AllButSourceFieldVisitor extends MultipleFieldsVisitor { public class AllButSourceFieldVisitor extends MultipleFieldsVisitor {
@Override @Override

View File

@ -31,7 +31,6 @@ import java.io.IOException;
/** /**
* An optimized field selector that loads just the uid and the routing. * An optimized field selector that loads just the uid and the routing.
*/ */
// LUCENE 4 UPGRADE: change into singleton
public class UidAndRoutingFieldVisitor extends BaseFieldVisitor { public class UidAndRoutingFieldVisitor extends BaseFieldVisitor {
private String uid; private String uid;
@ -65,12 +64,6 @@ public class UidAndRoutingFieldVisitor extends BaseFieldVisitor {
} }
} }
@Override
public void reset() {
uid = null;
routing = null;
}
public String uid() { public String uid() {
return uid; return uid;
} }

View File

@ -32,7 +32,6 @@ import java.io.IOException;
/** /**
* An optimized field selector that loads just the uid and the source. * An optimized field selector that loads just the uid and the source.
*/ */
// LUCENE 4 UPGRADE: change into singleton
public class UidAndSourceFieldVisitor extends BaseFieldVisitor { public class UidAndSourceFieldVisitor extends BaseFieldVisitor {
private String uid; private String uid;
@ -57,12 +56,6 @@ public class UidAndSourceFieldVisitor extends BaseFieldVisitor {
return uid != null && source != null ? Status.STOP : Status.NO; return uid != null && source != null ? Status.STOP : Status.NO;
} }
@Override
public void reset() {
uid = null;
source = null;
}
@Override @Override
public void binaryField(FieldInfo fieldInfo, byte[] value) throws IOException { public void binaryField(FieldInfo fieldInfo, byte[] value) throws IOException {
source = new BytesRef(value); source = new BytesRef(value);

View File

@ -32,15 +32,14 @@ import java.io.IOException;
*/ */
public class UidFieldVisitor extends BaseFieldVisitor { public class UidFieldVisitor extends BaseFieldVisitor {
public static final UidFieldVisitor INSTANCE = new UidFieldVisitor(); private String uid;
private static ThreadLocal<String> loadingContext = new ThreadLocal<String>();
private UidFieldVisitor() { public UidFieldVisitor() {
} }
@Override @Override
public void stringField(FieldInfo fieldInfo, String value) throws IOException { public void stringField(FieldInfo fieldInfo, String value) throws IOException {
loadingContext.set(value); uid = value;
} }
@Override @Override
@ -48,23 +47,18 @@ public class UidFieldVisitor extends BaseFieldVisitor {
if (UidFieldMapper.NAME.equals(fieldInfo.name)) { if (UidFieldMapper.NAME.equals(fieldInfo.name)) {
return Status.YES; return Status.YES;
} }
return loadingContext.get() != null ? Status.STOP : Status.NO; return uid != null ? Status.STOP : Status.NO;
}
@Override
public void reset() {
loadingContext.remove();
} }
@Override @Override
public Document createDocument() { public Document createDocument() {
Document document = new Document(); Document document = new Document();
document.add(new StoredField("_uid", loadingContext.get())); document.add(new StoredField("_uid", uid));
return document; return document;
} }
public String uid() { public String uid() {
return loadingContext.get(); return uid;
} }
@Override @Override

View File

@ -100,14 +100,14 @@ public class FetchPhase implements SearchPhase {
sourceRequested = false; sourceRequested = false;
} else if (context.hasScriptFields()) { } else if (context.hasScriptFields()) {
// we ask for script fields, and no field names, don't load the source // we ask for script fields, and no field names, don't load the source
fieldVisitor = UidFieldVisitor.INSTANCE; fieldVisitor = new UidFieldVisitor();
sourceRequested = false; sourceRequested = false;
} else { } else {
fieldVisitor = new UidAndSourceFieldVisitor(); fieldVisitor = new UidAndSourceFieldVisitor();
sourceRequested = true; sourceRequested = true;
} }
} else if (context.fieldNames().isEmpty()) { } else if (context.fieldNames().isEmpty()) {
fieldVisitor = UidFieldVisitor.INSTANCE; fieldVisitor = new UidFieldVisitor();
sourceRequested = false; sourceRequested = false;
} else { } else {
boolean loadAllStored = false; boolean loadAllStored = false;
@ -151,7 +151,7 @@ public class FetchPhase implements SearchPhase {
} else if (extractFieldNames != null || sourceRequested) { } else if (extractFieldNames != null || sourceRequested) {
fieldVisitor = new UidAndSourceFieldVisitor(); fieldVisitor = new UidAndSourceFieldVisitor();
} else { } else {
fieldVisitor = UidFieldVisitor.INSTANCE; fieldVisitor = new UidFieldVisitor();
} }
} }
@ -288,9 +288,7 @@ public class FetchPhase implements SearchPhase {
private Document loadDocument(SearchContext context, @Nullable BaseFieldVisitor fieldVisitor, int docId) { private Document loadDocument(SearchContext context, @Nullable BaseFieldVisitor fieldVisitor, int docId) {
try { try {
if (fieldVisitor != null) { if (fieldVisitor == null) {
fieldVisitor.reset();
} else {
return context.searcher().doc(docId); return context.searcher().doc(docId);
} }
context.searcher().doc(docId, fieldVisitor); context.searcher().doc(docId, fieldVisitor);

View File

@ -60,8 +60,9 @@ public class SourceLookup implements Map {
return source; return source;
} }
try { try {
reader.document(docId, SourceFieldVisitor.INSTANCE); SourceFieldVisitor sourceFieldVisitor = new SourceFieldVisitor();
BytesRef source = SourceFieldVisitor.INSTANCE.source(); reader.document(docId, sourceFieldVisitor);
BytesRef source = sourceFieldVisitor.source();
if (source == null) { if (source == null) {
this.source = ImmutableMap.of(); this.source = ImmutableMap.of();
} else { } else {
@ -69,8 +70,6 @@ public class SourceLookup implements Map {
} }
} catch (Exception e) { } catch (Exception e) {
throw new ElasticSearchParseException("failed to parse / load source", e); throw new ElasticSearchParseException("failed to parse / load source", e);
} finally {
SourceFieldVisitor.INSTANCE.reset();
} }
return this.source; return this.source;
} }