io.polyglotted.elastic.common.Verbose Maven / Gradle / Ivy
package io.polyglotted.elastic.common;
import io.polyglotted.common.model.MapResult;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import java.util.List;
import static io.polyglotted.common.model.MapResult.immutableResult;
import static io.polyglotted.common.util.ListBuilder.immutableList;
import static io.polyglotted.common.util.ListBuilder.simpleList;
import static io.polyglotted.common.util.ReflectionUtil.fieldValue;
import static io.polyglotted.elastic.common.MetaFields.ALL_FIELDS;
import static io.polyglotted.elastic.common.MetaFields.ID_FIELD;
import static io.polyglotted.elastic.common.MetaFields.KEY_FIELD;
import static io.polyglotted.elastic.common.MetaFields.LINK_FIELD;
import static io.polyglotted.elastic.common.MetaFields.MODEL_FIELD;
import static io.polyglotted.elastic.common.MetaFields.PARENT_FIELD;
import static io.polyglotted.elastic.common.MetaFields.TIMESTAMP_FIELD;
import static io.polyglotted.elastic.common.MetaFields.VERSION_FIELD;
import static io.polyglotted.elastic.common.MetaFields.readHeader;
import static io.polyglotted.elastic.common.MetaFields.readKey;
import static io.polyglotted.elastic.common.MetaFields.reqdId;
import static io.polyglotted.elastic.common.MetaFields.reqdKey;
import static org.elasticsearch.common.Strings.EMPTY_ARRAY;
@SuppressWarnings("unused")
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum Verbose {
NONE(immutableList()) {
@Override public T buildFrom(MapResult source, T result, long version) { return result; }
},
ID(immutableList(ID_FIELD)) {
@Override public T buildFrom(MapResult source, T result, long version) {
return addHeader(result, -1, immutableResult(ID_FIELD, reqdId(source)));
}
},
KEY(immutableList(KEY_FIELD)) {
@Override public T buildFrom(MapResult source, T result, long version) {
return addHeader(result, -1, immutableResult(KEY_FIELD, reqdKey(source)));
}
},
MINIMAL(immutableList(LINK_FIELD, MODEL_FIELD, PARENT_FIELD, ID_FIELD, TIMESTAMP_FIELD, KEY_FIELD)) {
@Override public T buildFrom(MapResult source, T result, long version) { return addHeader(result, version, readKey(source).result()); }
},
UNIQUE(immutableList(ID_FIELD, KEY_FIELD)) {
@Override public T buildFrom(MapResult source, T result, long version) {
return addHeader(result, -1, immutableResult(ID_FIELD, reqdId(source), KEY_FIELD, reqdKey(source)));
}
},
META(ALL_FIELDS.toArray(new String[ALL_FIELDS.size()]), new FetchSourceContext(true, EMPTY_ARRAY, EMPTY_ARRAY)) {
@Override public T buildFrom(MapResult source, T result, long version) { return addHeader(result, version, readHeader(source)); }
};
public final String[] fields;
public final FetchSourceContext fetchContext;
Verbose(List fields) { this(fields.toArray(new String[0]), new FetchSourceContext(true, EMPTY_ARRAY, excludeFromAll(fields))); }
public String[] includeFields(String[] includeFields) {
if (includeFields.length <= 0) { return includeFields; }
String[] result = new String[fields.length + includeFields.length];
System.arraycopy(fields, 0, result, 0, fields.length);
System.arraycopy(includeFields, 0, result, fields.length, includeFields.length);
return result;
}
public abstract T buildFrom(MapResult source, T result, long version);
public static Verbose toMeta(String verb) { return verb == null ? NONE : META; }
public static Verbose fromVerb(String verb) { return verb == null ? NONE : verb.isEmpty() ? META : Verbose.valueOf(verb.toUpperCase()); }
@SuppressWarnings("unchecked") private static T addHeader(T result, long version, MapResult header) {
MapResult metaResult = null;
if (result instanceof MapResult) { metaResult = (MapResult) result; }
else {
Object meta = fieldValue(result, "_meta");
if (meta instanceof MapResult) { metaResult = ((MapResult) meta); }
}
if (metaResult != null) { metaResult.putAll(header); if (version != -1) { metaResult.put(VERSION_FIELD, String.valueOf(version)); } }
return result;
}
private static String[] excludeFromAll(List fields) {
List excludes = simpleList(ALL_FIELDS); excludes.removeAll(fields); return excludes.toArray(new String[0]);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy