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 {
// 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.
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;
}
@Override
public void reset() {
doc = null;
}
@Override
public Document createDocument() {
return doc;

View File

@ -81,9 +81,4 @@ public class SingleFieldVisitor extends BaseFieldVisitor {
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() {
return SourceFieldVisitor.INSTANCE;
return new SourceFieldVisitor();
}
@Override

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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